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.post;
25 import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
26 import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
27 import static com.github.tomakehurst.wiremock.client.WireMock.*;
29 import static org.junit.Assert.assertEquals;
30 import static org.junit.Assert.assertNotNull;
31 import static org.junit.Assert.fail;
33 import java.nio.file.Files;
34 import java.util.ArrayList;
35 import java.util.List;
38 import javax.ws.rs.core.MediaType;
40 import ch.qos.logback.classic.spi.ILoggingEvent;
42 import org.apache.log4j.MDC;
43 import org.junit.BeforeClass;
44 import org.junit.Ignore;
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.OperationalInfo;
50 import org.onap.so.client.grm.beans.Property;
51 import org.onap.so.client.grm.beans.ServiceEndPoint;
52 import org.onap.so.client.grm.beans.ServiceEndPointList;
53 import org.onap.so.client.grm.beans.ServiceEndPointLookupRequest;
54 import org.onap.so.client.grm.beans.ServiceEndPointRequest;
55 import org.onap.so.client.grm.beans.Version;
56 import org.onap.so.client.grm.exceptions.GRMClientCallFailed;
58 import com.fasterxml.jackson.databind.ObjectMapper;
59 import com.github.tomakehurst.wiremock.junit.WireMockRule;
61 import org.onap.so.utils.TestAppender;
63 public class GRMClientTest {
66 public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().port(47389));
69 public ExpectedException thrown = ExpectedException.none();
71 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}$";
74 public static void setUp() throws Exception {
75 System.setProperty("mso.config.path", "src/test/resources");
78 private ObjectMapper mapper = new ObjectMapper();
81 public void testFind() throws Exception {
82 TestAppender.events.clear();
83 String endpoints = getFileContentsAsString("__files/grm/endpoints.json");
84 wireMockRule.stubFor(post(urlPathEqualTo("/GRMLWPService/v1/serviceEndPoint/findRunning"))
85 .willReturn(aResponse()
87 .withHeader("Content-Type", MediaType.APPLICATION_JSON)
88 .withBody(endpoints)));
90 MDC.put(ONAPLogConstants.MDCs.SERVICE_NAME, "/test");
91 GRMClient client = new GRMClient();
92 ServiceEndPointList sel = client.findRunningServices("TEST.ECOMP_PSL.*", 1, "TEST");
93 List<ServiceEndPoint> list = sel.getServiceEndPointList();
94 assertEquals(3, list.size());
96 boolean foundInvoke = false;
97 boolean foundInvokeReturn = false;
98 for(ILoggingEvent logEvent : TestAppender.events)
99 if(logEvent.getLoggerName().equals("org.onap.so.logging.jaxrs.filter.JaxRsClientLogging") &&
100 logEvent.getMarker().getName().equals("INVOKE")
102 Map<String,String> mdc = logEvent.getMDCPropertyMap();
103 assertNotNull(mdc.get(ONAPLogConstants.MDCs.INVOCATION_ID));
104 assertEquals("GRM",mdc.get("TargetEntity"));
105 assertEquals("INPROGRESS",mdc.get(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE));
107 }else if(logEvent.getLoggerName().equals("org.onap.so.logging.jaxrs.filter.JaxRsClientLogging") &&
108 logEvent.getMarker()!= null && logEvent.getMarker().getName().equals("INVOKE_RETURN")){
109 Map<String,String> mdc = logEvent.getMDCPropertyMap();
110 assertNotNull(mdc.get(ONAPLogConstants.MDCs.INVOCATION_ID));
111 assertEquals("200",mdc.get(ONAPLogConstants.MDCs.RESPONSE_CODE));
112 assertEquals("COMPLETED",mdc.get(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE));
113 foundInvokeReturn=true;
117 fail("INVOKE Marker not found");
119 if(!foundInvokeReturn)
120 fail("INVOKE RETURN Marker not found");
122 verify(postRequestedFor(urlEqualTo("/GRMLWPService/v1/serviceEndPoint/findRunning"))
123 .withHeader(ONAPLogConstants.Headers.INVOCATION_ID.toString(), matching(uuidRegex))
124 .withHeader(ONAPLogConstants.Headers.REQUEST_ID.toString(), matching(uuidRegex))
125 .withHeader(ONAPLogConstants.Headers.PARTNER_NAME.toString(), equalTo("SO")));
126 TestAppender.events.clear();
130 public void testFindFail() throws Exception {
132 wireMockRule.stubFor(post(urlPathEqualTo("/GRMLWPService/v1/serviceEndPoint/findRunning"))
133 .willReturn(aResponse()
135 .withHeader("Content-Type", MediaType.APPLICATION_JSON)
138 GRMClient client = new GRMClient();
139 thrown.expect(GRMClientCallFailed.class);
140 client.findRunningServices("TEST.ECOMP_PSL.*", 1, "TEST");
144 public void testAddFail() throws Exception {
145 wireMockRule.stubFor(post(urlPathEqualTo("/GRMLWPService/v1/serviceEndPoint/add"))
146 .willReturn(aResponse()
148 .withHeader("Content-Type", MediaType.APPLICATION_JSON)
150 ServiceEndPointRequest request = new ServiceEndPointRequest();
151 GRMClient client = new GRMClient();
152 thrown.expect(GRMClientCallFailed.class);
153 client.addServiceEndPoint(request);
157 public void testBuildServiceEndPointLookupRequest() {
158 GRMClient client = new GRMClient();
159 ServiceEndPointLookupRequest request = client.buildServiceEndPointlookupRequest("TEST.ECOMP_PSL.Inventory", 1, "DEV");
160 assertEquals("TEST.ECOMP_PSL.Inventory", request.getServiceEndPoint().getName());
161 assertEquals(Integer.valueOf(1), Integer.valueOf(request.getServiceEndPoint().getVersion().getMajor()));
162 assertEquals("DEV", request.getEnv());
166 protected String getFileContentsAsString(String fileName) {
169 ClassLoader classLoader = this.getClass().getClassLoader();
170 File file = new File(classLoader.getResource(fileName).getFile());
171 content = new String(Files.readAllBytes(file.toPath()));
175 System.out.println("Exception encountered reading " + fileName + ". Error: " + e.getMessage());