replace all fixed wiremock ports
[so.git] / adapters / mso-requests-db-adapter / src / test / java / org / onap / so / adapters / requestsdb / adapters / HealthCheckHandlerTest.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.adapters.requestsdb.adapters;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25
26 import java.util.Map;
27
28 import javax.ws.rs.core.Response;
29
30 import org.json.JSONException;
31 import org.junit.Test;
32 import org.onap.logging.ref.slf4j.ONAPLogConstants;
33 import org.onap.so.adapters.requestsdb.RequestsAdapterBase;
34 import org.onap.so.adapters.requestsdb.application.TestAppender;
35 import org.springframework.boot.test.web.client.TestRestTemplate;
36 import org.springframework.boot.web.server.LocalServerPort;
37 import org.springframework.http.HttpEntity;
38 import org.springframework.http.HttpHeaders;
39 import org.springframework.http.HttpMethod;
40 import org.springframework.http.ResponseEntity;
41
42 import ch.qos.logback.classic.spi.ILoggingEvent;
43
44
45 public class HealthCheckHandlerTest extends RequestsAdapterBase {
46         
47         @LocalServerPort
48         private int port;
49
50         TestRestTemplate restTemplate = new TestRestTemplate();
51
52         HttpHeaders headers = new HttpHeaders();
53
54         
55         @Test
56         public void testHealthcheck() throws JSONException {
57             TestAppender.events.clear();
58                 HttpEntity<String> entity = new HttpEntity<String>(null, headers);
59
60                 ResponseEntity<String> response = restTemplate.exchange(
61                                 createURLWithPort("/manage/health"),
62                                 HttpMethod.GET, entity, String.class);
63                 
64                 assertEquals(Response.Status.OK.getStatusCode(),response.getStatusCode().value());
65         for(ILoggingEvent logEvent : TestAppender.events)
66             if(logEvent.getLoggerName().equals("org.onap.so.logging.spring.interceptor.LoggingInterceptor") &&
67                         logEvent.getMarker() != null && logEvent.getMarker().getName().equals("ENTRY")
68                     ){
69                 Map<String,String> mdc = logEvent.getMDCPropertyMap();
70                 assertNotNull(mdc.get(ONAPLogConstants.MDCs.INSTANCE_UUID));
71                 assertNotNull(mdc.get(ONAPLogConstants.MDCs.REQUEST_ID));
72                 assertNotNull(mdc.get(ONAPLogConstants.MDCs.INVOCATION_ID));
73                 assertEquals("",mdc.get(ONAPLogConstants.MDCs.PARTNER_NAME));
74                 assertEquals("/manage/health",mdc.get(ONAPLogConstants.MDCs.SERVICE_NAME));
75                 assertEquals("INPROGRESS",mdc.get(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE));
76             }else if(logEvent.getLoggerName().equals("org.onap.so.logging.spring.interceptor.LoggingInterceptor") &&
77                         logEvent.getMarker() != null &&  logEvent.getMarker()!= null && logEvent.getMarker().getName().equals("EXIT")){
78                 Map<String,String> mdc = logEvent.getMDCPropertyMap();
79                 assertNotNull(mdc.get(ONAPLogConstants.MDCs.REQUEST_ID));
80                 assertNotNull(mdc.get(ONAPLogConstants.MDCs.INVOCATION_ID));
81                 assertEquals("200",mdc.get(ONAPLogConstants.MDCs.RESPONSE_CODE));
82                 assertEquals("",mdc.get(ONAPLogConstants.MDCs.PARTNER_NAME));
83                 assertEquals("/manage/health",mdc.get(ONAPLogConstants.MDCs.SERVICE_NAME));
84                 assertEquals("COMPLETED",mdc.get(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE));
85             }
86         TestAppender.events.clear();
87         }
88         
89         private String createURLWithPort(String uri) {
90                 return "http://localhost:" + port + uri;
91         }
92 }