1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you
5  * may not use this file except in compliance with the License. You may
6  * 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
13  * implied. See the License for the specific language governing
14  * permissions and limitations under the License.
15  */
16 
17 package com.android.vts.job;
18 
19 import static com.googlecode.objectify.ObjectifyService.factory;
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertTrue;
22 
23 import com.android.vts.entity.DeviceInfoEntity;
24 import com.android.vts.entity.TestAcknowledgmentEntity;
25 import com.android.vts.util.ObjectifyTestBase;
26 import com.google.appengine.api.datastore.Key;
27 import com.google.appengine.api.datastore.KeyFactory;
28 import com.google.appengine.api.users.User;
29 import com.google.appengine.api.users.UserServiceFactory;
30 import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
31 import com.google.appengine.tools.development.testing.LocalUserServiceTestConfig;
32 import java.util.ArrayList;
33 import java.util.HashSet;
34 import java.util.List;
35 import java.util.Set;
36 
37 import org.junit.jupiter.api.AfterEach;
38 import org.junit.jupiter.api.Test;
39 import org.junit.jupiter.api.BeforeEach;
40 
41 public class VtsAlertJobServletTest extends ObjectifyTestBase {
42     private final LocalServiceTestHelper userHelper =
43             new LocalServiceTestHelper(new LocalUserServiceTestConfig())
44                     .setEnvIsAdmin(true)
45                     .setEnvIsLoggedIn(true)
46                     .setEnvEmail("testemail@domain.com")
47                     .setEnvAuthDomain("test");
48 
49     User user;
50     private Key testKey;
51     private Set<String> allTestCases;
52     private List<DeviceInfoEntity> allDevices;
53 
54     @BeforeEach
setUpExtra()55     void setUpExtra() {
56         factory().register(DeviceInfoEntity.class);
57         factory().register(TestAcknowledgmentEntity.class);
58 
59         userHelper.setUp();
60         user = UserServiceFactory.getUserService().getCurrentUser();
61 
62         testKey = KeyFactory.createKey(TestAcknowledgmentEntity.KIND, "test");
63 
64         allTestCases = new HashSet<>();
65         allTestCases.add("testCase1");
66         allTestCases.add("testCase2");
67         allTestCases.add("testCase3");
68 
69         allDevices = new ArrayList<>();
70         DeviceInfoEntity device1 =
71                 new DeviceInfoEntity(
72                         testKey, "branch1", "product1", "flavor1", "1234", "32", "abi");
73         DeviceInfoEntity device2 =
74                 new DeviceInfoEntity(
75                         testKey, "branch2", "product2", "flavor2", "1235", "32", "abi");
76         allDevices.add(device1);
77         allDevices.add(device2);
78     }
79 
80     @AfterEach
tearDown()81     public void tearDown() {
82         userHelper.tearDown();
83     }
84 
85     /** Test that acknowledge-all works correctly. */
86     @Test
testSeparateAcknowledgedAll()87     public void testSeparateAcknowledgedAll() {
88 
89         Set<String> testCases = new HashSet<>(allTestCases);
90         List<TestAcknowledgmentEntity> acks = new ArrayList<>();
91         TestAcknowledgmentEntity ack =
92                 new TestAcknowledgmentEntity(testKey, user, null, null, null, null);
93         acks.add(ack);
94 
95         Set<String> acknowledged =
96                 VtsAlertJobServlet.separateAcknowledged(testCases, allDevices, acks);
97         assertEquals(allTestCases.size(), acknowledged.size());
98         assertTrue(acknowledged.containsAll(allTestCases));
99         assertEquals(0, testCases.size());
100     }
101 
102     /** Test that specific branch/device/test case acknowledgement works correctly. */
103     @Test
testSeparateAcknowledgedSpecific()104     public void testSeparateAcknowledgedSpecific() {
105 
106         Set<String> testCases = new HashSet<>(allTestCases);
107         List<TestAcknowledgmentEntity> acks = new ArrayList<>();
108         List<String> branches = new ArrayList<>();
109         branches.add("branch1");
110 
111         List<String> devices = new ArrayList<>();
112         devices.add("flavor2");
113 
114         List<String> testCaseNames = new ArrayList<>();
115         testCaseNames.add("testCase1");
116 
117         TestAcknowledgmentEntity ack =
118                 new TestAcknowledgmentEntity(
119                         testKey, user, branches, devices, testCaseNames, null);
120         acks.add(ack);
121 
122         Set<String> acknowledged =
123                 VtsAlertJobServlet.separateAcknowledged(testCases, allDevices, acks);
124         assertEquals(0, acknowledged.size());
125         assertEquals(allTestCases.size(), testCases.size());
126     }
127 
128     /** Test that specific branch/device/test case acknowledgement skips device mismatches. */
129     @Test
testSeparateAcknowledgedSpecificMismatch()130     public void testSeparateAcknowledgedSpecificMismatch() {
131 
132         Set<String> testCases = new HashSet<>(allTestCases);
133         List<TestAcknowledgmentEntity> acks = new ArrayList<>();
134         List<String> branches = new ArrayList<>();
135         branches.add("branch1");
136 
137         List<String> devices = new ArrayList<>();
138         devices.add("flavor1");
139 
140         List<String> testCaseNames = new ArrayList<>();
141         testCaseNames.add("testCase1");
142 
143         TestAcknowledgmentEntity ack =
144                 new TestAcknowledgmentEntity(
145                         testKey, user, branches, devices, testCaseNames, null);
146         acks.add(ack);
147 
148         Set<String> acknowledged =
149                 VtsAlertJobServlet.separateAcknowledged(testCases, allDevices, acks);
150         assertEquals(1, acknowledged.size());
151         assertTrue(acknowledged.contains("testCase1"));
152         assertTrue(!testCases.contains("testCase1"));
153     }
154 }
155