summaryrefslogtreecommitdiff
path: root/src/test/java/com/android/vts/job/VtsAlertJobServletTest.java
blob: 9c24c6567a6275f53b9d949698d325bc30354ba4 (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
150
151
152
153
154
/*
 * Copyright (C) 2017 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.vts.job;

import static com.googlecode.objectify.ObjectifyService.factory;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import com.android.vts.entity.DeviceInfoEntity;
import com.android.vts.entity.TestAcknowledgmentEntity;
import com.android.vts.util.ObjectifyTestBase;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
import com.google.appengine.tools.development.testing.LocalUserServiceTestConfig;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.BeforeEach;

public class VtsAlertJobServletTest extends ObjectifyTestBase {
    private final LocalServiceTestHelper userHelper =
            new LocalServiceTestHelper(new LocalUserServiceTestConfig())
                    .setEnvIsAdmin(true)
                    .setEnvIsLoggedIn(true)
                    .setEnvEmail("testemail@domain.com")
                    .setEnvAuthDomain("test");

    User user;
    private Key testKey;
    private Set<String> allTestCases;
    private List<DeviceInfoEntity> allDevices;

    @BeforeEach
    void setUpExtra() {
        factory().register(DeviceInfoEntity.class);
        factory().register(TestAcknowledgmentEntity.class);

        userHelper.setUp();
        user = UserServiceFactory.getUserService().getCurrentUser();

        testKey = KeyFactory.createKey(TestAcknowledgmentEntity.KIND, "test");

        allTestCases = new HashSet<>();
        allTestCases.add("testCase1");
        allTestCases.add("testCase2");
        allTestCases.add("testCase3");

        allDevices = new ArrayList<>();
        DeviceInfoEntity device1 =
                new DeviceInfoEntity(
                        testKey, "branch1", "product1", "flavor1", "1234", "32", "abi");
        DeviceInfoEntity device2 =
                new DeviceInfoEntity(
                        testKey, "branch2", "product2", "flavor2", "1235", "32", "abi");
        allDevices.add(device1);
        allDevices.add(device2);
    }

    @AfterEach
    public void tearDown() {
        userHelper.tearDown();
    }

    /** Test that acknowledge-all works correctly. */
    @Test
    public void testSeparateAcknowledgedAll() {

        Set<String> testCases = new HashSet<>(allTestCases);
        List<TestAcknowledgmentEntity> acks = new ArrayList<>();
        TestAcknowledgmentEntity ack =
                new TestAcknowledgmentEntity(testKey, user, null, null, null, null);
        acks.add(ack);

        Set<String> acknowledged =
                VtsAlertJobServlet.separateAcknowledged(testCases, allDevices, acks);
        assertEquals(allTestCases.size(), acknowledged.size());
        assertTrue(acknowledged.containsAll(allTestCases));
        assertEquals(0, testCases.size());
    }

    /** Test that specific branch/device/test case acknowledgement works correctly. */
    @Test
    public void testSeparateAcknowledgedSpecific() {

        Set<String> testCases = new HashSet<>(allTestCases);
        List<TestAcknowledgmentEntity> acks = new ArrayList<>();
        List<String> branches = new ArrayList<>();
        branches.add("branch1");

        List<String> devices = new ArrayList<>();
        devices.add("flavor2");

        List<String> testCaseNames = new ArrayList<>();
        testCaseNames.add("testCase1");

        TestAcknowledgmentEntity ack =
                new TestAcknowledgmentEntity(
                        testKey, user, branches, devices, testCaseNames, null);
        acks.add(ack);

        Set<String> acknowledged =
                VtsAlertJobServlet.separateAcknowledged(testCases, allDevices, acks);
        assertEquals(0, acknowledged.size());
        assertEquals(allTestCases.size(), testCases.size());
    }

    /** Test that specific branch/device/test case acknowledgement skips device mismatches. */
    @Test
    public void testSeparateAcknowledgedSpecificMismatch() {

        Set<String> testCases = new HashSet<>(allTestCases);
        List<TestAcknowledgmentEntity> acks = new ArrayList<>();
        List<String> branches = new ArrayList<>();
        branches.add("branch1");

        List<String> devices = new ArrayList<>();
        devices.add("flavor1");

        List<String> testCaseNames = new ArrayList<>();
        testCaseNames.add("testCase1");

        TestAcknowledgmentEntity ack =
                new TestAcknowledgmentEntity(
                        testKey, user, branches, devices, testCaseNames, null);
        acks.add(ack);

        Set<String> acknowledged =
                VtsAlertJobServlet.separateAcknowledged(testCases, allDevices, acks);
        assertEquals(1, acknowledged.size());
        assertTrue(acknowledged.contains("testCase1"));
        assertTrue(!testCases.contains("testCase1"));
    }
}