• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.adservices.data.measurement;
18 
19 import android.adservices.measurement.DeletionParam;
20 import android.adservices.measurement.DeletionRequest;
21 import android.content.res.AssetManager;
22 import android.net.Uri;
23 
24 import com.android.adservices.common.DbTestUtil;
25 import com.android.adservices.data.measurement.deletion.MeasurementDataDeleter;
26 import com.android.adservices.service.FlagsFactory;
27 import com.android.adservices.service.stats.AdServicesLogger;
28 import com.android.adservices.shared.errorlogging.AdServicesErrorLogger;
29 
30 import org.json.JSONArray;
31 import org.json.JSONException;
32 import org.json.JSONObject;
33 import org.junit.runner.RunWith;
34 import org.junit.runners.Parameterized;
35 import org.mockito.Mockito;
36 
37 import java.io.IOException;
38 import java.io.InputStream;
39 import java.time.Instant;
40 import java.util.ArrayList;
41 import java.util.Collection;
42 import java.util.Collections;
43 import java.util.List;
44 import java.util.Map;
45 
46 /** Tests for {@link MeasurementDao} browser deletion that affect the database. */
47 @RunWith(Parameterized.class)
48 public final class DeleteApiIntegrationTest extends AbstractDbIntegrationTest {
49     private static final String TEST_DIR = "msmt_browser_deletion_tests";
50     private final JSONObject mParam;
51     private final AdServicesLogger mLogger;
52     private final AdServicesErrorLogger mErrorLogger;
53 
54     // The 'name' parameter is needed for the JUnit parameterized
55     // test, although it's ostensibly unused by this constructor.
56     @SuppressWarnings("unused")
DeleteApiIntegrationTest( DbState input, DbState output, Map<String, String> flagsMap, JSONObject param, String name)57     public DeleteApiIntegrationTest(
58             DbState input,
59             DbState output,
60             Map<String, String> flagsMap,
61             JSONObject param,
62             String name) {
63         super(input, output, flagsMap);
64         mParam = param;
65         mLogger = Mockito.mock(AdServicesLogger.class);
66         mErrorLogger = Mockito.mock(AdServicesErrorLogger.class);
67     }
68 
69     @Parameterized.Parameters(name = "{4}")
data()70     public static Collection<Object[]> data() throws IOException, JSONException {
71         AssetManager assetManager = sContext.getAssets();
72         List<InputStream> inputStreams = new ArrayList<>();
73         String[] testFileList = assetManager.list(TEST_DIR);
74         for (String testFile : testFileList) {
75             inputStreams.add(assetManager.open(TEST_DIR + "/" + testFile));
76         }
77         return AbstractDbIntegrationTest.getTestCasesFromMultipleStreams(
78                 inputStreams, (testObj) -> testObj.getJSONObject("param"));
79     }
80 
runActionToTest()81     public void runActionToTest() {
82         final String registrantValue = (String) get("registrant");
83         Long startValue = (Long) get("start");
84         Long endValue = (Long) get("end");
85         final List<Uri> originList = getUriList("origins");
86         final List<Uri> domainList = getUriList("domains");
87         Integer matchBehavior = (Integer) get("matchBehavior");
88         Integer deletionMode = (Integer) get("deletionMode");
89         if (matchBehavior == null) {
90             matchBehavior = DeletionRequest.MATCH_BEHAVIOR_DELETE;
91         }
92         if (deletionMode == null) {
93             deletionMode = DeletionRequest.DELETION_MODE_ALL;
94         }
95         Instant startValueInstant =
96                 (startValue == null)
97                         ? Instant.ofEpochMilli(Long.MIN_VALUE)
98                         : Instant.ofEpochMilli(startValue);
99         Instant endValueInstant =
100                 (endValue == null)
101                         ? Instant.ofEpochMilli(Long.MAX_VALUE)
102                         : Instant.ofEpochMilli(endValue);
103 
104         Integer finalMatchBehavior = matchBehavior;
105         Integer finalDeletionMode = deletionMode;
106 
107         DatastoreManager datastoreManager =
108                 new SQLDatastoreManager(DbTestUtil.getMeasurementDbHelperForTest(), mErrorLogger);
109         MeasurementDataDeleter measurementDataDeleter =
110                 new MeasurementDataDeleter(
111                         datastoreManager, FlagsFactory.getFlags(), mLogger);
112         measurementDataDeleter.delete(
113                 new DeletionParam.Builder(
114                                 originList,
115                                 domainList,
116                                 startValueInstant,
117                                 endValueInstant,
118                                 registrantValue,
119                                 /* sdkPackageName = */ "")
120                         .setMatchBehavior(finalMatchBehavior)
121                         .setDeletionMode(finalDeletionMode)
122                         .build());
123     }
124 
get(String name)125     private Object get(String name) {
126         try {
127             return mParam.has(name) ? mParam.get(name) : null;
128         } catch (JSONException e) {
129             throw new IllegalArgumentException("error reading " + name);
130         }
131     }
132 
getUriList(String name)133     private List<Uri> getUriList(String name) {
134         try {
135             if (mParam.isNull(name)) {
136                 return Collections.emptyList();
137             } else {
138                 JSONArray arr = mParam.getJSONArray(name);
139                 List<Uri> strList = new ArrayList<>();
140                 for (int i = 0; i < arr.length(); i++) {
141                     strList.add(Uri.parse(arr.getString(i)));
142                 }
143                 return strList;
144             }
145         } catch (JSONException e) {
146             throw new IllegalArgumentException("error reading " + name);
147         }
148     }
149 }
150