Replaced all tabs with spaces in java and pom.xml
[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.ArgumentMatchers.any;
28 import static org.mockito.ArgumentMatchers.anyObject;
29 import static org.mockito.ArgumentMatchers.anyString;
30 import java.net.URI;
31 import java.util.Collections;
32 import java.util.List;
33 import org.springframework.test.util.ReflectionTestUtils;
34 import javax.ws.rs.container.ContainerRequestContext;
35 import javax.ws.rs.core.Response;
36 import javax.ws.rs.core.UriBuilder;
37 import org.springframework.core.ParameterizedTypeReference;
38 import org.springframework.http.HttpEntity;
39 import org.springframework.http.HttpMethod;
40 import org.springframework.http.HttpStatus;
41 import org.springframework.http.MediaType;
42 import org.json.JSONException;
43 import org.junit.Rule;
44 import org.junit.Test;
45 import org.mockito.InjectMocks;
46 import org.mockito.ArgumentMatchers;
47 import org.mockito.Mock;
48 import org.mockito.Mockito;
49 import org.mockito.Spy;
50 import org.mockito.junit.MockitoJUnit;
51 import org.mockito.junit.MockitoRule;
52 import org.springframework.http.ResponseEntity;
53 import org.springframework.web.client.RestTemplate;
54
55
56 public class GlobalHealthcheckHandlerTest {
57     @Mock
58     RestTemplate restTemplate;
59
60     @Mock
61     ContainerRequestContext requestContext;
62
63     @InjectMocks
64     @Spy
65     GlobalHealthcheckHandler globalhealth;
66
67     @Rule
68     public MockitoRule mockitoRule = MockitoJUnit.rule();
69
70     @Test
71     public void testQuerySubsystemHealthNullResult() {
72         ReflectionTestUtils.setField(globalhealth, "actuatorContextPath", "/manage");
73         ReflectionTestUtils.setField(globalhealth, "endpointBpmn", "http://localhost:8080");
74
75         Mockito.when(restTemplate.exchange(ArgumentMatchers.any(URI.class), ArgumentMatchers.any(HttpMethod.class),
76                 ArgumentMatchers.<HttpEntity<?>>any(), ArgumentMatchers.<Class<Object>>any())).thenReturn(null);
77
78         String result = globalhealth.querySubsystemHealth(MsoSubsystems.BPMN);
79         System.out.println(result);
80         assertEquals(HealthcheckStatus.DOWN.toString(), result);
81     }
82
83     @Test
84     public void testQuerySubsystemHealthNotNullResult() {
85         ReflectionTestUtils.setField(globalhealth, "actuatorContextPath", "/manage");
86         ReflectionTestUtils.setField(globalhealth, "endpointAsdc", "http://localhost:8080");
87
88         SubsystemHealthcheckResponse subSystemResponse = new SubsystemHealthcheckResponse();
89         subSystemResponse.setStatus("UP");
90         ResponseEntity<Object> r = new ResponseEntity<>(subSystemResponse, HttpStatus.OK);
91
92         Mockito.when(restTemplate.exchange(ArgumentMatchers.any(URI.class), ArgumentMatchers.any(HttpMethod.class),
93                 ArgumentMatchers.<HttpEntity<?>>any(), ArgumentMatchers.<Class<Object>>any())).thenReturn(r);
94
95         String result = globalhealth.querySubsystemHealth(MsoSubsystems.ASDC);
96         System.out.println(result);
97         assertEquals(HealthcheckStatus.UP.toString(), result);
98     }
99
100     private Response globalHealthcheck(String status) {
101         ReflectionTestUtils.setField(globalhealth, "actuatorContextPath", "/manage");
102         ReflectionTestUtils.setField(globalhealth, "endpointAsdc", "http://localhost:8080");
103         ReflectionTestUtils.setField(globalhealth, "endpointSdnc", "http://localhost:8081");
104         ReflectionTestUtils.setField(globalhealth, "endpointBpmn", "http://localhost:8082");
105         ReflectionTestUtils.setField(globalhealth, "endpointCatalogdb", "http://localhost:8083");
106         ReflectionTestUtils.setField(globalhealth, "endpointOpenstack", "http://localhost:8084");
107         ReflectionTestUtils.setField(globalhealth, "endpointRequestdb", "http://localhost:8085");
108         ReflectionTestUtils.setField(globalhealth, "endpointRequestdbAttsvc", "http://localhost:8086");
109
110         SubsystemHealthcheckResponse subSystemResponse = new SubsystemHealthcheckResponse();
111
112         subSystemResponse.setStatus(status);
113         ResponseEntity<Object> r = new ResponseEntity<>(subSystemResponse, HttpStatus.OK);
114         Mockito.when(restTemplate.exchange(ArgumentMatchers.any(URI.class), ArgumentMatchers.any(HttpMethod.class),
115                 ArgumentMatchers.<HttpEntity<?>>any(), ArgumentMatchers.<Class<Object>>any())).thenReturn(r);
116
117         Mockito.when(requestContext.getProperty(anyString())).thenReturn("1234567890");
118         Response response = globalhealth.globalHealthcheck(true, requestContext);
119
120         return response;
121     }
122
123     @Test
124     public void globalHealthcheckAllUPTest() throws JSONException {
125         Response response = globalHealthcheck("UP");
126         assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
127         HealthcheckResponse root;
128         root = (HealthcheckResponse) response.getEntity();
129         String apistatus = root.getApih();
130         assertTrue(apistatus.equalsIgnoreCase(HealthcheckStatus.UP.toString()));
131
132         String bpmnstatus = root.getBpmn();
133         assertTrue(bpmnstatus.equalsIgnoreCase(HealthcheckStatus.UP.toString()));
134
135         String sdncstatus = root.getSdncAdapter();
136         assertTrue(sdncstatus.equalsIgnoreCase(HealthcheckStatus.UP.toString()));
137
138         String asdcstatus = root.getAsdcController();
139         assertTrue(asdcstatus.equalsIgnoreCase(HealthcheckStatus.UP.toString()));
140
141         String catastatus = root.getCatalogdbAdapter();
142         assertTrue(catastatus.equalsIgnoreCase(HealthcheckStatus.UP.toString()));
143
144         String reqdbstatus = root.getRequestdbAdapter();
145         assertTrue(reqdbstatus.equalsIgnoreCase(HealthcheckStatus.UP.toString()));
146
147         String openstatus = root.getOpenstackAdapter();
148         assertTrue(openstatus.equalsIgnoreCase(HealthcheckStatus.UP.toString()));
149
150         String reqdbattstatus = root.getRequestdbAdapterAttsvc();
151         assertTrue(reqdbattstatus.equalsIgnoreCase(HealthcheckStatus.UP.toString()));
152     }
153
154     @Test
155     public void globalHealthcheckAllDOWNTest() throws JSONException {
156         Response response = globalHealthcheck("DOWN");
157         assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
158         HealthcheckResponse root;
159         root = (HealthcheckResponse) response.getEntity();
160         String apistatus = root.getApih();
161         assertTrue(apistatus.equalsIgnoreCase(HealthcheckStatus.UP.toString()));
162
163         String bpmnstatus = root.getBpmn();
164         assertTrue(bpmnstatus.equalsIgnoreCase(HealthcheckStatus.DOWN.toString()));
165
166         String sdncstatus = root.getSdncAdapter();
167         assertTrue(sdncstatus.equalsIgnoreCase(HealthcheckStatus.DOWN.toString()));
168
169         String asdcstatus = root.getAsdcController();
170         assertTrue(asdcstatus.equalsIgnoreCase(HealthcheckStatus.DOWN.toString()));
171
172         String catastatus = root.getCatalogdbAdapter();
173         assertTrue(catastatus.equalsIgnoreCase(HealthcheckStatus.DOWN.toString()));
174
175         String reqdbstatus = root.getRequestdbAdapter();
176         assertTrue(reqdbstatus.equalsIgnoreCase(HealthcheckStatus.DOWN.toString()));
177
178         String openstatus = root.getOpenstackAdapter();
179         assertTrue(openstatus.equalsIgnoreCase(HealthcheckStatus.DOWN.toString()));
180
181         String reqdbattstatus = root.getRequestdbAdapterAttsvc();
182         assertTrue(reqdbattstatus.equalsIgnoreCase(HealthcheckStatus.DOWN.toString()));
183     }
184
185     @Test
186     public void buildHttpEntityForRequestTest() {
187         HttpEntity<String> he = globalhealth.buildHttpEntityForRequest();
188         assertEquals(MediaType.APPLICATION_JSON, he.getHeaders().getAccept().get(0));
189         assertEquals(MediaType.APPLICATION_JSON, he.getHeaders().getContentType());
190     }
191
192     @Test
193     public void getEndpointUrlForSubsystemEnumTest() {
194         ReflectionTestUtils.setField(globalhealth, "actuatorContextPath", "/manage");
195         ReflectionTestUtils.setField(globalhealth, "endpointAsdc", "http://localhost:8080");
196         ReflectionTestUtils.setField(globalhealth, "endpointSdnc", "http://localhost:8081");
197         ReflectionTestUtils.setField(globalhealth, "endpointBpmn", "http://localhost:8082");
198         ReflectionTestUtils.setField(globalhealth, "endpointCatalogdb", "http://localhost:8083");
199         ReflectionTestUtils.setField(globalhealth, "endpointOpenstack", "http://localhost:8084");
200         ReflectionTestUtils.setField(globalhealth, "endpointRequestdb", "http://localhost:8085");
201         ReflectionTestUtils.setField(globalhealth, "endpointRequestdbAttsvc", "http://localhost:8086");
202
203         String result = globalhealth.getEndpointUrlForSubsystemEnum(MsoSubsystems.ASDC);
204         assertEquals("http://localhost:8080", result);
205         result = globalhealth.getEndpointUrlForSubsystemEnum(MsoSubsystems.SDNC);
206         assertEquals("http://localhost:8081", result);
207         result = globalhealth.getEndpointUrlForSubsystemEnum(MsoSubsystems.BPMN);
208         assertEquals("http://localhost:8082", result);
209         result = globalhealth.getEndpointUrlForSubsystemEnum(MsoSubsystems.CATALOGDB);
210         assertEquals("http://localhost:8083", result);
211         result = globalhealth.getEndpointUrlForSubsystemEnum(MsoSubsystems.OPENSTACK);
212         assertEquals("http://localhost:8084", result);
213         result = globalhealth.getEndpointUrlForSubsystemEnum(MsoSubsystems.REQUESTDB);
214         assertEquals("http://localhost:8085", result);
215         result = globalhealth.getEndpointUrlForSubsystemEnum(MsoSubsystems.REQUESTDBATT);
216         assertEquals("http://localhost:8086", result);
217     }
218
219     @Test
220     public void processResponseFromSubsystemTest() {
221         SubsystemHealthcheckResponse subSystemResponse = new SubsystemHealthcheckResponse();
222         subSystemResponse.setStatus("UP");
223         ResponseEntity<SubsystemHealthcheckResponse> r = new ResponseEntity<>(subSystemResponse, HttpStatus.OK);
224         String result = globalhealth.processResponseFromSubsystem(r, MsoSubsystems.BPMN);
225         assertEquals("UP", result);
226     }
227
228 }