Home | History | Annotate | Download | only in job
      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 org.junit.Assert.assertEquals;
     20 import static org.junit.Assert.assertTrue;
     21 
     22 import com.android.vts.entity.DeviceInfoEntity;
     23 import com.android.vts.entity.TestAcknowledgmentEntity;
     24 import com.google.appengine.api.datastore.Key;
     25 import com.google.appengine.api.datastore.KeyFactory;
     26 import com.google.appengine.api.users.User;
     27 import com.google.appengine.api.users.UserServiceFactory;
     28 import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
     29 import com.google.appengine.tools.development.testing.LocalUserServiceTestConfig;
     30 import java.util.ArrayList;
     31 import java.util.HashSet;
     32 import java.util.List;
     33 import java.util.Set;
     34 import org.junit.After;
     35 import org.junit.Before;
     36 import org.junit.Test;
     37 
     38 public class VtsAlertJobServletTest {
     39     private final LocalServiceTestHelper userHelper =
     40             new LocalServiceTestHelper(new LocalUserServiceTestConfig())
     41                     .setEnvIsAdmin(true)
     42                     .setEnvIsLoggedIn(true)
     43                     .setEnvEmail("testemail (at) domain.com")
     44                     .setEnvAuthDomain("test");
     45 
     46     User user;
     47     private Key testKey;
     48     private Set<String> allTestCases;
     49     private List<DeviceInfoEntity> allDevices;
     50 
     51     @Before
     52     public void setUp() {
     53         userHelper.setUp();
     54         user = UserServiceFactory.getUserService().getCurrentUser();
     55 
     56         testKey = KeyFactory.createKey(TestAcknowledgmentEntity.KIND, "test");
     57 
     58         allTestCases = new HashSet<>();
     59         allTestCases.add("testCase1");
     60         allTestCases.add("testCase2");
     61         allTestCases.add("testCase3");
     62 
     63         allDevices = new ArrayList<>();
     64         DeviceInfoEntity device1 =
     65                 new DeviceInfoEntity(
     66                         testKey, "branch1", "product1", "flavor1", "1234", "32", "abi");
     67         DeviceInfoEntity device2 =
     68                 new DeviceInfoEntity(
     69                         testKey, "branch2", "product2", "flavor2", "1235", "32", "abi");
     70         allDevices.add(device1);
     71         allDevices.add(device2);
     72     }
     73 
     74     @After
     75     public void tearDown() {
     76         userHelper.tearDown();
     77     }
     78 
     79     /** Test that acknowledge-all works correctly. */
     80     @Test
     81     public void testSeparateAcknowledgedAll() {
     82         Set<String> testCases = new HashSet<>(allTestCases);
     83         List<TestAcknowledgmentEntity> acks = new ArrayList<>();
     84         TestAcknowledgmentEntity ack =
     85                 new TestAcknowledgmentEntity(testKey, user, null, null, null, null);
     86         acks.add(ack);
     87 
     88         Set<String> acknowledged =
     89                 VtsAlertJobServlet.separateAcknowledged(testCases, allDevices, acks);
     90         assertEquals(allTestCases.size(), acknowledged.size());
     91         assertTrue(acknowledged.containsAll(allTestCases));
     92         assertEquals(0, testCases.size());
     93     }
     94 
     95     /** Test that specific branch/device/test case acknowledgement works correctly. */
     96     @Test
     97     public void testSeparateAcknowledgedSpecific() {
     98         Set<String> testCases = new HashSet<>(allTestCases);
     99         List<TestAcknowledgmentEntity> acks = new ArrayList<>();
    100         List<String> branches = new ArrayList<>();
    101         branches.add("branch1");
    102 
    103         List<String> devices = new ArrayList<>();
    104         devices.add("flavor2");
    105 
    106         List<String> testCaseNames = new ArrayList<>();
    107         testCaseNames.add("testCase1");
    108 
    109         TestAcknowledgmentEntity ack =
    110                 new TestAcknowledgmentEntity(
    111                         testKey, user, branches, devices, testCaseNames, null);
    112         acks.add(ack);
    113 
    114         Set<String> acknowledged =
    115                 VtsAlertJobServlet.separateAcknowledged(testCases, allDevices, acks);
    116         assertEquals(0, acknowledged.size());
    117         assertEquals(allTestCases.size(), testCases.size());
    118     }
    119 
    120     /** Test that specific branch/device/test case acknowledgement skips device mismatches. */
    121     @Test
    122     public void testSeparateAcknowledgedSpecificMismatch() {
    123         Set<String> testCases = new HashSet<>(allTestCases);
    124         List<TestAcknowledgmentEntity> acks = new ArrayList<>();
    125         List<String> branches = new ArrayList<>();
    126         branches.add("branch1");
    127 
    128         List<String> devices = new ArrayList<>();
    129         devices.add("flavor1");
    130 
    131         List<String> testCaseNames = new ArrayList<>();
    132         testCaseNames.add("testCase1");
    133 
    134         TestAcknowledgmentEntity ack =
    135                 new TestAcknowledgmentEntity(
    136                         testKey, user, branches, devices, testCaseNames, null);
    137         acks.add(ack);
    138 
    139         Set<String> acknowledged =
    140                 VtsAlertJobServlet.separateAcknowledged(testCases, allDevices, acks);
    141         assertEquals(1, acknowledged.size());
    142         assertTrue(acknowledged.contains("testCase1"));
    143         assertTrue(!testCases.contains("testCase1"));
    144     }
    145 }
    146