summaryrefslogtreecommitdiff
path: root/adservices/tests/unittest/service-core/src/com/android/adservices/service/measurement/reporting/EventReportSenderTest.java
blob: 6d96c9a95cb2952bbebe97e037313d53ec227e0e (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
/*
 * 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.reporting;

import static org.junit.Assert.assertEquals;

import android.net.Uri;

import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Test;
import org.mockito.Mockito;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;

public class EventReportSenderTest {

    private static final String ATTRIBUTION_DESTINATION = "https://toasters.example";
    private static final String SOURCE_EVENT_ID = "12345";
    private static final String TRIGGER_DATA = "2";
    private static final String REPORT_ID = "678";
    private static final String SOURCE_TYPE = "event";
    private static final double RANDOMIZED_TRIGGER_RATE = 0.0024;

    /**
     * Example event report payload.
     */
    private EventReportPayload createEventReportPayloadExample1() {
        return new EventReportPayload.Builder()
                .setAttributionDestination(ATTRIBUTION_DESTINATION)
                .setSourceEventId(SOURCE_EVENT_ID)
                .setTriggerData(TRIGGER_DATA)
                .setReportId(REPORT_ID)
                .setSourceType(SOURCE_TYPE)
                .setRandomizedTriggerRate(RANDOMIZED_TRIGGER_RATE)
                .build();
    }

    /**
     *
     * Tests posting a report with a mock HttpUrlConnection.
     */
    @Test
    public void testSendEventReport() throws JSONException, IOException {
        HttpURLConnection httpUrlConnection = Mockito.mock(HttpURLConnection.class);

        OutputStream outputStream = new ByteArrayOutputStream();
        Mockito.when(httpUrlConnection.getOutputStream()).thenReturn(outputStream);
        Mockito.when(httpUrlConnection.getResponseCode()).thenReturn(200);

        Uri reportingOrigin = Uri.parse("https://ad-tech.example");
        JSONObject eventReportJson = createEventReportPayloadExample1().toJson();

        EventReportSender eventReportSender = new EventReportSender();
        EventReportSender spyEventReportSender = Mockito.spy(eventReportSender);

        Mockito.doReturn(httpUrlConnection).when(spyEventReportSender)
                .createHttpUrlConnection(Mockito.any());

        int responseCode = spyEventReportSender.sendReport(reportingOrigin,
                eventReportJson);

        assertEquals(outputStream.toString(), eventReportJson.toString());
        assertEquals(responseCode, 200);
    }
}