1 /*
2  * Copyright (C) 2018 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.api;
18 
19 import static org.junit.Assert.assertEquals;
20 import static org.mockito.Mockito.when;
21 import static com.googlecode.objectify.ObjectifyService.factory;
22 
23 import com.android.vts.entity.ApiCoverageEntity;
24 import com.android.vts.entity.TestEntity;
25 import com.android.vts.entity.TestRunEntity;
26 import com.android.vts.util.ObjectifyTestBase;
27 import com.google.gson.Gson;
28 import com.google.gson.internal.LinkedTreeMap;
29 import com.googlecode.objectify.Key;
30 import java.io.IOException;
31 import java.io.PrintWriter;
32 import java.io.StringWriter;
33 
34 import java.util.Arrays;
35 import java.util.List;
36 import javax.servlet.ServletException;
37 import javax.servlet.http.HttpServletRequest;
38 import javax.servlet.http.HttpServletResponse;
39 
40 import org.junit.jupiter.api.Test;
41 import org.junit.jupiter.api.BeforeEach;
42 import org.mockito.Mock;
43 
44 public class CoverageRestServletTest extends ObjectifyTestBase {
45 
46     private Gson gson;
47 
48     @Mock
49     private HttpServletRequest request;
50 
51     @Mock
52     private HttpServletResponse response;
53 
54     /** It be executed before each @Test method */
55     @BeforeEach
setUpExtra()56     void setUpExtra() {
57         gson = new Gson();
58 
59         /********
60         System.getenv().forEach((k,v) -> {
61             System.out.println("key => " + k);
62             System.out.println("value => " + v);
63         });
64          *********/
65     }
66 
67     @Test
testApiData()68     public void testApiData() throws IOException, ServletException {
69 
70         factory().register(ApiCoverageEntity.class);
71 
72         List<String> halApi = Arrays.asList("allocate", "dumpDebugInfo");
73         List<String> coveredHalApi = Arrays.asList("allocate", "dumpDebugInfo");
74 
75         Key testParentKey = Key.create(TestEntity.class, "test1");
76         Key testRunParentKey = Key.create(testParentKey, TestRunEntity.class, 1);
77         ApiCoverageEntity apiCoverageEntity =
78                 new ApiCoverageEntity(
79                         testRunParentKey,
80                         "android.hardware.graphics.allocator",
81                         4,
82                         1,
83                         "IAllocator",
84                         halApi,
85                         coveredHalApi);
86         apiCoverageEntity.save();
87 
88         String key = apiCoverageEntity.getUrlSafeKey();
89 
90         when(request.getPathInfo()).thenReturn("/api/data");
91 
92         when(request.getParameter("key")).thenReturn(key);
93 
94         StringWriter sw = new StringWriter();
95         PrintWriter pw = new PrintWriter(sw);
96 
97         when(response.getWriter()).thenReturn(pw);
98 
99         CoverageRestServlet coverageRestServlet = new CoverageRestServlet();
100         coverageRestServlet.doGet(request, response);
101         String result = sw.getBuffer().toString().trim();
102 
103         LinkedTreeMap resultMap = gson.fromJson(result, LinkedTreeMap.class);
104 
105         assertEquals(resultMap.get("halInterfaceName"), "IAllocator");
106         assertEquals(resultMap.get("halPackageName"), "android.hardware.graphics.allocator");
107         assertEquals(resultMap.get("halApi"), Arrays.asList("allocate", "dumpDebugInfo"));
108         assertEquals(resultMap.get("coveredHalApi"), Arrays.asList("allocate", "dumpDebugInfo"));
109 
110     }
111 
112 }
113