From 14b8d9552808063686e0c22760cec6e35b960b59 Mon Sep 17 00:00:00 2001 From: EmmettCox Date: Mon, 14 Jan 2019 17:19:45 +0000 Subject: Add RequestId and InvocationId to DR Change-Id: I42575123396ce4b07c824846a8b2b3ca49dfbd85 Issue-ID: DMAAP-941 Signed-off-by: EmmettCox --- .../dmaap/datarouter/node/NodeServletTest.java | 43 +++++++++++++++++-- .../onap/dmaap/datarouter/node/NodeUtilsTest.java | 48 ++++++++++++++++++++++ 2 files changed, 88 insertions(+), 3 deletions(-) (limited to 'datarouter-node/src/test/java/org') diff --git a/datarouter-node/src/test/java/org/onap/dmaap/datarouter/node/NodeServletTest.java b/datarouter-node/src/test/java/org/onap/dmaap/datarouter/node/NodeServletTest.java index fbdd9230..4f31e922 100644 --- a/datarouter-node/src/test/java/org/onap/dmaap/datarouter/node/NodeServletTest.java +++ b/datarouter-node/src/test/java/org/onap/dmaap/datarouter/node/NodeServletTest.java @@ -22,22 +22,25 @@ ******************************************************************************/ package org.onap.dmaap.datarouter.node; +import ch.qos.logback.classic.spi.ILoggingEvent; +import ch.qos.logback.core.read.ListAppender; +import ch.qos.logback.classic.Logger; import org.apache.commons.lang3.reflect.FieldUtils; -import org.junit.Before; -import org.junit.Test; +import org.junit.*; import org.junit.runner.RunWith; import org.mockito.Mock; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor; import org.powermock.modules.junit4.PowerMockRunner; +import org.slf4j.LoggerFactory; -import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.*; import static org.hamcrest.Matchers.notNullValue; +import static org.junit.Assert.assertEquals; import static org.mockito.Matchers.eq; import static org.mockito.Mockito.*; @@ -53,8 +56,11 @@ public class NodeServletTest { @Mock private HttpServletResponse response; + ListAppender listAppender; + @Before public void setUp() throws Exception{ + listAppender = setTestLogger(); nodeServlet = new NodeServlet(); setBehalfHeader("Stub_Value"); when(request.getPathInfo()).thenReturn("2"); @@ -70,6 +76,7 @@ public class NodeServletTest { setNodeConfigManagerIsConfiguredToReturnFalse(); nodeServlet.doGet(request, response); verify(response).sendError(eq(HttpServletResponse.SC_SERVICE_UNAVAILABLE)); + verifyEnteringExitCalled(listAppender); } @Test @@ -77,6 +84,7 @@ public class NodeServletTest { when(request.getPathInfo()).thenReturn("/internal/fetchProv"); nodeServlet.doGet(request, response); verify(response).setStatus(eq(HttpServletResponse.SC_NO_CONTENT)); + verifyEnteringExitCalled(listAppender); } @Test @@ -84,6 +92,7 @@ public class NodeServletTest { when(request.getPathInfo()).thenReturn("/internal/resetSubscription/1"); nodeServlet.doGet(request, response); verify(response).setStatus(eq(HttpServletResponse.SC_NO_CONTENT)); + verifyEnteringExitCalled(listAppender); } @Test @@ -91,6 +100,7 @@ public class NodeServletTest { when(request.getPathInfo()).thenReturn("/incorrect"); nodeServlet.doGet(request, response); verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND)); + verifyEnteringExitCalled(listAppender); } @Test @@ -98,6 +108,7 @@ public class NodeServletTest { setNodeConfigManagerIsConfiguredToReturnFalse(); nodeServlet.doPut(request, response); verify(response).sendError(eq(HttpServletResponse.SC_SERVICE_UNAVAILABLE)); + verifyEnteringExitCalled(listAppender); } @Test @@ -105,6 +116,7 @@ public class NodeServletTest { when(request.getPathInfo()).thenReturn("/incorrect/"); nodeServlet.doPut(request, response); verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class))); + verifyEnteringExitCalled(listAppender); } @Test @@ -112,6 +124,7 @@ public class NodeServletTest { when(request.isSecure()).thenReturn(false); nodeServlet.doPut(request, response); verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class))); + verifyEnteringExitCalled(listAppender); } @Test @@ -119,6 +132,7 @@ public class NodeServletTest { when(request.getPathInfo()).thenReturn(null); nodeServlet.doPut(request, response); verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class))); + verifyEnteringExitCalled(listAppender); } @Test @@ -126,6 +140,7 @@ public class NodeServletTest { when(request.getHeader("Authorization")).thenReturn(null); nodeServlet.doPut(request, response); verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class))); + verifyEnteringExitCalled(listAppender); } @Test @@ -133,6 +148,7 @@ public class NodeServletTest { when(request.getPathInfo()).thenReturn("/publish/"); nodeServlet.doPut(request, response); verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class))); + verifyEnteringExitCalled(listAppender); } @Test @@ -141,6 +157,7 @@ public class NodeServletTest { setNodeConfigManagerIsPublishPermittedToReturnAReason(); nodeServlet.doPut(request, response); verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class))); + verifyEnteringExitCalled(listAppender); } @Test @@ -149,6 +166,7 @@ public class NodeServletTest { setNodeConfigManagerIsPublishPermittedToReturnAReason(); nodeServlet.doPut(request, response); verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN)); + verifyEnteringExitCalled(listAppender); } @Test @@ -156,6 +174,7 @@ public class NodeServletTest { when(request.getPathInfo()).thenReturn("/internal/publish/1/"); nodeServlet.doPut(request, response); verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class))); + verifyEnteringExitCalled(listAppender); } @Test @@ -164,6 +183,7 @@ public class NodeServletTest { setNodeConfigManagerToAllowRedirectOnIngressNode(); nodeServlet.doPut(request, response); verify(response).sendRedirect(anyString()); + verifyEnteringExitCalled(listAppender); } @Test @@ -172,6 +192,7 @@ public class NodeServletTest { setHeadersForValidRequest(true); nodeServlet.doPut(request, response); verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class))); + verifyEnteringExitCalled(listAppender); } @Test @@ -180,6 +201,7 @@ public class NodeServletTest { setHeadersForValidRequest(false); nodeServlet.doPut(request, response); verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class))); + verifyEnteringExitCalled(listAppender); } @Test @@ -188,6 +210,7 @@ public class NodeServletTest { setHeadersForValidRequest(false); nodeServlet.doDelete(request, response); verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class))); + verifyEnteringExitCalled(listAppender); } @@ -195,6 +218,20 @@ public class NodeServletTest { when(request.getHeader("X-ATT-DR-ON-BEHALF-OF")).thenReturn(headerValue); } + private ListAppender setTestLogger() { + Logger Logger = (Logger) LoggerFactory.getLogger(NodeServlet.class); + ListAppender listAppender = new ListAppender<>(); + listAppender.start(); + Logger.addAppender(listAppender); + return listAppender; + } + + private void verifyEnteringExitCalled(ListAppender listAppender) { + assertEquals("EELF0004I Entering data router node component with RequestId and InvocationId", listAppender.list.get(0).getMessage()); + assertEquals("EELF0005I Exiting data router node component with RequestId and InvocationId", listAppender.list.get(2).getMessage()); + assertEquals(3, listAppender.list.size()); + } + private void setUpConfig() throws IllegalAccessException{ NodeConfigManager config = mock(NodeConfigManager.class); PowerMockito.mockStatic(NodeConfigManager.class); diff --git a/datarouter-node/src/test/java/org/onap/dmaap/datarouter/node/NodeUtilsTest.java b/datarouter-node/src/test/java/org/onap/dmaap/datarouter/node/NodeUtilsTest.java index 7dde043d..27fcd1c4 100644 --- a/datarouter-node/src/test/java/org/onap/dmaap/datarouter/node/NodeUtilsTest.java +++ b/datarouter-node/src/test/java/org/onap/dmaap/datarouter/node/NodeUtilsTest.java @@ -25,13 +25,31 @@ package org.onap.dmaap.datarouter.node; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor; import org.powermock.modules.junit4.PowerMockRunner; +import org.slf4j.MDC; + +import javax.servlet.http.HttpServletRequest; + +import java.io.IOException; +import java.net.InetAddress; +import java.util.UUID; + +import static com.att.eelf.configuration.Configuration.MDC_SERVER_FQDN; +import static com.att.eelf.configuration.Configuration.MDC_SERVER_IP_ADDRESS; +import static org.mockito.Mockito.when; +import static org.powermock.api.mockito.PowerMockito.mockStatic; @RunWith(PowerMockRunner.class) @SuppressStaticInitializationFor("org.onap.dmaap.datarouter.node.NodeUtils") +@PrepareForTest({ UUID.class, InetAddress.class }) public class NodeUtilsTest { + @Mock + private HttpServletRequest request; + @Test public void Given_Uri_With_Params_Then_Get_Feed_And_File_Id_Returns_Correct_Values() { String uri = "prov.datarouternew.com:8443/feed/12/fileName"; @@ -58,4 +76,34 @@ public class NodeUtilsTest { String s = NodeUtils.unloge("\\esearch\\ppub\\p12\\n"); Assert.assertEquals("\\search|pub|12\n", s); } + + @Test + public void Given_Request_Has_RequestId_And_InvocationId_Headers_Set_MDC_Values() { + when(request.getHeader("X-ONAP-RequestID")).thenReturn("123"); + when(request.getHeader("X-InvocationID")).thenReturn("456"); + NodeUtils.setRequestIdAndInvocationId(request); + Assert.assertEquals("123", MDC.get("RequestId")); + Assert.assertEquals("456", MDC.get("InvocationId")); + } + + @Test + public void Given_setIpAndFqdnForEelf_Called_Set_MDC_Values() throws IOException { + mockStatic(InetAddress.class); + when(InetAddress.getLocalHost().getHostName()).thenReturn("testHostName"); + when(InetAddress.getLocalHost().getHostAddress()).thenReturn("testHostAddress"); + NodeUtils.setIpAndFqdnForEelf("doGet"); + Assert.assertEquals("testHostName", MDC.get(MDC_SERVER_FQDN)); + Assert.assertEquals("testHostAddress", MDC.get(MDC_SERVER_IP_ADDRESS)); + } + + @Test + public void Given_Request_Has_Empty_RequestId_And_InvocationId_Headers_Generate_MDC_Values() { + when(request.getHeader("X-ONAP-RequestID")).thenReturn(""); + when(request.getHeader("X-InvocationID")).thenReturn(""); + mockStatic(UUID.class); + when(UUID.randomUUID().toString()).thenReturn("123", "456"); + NodeUtils.setRequestIdAndInvocationId(request); + Assert.assertEquals("123", MDC.get("RequestId")); + Assert.assertEquals("456", MDC.get("InvocationId")); + } } -- cgit 1.2.3-korg