2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.so.client.grm;
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 org.junit.Assert.assertEquals;
31 import static org.junit.Assert.assertNotNull;
32 import static org.junit.Assert.fail;
35 import java.nio.file.Files;
36 import java.util.List;
39 import javax.ws.rs.core.MediaType;
41 import org.junit.BeforeClass;
42 import org.junit.Rule;
43 import org.junit.Test;
44 import org.junit.rules.ExpectedException;
45 import org.onap.logging.ref.slf4j.ONAPLogConstants;
46 import org.onap.so.apihandlerinfra.BaseTest;
47 import org.onap.so.apihandlerinfra.TestAppender;
48 import org.onap.so.client.grm.beans.ServiceEndPoint;
49 import org.onap.so.client.grm.beans.ServiceEndPointList;
50 import org.onap.so.client.grm.beans.ServiceEndPointLookupRequest;
51 import org.onap.so.client.grm.beans.ServiceEndPointRequest;
52 import org.onap.so.client.grm.exceptions.GRMClientCallFailed;
55 import ch.qos.logback.classic.spi.ILoggingEvent;
58 public class GRMClientTest extends BaseTest{
62 public ExpectedException thrown = ExpectedException.none();
64 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}$";
67 public static void setUp() throws Exception {
68 System.setProperty("mso.config.path", "src/test/resources");
72 public void testFind() throws Exception {
73 TestAppender.events.clear();
74 String endpoints = getFileContentsAsString("__files/grm/endpoints.json");
75 wireMockServer.stubFor(post(urlPathEqualTo("/GRMLWPService/v1/serviceEndPoint/findRunning"))
76 .willReturn(aResponse()
78 .withHeader("Content-Type", MediaType.APPLICATION_JSON)
79 .withBody(endpoints)));
81 MDC.put(ONAPLogConstants.MDCs.SERVICE_NAME, "/test");
82 GRMClient client = new GRMClient();
83 ServiceEndPointList sel = client.findRunningServices("TEST.ECOMP_PSL.*", 1, "TEST");
84 List<ServiceEndPoint> list = sel.getServiceEndPointList();
85 assertEquals(3, list.size());
87 boolean foundInvoke = false;
88 boolean foundInvokeReturn = false;
89 for(ILoggingEvent logEvent : TestAppender.events)
90 if(logEvent.getLoggerName().equals("org.onap.so.logging.jaxrs.filter.JaxRsClientLogging") &&
91 logEvent.getMarker() != null && logEvent.getMarker().getName().equals("INVOKE")
93 Map<String,String> mdc = logEvent.getMDCPropertyMap();
94 assertNotNull(mdc.get(ONAPLogConstants.MDCs.INVOCATION_ID));
95 assertEquals("GRM",mdc.get("TargetEntity"));
96 assertEquals("INPROGRESS",mdc.get(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE));
98 }else if(logEvent.getLoggerName().equals("org.onap.so.logging.jaxrs.filter.JaxRsClientLogging") &&
99 logEvent.getMarker()!= null && logEvent.getMarker().getName().equals("INVOKE_RETURN")){
100 Map<String,String> mdc = logEvent.getMDCPropertyMap();
101 assertNotNull(mdc.get(ONAPLogConstants.MDCs.INVOCATION_ID));
102 assertEquals("200",mdc.get(ONAPLogConstants.MDCs.RESPONSE_CODE));
103 assertEquals("COMPLETED",mdc.get(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE));
104 foundInvokeReturn=true;
108 fail("INVOKE Marker not found");
110 if(!foundInvokeReturn)
111 fail("INVOKE RETURN Marker not found");
113 wireMockServer.verify(postRequestedFor(urlEqualTo("/GRMLWPService/v1/serviceEndPoint/findRunning"))
114 .withHeader(ONAPLogConstants.Headers.INVOCATION_ID.toString(), matching(uuidRegex))
115 .withHeader(ONAPLogConstants.Headers.REQUEST_ID.toString(), matching(uuidRegex))
116 .withHeader(ONAPLogConstants.Headers.PARTNER_NAME.toString(), equalTo("SO")));
117 TestAppender.events.clear();
121 public void testFindFail() throws Exception {
122 wireMockServer.stubFor(post(urlPathEqualTo("/GRMLWPService/v1/serviceEndPoint/findRunning"))
123 .willReturn(aResponse()
125 .withHeader("Content-Type", MediaType.APPLICATION_JSON)
128 GRMClient client = new GRMClient();
129 thrown.expect(GRMClientCallFailed.class);
130 client.findRunningServices("TEST.ECOMP_PSL.*", 1, "TEST");
134 public void testAddFail() throws Exception {
135 wireMockServer.stubFor(post(urlPathEqualTo("/GRMLWPService/v1/serviceEndPoint/add"))
136 .willReturn(aResponse()
138 .withHeader("Content-Type", MediaType.APPLICATION_JSON)
140 ServiceEndPointRequest request = new ServiceEndPointRequest();
141 GRMClient client = new GRMClient();
142 thrown.expect(GRMClientCallFailed.class);
143 client.addServiceEndPoint(request);
147 public void testBuildServiceEndPointLookupRequest() {
148 GRMClient client = new GRMClient();
149 ServiceEndPointLookupRequest request = client.buildServiceEndPointlookupRequest("TEST.ECOMP_PSL.Inventory", 1, "DEV");
150 assertEquals("TEST.ECOMP_PSL.Inventory", request.getServiceEndPoint().getName());
151 assertEquals(Integer.valueOf(1), Integer.valueOf(request.getServiceEndPoint().getVersion().getMajor()));
152 assertEquals("DEV", request.getEnv());
156 protected String getFileContentsAsString(String fileName) {
159 ClassLoader classLoader = this.getClass().getClassLoader();
160 File file = new File(classLoader.getResource(fileName).getFile());
161 content = new String(Files.readAllBytes(file.toPath()));
165 System.out.println("Exception encountered reading " + fileName + ". Error: " + e.getMessage());