Client Logging
[so.git] / common / src / test / java / org / onap / so / client / grm / GRMClientTest.java
index 3cce6a6..7b78376 100644 (file)
@@ -24,9 +24,11 @@ import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
 import static com.github.tomakehurst.wiremock.client.WireMock.post;
 import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
 import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
+import static com.github.tomakehurst.wiremock.client.WireMock.*;
+
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
-
+import static org.junit.Assert.fail;
 import java.io.File;
 import java.nio.file.Files;
 import java.util.ArrayList;
@@ -43,6 +45,7 @@ import org.junit.Ignore;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
+import org.onap.logging.ref.slf4j.ONAPLogConstants;
 import org.onap.so.client.grm.beans.OperationalInfo;
 import org.onap.so.client.grm.beans.Property;
 import org.onap.so.client.grm.beans.ServiceEndPoint;
@@ -54,8 +57,7 @@ import org.onap.so.client.grm.exceptions.GRMClientCallFailed;
 
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.github.tomakehurst.wiremock.junit.WireMockRule;
-import org.onap.so.logger.MsoLogger;
-import org.onap.so.logger.MsoLogger.Catalog;
+
 import org.onap.so.utils.TestAppender;
 
 public class GRMClientTest {
@@ -66,6 +68,8 @@ public class GRMClientTest {
        @Rule
        public ExpectedException thrown = ExpectedException.none();
        
+       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}$";
+       
        @BeforeClass
        public static void setUp() throws Exception {
                System.setProperty("mso.config.path", "src/test/resources");
@@ -81,25 +85,45 @@ public class GRMClientTest {
                        .willReturn(aResponse()
                                .withStatus(200)
                                .withHeader("Content-Type", MediaType.APPLICATION_JSON)
-                               .withHeader("X-FromAppId", "GRM")
                                .withBody(endpoints)));
 
-               MDC.put(MsoLogger.SERVICE_NAME, "my-value");
+               MDC.put(ONAPLogConstants.MDCs.SERVICE_NAME, "/test");
                GRMClient client = new GRMClient();
                ServiceEndPointList sel = client.findRunningServices("TEST.ECOMP_PSL.*", 1, "TEST");
                List<ServiceEndPoint> list = sel.getServiceEndPointList();
                assertEquals(3, list.size());
-        ILoggingEvent logEvent = TestAppender.events.get(0);
-        Map<String,String> mdc = logEvent.getMDCPropertyMap();
-        assertNotNull(mdc.get(MsoLogger.METRIC_BEGIN_TIME));
-         assertNotNull(mdc.get(MsoLogger.METRIC_END_TIME));
-        assertNotNull(mdc.get(MsoLogger.REQUEST_ID));
-        assertNotNull(mdc.get(MsoLogger.METRIC_TIMER));
-        assertEquals("200",mdc.get(MsoLogger.RESPONSECODE));
-        assertEquals("GRM",mdc.get(MsoLogger.PARTNERNAME));
-        assertEquals("expect value to not be overwritten by jax rs client interceptor", "my-value",mdc.get(MsoLogger.SERVICE_NAME));
-        assertEquals("COMPLETE",mdc.get(MsoLogger.STATUSCODE));
-        assertNotNull(mdc.get(MsoLogger.RESPONSEDESC));
+               
+               boolean foundInvoke = false;
+               boolean foundInvokeReturn = false;
+        for(ILoggingEvent logEvent : TestAppender.events)
+            if(logEvent.getLoggerName().equals("org.onap.so.logging.jaxrs.filter.JaxRsClientLogging") &&
+                    logEvent.getMarker().getName().equals("INVOKE")
+                    ){
+                Map<String,String> mdc = logEvent.getMDCPropertyMap();
+                assertNotNull(mdc.get(ONAPLogConstants.MDCs.INVOCATION_ID));
+                assertEquals("GRM",mdc.get("TargetEntity"));
+                assertEquals("INPROGRESS",mdc.get(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE));
+                foundInvoke=true;
+            }else if(logEvent.getLoggerName().equals("org.onap.so.logging.jaxrs.filter.JaxRsClientLogging") &&
+                    logEvent.getMarker()!= null && logEvent.getMarker().getName().equals("INVOKE_RETURN")){
+                Map<String,String> mdc = logEvent.getMDCPropertyMap();
+                assertNotNull(mdc.get(ONAPLogConstants.MDCs.INVOCATION_ID));
+                assertEquals("200",mdc.get(ONAPLogConstants.MDCs.RESPONSE_CODE));
+                assertEquals("COMPLETED",mdc.get(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE));
+                foundInvokeReturn=true;
+            }
+        
+        if(!foundInvoke)
+            fail("INVOKE Marker not found");
+        
+        if(!foundInvokeReturn)
+            fail("INVOKE RETURN Marker not found");
+        
+        verify(postRequestedFor(urlEqualTo("/GRMLWPService/v1/serviceEndPoint/findRunning"))
+                .withHeader(ONAPLogConstants.Headers.INVOCATION_ID.toString(), matching(uuidRegex))
+                        .withHeader(ONAPLogConstants.Headers.REQUEST_ID.toString(), matching(uuidRegex))
+                                .withHeader(ONAPLogConstants.Headers.PARTNER_NAME.toString(), equalTo("SO")));
+        TestAppender.events.clear();
        }
        
        @Test 
@@ -116,68 +140,6 @@ public class GRMClientTest {
                client.findRunningServices("TEST.ECOMP_PSL.*", 1, "TEST");
        }
        
-       @Ignore
-       @Test
-       public void testAdd() throws Exception {
-
-               wireMockRule.stubFor(post(urlPathEqualTo("/GRMLWPService/v1/serviceEndPoint/add"))
-                       .willReturn(aResponse()
-                               .withStatus(202)
-                               .withHeader("Content-Type", MediaType.APPLICATION_JSON)
-                               .withBody("test")));
-               wireMockRule.addMockServiceRequestListener((request, response) -> {
-                       System.out.println("URL Requested => " + request.getAbsoluteUrl());
-                       System.out.println("Request Body => " + request.getBodyAsString());
-                       System.out.println("Request Headers => " + request.getHeaders().toString());
-                       System.out.println("Response Status => " + response.getStatus());
-                       System.out.println("Response Body => " + response.getBodyAsString());
-               });     
-               
-               Version ver = new Version();
-               ver.setMajor(1);
-               ver.setMinor(0);
-               ver.setPatch("0");
-
-               ServiceEndPoint sep = new ServiceEndPoint();
-               sep.setName("TEST.ECOMP_PSL.Inventory");
-               sep.setVersion(ver);
-               sep.setHostAddress("127.0.0.1");
-               sep.setListenPort("8080");
-               sep.setLatitude("37.7022");
-               sep.setLongitude("121.9358");
-               sep.setContextPath("/");
-               sep.setRouteOffer("TEST");
-               
-               OperationalInfo operInfo = new OperationalInfo();
-               operInfo.setCreatedBy("edge");
-               operInfo.setUpdatedBy("edge");
-               
-               sep.setOperationalInfo(operInfo);
-               
-               Property prop1 = new Property();
-               prop1.setName("Environment");
-               prop1.setValue("TEST");
-               
-               Property prop2 = new Property();
-               prop2.setName("cpfrun_cluster_name");
-               prop2.setValue("testcase_cluster_no_cluster");
-               
-               List<Property> props = new ArrayList<Property>();
-               props.add(prop1);
-               props.add(prop2);
-               
-               sep.setProperties(props);
-
-               ServiceEndPointRequest request = new ServiceEndPointRequest();
-               request.setEnv("DEV");
-               request.setServiceEndPoint(sep);
-               
-               System.out.println("Request in JSON: " + mapper.writeValueAsString(request));
-               
-               GRMClient client = new GRMClient();
-               client.addServiceEndPoint(request);
-       }
-       
        @Test
        public void testAddFail() throws Exception {
                wireMockRule.stubFor(post(urlPathEqualTo("/GRMLWPService/v1/serviceEndPoint/add"))