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 org.junit.Assert.assertEquals;
28 import static org.junit.Assert.assertNotNull;
31 import java.nio.file.Files;
32 import java.util.ArrayList;
33 import java.util.List;
36 import javax.ws.rs.core.MediaType;
38 import ch.qos.logback.classic.spi.ILoggingEvent;
40 import org.apache.log4j.MDC;
41 import org.junit.BeforeClass;
42 import org.junit.Ignore;
43 import org.junit.Rule;
44 import org.junit.Test;
45 import org.junit.rules.ExpectedException;
46 import org.onap.so.client.grm.beans.OperationalInfo;
47 import org.onap.so.client.grm.beans.Property;
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.beans.Version;
53 import org.onap.so.client.grm.exceptions.GRMClientCallFailed;
55 import com.fasterxml.jackson.databind.ObjectMapper;
56 import com.github.tomakehurst.wiremock.junit.WireMockRule;
57 import org.onap.so.logger.MsoLogger;
58 import org.onap.so.logger.MsoLogger.Catalog;
59 import org.onap.so.utils.TestAppender;
61 public class GRMClientTest {
64 public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().port(47389));
67 public ExpectedException thrown = ExpectedException.none();
70 public static void setUp() throws Exception {
71 System.setProperty("mso.config.path", "src/test/resources");
74 private ObjectMapper mapper = new ObjectMapper();
77 public void testFind() throws Exception {
78 TestAppender.events.clear();
79 String endpoints = getFileContentsAsString("__files/grm/endpoints.json");
80 wireMockRule.stubFor(post(urlPathEqualTo("/GRMLWPService/v1/serviceEndPoint/findRunning"))
81 .willReturn(aResponse()
83 .withHeader("Content-Type", MediaType.APPLICATION_JSON)
84 .withHeader("X-FromAppId", "GRM")
85 .withBody(endpoints)));
87 MDC.put(MsoLogger.SERVICE_NAME, "my-value");
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 ILoggingEvent logEvent = TestAppender.events.get(0);
93 Map<String,String> mdc = logEvent.getMDCPropertyMap();
94 assertNotNull(mdc.get(MsoLogger.METRIC_BEGIN_TIME));
95 assertNotNull(mdc.get(MsoLogger.METRIC_END_TIME));
96 assertNotNull(mdc.get(MsoLogger.REQUEST_ID));
97 assertNotNull(mdc.get(MsoLogger.METRIC_TIMER));
98 assertEquals("200",mdc.get(MsoLogger.RESPONSECODE));
99 assertEquals("GRM",mdc.get(MsoLogger.PARTNERNAME));
100 assertEquals("expect value to not be overwritten by jax rs client interceptor", "my-value",mdc.get(MsoLogger.SERVICE_NAME));
101 assertEquals("COMPLETE",mdc.get(MsoLogger.STATUSCODE));
102 assertNotNull(mdc.get(MsoLogger.RESPONSEDESC));
106 public void testFindFail() throws Exception {
108 wireMockRule.stubFor(post(urlPathEqualTo("/GRMLWPService/v1/serviceEndPoint/findRunning"))
109 .willReturn(aResponse()
111 .withHeader("Content-Type", MediaType.APPLICATION_JSON)
114 GRMClient client = new GRMClient();
115 thrown.expect(GRMClientCallFailed.class);
116 client.findRunningServices("TEST.ECOMP_PSL.*", 1, "TEST");
121 public void testAdd() throws Exception {
123 wireMockRule.stubFor(post(urlPathEqualTo("/GRMLWPService/v1/serviceEndPoint/add"))
124 .willReturn(aResponse()
126 .withHeader("Content-Type", MediaType.APPLICATION_JSON)
128 wireMockRule.addMockServiceRequestListener((request, response) -> {
129 System.out.println("URL Requested => " + request.getAbsoluteUrl());
130 System.out.println("Request Body => " + request.getBodyAsString());
131 System.out.println("Request Headers => " + request.getHeaders().toString());
132 System.out.println("Response Status => " + response.getStatus());
133 System.out.println("Response Body => " + response.getBodyAsString());
136 Version ver = new Version();
141 ServiceEndPoint sep = new ServiceEndPoint();
142 sep.setName("TEST.ECOMP_PSL.Inventory");
144 sep.setHostAddress("127.0.0.1");
145 sep.setListenPort("8080");
146 sep.setLatitude("37.7022");
147 sep.setLongitude("121.9358");
148 sep.setContextPath("/");
149 sep.setRouteOffer("TEST");
151 OperationalInfo operInfo = new OperationalInfo();
152 operInfo.setCreatedBy("edge");
153 operInfo.setUpdatedBy("edge");
155 sep.setOperationalInfo(operInfo);
157 Property prop1 = new Property();
158 prop1.setName("Environment");
159 prop1.setValue("TEST");
161 Property prop2 = new Property();
162 prop2.setName("cpfrun_cluster_name");
163 prop2.setValue("testcase_cluster_no_cluster");
165 List<Property> props = new ArrayList<Property>();
169 sep.setProperties(props);
171 ServiceEndPointRequest request = new ServiceEndPointRequest();
172 request.setEnv("DEV");
173 request.setServiceEndPoint(sep);
175 System.out.println("Request in JSON: " + mapper.writeValueAsString(request));
177 GRMClient client = new GRMClient();
178 client.addServiceEndPoint(request);
182 public void testAddFail() throws Exception {
183 wireMockRule.stubFor(post(urlPathEqualTo("/GRMLWPService/v1/serviceEndPoint/add"))
184 .willReturn(aResponse()
186 .withHeader("Content-Type", MediaType.APPLICATION_JSON)
188 ServiceEndPointRequest request = new ServiceEndPointRequest();
189 GRMClient client = new GRMClient();
190 thrown.expect(GRMClientCallFailed.class);
191 client.addServiceEndPoint(request);
195 public void testBuildServiceEndPointLookupRequest() {
196 GRMClient client = new GRMClient();
197 ServiceEndPointLookupRequest request = client.buildServiceEndPointlookupRequest("TEST.ECOMP_PSL.Inventory", 1, "DEV");
198 assertEquals("TEST.ECOMP_PSL.Inventory", request.getServiceEndPoint().getName());
199 assertEquals(Integer.valueOf(1), Integer.valueOf(request.getServiceEndPoint().getVersion().getMajor()));
200 assertEquals("DEV", request.getEnv());
204 protected String getFileContentsAsString(String fileName) {
207 ClassLoader classLoader = this.getClass().getClassLoader();
208 File file = new File(classLoader.getResource(fileName).getFile());
209 content = new String(Files.readAllBytes(file.toPath()));
213 System.out.println("Exception encountered reading " + fileName + ". Error: " + e.getMessage());