3cce6a626b99d39952274450090dfc05a33a75bd
[so.git] / common / src / test / java / org / onap / so / client / grm / GRMClientTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.so.client.grm;
22
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;
29
30 import java.io.File;
31 import java.nio.file.Files;
32 import java.util.ArrayList;
33 import java.util.List;
34 import java.util.Map;
35
36 import javax.ws.rs.core.MediaType;
37
38 import ch.qos.logback.classic.spi.ILoggingEvent;
39
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;
54
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;
60
61 public class GRMClientTest {
62         
63         @Rule
64         public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().port(47389));
65         
66         @Rule
67         public ExpectedException thrown = ExpectedException.none();
68         
69         @BeforeClass
70         public static void setUp() throws Exception {
71                 System.setProperty("mso.config.path", "src/test/resources");
72         }
73         
74         private ObjectMapper mapper = new ObjectMapper();
75         
76         @Test
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()
82                                 .withStatus(200)
83                                 .withHeader("Content-Type", MediaType.APPLICATION_JSON)
84                                 .withHeader("X-FromAppId", "GRM")
85                                 .withBody(endpoints)));
86
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));
103         }
104         
105         @Test 
106         public void testFindFail() throws Exception {
107                 
108                 wireMockRule.stubFor(post(urlPathEqualTo("/GRMLWPService/v1/serviceEndPoint/findRunning"))
109                         .willReturn(aResponse()
110                                 .withStatus(400)
111                                 .withHeader("Content-Type", MediaType.APPLICATION_JSON)
112                                 .withBody("")));
113                 
114                 GRMClient client = new GRMClient();
115                 thrown.expect(GRMClientCallFailed.class);
116                 client.findRunningServices("TEST.ECOMP_PSL.*", 1, "TEST");
117         }
118         
119         @Ignore
120         @Test
121         public void testAdd() throws Exception {
122
123                 wireMockRule.stubFor(post(urlPathEqualTo("/GRMLWPService/v1/serviceEndPoint/add"))
124                         .willReturn(aResponse()
125                                 .withStatus(202)
126                                 .withHeader("Content-Type", MediaType.APPLICATION_JSON)
127                                 .withBody("test")));
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());
134                 });     
135                 
136                 Version ver = new Version();
137                 ver.setMajor(1);
138                 ver.setMinor(0);
139                 ver.setPatch("0");
140
141                 ServiceEndPoint sep = new ServiceEndPoint();
142                 sep.setName("TEST.ECOMP_PSL.Inventory");
143                 sep.setVersion(ver);
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");
150                 
151                 OperationalInfo operInfo = new OperationalInfo();
152                 operInfo.setCreatedBy("edge");
153                 operInfo.setUpdatedBy("edge");
154                 
155                 sep.setOperationalInfo(operInfo);
156                 
157                 Property prop1 = new Property();
158                 prop1.setName("Environment");
159                 prop1.setValue("TEST");
160                 
161                 Property prop2 = new Property();
162                 prop2.setName("cpfrun_cluster_name");
163                 prop2.setValue("testcase_cluster_no_cluster");
164                 
165                 List<Property> props = new ArrayList<Property>();
166                 props.add(prop1);
167                 props.add(prop2);
168                 
169                 sep.setProperties(props);
170
171                 ServiceEndPointRequest request = new ServiceEndPointRequest();
172                 request.setEnv("DEV");
173                 request.setServiceEndPoint(sep);
174                 
175                 System.out.println("Request in JSON: " + mapper.writeValueAsString(request));
176                 
177                 GRMClient client = new GRMClient();
178                 client.addServiceEndPoint(request);
179         }
180         
181         @Test
182         public void testAddFail() throws Exception {
183                 wireMockRule.stubFor(post(urlPathEqualTo("/GRMLWPService/v1/serviceEndPoint/add"))
184                                 .willReturn(aResponse()
185                                         .withStatus(404)
186                                         .withHeader("Content-Type", MediaType.APPLICATION_JSON)
187                                         .withBody("test")));
188                 ServiceEndPointRequest request = new ServiceEndPointRequest();
189                 GRMClient client = new GRMClient();
190                 thrown.expect(GRMClientCallFailed.class);
191                 client.addServiceEndPoint(request);
192         }
193
194         @Test
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());
201                 
202         }
203         
204         protected String getFileContentsAsString(String fileName) {
205                 String content = "";
206                 try {
207                         ClassLoader classLoader = this.getClass().getClassLoader();
208                         File file = new File(classLoader.getResource(fileName).getFile());
209                         content = new String(Files.readAllBytes(file.toPath()));
210                 }
211                 catch(Exception e) {
212                         e.printStackTrace();
213                         System.out.println("Exception encountered reading " + fileName + ". Error: " + e.getMessage());
214                 }
215                 return content;
216         }
217 }