From 390e0476092c59676406512f8ce8cdda5dfdd328 Mon Sep 17 00:00:00 2001 From: "Smokowski, Kevin (ks6305)" Date: Tue, 10 Sep 2019 18:14:41 +0000 Subject: updating logging filters maximize re-use in logging filter classes Issue-ID: LOG-1125 Signed-off-by: Smokowski, Kevin (ks6305) Change-Id: I063d57635fe56a60d97945873d0c7b64852182a5 --- .../logging/filter/base/AbstractFilterTest.java | 301 ++++++++++++++++++++ .../filter/base/AuditLogContainerFilterTest.java | 20 +- .../filter/base/AuditLogServletFilterTest.java | 14 +- .../filter/base/LoggingContainerFilterTest.java | 2 - .../org/onap/logging/filter/base/MDCSetupTest.java | 306 --------------------- .../filter/base/MetricLogClientFilterTest.java | 25 +- .../base/PayloadLoggingClientFilterTest.java | 2 - .../onap/logging/filter/base/PropertyUtilTest.java | 50 ---- .../filter/base/SimpleJaxrsHeadersMapTest.java | 2 - .../filter/base/SimpleServletHeadersMapTest.java | 1 - 10 files changed, 325 insertions(+), 398 deletions(-) create mode 100644 reference/logging-filter/logging-filter-base/src/test/java/org/onap/logging/filter/base/AbstractFilterTest.java delete mode 100644 reference/logging-filter/logging-filter-base/src/test/java/org/onap/logging/filter/base/MDCSetupTest.java delete mode 100644 reference/logging-filter/logging-filter-base/src/test/java/org/onap/logging/filter/base/PropertyUtilTest.java (limited to 'reference/logging-filter/logging-filter-base/src/test/java/org/onap/logging/filter/base') diff --git a/reference/logging-filter/logging-filter-base/src/test/java/org/onap/logging/filter/base/AbstractFilterTest.java b/reference/logging-filter/logging-filter-base/src/test/java/org/onap/logging/filter/base/AbstractFilterTest.java new file mode 100644 index 0000000..cfa74ad --- /dev/null +++ b/reference/logging-filter/logging-filter-base/src/test/java/org/onap/logging/filter/base/AbstractFilterTest.java @@ -0,0 +1,301 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - Logging + * ================================================================================ + * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.logging.filter.base; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.mockito.Mockito.when; +import java.util.HashMap; +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.core.HttpHeaders; +import javax.ws.rs.core.MultivaluedHashMap; +import javax.ws.rs.core.MultivaluedMap; +import org.junit.After; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; +import org.onap.logging.ref.slf4j.ONAPLogConstants; +import org.slf4j.MDC; + +@RunWith(MockitoJUnitRunner.class) +public class AbstractFilterTest extends AbstractFilter { + + @Mock + private HttpServletRequest httpServletRequest; + + private String requestId = "4d31fe02-4918-4975-942f-fe51a44e6a9b"; + private String invocationId = "4d31fe02-4918-4975-942f-fe51a44e6a9a"; + + @After + public void tearDown() { + MDC.clear(); + System.clearProperty("partnerName"); + } + + @Test + public void setElapsedTimeTest() { + String expected = "318"; + MDC.put(ONAPLogConstants.MDCs.ENTRY_TIMESTAMP, "2019-06-18T02:09:06.024Z"); + MDC.put(ONAPLogConstants.MDCs.LOG_TIMESTAMP, "2019-06-18T02:09:06.342Z"); + + setElapsedTime(); + assertEquals(expected, MDC.get(ONAPLogConstants.MDCs.ELAPSED_TIME)); + } + + @Test + public void setElapsedTimeInvokeTimestampTest() { + String expected = "318"; + MDC.put(ONAPLogConstants.MDCs.INVOKE_TIMESTAMP, "2019-06-18T02:09:06.024Z"); + MDC.put(ONAPLogConstants.MDCs.LOG_TIMESTAMP, "2019-06-18T02:09:06.342Z"); + + setElapsedTimeInvokeTimestamp(); + assertEquals(expected, MDC.get(ONAPLogConstants.MDCs.ELAPSED_TIME)); + } + + @Test + public void setRequestIdTest() { + HashMap headers = new HashMap<>(); + headers.put(ONAPLogConstants.Headers.REQUEST_ID, requestId); + String fetchedRequestId = getRequestId(new SimpleHashMap(headers)); + assertEquals(requestId, fetchedRequestId); + } + + @Test + public void setRequestIdRequestIdHeaderTest() { + HashMap headers = new HashMap<>(); + headers.put(Constants.HttpHeaders.HEADER_REQUEST_ID, requestId); + String fetchedRequestId = getRequestId(new SimpleHashMap(headers)); + assertEquals(requestId, fetchedRequestId); + } + + @Test + public void setRequestIdTransactionIdHeaderTest() { + HashMap headers = new HashMap<>(); + headers.put(Constants.HttpHeaders.TRANSACTION_ID, requestId); + String fetchedRequestId = getRequestId(new SimpleHashMap(headers)); + assertEquals(requestId, fetchedRequestId); + } + + @Test + public void setRequestIdEcompRequestIdHeaderTest() { + HashMap headers = new HashMap<>(); + headers.put(Constants.HttpHeaders.ECOMP_REQUEST_ID, requestId); + String fetchedRequestId = getRequestId(new SimpleHashMap(headers)); + assertEquals(requestId, fetchedRequestId); + } + + @Test + public void setRequestIdNoHeaderTest() { + HashMap headers = new HashMap<>(); + String fetchedRequestId = getRequestId(new SimpleHashMap(headers)); + assertNotNull(fetchedRequestId); + } + + @Test + public void setInvocationIdTest() { + HashMap headers = new HashMap<>(); + headers.put(ONAPLogConstants.Headers.INVOCATION_ID, invocationId); + setInvocationId(new SimpleHashMap(headers)); + assertEquals(invocationId, MDC.get(ONAPLogConstants.MDCs.INVOCATION_ID)); + } + + @Test + public void setInvocationIdNoHeaderTest() { + HashMap headers = new HashMap<>(); + setInvocationId(new SimpleHashMap(headers)); + assertNotNull(MDC.get(ONAPLogConstants.MDCs.INVOCATION_ID)); + } + + @Test + public void setInvovationIdFromMDCTest() { + MDC.put(ONAPLogConstants.MDCs.INVOCATION_ID, "7b77143c-9b50-410c-ac2f-05758a68e3e8"); + setInvocationIdFromMDC(); + assertEquals("7b77143c-9b50-410c-ac2f-05758a68e3e8", MDC.get(ONAPLogConstants.MDCs.INVOCATION_ID)); + } + + @Test + public void setInvocationIdFromMDCNoInvocationIdTest() { + setInvocationIdFromMDC(); + // InvocationId is set to a random UUID + assertNotNull(MDC.get(ONAPLogConstants.MDCs.INVOCATION_ID)); + } + + @Test + public void setResponseStatusCodeTest() { + setResponseStatusCode(200); + assertEquals("COMPLETE", MDC.get(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE)); + } + + @Test + public void setResponseStatusCodeErrorTest() { + setResponseStatusCode(400); + assertEquals("ERROR", MDC.get(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE)); + assertEquals("400", MDC.get(ONAPLogConstants.MDCs.ERROR_CODE)); + assertEquals("Bad Request", MDC.get(ONAPLogConstants.MDCs.ERROR_DESC)); + } + + @Test + public void clearClientMDCsTest() { + MDC.put(ONAPLogConstants.MDCs.INVOCATION_ID, "7b77143c-9b50-410c-ac2f-05758a68e3e9"); + MDC.put(ONAPLogConstants.MDCs.RESPONSE_DESCRIPTION, "Bad Gateway"); + MDC.put(ONAPLogConstants.MDCs.ERROR_DESC, "Bad Gateway"); + MDC.put(ONAPLogConstants.MDCs.ERROR_CODE, "502"); + MDC.put(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE, "502"); + MDC.put(ONAPLogConstants.MDCs.RESPONSE_CODE, "502"); + MDC.put(ONAPLogConstants.MDCs.TARGET_ENTITY, "SO"); + MDC.put(ONAPLogConstants.MDCs.TARGET_SERVICE_NAME, "SDNC"); + MDC.put(ONAPLogConstants.MDCs.INVOKE_TIMESTAMP, "2019-06-18T02:09:06.024Z"); + + clearClientMDCs(); + assertNull(MDC.get(ONAPLogConstants.MDCs.INVOCATION_ID)); + assertNull(MDC.get(ONAPLogConstants.MDCs.RESPONSE_DESCRIPTION)); + assertNull(MDC.get(ONAPLogConstants.MDCs.ERROR_CODE)); + assertNull(MDC.get(ONAPLogConstants.MDCs.ERROR_DESC)); + assertNull(MDC.get(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE)); + assertNull(MDC.get(ONAPLogConstants.MDCs.RESPONSE_CODE)); + assertNull(MDC.get(ONAPLogConstants.MDCs.TARGET_ENTITY)); + assertNull(MDC.get(ONAPLogConstants.MDCs.TARGET_SERVICE_NAME)); + assertNull(MDC.get(ONAPLogConstants.MDCs.INVOKE_TIMESTAMP)); + } + + @Test + public void setTargetEntityTest() { + setTargetEntity(ONAPComponents.SO); + assertEquals("SO", MDC.get(ONAPLogConstants.MDCs.TARGET_ENTITY)); + } + + @Test + public void setResponseDescriptionTest() { + setResponseDescription(502); + assertEquals("Bad Gateway", MDC.get(ONAPLogConstants.MDCs.RESPONSE_DESCRIPTION)); + } + + @Test + public void setMDCPartnerNameTest() { + MultivaluedMap headerMap = new MultivaluedHashMap<>(); + headerMap.putSingle(ONAPLogConstants.Headers.PARTNER_NAME, "SO"); + SimpleMap headers = new SimpleJaxrsHeadersMap(headerMap); + + setMDCPartnerName(headers); + + assertEquals("SO", MDC.get(ONAPLogConstants.MDCs.PARTNER_NAME)); + } + + @Test + public void setMDCPartnerNameUserAgentHeaderTest() { + MultivaluedMap headerMap = new MultivaluedHashMap<>(); + headerMap.putSingle(HttpHeaders.USER_AGENT, "Apache-HttpClient/4.5.8 (Java/1.8.0_191)"); + SimpleMap headers = new SimpleJaxrsHeadersMap(headerMap); + + setMDCPartnerName(headers); + + assertEquals("Apache-HttpClient/4.5.8 (Java/1.8.0_191)", MDC.get(ONAPLogConstants.MDCs.PARTNER_NAME)); + } + + @Test + public void setMDCPartnerNameClientIdHeaderTest() { + MultivaluedMap headerMap = new MultivaluedHashMap<>(); + headerMap.putSingle(Constants.HttpHeaders.CLIENT_ID, "SO"); + SimpleMap headers = new SimpleJaxrsHeadersMap(headerMap); + + setMDCPartnerName(headers); + + assertEquals("SO", MDC.get(ONAPLogConstants.MDCs.PARTNER_NAME)); + } + + @Test + public void setMDCPartnerNameNoHeaderTest() { + MultivaluedMap headerMap = new MultivaluedHashMap<>(); + SimpleMap headers = new SimpleJaxrsHeadersMap(headerMap); + + setMDCPartnerName(headers); + + assertEquals("UNKNOWN", MDC.get(ONAPLogConstants.MDCs.PARTNER_NAME)); + } + + @Test + public void setServerFQDNTest() { + setServerFQDN(); + assertNotNull(MDC.get(ONAPLogConstants.MDCs.SERVER_IP_ADDRESS)); + assertNotNull(MDC.get(ONAPLogConstants.MDCs.SERVER_FQDN)); + } + + @Test + public void setClientIPAddressTest() { + when(httpServletRequest.getHeader("X-Forwarded-For")).thenReturn("127.0.0.2"); + setClientIPAddress(httpServletRequest); + + assertEquals("127.0.0.2", MDC.get(ONAPLogConstants.MDCs.CLIENT_IP_ADDRESS)); + } + + @Test + public void setClientIPAddressNoHeaderTest() { + when(httpServletRequest.getRemoteAddr()).thenReturn("127.0.0.1"); + setClientIPAddress(httpServletRequest); + + assertEquals("127.0.0.1", MDC.get(ONAPLogConstants.MDCs.CLIENT_IP_ADDRESS)); + } + + @Test + public void setClientIPAddressNullTest() { + setClientIPAddress(null); + + assertEquals("", MDC.get(ONAPLogConstants.MDCs.CLIENT_IP_ADDRESS)); + } + + @Test + public void setEntryTimeStampTest() { + setEntryTimeStamp(); + + assertNotNull(MDC.get(ONAPLogConstants.MDCs.ENTRY_TIMESTAMP)); + } + + @Test + public void setLogTimestampTest() { + setLogTimestamp(); + + assertNotNull(MDC.get(ONAPLogConstants.MDCs.LOG_TIMESTAMP)); + } + + @Test + public void setInstanceIDTest() { + setInstanceID(); + + assertNotNull(MDC.get(ONAPLogConstants.MDCs.INSTANCE_UUID)); + } + + @Test + public void getPropertyTest() { + System.setProperty("partnerName", "partnerName"); + + String partnerName = getProperty("partnerName"); + assertEquals("partnerName", partnerName); + } + + @Test + public void getPropertyNullTest() { + String partnerName = getProperty("partner"); + assertEquals("UNKNOWN", partnerName); + } + +} diff --git a/reference/logging-filter/logging-filter-base/src/test/java/org/onap/logging/filter/base/AuditLogContainerFilterTest.java b/reference/logging-filter/logging-filter-base/src/test/java/org/onap/logging/filter/base/AuditLogContainerFilterTest.java index af80b19..119bdf1 100644 --- a/reference/logging-filter/logging-filter-base/src/test/java/org/onap/logging/filter/base/AuditLogContainerFilterTest.java +++ b/reference/logging-filter/logging-filter-base/src/test/java/org/onap/logging/filter/base/AuditLogContainerFilterTest.java @@ -21,11 +21,11 @@ package org.onap.logging.filter.base; import static org.junit.Assert.assertEquals; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.when; import javax.ws.rs.container.ContainerRequestContext; import javax.ws.rs.container.ContainerResponseContext; +import javax.ws.rs.core.MultivaluedHashMap; +import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.UriInfo; import org.junit.After; import org.junit.Test; @@ -34,14 +34,14 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.Spy; import org.mockito.junit.MockitoJUnitRunner; -import org.onap.logging.filter.base.AuditLogContainerFilter; -import org.onap.logging.filter.base.MDCSetup; -import org.onap.logging.filter.base.SimpleMap; import org.onap.logging.ref.slf4j.ONAPLogConstants; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.slf4j.MDC; @RunWith(MockitoJUnitRunner.class) public class AuditLogContainerFilterTest { + protected static final Logger logger = LoggerFactory.getLogger(AbstractMetricLogFilter.class); @Mock private ContainerRequestContext containerRequest; @@ -49,9 +49,6 @@ public class AuditLogContainerFilterTest { @Mock private ContainerResponseContext containerResponse; - @Mock - private MDCSetup mdcSetup; - @Mock private UriInfo uriInfo; @@ -64,11 +61,14 @@ public class AuditLogContainerFilterTest { MDC.clear(); } + @Test public void filterTest() { - when(mdcSetup.getRequestId(any(SimpleMap.class))).thenReturn("e3b08fa3-535f-4c1b-8228-91318d2bb4ee"); + MultivaluedMap headerMap = new MultivaluedHashMap<>(); + headerMap.putSingle(ONAPLogConstants.Headers.REQUEST_ID, "e3b08fa3-535f-4c1b-8228-91318d2bb4ee"); + when(containerRequest.getHeaders()).thenReturn(headerMap); when(uriInfo.getPath()).thenReturn("onap/so/serviceInstances"); - doReturn(uriInfo).when(containerRequest).getUriInfo(); + when(containerRequest.getUriInfo()).thenReturn(uriInfo); auditLogContainerFilter.filter(containerRequest); assertEquals("e3b08fa3-535f-4c1b-8228-91318d2bb4ee", MDC.get(ONAPLogConstants.MDCs.REQUEST_ID)); diff --git a/reference/logging-filter/logging-filter-base/src/test/java/org/onap/logging/filter/base/AuditLogServletFilterTest.java b/reference/logging-filter/logging-filter-base/src/test/java/org/onap/logging/filter/base/AuditLogServletFilterTest.java index 1a0d0ef..f2a8218 100644 --- a/reference/logging-filter/logging-filter-base/src/test/java/org/onap/logging/filter/base/AuditLogServletFilterTest.java +++ b/reference/logging-filter/logging-filter-base/src/test/java/org/onap/logging/filter/base/AuditLogServletFilterTest.java @@ -21,7 +21,7 @@ package org.onap.logging.filter.base; import static org.junit.Assert.assertEquals; -import static org.mockito.ArgumentMatchers.any; +import static org.junit.Assert.assertNotNull; import static org.mockito.Mockito.when; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; @@ -34,9 +34,6 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.Spy; import org.mockito.junit.MockitoJUnitRunner; -import org.onap.logging.filter.base.AuditLogServletFilter; -import org.onap.logging.filter.base.MDCSetup; -import org.onap.logging.filter.base.SimpleMap; import org.onap.logging.ref.slf4j.ONAPLogConstants; import org.slf4j.MDC; @@ -49,9 +46,6 @@ public class AuditLogServletFilterTest { @Mock private ServletResponse response; - @Mock - private MDCSetup mdcSetup; - @Mock private HttpServletRequest servletRequest; @@ -69,11 +63,10 @@ public class AuditLogServletFilterTest { @Test public void preTest() { - when(mdcSetup.getRequestId(any(SimpleMap.class))).thenReturn("e3b08fa3-535f-4c1b-8228-91318d2bb4ee"); when(servletRequest.getRequestURI()).thenReturn("onap/so/serviceInstances"); - auditLogServletFilter.pre(servletRequest, mdcSetup); + auditLogServletFilter.pre(servletRequest); - assertEquals("e3b08fa3-535f-4c1b-8228-91318d2bb4ee", MDC.get(ONAPLogConstants.MDCs.REQUEST_ID)); + assertNotNull(MDC.get(ONAPLogConstants.MDCs.REQUEST_ID)); assertEquals("onap/so/serviceInstances", MDC.get(ONAPLogConstants.MDCs.SERVICE_NAME)); assertEquals("INPROGRESS", MDC.get(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE)); } @@ -85,4 +78,5 @@ public class AuditLogServletFilterTest { assertEquals(200, responseCode); } + } diff --git a/reference/logging-filter/logging-filter-base/src/test/java/org/onap/logging/filter/base/LoggingContainerFilterTest.java b/reference/logging-filter/logging-filter-base/src/test/java/org/onap/logging/filter/base/LoggingContainerFilterTest.java index 4f8d16d..4d1f102 100644 --- a/reference/logging-filter/logging-filter-base/src/test/java/org/onap/logging/filter/base/LoggingContainerFilterTest.java +++ b/reference/logging-filter/logging-filter-base/src/test/java/org/onap/logging/filter/base/LoggingContainerFilterTest.java @@ -24,8 +24,6 @@ import static org.junit.Assert.assertEquals; import javax.ws.rs.core.MultivaluedHashMap; import javax.ws.rs.core.MultivaluedMap; import org.junit.Test; -import org.onap.logging.filter.base.SimpleJaxrsHeadersMap; -import org.onap.logging.filter.base.SimpleMap; import org.onap.logging.ref.slf4j.ONAPLogConstants; public class LoggingContainerFilterTest { diff --git a/reference/logging-filter/logging-filter-base/src/test/java/org/onap/logging/filter/base/MDCSetupTest.java b/reference/logging-filter/logging-filter-base/src/test/java/org/onap/logging/filter/base/MDCSetupTest.java deleted file mode 100644 index 1687c19..0000000 --- a/reference/logging-filter/logging-filter-base/src/test/java/org/onap/logging/filter/base/MDCSetupTest.java +++ /dev/null @@ -1,306 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP - Logging - * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.logging.filter.base; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.mockito.Mockito.when; -import java.util.HashMap; -import javax.servlet.http.HttpServletRequest; -import javax.ws.rs.core.HttpHeaders; -import javax.ws.rs.core.MultivaluedHashMap; -import javax.ws.rs.core.MultivaluedMap; -import org.junit.After; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.Spy; -import org.mockito.junit.MockitoJUnitRunner; -import org.onap.logging.filter.base.Constants; -import org.onap.logging.filter.base.MDCSetup; -import org.onap.logging.filter.base.ONAPComponents; -import org.onap.logging.filter.base.SimpleHashMap; -import org.onap.logging.filter.base.SimpleJaxrsHeadersMap; -import org.onap.logging.filter.base.SimpleMap; -import org.onap.logging.ref.slf4j.ONAPLogConstants; -import org.slf4j.MDC; - -@RunWith(MockitoJUnitRunner.class) -public class MDCSetupTest { - - @Mock - private HttpServletRequest httpServletRequest; - - @Spy - @InjectMocks - private MDCSetup mdcSetup = new MDCSetup(); - - private String requestId = "4d31fe02-4918-4975-942f-fe51a44e6a9b"; - private String invocationId = "4d31fe02-4918-4975-942f-fe51a44e6a9a"; - - @After - public void tearDown() { - MDC.clear(); - } - - @Test - public void setElapsedTimeTest() { - String expected = "318"; - MDC.put(ONAPLogConstants.MDCs.ENTRY_TIMESTAMP, "2019-06-18T02:09:06.024Z"); - MDC.put(ONAPLogConstants.MDCs.LOG_TIMESTAMP, "2019-06-18T02:09:06.342Z"); - - mdcSetup.setElapsedTime(); - assertEquals(expected, MDC.get(ONAPLogConstants.MDCs.ELAPSED_TIME)); - } - - @Test - public void setElapsedTimeInvokeTimestampTest() { - String expected = "318"; - MDC.put(ONAPLogConstants.MDCs.INVOKE_TIMESTAMP, "2019-06-18T02:09:06.024Z"); - MDC.put(ONAPLogConstants.MDCs.LOG_TIMESTAMP, "2019-06-18T02:09:06.342Z"); - - mdcSetup.setElapsedTimeInvokeTimestamp(); - assertEquals(expected, MDC.get(ONAPLogConstants.MDCs.ELAPSED_TIME)); - } - - @Test - public void setRequestIdTest() { - HashMap headers = new HashMap<>(); - headers.put(ONAPLogConstants.Headers.REQUEST_ID, requestId); - String fetchedRequestId = mdcSetup.getRequestId(new SimpleHashMap(headers)); - assertEquals(requestId, fetchedRequestId); - } - - @Test - public void setRequestIdRequestIdHeaderTest() { - HashMap headers = new HashMap<>(); - headers.put(Constants.HttpHeaders.HEADER_REQUEST_ID, requestId); - String fetchedRequestId = mdcSetup.getRequestId(new SimpleHashMap(headers)); - assertEquals(requestId, fetchedRequestId); - } - - @Test - public void setRequestIdTransactionIdHeaderTest() { - HashMap headers = new HashMap<>(); - headers.put(Constants.HttpHeaders.TRANSACTION_ID, requestId); - String fetchedRequestId = mdcSetup.getRequestId(new SimpleHashMap(headers)); - assertEquals(requestId, fetchedRequestId); - } - - @Test - public void setRequestIdEcompRequestIdHeaderTest() { - HashMap headers = new HashMap<>(); - headers.put(Constants.HttpHeaders.ECOMP_REQUEST_ID, requestId); - String fetchedRequestId = mdcSetup.getRequestId(new SimpleHashMap(headers)); - assertEquals(requestId, fetchedRequestId); - } - - @Test - public void setRequestIdNoHeaderTest() { - HashMap headers = new HashMap<>(); - String fetchedRequestId = mdcSetup.getRequestId(new SimpleHashMap(headers)); - assertNotNull(fetchedRequestId); - } - - @Test - public void setInvocationIdTest() { - HashMap headers = new HashMap<>(); - headers.put(ONAPLogConstants.Headers.INVOCATION_ID, invocationId); - mdcSetup.setInvocationId(new SimpleHashMap(headers)); - assertEquals(invocationId, MDC.get(ONAPLogConstants.MDCs.INVOCATION_ID)); - } - - @Test - public void setInvocationIdNoHeaderTest() { - HashMap headers = new HashMap<>(); - mdcSetup.setInvocationId(new SimpleHashMap(headers)); - assertNotNull(MDC.get(ONAPLogConstants.MDCs.INVOCATION_ID)); - } - - @Test - public void setInvovationIdFromMDCTest() { - MDC.put(ONAPLogConstants.MDCs.INVOCATION_ID, "7b77143c-9b50-410c-ac2f-05758a68e3e8"); - mdcSetup.setInvocationIdFromMDC(); - assertEquals("7b77143c-9b50-410c-ac2f-05758a68e3e8", MDC.get(ONAPLogConstants.MDCs.INVOCATION_ID)); - } - - @Test - public void setInvocationIdFromMDCNoInvocationIdTest() { - mdcSetup.setInvocationIdFromMDC(); - // InvocationId is set to a random UUID - assertNotNull(MDC.get(ONAPLogConstants.MDCs.INVOCATION_ID)); - } - - @Test - public void setResponseStatusCodeTest() { - mdcSetup.setResponseStatusCode(200); - assertEquals("COMPLETE", MDC.get(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE)); - } - - @Test - public void setResponseStatusCodeErrorTest() { - mdcSetup.setResponseStatusCode(400); - assertEquals("ERROR", MDC.get(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE)); - assertEquals("400", MDC.get(ONAPLogConstants.MDCs.ERROR_CODE)); - assertEquals("Bad Request", MDC.get(ONAPLogConstants.MDCs.ERROR_DESC)); - } - - @Test - public void clearClientMDCsTest() { - MDC.put(ONAPLogConstants.MDCs.INVOCATION_ID, "7b77143c-9b50-410c-ac2f-05758a68e3e9"); - MDC.put(ONAPLogConstants.MDCs.RESPONSE_DESCRIPTION, "Bad Gateway"); - MDC.put(ONAPLogConstants.MDCs.ERROR_DESC, "Bad Gateway"); - MDC.put(ONAPLogConstants.MDCs.ERROR_CODE, "502"); - MDC.put(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE, "502"); - MDC.put(ONAPLogConstants.MDCs.RESPONSE_CODE, "502"); - MDC.put(ONAPLogConstants.MDCs.TARGET_ENTITY, "SO"); - MDC.put(ONAPLogConstants.MDCs.TARGET_SERVICE_NAME, "SDNC"); - MDC.put(ONAPLogConstants.MDCs.INVOKE_TIMESTAMP, "2019-06-18T02:09:06.024Z"); - - mdcSetup.clearClientMDCs(); - assertNull(MDC.get(ONAPLogConstants.MDCs.INVOCATION_ID)); - assertNull(MDC.get(ONAPLogConstants.MDCs.RESPONSE_DESCRIPTION)); - assertNull(MDC.get(ONAPLogConstants.MDCs.ERROR_CODE)); - assertNull(MDC.get(ONAPLogConstants.MDCs.ERROR_DESC)); - assertNull(MDC.get(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE)); - assertNull(MDC.get(ONAPLogConstants.MDCs.RESPONSE_CODE)); - assertNull(MDC.get(ONAPLogConstants.MDCs.TARGET_ENTITY)); - assertNull(MDC.get(ONAPLogConstants.MDCs.TARGET_SERVICE_NAME)); - assertNull(MDC.get(ONAPLogConstants.MDCs.INVOKE_TIMESTAMP)); - } - - @Test - public void setTargetEntityTest() { - mdcSetup.setTargetEntity(ONAPComponents.SO); - assertEquals("SO", MDC.get(ONAPLogConstants.MDCs.TARGET_ENTITY)); - } - - @Test - public void setResponseDescriptionTest() { - mdcSetup.setResponseDescription(502); - assertEquals("Bad Gateway", MDC.get(ONAPLogConstants.MDCs.RESPONSE_DESCRIPTION)); - } - - @Test - public void setMDCPartnerNameTest() { - MultivaluedMap headerMap = new MultivaluedHashMap<>(); - headerMap.putSingle(ONAPLogConstants.Headers.PARTNER_NAME, "SO"); - SimpleMap headers = new SimpleJaxrsHeadersMap(headerMap); - - mdcSetup.setMDCPartnerName(headers); - - assertEquals("SO", MDC.get(ONAPLogConstants.MDCs.PARTNER_NAME)); - } - - @Test - public void setMDCPartnerNameUserAgentHeaderTest() { - MultivaluedMap headerMap = new MultivaluedHashMap<>(); - headerMap.putSingle(HttpHeaders.USER_AGENT, "Apache-HttpClient/4.5.8 (Java/1.8.0_191)"); - SimpleMap headers = new SimpleJaxrsHeadersMap(headerMap); - - mdcSetup.setMDCPartnerName(headers); - - assertEquals("Apache-HttpClient/4.5.8 (Java/1.8.0_191)", MDC.get(ONAPLogConstants.MDCs.PARTNER_NAME)); - } - - @Test - public void setMDCPartnerNameClientIdHeaderTest() { - MultivaluedMap headerMap = new MultivaluedHashMap<>(); - headerMap.putSingle(Constants.HttpHeaders.CLIENT_ID, "SO"); - SimpleMap headers = new SimpleJaxrsHeadersMap(headerMap); - - mdcSetup.setMDCPartnerName(headers); - - assertEquals("SO", MDC.get(ONAPLogConstants.MDCs.PARTNER_NAME)); - } - - @Test - public void setMDCPartnerNameNoHeaderTest() { - MultivaluedMap headerMap = new MultivaluedHashMap<>(); - SimpleMap headers = new SimpleJaxrsHeadersMap(headerMap); - - mdcSetup.setMDCPartnerName(headers); - - assertEquals("UNKNOWN", MDC.get(ONAPLogConstants.MDCs.PARTNER_NAME)); - } - - @Test - public void setServerFQDNTest() { - mdcSetup.setServerFQDN(); - assertNotNull(MDC.get(ONAPLogConstants.MDCs.SERVER_IP_ADDRESS)); - assertNotNull(MDC.get(ONAPLogConstants.MDCs.SERVER_FQDN)); - } - - @Test - public void setClientIPAddressTest() { - when(httpServletRequest.getHeader("X-Forwarded-For")).thenReturn("127.0.0.2"); - mdcSetup.setClientIPAddress(httpServletRequest); - - assertEquals("127.0.0.2", MDC.get(ONAPLogConstants.MDCs.CLIENT_IP_ADDRESS)); - } - - @Test - public void setClientIPAddressNoHeaderTest() { - when(httpServletRequest.getRemoteAddr()).thenReturn("127.0.0.1"); - mdcSetup.setClientIPAddress(httpServletRequest); - - assertEquals("127.0.0.1", MDC.get(ONAPLogConstants.MDCs.CLIENT_IP_ADDRESS)); - } - - @Test - public void setClientIPAddressNullTest() { - mdcSetup.setClientIPAddress(null); - - assertEquals("", MDC.get(ONAPLogConstants.MDCs.CLIENT_IP_ADDRESS)); - } - - @Test - public void setEntryTimeStampTest() { - mdcSetup.setEntryTimeStamp(); - - assertNotNull(MDC.get(ONAPLogConstants.MDCs.ENTRY_TIMESTAMP)); - } - - @Test - public void setLogTimestampTest() { - mdcSetup.setLogTimestamp(); - - assertNotNull(MDC.get(ONAPLogConstants.MDCs.LOG_TIMESTAMP)); - } - - @Test - public void setInstanceIDTest() { - mdcSetup.setInstanceID(); - - assertNotNull(MDC.get(ONAPLogConstants.MDCs.INSTANCE_UUID)); - } - - @Test - public void setServiceNameTest() { - String requestUri = "onap/so/serviceInstances"; - when(httpServletRequest.getRequestURI()).thenReturn(requestUri); - mdcSetup.setServiceName(httpServletRequest); - - assertEquals("onap/so/serviceInstances", MDC.get(ONAPLogConstants.MDCs.SERVICE_NAME)); - } -} diff --git a/reference/logging-filter/logging-filter-base/src/test/java/org/onap/logging/filter/base/MetricLogClientFilterTest.java b/reference/logging-filter/logging-filter-base/src/test/java/org/onap/logging/filter/base/MetricLogClientFilterTest.java index 9a9f883..ed217aa 100644 --- a/reference/logging-filter/logging-filter-base/src/test/java/org/onap/logging/filter/base/MetricLogClientFilterTest.java +++ b/reference/logging-filter/logging-filter-base/src/test/java/org/onap/logging/filter/base/MetricLogClientFilterTest.java @@ -23,9 +23,11 @@ package org.onap.logging.filter.base; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.when; import java.net.URI; import java.net.URISyntaxException; +import java.time.ZoneOffset; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; import javax.ws.rs.client.ClientRequestContext; import javax.ws.rs.core.MultivaluedHashMap; import javax.ws.rs.core.MultivaluedMap; @@ -36,18 +38,11 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.Spy; import org.mockito.junit.MockitoJUnitRunner; -import org.onap.logging.filter.base.Constants; -import org.onap.logging.filter.base.MDCSetup; -import org.onap.logging.filter.base.MetricLogClientFilter; import org.onap.logging.ref.slf4j.ONAPLogConstants; import org.slf4j.MDC; @RunWith(MockitoJUnitRunner.class) public class MetricLogClientFilterTest { - - @Mock - private MDCSetup mdcSetup; - @Mock private ClientRequestContext clientRequest; @@ -64,10 +59,9 @@ public class MetricLogClientFilterTest { public void setupHeadersTest() { MDC.put(ONAPLogConstants.MDCs.INVOCATION_ID, "8819bfb4-69d2-43fc-b0d6-81d2690533ea"); MultivaluedMap headers = new MultivaluedHashMap<>(); - when(clientRequest.getHeaders()).thenReturn(headers); - doReturn("0a908a5d-e774-4558-96ff-6edcbba65483").when(metricLogClientFilter).extractRequestID(clientRequest); + doReturn("0a908a5d-e774-4558-96ff-6edcbba65483").when(metricLogClientFilter).extractRequestID(); - metricLogClientFilter.setupHeaders(clientRequest); + metricLogClientFilter.setupHeaders(clientRequest, headers); assertEquals("0a908a5d-e774-4558-96ff-6edcbba65483", headers.getFirst(ONAPLogConstants.Headers.REQUEST_ID)); assertEquals("0a908a5d-e774-4558-96ff-6edcbba65483", headers.getFirst(Constants.HttpHeaders.HEADER_REQUEST_ID)); @@ -80,6 +74,7 @@ public class MetricLogClientFilterTest { @Test public void setupMDCTest() throws URISyntaxException { + // TODO ingest change from upstream MDC.put(ONAPLogConstants.MDCs.TARGET_ENTITY, "SO"); URI uri = new URI("onap/so/serviceInstances"); doReturn(uri).when(clientRequest).getUri(); @@ -110,15 +105,15 @@ public class MetricLogClientFilterTest { @Test public void extractRequestIDTest() { MDC.put(ONAPLogConstants.MDCs.REQUEST_ID, "0a908a5d-e774-4558-96ff-6edcbba65483"); - String requestId = metricLogClientFilter.extractRequestID(clientRequest); + String requestId = metricLogClientFilter.extractRequestID(); assertEquals("0a908a5d-e774-4558-96ff-6edcbba65483", requestId); } @Test public void extractRequestIDNullTest() throws URISyntaxException { - URI uri = new URI("onap/so/serviceInstances"); - doReturn(uri).when(clientRequest).getUri(); - String requestId = metricLogClientFilter.extractRequestID(clientRequest); + MDC.put(ONAPLogConstants.MDCs.INVOKE_TIMESTAMP, + ZonedDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ISO_INSTANT)); + String requestId = metricLogClientFilter.extractRequestID(); assertNotNull(requestId); assertNotNull(ONAPLogConstants.MDCs.LOG_TIMESTAMP); assertNotNull(ONAPLogConstants.MDCs.ELAPSED_TIME); diff --git a/reference/logging-filter/logging-filter-base/src/test/java/org/onap/logging/filter/base/PayloadLoggingClientFilterTest.java b/reference/logging-filter/logging-filter-base/src/test/java/org/onap/logging/filter/base/PayloadLoggingClientFilterTest.java index 5156516..0290784 100644 --- a/reference/logging-filter/logging-filter-base/src/test/java/org/onap/logging/filter/base/PayloadLoggingClientFilterTest.java +++ b/reference/logging-filter/logging-filter-base/src/test/java/org/onap/logging/filter/base/PayloadLoggingClientFilterTest.java @@ -33,8 +33,6 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.Spy; import org.mockito.junit.MockitoJUnitRunner; -import org.onap.logging.filter.base.Constants; -import org.onap.logging.filter.base.PayloadLoggingClientFilter; @RunWith(MockitoJUnitRunner.class) public class PayloadLoggingClientFilterTest { diff --git a/reference/logging-filter/logging-filter-base/src/test/java/org/onap/logging/filter/base/PropertyUtilTest.java b/reference/logging-filter/logging-filter-base/src/test/java/org/onap/logging/filter/base/PropertyUtilTest.java deleted file mode 100644 index 4ec14bc..0000000 --- a/reference/logging-filter/logging-filter-base/src/test/java/org/onap/logging/filter/base/PropertyUtilTest.java +++ /dev/null @@ -1,50 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP - Logging - * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.logging.filter.base; - -import static org.junit.Assert.assertEquals; -import org.junit.After; -import org.junit.Test; -import org.onap.logging.filter.base.PropertyUtil; - -public class PropertyUtilTest { - - private PropertyUtil propertyUtil = new PropertyUtil(); - - @After - public void tearDown() { - System.clearProperty("partnerName"); - } - - @Test - public void getPropertyTest() { - System.setProperty("partnerName", "partnerName"); - - String partnerName = propertyUtil.getProperty("partnerName"); - assertEquals("partnerName", partnerName); - } - - @Test - public void getPropertyNullTest() { - String partnerName = propertyUtil.getProperty("partner"); - assertEquals("UNKNOWN", partnerName); - } -} diff --git a/reference/logging-filter/logging-filter-base/src/test/java/org/onap/logging/filter/base/SimpleJaxrsHeadersMapTest.java b/reference/logging-filter/logging-filter-base/src/test/java/org/onap/logging/filter/base/SimpleJaxrsHeadersMapTest.java index a1c5f9b..e2c5da9 100644 --- a/reference/logging-filter/logging-filter-base/src/test/java/org/onap/logging/filter/base/SimpleJaxrsHeadersMapTest.java +++ b/reference/logging-filter/logging-filter-base/src/test/java/org/onap/logging/filter/base/SimpleJaxrsHeadersMapTest.java @@ -24,8 +24,6 @@ import static org.junit.Assert.assertEquals; import javax.ws.rs.core.MultivaluedHashMap; import javax.ws.rs.core.MultivaluedMap; import org.junit.Test; -import org.onap.logging.filter.base.SimpleJaxrsHeadersMap; -import org.onap.logging.filter.base.SimpleMap; import org.onap.logging.ref.slf4j.ONAPLogConstants; public class SimpleJaxrsHeadersMapTest { diff --git a/reference/logging-filter/logging-filter-base/src/test/java/org/onap/logging/filter/base/SimpleServletHeadersMapTest.java b/reference/logging-filter/logging-filter-base/src/test/java/org/onap/logging/filter/base/SimpleServletHeadersMapTest.java index f1e6af8..fff6776 100644 --- a/reference/logging-filter/logging-filter-base/src/test/java/org/onap/logging/filter/base/SimpleServletHeadersMapTest.java +++ b/reference/logging-filter/logging-filter-base/src/test/java/org/onap/logging/filter/base/SimpleServletHeadersMapTest.java @@ -29,7 +29,6 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.Spy; import org.mockito.junit.MockitoJUnitRunner; -import org.onap.logging.filter.base.SimpleServletHeadersMap; @RunWith(MockitoJUnitRunner.class) public class SimpleServletHeadersMapTest { -- cgit 1.2.3-korg