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