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.openecomp.mso.client.grm;
23 import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
24 import static org.junit.Assert.*;
27 import java.nio.file.Files;
28 import java.util.ArrayList;
29 import java.util.List;
31 import javax.ws.rs.core.MediaType;
33 import org.junit.Ignore;
34 import org.junit.Rule;
35 import org.junit.Test;
36 import org.openecomp.mso.client.grm.exceptions.GRMClientCallFailed;
37 import org.openecomp.mso.client.grm.GRMClient;
38 import org.openecomp.mso.client.grm.beans.ServiceEndPointRequest;
39 import org.openecomp.mso.client.grm.beans.OperationalInfo;
40 import org.openecomp.mso.client.grm.beans.Property;
41 import org.openecomp.mso.client.grm.beans.ServiceEndPoint;
42 import org.openecomp.mso.client.grm.beans.ServiceEndPointList;
43 import org.openecomp.mso.client.grm.beans.ServiceEndPointLookupRequest;
44 import org.openecomp.mso.client.grm.beans.Version;
46 import com.fasterxml.jackson.databind.ObjectMapper;
47 import com.github.tomakehurst.wiremock.junit.WireMockRule;
48 import static com.github.tomakehurst.wiremock.client.WireMock.post;
49 import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
50 import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
52 public class GRMClientTest {
55 public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().port(28090));
57 private ObjectMapper mapper = new ObjectMapper();
60 public void testFind() throws Exception {
61 String endpoints = getFileContentsAsString("__files/grm/endpoints.json");
62 wireMockRule.stubFor(post(urlPathEqualTo("/GRMLWPService/v1/serviceEndPoint/findRunning"))
63 .willReturn(aResponse()
65 .withHeader("Content-Type", MediaType.APPLICATION_JSON)
66 .withBody(endpoints)));
69 GRMClient client = new GRMClient();
70 ServiceEndPointList sel = client.findRunningServices("TEST.ECOMP_PSL.*", 1, "TEST");
71 List<ServiceEndPoint> list = sel.getServiceEndPointList();
72 assertEquals(3, list.size());
75 @Test(expected = GRMClientCallFailed.class)
76 public void testFindFail() throws Exception {
78 wireMockRule.stubFor(post(urlPathEqualTo("/GRMLWPService/v1/serviceEndPoint/findRunning"))
79 .willReturn(aResponse()
81 .withHeader("Content-Type", MediaType.APPLICATION_JSON)
84 GRMClient client = new GRMClient();
85 client.findRunningServices("TEST.ECOMP_PSL.*", 1, "TEST");
90 public void testAdd() throws Exception {
92 wireMockRule.stubFor(post(urlPathEqualTo("/GRMLWPService/v1/serviceEndPoint/add"))
93 .willReturn(aResponse()
95 .withHeader("Content-Type", MediaType.APPLICATION_JSON)
97 wireMockRule.addMockServiceRequestListener((request, response) -> {
98 System.out.println("URL Requested => " + request.getAbsoluteUrl());
99 System.out.println("Request Body => " + request.getBodyAsString());
100 System.out.println("Request Headers => " + request.getHeaders().toString());
101 System.out.println("Response Status => " + response.getStatus());
102 System.out.println("Response Body => " + response.getBodyAsString());
105 Version ver = new Version();
110 ServiceEndPoint sep = new ServiceEndPoint();
111 sep.setName("TEST.ECOMP_PSL.Inventory");
113 sep.setHostAddress("127.0.0.1");
114 sep.setListenPort("8080");
115 sep.setLatitude("37.7022");
116 sep.setLongitude("121.9358");
117 sep.setContextPath("/");
118 sep.setRouteOffer("TEST");
120 OperationalInfo operInfo = new OperationalInfo();
121 operInfo.setCreatedBy("edge");
122 operInfo.setUpdatedBy("edge");
124 sep.setOperationalInfo(operInfo);
126 Property prop1 = new Property();
127 prop1.setName("Environment");
128 prop1.setValue("TEST");
130 Property prop2 = new Property();
131 prop2.setName("cpfrun_cluster_name");
132 prop2.setValue("testcase_cluster_no_cluster");
134 List<Property> props = new ArrayList<Property>();
138 sep.setProperties(props);
140 ServiceEndPointRequest request = new ServiceEndPointRequest();
141 request.setEnv("DEV");
142 request.setServiceEndPoint(sep);
144 System.out.println("Request in JSON: " + mapper.writeValueAsString(request));
146 GRMClient client = new GRMClient();
147 client.addServiceEndPoint(request);
150 @Test(expected = GRMClientCallFailed.class)
151 public void testAddFail() throws Exception {
152 wireMockRule.stubFor(post(urlPathEqualTo("/GRMLWPService/v1/serviceEndPoint/add"))
153 .willReturn(aResponse()
155 .withHeader("Content-Type", MediaType.APPLICATION_JSON)
157 ServiceEndPointRequest request = new ServiceEndPointRequest();
158 GRMClient client = new GRMClient();
159 client.addServiceEndPoint(request);
163 public void testBuildServiceEndPointLookupRequest() {
164 GRMClient client = new GRMClient();
165 ServiceEndPointLookupRequest request = client.buildServiceEndPointlookupRequest("TEST.ECOMP_PSL.Inventory", 1, "DEV");
166 assertEquals("TEST.ECOMP_PSL.Inventory", request.getServiceEndPoint().getName());
167 assertEquals(Integer.valueOf(1), Integer.valueOf(request.getServiceEndPoint().getVersion().getMajor()));
168 assertEquals("DEV", request.getEnv());
172 protected String getFileContentsAsString(String fileName) {
175 ClassLoader classLoader = this.getClass().getClassLoader();
176 File file = new File(classLoader.getResource(fileName).getFile());
177 content = new String(Files.readAllBytes(file.toPath()));
181 System.out.println("Exception encountered reading " + fileName + ". Error: " + e.getMessage());