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