ed77e2527c35a1bf8f2d4e6f5a9276dfd8ea72d5
[aai/sparky-be.git] / src / test / java / org / onap / aai / sparky / search / VnfSearchServiceTest.java
1 /**\r
2  * ============LICENSE_START=======================================================\r
3  * org.onap.aai\r
4  * ================================================================================\r
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * Copyright © 2017 Amdocs\r
7  * ================================================================================\r
8  * Licensed under the Apache License, Version 2.0 (the "License");\r
9  * you may not use this file except in compliance with the License.\r
10  * You may obtain a copy of the License at\r
11  *\r
12  *       http://www.apache.org/licenses/LICENSE-2.0\r
13  *\r
14  * Unless required by applicable law or agreed to in writing, software\r
15  * distributed under the License is distributed on an "AS IS" BASIS,\r
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
17  * See the License for the specific language governing permissions and\r
18  * limitations under the License.\r
19  * ============LICENSE_END=========================================================\r
20  *\r
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
22  */\r
23 package org.onap.aai.sparky.search;\r
24 \r
25 import java.io.File;\r
26 import java.io.IOException;\r
27 import java.io.PrintWriter;\r
28 import java.lang.reflect.Field;\r
29 import java.lang.reflect.Modifier;\r
30 \r
31 import javax.servlet.http.HttpServletResponse;\r
32 \r
33 import org.junit.Before;\r
34 import org.junit.Test;\r
35 import org.mockito.Mockito;\r
36 import org.onap.aai.sparky.dal.elasticsearch.SearchAdapter;\r
37 import org.onap.aai.sparky.dal.elasticsearch.config.ElasticSearchConfig;\r
38 import org.onap.aai.sparky.dal.rest.OperationResult;\r
39 import org.onap.aai.sparky.viewandinspect.config.TierSupportUiConstants;\r
40 import org.onap.aai.sparky.viewandinspect.entity.QuerySearchEntity;\r
41 \r
42 public class VnfSearchServiceTest {\r
43 \r
44   VnfSearchService searchService;\r
45 \r
46   @Before\r
47   public void init() throws NoSuchFieldException, SecurityException, IllegalArgumentException,\r
48       IllegalAccessException, IOException {\r
49     System.setProperty("AJSC_HOME", new File(".").getCanonicalPath().replace('\\', '/'));\r
50     TierSupportUiConstants.DYNAMIC_CONFIG_APP_LOCATION =\r
51         System.getProperty("AJSC_HOME") + "/src/test/resources/appconfig/";\r
52     ElasticSearchConfig.setConfig(null);\r
53     setFinalStatic();\r
54     searchService = new VnfSearchService();\r
55   }\r
56 \r
57   @Test\r
58   public void testSetZeroCountResponse() throws IOException {\r
59     HttpServletResponse response = Mockito.mock(HttpServletResponse.class);\r
60     Mockito.doNothing().when(response).setStatus(1);\r
61     Mockito.doNothing().when(response).setContentType(Mockito.anyString());\r
62     PrintWriter writer = Mockito.mock(PrintWriter.class);\r
63     Mockito.doNothing().when(writer).println(Mockito.anyString());\r
64     Mockito.doNothing().when(writer).close();\r
65     Mockito.when(response.getWriter()).thenReturn(writer);\r
66     searchService.setZeroCountResponse(response);\r
67   }\r
68 \r
69   @Test\r
70   public void testSetEmptyAggResponse() throws IOException {\r
71     HttpServletResponse response = Mockito.mock(HttpServletResponse.class);\r
72     Mockito.doNothing().when(response).setStatus(1);\r
73     Mockito.doNothing().when(response).setContentType(Mockito.anyString());\r
74     PrintWriter writer = Mockito.mock(PrintWriter.class);\r
75     Mockito.doNothing().when(writer).println(Mockito.anyString());\r
76     Mockito.doNothing().when(writer).close();\r
77     Mockito.when(response.getWriter()).thenReturn(writer);\r
78     searchService.setEmptyAggResponse(response);\r
79   }\r
80 \r
81 \r
82   static void setFinalStatic() throws NoSuchFieldException, SecurityException,\r
83       IllegalArgumentException, IllegalAccessException {\r
84     Field configField = ElasticSearchConfig.class.getDeclaredField("CONFIG_FILE");\r
85     configField.setAccessible(true);\r
86 \r
87     Field modifiersField = Field.class.getDeclaredField("modifiers");\r
88     modifiersField.setAccessible(true);\r
89     modifiersField.setInt(configField, configField.getModifiers() & ~Modifier.FINAL);\r
90 \r
91     configField.set(null,\r
92         System.getProperty("AJSC_HOME") + "/src/test/resources/appconfig/elasticsearch.properties");\r
93   }\r
94 }\r