Springboot 2.0 upgrade
[so.git] / common / src / test / java / org / onap / so / client / grm / GRMClientTest.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.client.grm;
22
23 import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
24 import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
25 import static com.github.tomakehurst.wiremock.client.WireMock.matching;
26 import static com.github.tomakehurst.wiremock.client.WireMock.post;
27 import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor;
28 import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
29 import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
30 import static com.github.tomakehurst.wiremock.client.WireMock.verify;
31 import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
32 import static org.junit.Assert.assertEquals;
33 import static org.junit.Assert.assertNotNull;
34 import static org.junit.Assert.fail;
35
36 import java.io.File;
37 import java.nio.file.Files;
38 import java.util.List;
39 import java.util.Map;
40
41 import javax.ws.rs.core.MediaType;
42
43
44 import org.junit.BeforeClass;
45 import org.junit.Rule;
46 import org.junit.Test;
47 import org.junit.rules.ExpectedException;
48 import org.onap.logging.ref.slf4j.ONAPLogConstants;
49 import org.onap.so.client.grm.beans.ServiceEndPoint;
50 import org.onap.so.client.grm.beans.ServiceEndPointList;
51 import org.onap.so.client.grm.beans.ServiceEndPointLookupRequest;
52 import org.onap.so.client.grm.beans.ServiceEndPointRequest;
53 import org.onap.so.client.grm.exceptions.GRMClientCallFailed;
54 import org.onap.so.utils.TestAppender;
55 import org.slf4j.MDC;
56
57 import com.fasterxml.jackson.databind.ObjectMapper;
58 import com.github.tomakehurst.wiremock.junit.WireMockRule;
59
60 import ch.qos.logback.classic.spi.ILoggingEvent;
61
62 public class GRMClientTest {
63         
64         @Rule
65         public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().port(47389));
66         
67         @Rule
68         public ExpectedException thrown = ExpectedException.none();
69         
70         private static final String uuidRegex = "(?i)^[0-9a-f]{8}-?[0-9a-f]{4}-?[0-5][0-9a-f]{3}-?[089ab][0-9a-f]{3}-?[0-9a-f]{12}$";
71         
72         @BeforeClass
73         public static void setUp() throws Exception {
74                 System.setProperty("mso.config.path", "src/test/resources");
75         }
76         
77         @Test
78         public void testFind() throws Exception {
79         TestAppender.events.clear();
80                 String endpoints = getFileContentsAsString("__files/grm/endpoints.json");
81                 wireMockRule.stubFor(post(urlPathEqualTo("/GRMLWPService/v1/serviceEndPoint/findRunning"))
82                         .willReturn(aResponse()
83                                 .withStatus(200)
84                                 .withHeader("Content-Type", MediaType.APPLICATION_JSON)
85                                 .withBody(endpoints)));
86
87                 MDC.put(ONAPLogConstants.MDCs.SERVICE_NAME, "/test");
88                 GRMClient client = new GRMClient();
89                 ServiceEndPointList sel = client.findRunningServices("TEST.ECOMP_PSL.*", 1, "TEST");
90                 List<ServiceEndPoint> list = sel.getServiceEndPointList();
91                 assertEquals(3, list.size());
92                 
93                 boolean foundInvoke = false;
94                 boolean foundInvokeReturn = false;
95         for(ILoggingEvent logEvent : TestAppender.events)
96             if(logEvent.getLoggerName().equals("org.onap.so.logging.jaxrs.filter.JaxRsClientLogging") &&
97                     logEvent.getMarker().getName().equals("INVOKE")
98                     ){
99                 Map<String,String> mdc = logEvent.getMDCPropertyMap();
100                 assertNotNull(mdc.get(ONAPLogConstants.MDCs.INVOCATION_ID));
101                 assertEquals("GRM",mdc.get("TargetEntity"));
102                 assertEquals("INPROGRESS",mdc.get(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE));
103                 foundInvoke=true;
104             }else if(logEvent.getLoggerName().equals("org.onap.so.logging.jaxrs.filter.JaxRsClientLogging") &&
105                     logEvent.getMarker()!= null && logEvent.getMarker().getName().equals("INVOKE_RETURN")){
106                 Map<String,String> mdc = logEvent.getMDCPropertyMap();
107                 assertNotNull(mdc.get(ONAPLogConstants.MDCs.INVOCATION_ID));
108                 assertEquals("200",mdc.get(ONAPLogConstants.MDCs.RESPONSE_CODE));
109                 assertEquals("COMPLETED",mdc.get(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE));
110                 foundInvokeReturn=true;
111             }
112         
113         if(!foundInvoke)
114             fail("INVOKE Marker not found");
115         
116         if(!foundInvokeReturn)
117             fail("INVOKE RETURN Marker not found");
118         
119         verify(postRequestedFor(urlEqualTo("/GRMLWPService/v1/serviceEndPoint/findRunning"))
120                 .withHeader(ONAPLogConstants.Headers.INVOCATION_ID.toString(), matching(uuidRegex))
121                         .withHeader(ONAPLogConstants.Headers.REQUEST_ID.toString(), matching(uuidRegex))
122                                 .withHeader(ONAPLogConstants.Headers.PARTNER_NAME.toString(), equalTo("SO")));
123         TestAppender.events.clear();
124         }
125         
126         @Test 
127         public void testFindFail() throws Exception {
128                 
129                 wireMockRule.stubFor(post(urlPathEqualTo("/GRMLWPService/v1/serviceEndPoint/findRunning"))
130                         .willReturn(aResponse()
131                                 .withStatus(400)
132                                 .withHeader("Content-Type", MediaType.APPLICATION_JSON)
133                                 .withBody("")));
134                 
135                 GRMClient client = new GRMClient();
136                 thrown.expect(GRMClientCallFailed.class);
137                 client.findRunningServices("TEST.ECOMP_PSL.*", 1, "TEST");
138         }
139         
140         @Test
141         public void testAddFail() throws Exception {
142                 wireMockRule.stubFor(post(urlPathEqualTo("/GRMLWPService/v1/serviceEndPoint/add"))
143                                 .willReturn(aResponse()
144                                         .withStatus(404)
145                                         .withHeader("Content-Type", MediaType.APPLICATION_JSON)
146                                         .withBody("test")));
147                 ServiceEndPointRequest request = new ServiceEndPointRequest();
148                 GRMClient client = new GRMClient();
149                 thrown.expect(GRMClientCallFailed.class);
150                 client.addServiceEndPoint(request);
151         }
152
153         @Test
154         public void testBuildServiceEndPointLookupRequest() {
155                 GRMClient client = new GRMClient();
156                 ServiceEndPointLookupRequest request = client.buildServiceEndPointlookupRequest("TEST.ECOMP_PSL.Inventory", 1, "DEV");
157                 assertEquals("TEST.ECOMP_PSL.Inventory", request.getServiceEndPoint().getName());
158                 assertEquals(Integer.valueOf(1), Integer.valueOf(request.getServiceEndPoint().getVersion().getMajor()));
159                 assertEquals("DEV", request.getEnv());
160                 
161         }
162         
163         protected String getFileContentsAsString(String fileName) {
164                 String content = "";
165                 try {
166                         ClassLoader classLoader = this.getClass().getClassLoader();
167                         File file = new File(classLoader.getResource(fileName).getFile());
168                         content = new String(Files.readAllBytes(file.toPath()));
169                 }
170                 catch(Exception e) {
171                         e.printStackTrace();
172                         System.out.println("Exception encountered reading " + fileName + ". Error: " + e.getMessage());
173                 }
174                 return content;
175         }
176 }