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 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;
37 import java.nio.file.Files;
38 import java.util.List;
41 import javax.ws.rs.core.MediaType;
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;
57 import com.fasterxml.jackson.databind.ObjectMapper;
58 import com.github.tomakehurst.wiremock.junit.WireMockRule;
60 import ch.qos.logback.classic.spi.ILoggingEvent;
62 public class GRMClientTest {
65 public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().port(47389));
68 public ExpectedException thrown = ExpectedException.none();
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}$";
73 public static void setUp() throws Exception {
74 System.setProperty("mso.config.path", "src/test/resources");
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()
84 .withHeader("Content-Type", MediaType.APPLICATION_JSON)
85 .withBody(endpoints)));
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());
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")
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));
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;
114 fail("INVOKE Marker not found");
116 if(!foundInvokeReturn)
117 fail("INVOKE RETURN Marker not found");
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();
127 public void testFindFail() throws Exception {
129 wireMockRule.stubFor(post(urlPathEqualTo("/GRMLWPService/v1/serviceEndPoint/findRunning"))
130 .willReturn(aResponse()
132 .withHeader("Content-Type", MediaType.APPLICATION_JSON)
135 GRMClient client = new GRMClient();
136 thrown.expect(GRMClientCallFailed.class);
137 client.findRunningServices("TEST.ECOMP_PSL.*", 1, "TEST");
141 public void testAddFail() throws Exception {
142 wireMockRule.stubFor(post(urlPathEqualTo("/GRMLWPService/v1/serviceEndPoint/add"))
143 .willReturn(aResponse()
145 .withHeader("Content-Type", MediaType.APPLICATION_JSON)
147 ServiceEndPointRequest request = new ServiceEndPointRequest();
148 GRMClient client = new GRMClient();
149 thrown.expect(GRMClientCallFailed.class);
150 client.addServiceEndPoint(request);
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());
163 protected String getFileContentsAsString(String fileName) {
166 ClassLoader classLoader = this.getClass().getClassLoader();
167 File file = new File(classLoader.getResource(fileName).getFile());
168 content = new String(Files.readAllBytes(file.toPath()));
172 System.out.println("Exception encountered reading " + fileName + ". Error: " + e.getMessage());