Springboot 2.0 upgrade
[so.git] / mso-api-handlers / mso-api-handler-infra / src / test / java / org / onap / so / apihandlerinfra / GlobalHealthcheckHandlerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.apihandlerinfra;
22
23 import static org.junit.Assert.assertArrayEquals;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertTrue;
27 import static org.mockito.Matchers.any;
28
29 import static org.mockito.Matchers.anyObject;
30 import static org.mockito.Matchers.anyString;
31 import java.net.URI;
32 import java.util.Collections;
33 import java.util.List;
34
35 import org.springframework.test.util.ReflectionTestUtils;
36 import javax.ws.rs.container.ContainerRequestContext;
37 import javax.ws.rs.core.Response;
38 import javax.ws.rs.core.UriBuilder;
39
40 import org.springframework.core.ParameterizedTypeReference;
41 import org.springframework.http.HttpEntity;
42 import org.springframework.http.HttpMethod;
43 import org.springframework.http.HttpStatus;
44 import org.springframework.http.MediaType;
45 import org.json.JSONException;
46 import org.junit.Rule;
47 import org.junit.Test;
48 import org.mockito.InjectMocks;
49 import org.mockito.Matchers;
50 import org.mockito.Mock;
51 import org.mockito.Mockito;
52 import org.mockito.Spy;
53 import org.mockito.junit.MockitoJUnit;
54 import org.mockito.junit.MockitoRule;
55 import org.springframework.http.ResponseEntity;
56 import org.springframework.web.client.RestTemplate;
57
58
59 public class GlobalHealthcheckHandlerTest {
60     @Mock
61     RestTemplate restTemplate;
62
63     @Mock
64     ContainerRequestContext requestContext;
65     
66     @InjectMocks
67     @Spy
68         GlobalHealthcheckHandler globalhealth;
69         
70     @Rule public MockitoRule mockitoRule = MockitoJUnit.rule(); 
71     
72     @Test
73     public void testQuerySubsystemHealthNullResult(){
74         ReflectionTestUtils.setField(globalhealth, "actuatorContextPath", "/manage");
75         ReflectionTestUtils.setField(globalhealth, "endpointBpmn", "http://localhost:8080");
76         
77                 Mockito.when(restTemplate.exchange(Matchers.any(URI.class), 
78                                 Matchers.any(HttpMethod.class), 
79                                 Matchers.<HttpEntity<?>> any(), 
80                                 Matchers.<Class<Object>> any())).thenReturn(null);
81                 
82                 String result = globalhealth.querySubsystemHealth(MsoSubsystems.BPMN);
83                 System.out.println(result);
84                 assertEquals(HealthcheckStatus.DOWN.toString(),result);         
85     }
86     
87     @Test
88     public void testQuerySubsystemHealthNotNullResult(){
89         ReflectionTestUtils.setField(globalhealth, "actuatorContextPath", "/manage");
90         ReflectionTestUtils.setField(globalhealth, "endpointAsdc", "http://localhost:8080");
91         
92                 SubsystemHealthcheckResponse subSystemResponse = new SubsystemHealthcheckResponse();
93                 subSystemResponse.setStatus("UP");
94                 ResponseEntity<Object> r = new ResponseEntity<>(subSystemResponse,HttpStatus.OK);
95                 
96                 Mockito.when(restTemplate.exchange(Matchers.any(URI.class), 
97                                 Matchers.any(HttpMethod.class), 
98                                 Matchers.<HttpEntity<?>> any(), 
99                                 Matchers.<Class<Object>> any())).thenReturn(r);
100                 
101                 String result = globalhealth.querySubsystemHealth(MsoSubsystems.ASDC);
102                 System.out.println(result);
103                 assertEquals(HealthcheckStatus.UP.toString(),result);           
104     }
105     
106     private Response globalHealthcheck (String status){
107         ReflectionTestUtils.setField(globalhealth, "actuatorContextPath", "/manage");
108         ReflectionTestUtils.setField(globalhealth, "endpointAsdc", "http://localhost:8080");
109         ReflectionTestUtils.setField(globalhealth, "endpointSdnc", "http://localhost:8081");
110         ReflectionTestUtils.setField(globalhealth, "endpointBpmn", "http://localhost:8082");
111         ReflectionTestUtils.setField(globalhealth, "endpointCatalogdb", "http://localhost:8083");
112         ReflectionTestUtils.setField(globalhealth, "endpointOpenstack", "http://localhost:8084");
113         ReflectionTestUtils.setField(globalhealth, "endpointRequestdb", "http://localhost:8085");
114         ReflectionTestUtils.setField(globalhealth, "endpointRequestdbAttsvc", "http://localhost:8086");
115
116                 SubsystemHealthcheckResponse subSystemResponse = new SubsystemHealthcheckResponse();
117
118                 subSystemResponse.setStatus(status);
119                 ResponseEntity<Object> r = new ResponseEntity<>(subSystemResponse,HttpStatus.OK);               
120                 Mockito.when(restTemplate.exchange(Matchers.any(URI.class), 
121                                 Matchers.any(HttpMethod.class), 
122                                 Matchers.<HttpEntity<?>> any(), 
123                                 Matchers.<Class<Object>> any())).thenReturn(r);
124                 
125                 Mockito.when(requestContext.getProperty(anyString())).thenReturn("1234567890");
126                 Response response = globalhealth.globalHealthcheck(true, requestContext);
127                 
128                 return response;
129     }
130     
131         @Test
132         public void globalHealthcheckAllUPTest() throws JSONException {
133                 Response response = globalHealthcheck("UP");
134                 assertEquals(Response.Status.OK.getStatusCode(),response.getStatus());
135                 HealthcheckResponse root;
136                 root = (HealthcheckResponse) response.getEntity();              
137                 String apistatus = root.getApih();
138                 assertTrue(apistatus.equalsIgnoreCase(HealthcheckStatus.UP.toString()));
139                 
140                 String bpmnstatus = root.getBpmn();
141                 assertTrue(bpmnstatus.equalsIgnoreCase(HealthcheckStatus.UP.toString()));
142                 
143                 String sdncstatus = root.getSdncAdapter();
144                 assertTrue(sdncstatus.equalsIgnoreCase(HealthcheckStatus.UP.toString()));
145                 
146                 String asdcstatus = root.getAsdcController();
147                 assertTrue(asdcstatus.equalsIgnoreCase(HealthcheckStatus.UP.toString()));
148                 
149                 String catastatus = root.getCatalogdbAdapter();
150                 assertTrue(catastatus.equalsIgnoreCase(HealthcheckStatus.UP.toString()));                       
151                 
152                 String reqdbstatus = root.getRequestdbAdapter();
153                 assertTrue(reqdbstatus.equalsIgnoreCase(HealthcheckStatus.UP.toString()));                      
154                 
155                 String openstatus = root.getOpenstackAdapter();
156                 assertTrue(openstatus.equalsIgnoreCase(HealthcheckStatus.UP.toString()));                       
157
158                 String reqdbattstatus = root.getRequestdbAdapterAttsvc();
159                 assertTrue(reqdbattstatus.equalsIgnoreCase(HealthcheckStatus.UP.toString()));                   
160         }
161         
162         @Test
163         public void globalHealthcheckAllDOWNTest() throws JSONException {
164                 Response response = globalHealthcheck("DOWN");
165                 assertEquals(Response.Status.OK.getStatusCode(),response.getStatus());
166                 HealthcheckResponse root;
167                 root = (HealthcheckResponse) response.getEntity();              
168                 String apistatus = root.getApih();
169                 assertTrue(apistatus.equalsIgnoreCase(HealthcheckStatus.UP.toString()));
170                 
171                 String bpmnstatus = root.getBpmn();
172                 assertTrue(bpmnstatus.equalsIgnoreCase(HealthcheckStatus.DOWN.toString()));
173                 
174                 String sdncstatus = root.getSdncAdapter();
175                 assertTrue(sdncstatus.equalsIgnoreCase(HealthcheckStatus.DOWN.toString()));
176                 
177                 String asdcstatus = root.getAsdcController();
178                 assertTrue(asdcstatus.equalsIgnoreCase(HealthcheckStatus.DOWN.toString()));
179                 
180                 String catastatus = root.getCatalogdbAdapter();
181                 assertTrue(catastatus.equalsIgnoreCase(HealthcheckStatus.DOWN.toString()));                     
182                 
183                 String reqdbstatus = root.getRequestdbAdapter();
184                 assertTrue(reqdbstatus.equalsIgnoreCase(HealthcheckStatus.DOWN.toString()));                    
185                 
186                 String openstatus = root.getOpenstackAdapter();
187                 assertTrue(openstatus.equalsIgnoreCase(HealthcheckStatus.DOWN.toString()));                     
188
189                 String reqdbattstatus = root.getRequestdbAdapterAttsvc();
190                 assertTrue(reqdbattstatus.equalsIgnoreCase(HealthcheckStatus.DOWN.toString()));                 
191         }
192
193         @Test
194         public void buildHttpEntityForRequestTest(){
195                 HttpEntity<String> he = globalhealth.buildHttpEntityForRequest();
196                 assertEquals (MediaType.APPLICATION_JSON,he.getHeaders().getAccept().get(0));
197                 assertEquals (MediaType.APPLICATION_JSON,he.getHeaders().getContentType());
198         }
199         
200         @Test
201         public void getEndpointUrlForSubsystemEnumTest(){
202         ReflectionTestUtils.setField(globalhealth, "actuatorContextPath", "/manage");
203         ReflectionTestUtils.setField(globalhealth, "endpointAsdc", "http://localhost:8080");
204         ReflectionTestUtils.setField(globalhealth, "endpointSdnc", "http://localhost:8081");
205         ReflectionTestUtils.setField(globalhealth, "endpointBpmn", "http://localhost:8082");
206         ReflectionTestUtils.setField(globalhealth, "endpointCatalogdb", "http://localhost:8083");
207         ReflectionTestUtils.setField(globalhealth, "endpointOpenstack", "http://localhost:8084");
208         ReflectionTestUtils.setField(globalhealth, "endpointRequestdb", "http://localhost:8085");
209         ReflectionTestUtils.setField(globalhealth, "endpointRequestdbAttsvc", "http://localhost:8086");
210         
211                 String result = globalhealth.getEndpointUrlForSubsystemEnum(MsoSubsystems.ASDC);
212                 assertEquals("http://localhost:8080", result);
213                 result = globalhealth.getEndpointUrlForSubsystemEnum(MsoSubsystems.SDNC);
214                 assertEquals("http://localhost:8081", result);
215                 result = globalhealth.getEndpointUrlForSubsystemEnum(MsoSubsystems.BPMN);
216                 assertEquals("http://localhost:8082", result);
217                 result = globalhealth.getEndpointUrlForSubsystemEnum(MsoSubsystems.CATALOGDB);
218                 assertEquals("http://localhost:8083", result);
219                 result = globalhealth.getEndpointUrlForSubsystemEnum(MsoSubsystems.OPENSTACK);
220                 assertEquals("http://localhost:8084", result);
221                 result = globalhealth.getEndpointUrlForSubsystemEnum(MsoSubsystems.REQUESTDB);
222                 assertEquals("http://localhost:8085", result);
223                 result = globalhealth.getEndpointUrlForSubsystemEnum(MsoSubsystems.REQUESTDBATT);
224                 assertEquals("http://localhost:8086", result);
225         }
226         
227         @Test
228         public void processResponseFromSubsystemTest(){
229                 SubsystemHealthcheckResponse subSystemResponse = new SubsystemHealthcheckResponse();
230                 subSystemResponse.setStatus("UP");
231                 ResponseEntity<SubsystemHealthcheckResponse> r = new ResponseEntity<>(subSystemResponse,HttpStatus.OK);
232                 String result = globalhealth.processResponseFromSubsystem(r,MsoSubsystems.BPMN);
233                 assertEquals("UP",result);
234         }
235         
236 }