580252ae7359a2002a826dbf41ce95b346c2140c
[so.git] / mso-api-handlers / mso-api-handler-infra / 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.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;
33
34 import java.io.File;
35 import java.nio.file.Files;
36 import java.util.List;
37 import java.util.Map;
38
39 import javax.ws.rs.core.MediaType;
40
41 import org.junit.BeforeClass;
42 import org.junit.Rule;
43 import org.junit.Test;
44 import org.junit.rules.ExpectedException;
45 import org.onap.logging.ref.slf4j.ONAPLogConstants;
46 import org.onap.so.apihandlerinfra.BaseTest;
47 import org.onap.so.apihandlerinfra.TestAppender;
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.exceptions.GRMClientCallFailed;
53 import org.slf4j.MDC;
54
55 import ch.qos.logback.classic.spi.ILoggingEvent;
56
57
58 public class GRMClientTest extends BaseTest{
59         
60         
61         @Rule
62         public ExpectedException thrown = ExpectedException.none();
63         
64         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}$";
65         
66         @BeforeClass
67         public static void setUp() throws Exception {
68                 System.setProperty("mso.config.path", "src/test/resources");
69         }
70         
71         @Test
72         public void testFind() throws Exception {
73         TestAppender.events.clear();
74                 String endpoints = getFileContentsAsString("__files/grm/endpoints.json");
75                 wireMockServer.stubFor(post(urlPathEqualTo("/GRMLWPService/v1/serviceEndPoint/findRunning"))
76                         .willReturn(aResponse()
77                                 .withStatus(200)
78                                 .withHeader("Content-Type", MediaType.APPLICATION_JSON)
79                                 .withBody(endpoints)));
80
81                 MDC.put(ONAPLogConstants.MDCs.SERVICE_NAME, "/test");
82                 GRMClient client = new GRMClient();
83                 ServiceEndPointList sel = client.findRunningServices("TEST.ECOMP_PSL.*", 1, "TEST");
84                 List<ServiceEndPoint> list = sel.getServiceEndPointList();
85                 assertEquals(3, list.size());
86                 
87                 boolean foundInvoke = false;
88                 boolean foundInvokeReturn = false;
89         for(ILoggingEvent logEvent : TestAppender.events)
90             if(logEvent.getLoggerName().equals("org.onap.so.logging.jaxrs.filter.JaxRsClientLogging") &&
91                         logEvent.getMarker() != null && logEvent.getMarker().getName().equals("INVOKE")
92                     ){
93                 Map<String,String> mdc = logEvent.getMDCPropertyMap();
94                 assertNotNull(mdc.get(ONAPLogConstants.MDCs.INVOCATION_ID));
95                 assertEquals("GRM",mdc.get("TargetEntity"));
96                 assertEquals("INPROGRESS",mdc.get(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE));
97                 foundInvoke=true;
98             }else if(logEvent.getLoggerName().equals("org.onap.so.logging.jaxrs.filter.JaxRsClientLogging") &&
99                     logEvent.getMarker()!= null && logEvent.getMarker().getName().equals("INVOKE_RETURN")){
100                 Map<String,String> mdc = logEvent.getMDCPropertyMap();
101                 assertNotNull(mdc.get(ONAPLogConstants.MDCs.INVOCATION_ID));
102                 assertEquals("200",mdc.get(ONAPLogConstants.MDCs.RESPONSE_CODE));
103                 assertEquals("COMPLETED",mdc.get(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE));
104                 foundInvokeReturn=true;
105             }
106         
107         if(!foundInvoke)
108             fail("INVOKE Marker not found");
109         
110         if(!foundInvokeReturn)
111             fail("INVOKE RETURN Marker not found");
112         
113        wireMockServer.verify(postRequestedFor(urlEqualTo("/GRMLWPService/v1/serviceEndPoint/findRunning"))
114                 .withHeader(ONAPLogConstants.Headers.INVOCATION_ID.toString(), matching(uuidRegex))
115                         .withHeader(ONAPLogConstants.Headers.REQUEST_ID.toString(), matching(uuidRegex))
116                                 .withHeader(ONAPLogConstants.Headers.PARTNER_NAME.toString(), equalTo("SO")));
117         TestAppender.events.clear();
118         }
119         
120         @Test 
121         public void testFindFail() throws Exception {           
122                 wireMockServer.stubFor(post(urlPathEqualTo("/GRMLWPService/v1/serviceEndPoint/findRunning"))
123                         .willReturn(aResponse()
124                                 .withStatus(400)
125                                 .withHeader("Content-Type", MediaType.APPLICATION_JSON)
126                                 .withBody("")));
127                 
128                 GRMClient client = new GRMClient();
129                 thrown.expect(GRMClientCallFailed.class);
130                 client.findRunningServices("TEST.ECOMP_PSL.*", 1, "TEST");
131         }
132         
133         @Test
134         public void testAddFail() throws Exception {
135                 wireMockServer.stubFor(post(urlPathEqualTo("/GRMLWPService/v1/serviceEndPoint/add"))
136                                 .willReturn(aResponse()
137                                         .withStatus(404)
138                                         .withHeader("Content-Type", MediaType.APPLICATION_JSON)
139                                         .withBody("test")));
140                 ServiceEndPointRequest request = new ServiceEndPointRequest();
141                 GRMClient client = new GRMClient();
142                 thrown.expect(GRMClientCallFailed.class);
143                 client.addServiceEndPoint(request);
144         }
145
146         @Test
147         public void testBuildServiceEndPointLookupRequest() {
148                 GRMClient client = new GRMClient();
149                 ServiceEndPointLookupRequest request = client.buildServiceEndPointlookupRequest("TEST.ECOMP_PSL.Inventory", 1, "DEV");
150                 assertEquals("TEST.ECOMP_PSL.Inventory", request.getServiceEndPoint().getName());
151                 assertEquals(Integer.valueOf(1), Integer.valueOf(request.getServiceEndPoint().getVersion().getMajor()));
152                 assertEquals("DEV", request.getEnv());
153                 
154         }
155         
156         protected String getFileContentsAsString(String fileName) {
157                 String content = "";
158                 try {
159                         ClassLoader classLoader = this.getClass().getClassLoader();
160                         File file = new File(classLoader.getResource(fileName).getFile());
161                         content = new String(Files.readAllBytes(file.toPath()));
162                 }
163                 catch(Exception e) {
164                         e.printStackTrace();
165                         System.out.println("Exception encountered reading " + fileName + ". Error: " + e.getMessage());
166                 }
167                 return content;
168         }
169 }