999f441178989149aed848679ac511f85786a5f2
[so.git] /
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 com.shazam.shazamcrest.matcher.Matchers.sameBeanAs;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertThat;
26 import static org.mockito.ArgumentMatchers.anyString;
27 import java.net.URI;
28 import javax.ws.rs.container.ContainerRequestContext;
29 import javax.ws.rs.core.Response;
30 import javax.ws.rs.core.UriBuilder;
31 import org.json.JSONException;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.mockito.ArgumentMatchers;
35 import org.mockito.Mockito;
36 import org.onap.so.apihandlerinfra.HealthCheckConfig.Endpoint;
37 import org.springframework.boot.context.properties.EnableConfigurationProperties;
38 import org.springframework.boot.test.context.ConfigFileApplicationContextInitializer;
39 import org.springframework.boot.test.mock.mockito.MockBean;
40 import org.springframework.boot.test.mock.mockito.SpyBean;
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.springframework.http.ResponseEntity;
46 import org.springframework.test.context.ActiveProfiles;
47 import org.springframework.test.context.ContextConfiguration;
48 import org.springframework.test.context.junit4.SpringRunner;
49 import org.springframework.test.util.ReflectionTestUtils;
50 import org.springframework.web.client.RestTemplate;
51
52
53 @RunWith(SpringRunner.class)
54 @ContextConfiguration(classes = {GenericStringConverter.class, HealthCheckConverter.class},
55         initializers = {ConfigFileApplicationContextInitializer.class})
56 @ActiveProfiles("test")
57 @EnableConfigurationProperties({HealthCheckConfig.class})
58 public class GlobalHealthcheckHandlerTest {
59     @MockBean
60     private RestTemplate restTemplate;
61
62     @MockBean
63     private ContainerRequestContext requestContext;
64
65     @SpyBean
66     private GlobalHealthcheckHandler globalhealth;
67
68     @Test
69     public void testQuerySubsystemHealthNullResult() {
70         ReflectionTestUtils.setField(globalhealth, "actuatorContextPath", "/manage");
71
72         Mockito.when(restTemplate.exchange(ArgumentMatchers.any(URI.class), ArgumentMatchers.any(HttpMethod.class),
73                 ArgumentMatchers.<HttpEntity<?>>any(), ArgumentMatchers.<Class<Object>>any())).thenReturn(null);
74
75         HealthCheckSubsystem result = globalhealth
76                 .querySubsystemHealth(new Endpoint(SoSubsystems.BPMN, UriBuilder.fromPath("http://localhost").build()));
77         assertEquals(HealthCheckStatus.DOWN, result.getStatus());
78     }
79
80     @Test
81     public void testQuerySubsystemHealthNotNullResult() {
82         ReflectionTestUtils.setField(globalhealth, "actuatorContextPath", "/manage");
83
84         SubsystemHealthcheckResponse subSystemResponse = new SubsystemHealthcheckResponse();
85         subSystemResponse.setStatus("UP");
86         ResponseEntity<Object> r = new ResponseEntity<>(subSystemResponse, HttpStatus.OK);
87
88         Mockito.when(restTemplate.exchange(ArgumentMatchers.any(URI.class), ArgumentMatchers.any(HttpMethod.class),
89                 ArgumentMatchers.<HttpEntity<?>>any(), ArgumentMatchers.<Class<Object>>any())).thenReturn(r);
90
91         HealthCheckSubsystem result = globalhealth
92                 .querySubsystemHealth(new Endpoint(SoSubsystems.ASDC, UriBuilder.fromPath("http://localhost").build()));
93         assertEquals(HealthCheckStatus.UP, result.getStatus());
94     }
95
96     private Response globalHealthcheck(String status) {
97         ReflectionTestUtils.setField(globalhealth, "actuatorContextPath", "/manage");
98
99         SubsystemHealthcheckResponse subSystemResponse = new SubsystemHealthcheckResponse();
100
101         subSystemResponse.setStatus(status);
102         ResponseEntity<Object> r = new ResponseEntity<>(subSystemResponse, HttpStatus.OK);
103         Mockito.when(restTemplate.exchange(ArgumentMatchers.any(URI.class), ArgumentMatchers.any(HttpMethod.class),
104                 ArgumentMatchers.<HttpEntity<?>>any(), ArgumentMatchers.<Class<Object>>any())).thenReturn(r);
105
106         Mockito.when(requestContext.getProperty(anyString())).thenReturn("1234567890");
107         Response response = globalhealth.globalHealthcheck(true, requestContext);
108         return response;
109     }
110
111     @Test
112     public void globalHealthcheckAllUPTest() throws JSONException {
113
114         HealthCheckResponse expected = new HealthCheckResponse();
115
116         for (Subsystem system : SoSubsystems.values()) {
117             expected.getSubsystems().add(new HealthCheckSubsystem(system,
118                     UriBuilder.fromUri("http://localhost").build(), HealthCheckStatus.UP));
119         }
120         expected.setMessage("HttpStatus: 200");
121         Response response = globalHealthcheck("UP");
122         assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
123         HealthCheckResponse root;
124         root = (HealthCheckResponse) response.getEntity();
125         assertThat(root, sameBeanAs(expected).ignoring("subsystems.uri").ignoring("subsystems.subsystem"));
126
127     }
128
129     @Test
130     public void globalHealthcheckAllDOWNTest() throws JSONException {
131         HealthCheckResponse expected = new HealthCheckResponse();
132
133         for (Subsystem system : SoSubsystems.values()) {
134             expected.getSubsystems().add(new HealthCheckSubsystem(system,
135                     UriBuilder.fromUri("http://localhost").build(), HealthCheckStatus.DOWN));
136         }
137         expected.setMessage("HttpStatus: 200");
138         Response response = globalHealthcheck("DOWN");
139         assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
140         HealthCheckResponse root;
141         root = (HealthCheckResponse) response.getEntity();
142         assertThat(root, sameBeanAs(expected).ignoring("subsystems.uri").ignoring("subsystems.subsystem"));
143     }
144
145     @Test
146     public void buildHttpEntityForRequestTest() {
147         HttpEntity<String> he = globalhealth.buildHttpEntityForRequest();
148         assertEquals(MediaType.APPLICATION_JSON, he.getHeaders().getAccept().get(0));
149         assertEquals(MediaType.APPLICATION_JSON, he.getHeaders().getContentType());
150     }
151
152
153     @Test
154     public void processResponseFromSubsystemTest() {
155         SubsystemHealthcheckResponse subSystemResponse = new SubsystemHealthcheckResponse();
156         subSystemResponse.setStatus("UP");
157         ResponseEntity<SubsystemHealthcheckResponse> r = new ResponseEntity<>(subSystemResponse, HttpStatus.OK);
158         Endpoint endpoint = new Endpoint(SoSubsystems.BPMN, UriBuilder.fromUri("http://localhost").build());
159         HealthCheckStatus result = globalhealth.processResponseFromSubsystem(r, endpoint);
160         assertEquals(HealthCheckStatus.UP, result);
161     }
162
163 }