2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2019 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
 
  11  *      http://www.apache.org/licenses/LICENSE-2.0
 
  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=========================================================
 
  21 package org.onap.logging.filter.base;
 
  23 import static org.junit.Assert.assertEquals;
 
  24 import static org.junit.Assert.assertNotNull;
 
  25 import static org.mockito.Mockito.doReturn;
 
  27 import java.net.URISyntaxException;
 
  28 import java.time.ZoneOffset;
 
  29 import java.time.ZonedDateTime;
 
  30 import java.time.format.DateTimeFormatter;
 
  31 import javax.ws.rs.client.ClientRequestContext;
 
  32 import javax.ws.rs.core.MultivaluedHashMap;
 
  33 import javax.ws.rs.core.MultivaluedMap;
 
  34 import org.junit.After;
 
  35 import org.junit.Test;
 
  36 import org.junit.runner.RunWith;
 
  37 import org.mockito.InjectMocks;
 
  38 import org.mockito.Mock;
 
  39 import org.mockito.Spy;
 
  40 import org.mockito.junit.MockitoJUnitRunner;
 
  41 import org.onap.logging.ref.slf4j.ONAPLogConstants;
 
  44 @RunWith(MockitoJUnitRunner.class)
 
  45 public class MetricLogClientFilterTest {
 
  47     private ClientRequestContext clientRequest;
 
  51     private MetricLogClientFilter metricLogClientFilter;
 
  54     public void tearDown() {
 
  59     public void setupHeadersTest() {
 
  60         MultivaluedMap<String, Object> headers = new MultivaluedHashMap<>();
 
  61         doReturn("0a908a5d-e774-4558-96ff-6edcbba65483").when(metricLogClientFilter).extractRequestID();
 
  63         metricLogClientFilter.setupHeaders(clientRequest, headers);
 
  65         assertEquals("0a908a5d-e774-4558-96ff-6edcbba65483", headers.getFirst(ONAPLogConstants.Headers.REQUEST_ID));
 
  66         assertEquals("0a908a5d-e774-4558-96ff-6edcbba65483", headers.getFirst(Constants.HttpHeaders.HEADER_REQUEST_ID));
 
  67         assertEquals("0a908a5d-e774-4558-96ff-6edcbba65483", headers.getFirst(Constants.HttpHeaders.TRANSACTION_ID));
 
  68         assertEquals("0a908a5d-e774-4558-96ff-6edcbba65483", headers.getFirst(Constants.HttpHeaders.TRANSACTION_ID));
 
  69         assertEquals("0a908a5d-e774-4558-96ff-6edcbba65483", headers.getFirst(Constants.HttpHeaders.ECOMP_REQUEST_ID));
 
  70         assertNotNull(headers.getFirst(ONAPLogConstants.Headers.INVOCATION_ID));
 
  71         assertEquals("UNKNOWN", headers.getFirst(ONAPLogConstants.Headers.PARTNER_NAME));
 
  75     public void setInvocationIdTest() {
 
  76         String invocationId = metricLogClientFilter.setInvocationId();
 
  78         assertEquals(invocationId, MDC.get(ONAPLogConstants.MDCs.CLIENT_INVOCATION_ID));
 
  79         assertEquals(invocationId, MDC.get(ONAPLogConstants.MDCs.INVOCATION_ID));
 
  83     public void setupMDCTest() throws URISyntaxException {
 
  84         // TODO ingest change from upstream
 
  85         MDC.put(ONAPLogConstants.MDCs.TARGET_ENTITY, "SO");
 
  86         URI uri = new URI("onap/so/serviceInstances");
 
  87         doReturn(uri).when(clientRequest).getUri();
 
  89         metricLogClientFilter.setupMDC(clientRequest);
 
  91         assertEquals("onap/so/serviceInstances", MDC.get(ONAPLogConstants.MDCs.TARGET_SERVICE_NAME));
 
  92         assertEquals("SO", MDC.get(ONAPLogConstants.MDCs.TARGET_ENTITY));
 
  93         assertEquals("INPROGRESS", MDC.get(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE));
 
  94         assertNotNull(ONAPLogConstants.MDCs.SERVICE_NAME);
 
  95         assertNotNull(ONAPLogConstants.MDCs.SERVER_FQDN);
 
  96         assertNotNull(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE);
 
 100     public void setupMDCNoTargetEntityTest() throws URISyntaxException {
 
 101         URI uri = new URI("onap/so/serviceInstances");
 
 102         doReturn(uri).when(clientRequest).getUri();
 
 104         metricLogClientFilter.setupMDC(clientRequest);
 
 106         assertEquals("onap/so/serviceInstances", MDC.get(ONAPLogConstants.MDCs.TARGET_SERVICE_NAME));
 
 107         assertEquals("Unknown-Target-Entity", MDC.get(ONAPLogConstants.MDCs.TARGET_ENTITY));
 
 108         assertEquals("INPROGRESS", MDC.get(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE));
 
 109         assertNotNull(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE);
 
 113     public void extractRequestIDTest() {
 
 114         MDC.put(ONAPLogConstants.MDCs.REQUEST_ID, "0a908a5d-e774-4558-96ff-6edcbba65483");
 
 115         String requestId = metricLogClientFilter.extractRequestID();
 
 116         assertEquals("0a908a5d-e774-4558-96ff-6edcbba65483", requestId);
 
 120     public void extractRequestIDNullTest() throws URISyntaxException {
 
 121         MDC.put(ONAPLogConstants.MDCs.INVOKE_TIMESTAMP,
 
 122                 ZonedDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ISO_INSTANT));
 
 123         String requestId = metricLogClientFilter.extractRequestID();
 
 124         assertNotNull(requestId);
 
 125         assertNotNull(ONAPLogConstants.MDCs.LOG_TIMESTAMP);
 
 126         assertNotNull(ONAPLogConstants.MDCs.ELAPSED_TIME);