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;
34 import java.nio.file.Files;
35 import java.util.List;
37 import javax.ws.rs.core.MediaType;
38 import org.junit.BeforeClass;
39 import org.junit.Rule;
40 import org.junit.Test;
41 import org.junit.rules.ExpectedException;
42 import org.onap.logging.ref.slf4j.ONAPLogConstants;
43 import org.onap.so.apihandlerinfra.BaseTest;
44 import org.onap.so.apihandlerinfra.TestAppender;
45 import org.onap.so.client.grm.beans.ServiceEndPoint;
46 import org.onap.so.client.grm.beans.ServiceEndPointList;
47 import org.onap.so.client.grm.beans.ServiceEndPointLookupRequest;
48 import org.onap.so.client.grm.beans.ServiceEndPointRequest;
49 import org.onap.so.client.grm.exceptions.GRMClientCallFailed;
51 import ch.qos.logback.classic.spi.ILoggingEvent;
54 public class GRMClientTest extends BaseTest {
58 public ExpectedException thrown = ExpectedException.none();
60 private static final String uuidRegex =
61 "(?i)^[0-9a-f]{8}-?[0-9a-f]{4}-?[0-5][0-9a-f]{3}-?[089ab][0-9a-f]{3}-?[0-9a-f]{12}$";
64 public static void setUp() throws Exception {
65 System.setProperty("mso.config.path", "src/test/resources");
69 public void testFind() throws Exception {
70 TestAppender.events.clear();
71 String endpoints = getFileContentsAsString("__files/grm/endpoints.json");
73 .stubFor(post(urlPathEqualTo("/GRMLWPService/v1/serviceEndPoint/findRunning")).willReturn(aResponse()
74 .withStatus(200).withHeader("Content-Type", MediaType.APPLICATION_JSON).withBody(endpoints)));
76 MDC.put(ONAPLogConstants.MDCs.SERVICE_NAME, "/test");
77 GRMClient client = new GRMClient();
78 ServiceEndPointList sel = client.findRunningServices("TEST.ECOMP_PSL.*", 1, "TEST");
79 List<ServiceEndPoint> list = sel.getServiceEndPointList();
80 assertEquals(3, list.size());
82 boolean foundInvoke = false;
83 boolean foundInvokeReturn = false;
84 for (ILoggingEvent logEvent : TestAppender.events)
85 if (logEvent.getLoggerName().equals("org.onap.so.logging.jaxrs.filter.JaxRsClientLogging")
86 && logEvent.getMarker() != null && logEvent.getMarker().getName().equals("INVOKE")) {
87 Map<String, String> mdc = logEvent.getMDCPropertyMap();
88 assertNotNull(mdc.get(ONAPLogConstants.MDCs.INVOCATION_ID));
89 assertEquals("GRM", mdc.get("TargetEntity"));
90 assertEquals("INPROGRESS", mdc.get(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE));
92 } else if (logEvent.getLoggerName().equals("org.onap.so.logging.jaxrs.filter.JaxRsClientLogging")
93 && logEvent.getMarker() != null && logEvent.getMarker().getName().equals("INVOKE_RETURN")) {
94 Map<String, String> mdc = logEvent.getMDCPropertyMap();
95 assertNotNull(mdc.get(ONAPLogConstants.MDCs.INVOCATION_ID));
96 assertEquals("200", mdc.get(ONAPLogConstants.MDCs.RESPONSE_CODE));
97 assertEquals("COMPLETED", mdc.get(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE));
98 foundInvokeReturn = true;
102 fail("INVOKE Marker not found");
104 if (!foundInvokeReturn)
105 fail("INVOKE RETURN Marker not found");
107 wireMockServer.verify(postRequestedFor(urlEqualTo("/GRMLWPService/v1/serviceEndPoint/findRunning"))
108 .withHeader(ONAPLogConstants.Headers.INVOCATION_ID.toString(), matching(uuidRegex))
109 .withHeader(ONAPLogConstants.Headers.REQUEST_ID.toString(), matching(uuidRegex))
110 .withHeader(ONAPLogConstants.Headers.PARTNER_NAME.toString(), equalTo("SO")));
111 TestAppender.events.clear();
115 public void testFindFail() throws Exception {
116 wireMockServer.stubFor(post(urlPathEqualTo("/GRMLWPService/v1/serviceEndPoint/findRunning")).willReturn(
117 aResponse().withStatus(400).withHeader("Content-Type", MediaType.APPLICATION_JSON).withBody("")));
119 GRMClient client = new GRMClient();
120 thrown.expect(GRMClientCallFailed.class);
121 client.findRunningServices("TEST.ECOMP_PSL.*", 1, "TEST");
125 public void testAddFail() throws Exception {
126 wireMockServer.stubFor(post(urlPathEqualTo("/GRMLWPService/v1/serviceEndPoint/add")).willReturn(
127 aResponse().withStatus(404).withHeader("Content-Type", MediaType.APPLICATION_JSON).withBody("test")));
128 ServiceEndPointRequest request = new ServiceEndPointRequest();
129 GRMClient client = new GRMClient();
130 thrown.expect(GRMClientCallFailed.class);
131 client.addServiceEndPoint(request);
135 public void testBuildServiceEndPointLookupRequest() {
136 GRMClient client = new GRMClient();
137 ServiceEndPointLookupRequest request =
138 client.buildServiceEndPointlookupRequest("TEST.ECOMP_PSL.Inventory", 1, "DEV");
139 assertEquals("TEST.ECOMP_PSL.Inventory", request.getServiceEndPoint().getName());
140 assertEquals(Integer.valueOf(1), Integer.valueOf(request.getServiceEndPoint().getVersion().getMajor()));
141 assertEquals("DEV", request.getEnv());
145 protected String getFileContentsAsString(String fileName) {
148 ClassLoader classLoader = this.getClass().getClassLoader();
149 File file = new File(classLoader.getResource(fileName).getFile());
150 content = new String(Files.readAllBytes(file.toPath()));
151 } catch (Exception e) {
153 System.out.println("Exception encountered reading " + fileName + ". Error: " + e.getMessage());