summaryrefslogtreecommitdiff
path: root/adservices/tests/unittest/service-core/src/com/android/adservices/service/measurement/E2EImpressionNoiseMockTest.java
blob: 997041834902f9542866581d926c4eaba864e86a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.adservices.service.measurement;

import android.net.Uri;

import com.android.adservices.data.measurement.DatastoreException;
import com.android.adservices.service.measurement.actions.Action;
import com.android.adservices.service.measurement.actions.ReportObjects;

import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Assert;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * End-to-end test from source and trigger registration to attribution reporting, using mocked HTTP
 * requests, testing functionality when the test adds trigger data noise with 100% probability.
 */
@RunWith(Parameterized.class)
public class E2EImpressionNoiseMockTest extends E2EMockTest {
    private static final String TEST_DIR_NAME = "msmt_e2e_noise_tests";
    private final Map<String, Map<String, Integer>>
            mActualTriggerDataDistributions = new HashMap<>();
    private final Map<String, Map<String, Integer>>
            mExpectedTriggerDataDistributions = new HashMap<>();

    private interface PayloadKeys {
        String EVENT_ID = "source_event_id";
        String TRIGGER_DATA = "trigger_data";
    }

    @Parameterized.Parameters(name = "{3}")
    public static Collection<Object[]> getData() throws IOException, JSONException {
        return data(TEST_DIR_NAME);
    }

    public E2EImpressionNoiseMockTest(Collection<Action> actions, ReportObjects expectedOutput,
            PrivacyParamsProvider privacyParamsProvider, String name) throws DatastoreException {
        super(actions, expectedOutput, privacyParamsProvider, name);
        mAttributionHelper = TestObjectProvider.getAttributionJobHandler(sDatastoreManager);
        mMeasurementImpl =
                TestObjectProvider.getMeasurementImpl(
                        sDatastoreManager,
                        mClickVerifier,
                        mFlags,
                        mMeasurementDataDeleter,
                        sEnrollmentDao);
        mAsyncRegistrationQueueRunner =
                TestObjectProvider.getAsyncRegistrationQueueRunner(
                        TestObjectProvider.Type.NOISY,
                        sDatastoreManager,
                        mAsyncSourceFetcher,
                        mAsyncTriggerFetcher,
                        sEnrollmentDao);
        getExpectedTriggerDataDistributions();
    }

    @Override
    void processEventReports(List<EventReport> eventReports, List<Uri> destinations,
            List<JSONObject> payloads) throws JSONException {
        // Each report-destination × event-ID should have the same count of trigger_data as in the
        // expected output, but the trigger_data value distribution should be different. The test
        // is currently supporting only one reporting job, which batches multiple reports at once,
        // although each is a separate network request.
        for (int i = 0; i < destinations.size(); i++) {
            String uri = getReportUrl(ReportType.EVENT, destinations.get(i).toString());
            JSONObject payload = payloads.get(i);
            String eventId = payload.getString(PayloadKeys.EVENT_ID);
            String triggerData = payload.getString(PayloadKeys.TRIGGER_DATA);
            mActualTriggerDataDistributions.computeIfAbsent(
                    getKey(uri, eventId), k -> new HashMap<String, Integer>()).merge(
                        triggerData, 1, Integer::sum);
        }
    }

    @Override
    void evaluateResults() {
        for (String key : mActualTriggerDataDistributions.keySet()) {
            if (!mExpectedTriggerDataDistributions.containsKey(key)) {
                Assert.assertTrue(getTestFailureMessage(
                        "Missing key in expected trigger data distributions"
                        + getDatastoreState()), false);
            }
        }
        boolean testPassed = false;
        for (String key1 : mExpectedTriggerDataDistributions.keySet()) {
            // No reports for a source is valid impression noise.
            if (!mActualTriggerDataDistributions.containsKey(key1)) {
                continue;
            }
            Map<String, Integer> expectedDistribution = mExpectedTriggerDataDistributions.get(key1);
            Map<String, Integer> actualDistribution = mActualTriggerDataDistributions.get(key1);
            for (String key2 : expectedDistribution.keySet()) {
                if (!actualDistribution.containsKey(key2) || !actualDistribution.get(key2).equals(
                        expectedDistribution.get(key2))) {
                    testPassed = true;
                    break;
                }
            }
        }
        Assert.assertTrue(
                getTestFailureMessage(
                        "Trigger data distributions were the same " + getDatastoreState()),
                testPassed);
    }

    private void getExpectedTriggerDataDistributions() {
        for (JSONObject reportObject : mExpectedOutput.mEventReportObjects) {
            String uri = reportObject.optString(TestFormatJsonMapping.REPORT_TO_KEY);
            JSONObject payload = reportObject.optJSONObject(TestFormatJsonMapping.PAYLOAD_KEY);
            String eventId = payload.optString(PayloadKeys.EVENT_ID);
            String triggerData = payload.optString(PayloadKeys.TRIGGER_DATA);
            mExpectedTriggerDataDistributions.computeIfAbsent(
                    getKey(uri, eventId), k -> new HashMap<String, Integer>()).merge(
                        triggerData, 1, Integer::sum);
        }
    }

    private String getTestFailureMessage(String message) {
        return String.format("%s:\n\nExpected distributions: %s\n\nActual distributions: %s",
                message, mExpectedTriggerDataDistributions, mActualTriggerDataDistributions);
    }

    private String getKey(String uri, String eventId) {
        return uri + "," + eventId;
    }
}