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