diff options
author | EmmettCox <emmett.cox@est.tech> | 2019-01-14 17:19:45 +0000 |
---|---|---|
committer | ezcoxem <emmett.cox@est.tech> | 2019-01-31 16:15:21 +0000 |
commit | 14b8d9552808063686e0c22760cec6e35b960b59 (patch) | |
tree | 286336813c8da64ca8a8b5d27f21f2bc83f26e0f /datarouter-prov/src/test | |
parent | 72cd9f3033731242779e708a05f9519f36cf8d43 (diff) |
Add RequestId and InvocationId to DR
Change-Id: I42575123396ce4b07c824846a8b2b3ca49dfbd85
Issue-ID: DMAAP-941
Signed-off-by: EmmettCox <emmett.cox@est.tech>
Diffstat (limited to 'datarouter-prov/src/test')
10 files changed, 304 insertions, 17 deletions
diff --git a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/BaseServletTest.java b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/BaseServletTest.java index 8cc48683..79c3d219 100755 --- a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/BaseServletTest.java +++ b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/BaseServletTest.java @@ -24,6 +24,7 @@ package org.onap.dmaap.datarouter.provisioning; import org.apache.commons.lang3.reflect.FieldUtils; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -33,11 +34,16 @@ import org.onap.dmaap.datarouter.provisioning.beans.FeedAuthorization; import org.onap.dmaap.datarouter.provisioning.beans.Group; import org.onap.dmaap.datarouter.provisioning.beans.Subscription; import org.powermock.api.mockito.PowerMockito; +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.util.HashSet; import java.util.Set; +import java.util.UUID; + import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.nullValue; import static org.junit.Assert.assertNull; @@ -45,11 +51,14 @@ import static org.junit.Assert.assertThat; import static org.mockito.Matchers.anyInt; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +import static org.powermock.api.mockito.PowerMockito.mockStatic; @RunWith(PowerMockRunner.class) @SuppressStaticInitializationFor({"org.onap.dmaap.datarouter.provisioning.beans.Feed", "org.onap.dmaap.datarouter.provisioning.beans.Subscription", - "org.onap.dmaap.datarouter.provisioning.beans.Group"}) + "org.onap.dmaap.datarouter.provisioning.beans.Group", + "org.onap.dmaap.datarouter.provisioning.BaseServlet"}) +@PrepareForTest({ UUID.class}) public class BaseServletTest extends DrServletTestBase { private BaseServlet baseServlet; @@ -193,4 +202,26 @@ public class BaseServletTest extends DrServletTestBase { when(group.getAuthid()).thenReturn("stub_authID"); assertThat(baseServlet.getGroupBySubGroupId("stub_user", "3"), is(nullValue())); } + + @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"); + baseServlet.setIpFqdnRequestIDandInvocationIDForEelf("doDelete", request); + Assert.assertEquals("123", MDC.get("RequestId")); + Assert.assertEquals("456", MDC.get("InvocationId")); + } + + @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"); + baseServlet.setIpFqdnRequestIDandInvocationIDForEelf("doDelete", request); + Assert.assertEquals("123", MDC.get("RequestId")); + Assert.assertEquals("456", MDC.get("InvocationId")); + } + + } diff --git a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/DRFeedsServletTest.java b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/DRFeedsServletTest.java index 87390bc5..1c8040f6 100755 --- a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/DRFeedsServletTest.java +++ b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/DRFeedsServletTest.java @@ -37,6 +37,9 @@ import java.util.Set; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; + +import ch.qos.logback.classic.spi.ILoggingEvent; +import ch.qos.logback.core.read.ListAppender; import org.apache.commons.lang3.reflect.FieldUtils; import org.jetbrains.annotations.NotNull; import org.json.JSONArray; @@ -65,9 +68,12 @@ public class DRFeedsServletTest extends DrServletTestBase { @Mock private HttpServletResponse response; + ListAppender<ILoggingEvent> listAppender; + @Before public void setUp() throws Exception { super.setUp(); + listAppender = setTestLogger(DRFeedsServlet.class); drfeedsServlet = new DRFeedsServlet(); setAuthoriserToReturnRequestIsAuthorized(); setPokerToNotCreateTimersWhenDeleteFeedIsCalled(); @@ -80,6 +86,7 @@ public class DRFeedsServletTest extends DrServletTestBase { public void Given_Request_Is_HTTP_DELETE_SC_METHOD_NOT_ALLOWED_Response_Is_Generated() throws Exception { drfeedsServlet.doDelete(request, response); verify(response).sendError(eq(HttpServletResponse.SC_METHOD_NOT_ALLOWED), argThat(notNullValue(String.class))); + verifyEnteringExitCalled(listAppender); } @Test @@ -89,6 +96,7 @@ public class DRFeedsServletTest extends DrServletTestBase { FieldUtils.writeDeclaredStaticField(BaseServlet.class, "isAddressAuthEnabled", "true", true); drfeedsServlet.doGet(request, response); verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class))); + verifyEnteringExitCalled(listAppender); } @Test @@ -137,6 +145,7 @@ public class DRFeedsServletTest extends DrServletTestBase { when(feed.asJSONObject(true)).thenReturn(mock(JSONObject.class)); drfeedsServlet.doGet(request, response); verify(response).setStatus(eq(HttpServletResponse.SC_OK)); + verifyEnteringExitCalled(listAppender); } @@ -153,6 +162,7 @@ public class DRFeedsServletTest extends DrServletTestBase { public void Given_Request_Is_HTTP_PUT_SC_METHOD_NOT_ALLOWED_Response_Is_Generated() throws Exception { drfeedsServlet.doPut(request, response); verify(response).sendError(eq(HttpServletResponse.SC_METHOD_NOT_ALLOWED), argThat(notNullValue(String.class))); + verifyEnteringExitCalled(listAppender); } @@ -163,6 +173,7 @@ public class DRFeedsServletTest extends DrServletTestBase { FieldUtils.writeDeclaredStaticField(BaseServlet.class, "isAddressAuthEnabled", "true", true); drfeedsServlet.doPost(request, response); verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class))); + verifyEnteringExitCalled(listAppender); } @Test @@ -301,6 +312,7 @@ public class DRFeedsServletTest extends DrServletTestBase { }; drfeedsServlet.doPost(request, response); verify(response).setStatus(eq(HttpServletResponse.SC_CREATED)); + verifyEnteringExitCalled(listAppender); } @NotNull diff --git a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/DrServletTestBase.java b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/DrServletTestBase.java index c7f639ed..265a2ee9 100644 --- a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/DrServletTestBase.java +++ b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/DrServletTestBase.java @@ -23,17 +23,30 @@ package org.onap.dmaap.datarouter.provisioning; +import ch.qos.logback.classic.Logger; +import ch.qos.logback.classic.spi.ILoggingEvent; +import ch.qos.logback.core.read.ListAppender; import org.apache.commons.lang3.reflect.FieldUtils; import org.junit.After; +import org.junit.AfterClass; +import org.junit.Assert; import org.junit.Before; import org.onap.dmaap.datarouter.provisioning.utils.DB; +import org.slf4j.LoggerFactory; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.PrintWriter; import java.util.Properties; +import java.util.Scanner; +import static org.junit.Assert.assertEquals; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; public class DrServletTestBase { + @Before public void setUp() throws Exception { Properties props = new Properties(); @@ -48,8 +61,21 @@ public class DrServletTestBase { FieldUtils.writeDeclaredStaticField(BaseServlet.class, "synctask", synchronizerTask, true); } + public ListAppender<ILoggingEvent> setTestLogger(Class c) { + Logger logger = (Logger) LoggerFactory.getLogger(c); + ListAppender<ILoggingEvent> listAppender = new ListAppender<>(); + listAppender.start(); + logger.addAppender(listAppender); + return listAppender; + } + + public void verifyEnteringExitCalled(ListAppender<ILoggingEvent> listAppender) { + assertEquals("EELF0004I Entering data router provisioning component with RequestId and InvocationId", listAppender.list.get(0).getMessage()); + assertEquals("EELF0005I Exiting data router provisioning component with RequestId and InvocationId", listAppender.list.get(2).getMessage()); + assertEquals(3, listAppender.list.size()); + } + @After public void tearDown() throws Exception { - } } diff --git a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/FeedServletTest.java b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/FeedServletTest.java index 78ac0939..ca4ccd8c 100755 --- a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/FeedServletTest.java +++ b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/FeedServletTest.java @@ -22,6 +22,8 @@ ******************************************************************************/ package org.onap.dmaap.datarouter.provisioning; +import ch.qos.logback.classic.spi.ILoggingEvent; +import ch.qos.logback.core.read.ListAppender; import org.apache.commons.lang3.reflect.FieldUtils; import org.jetbrains.annotations.NotNull; import org.json.JSONArray; @@ -69,6 +71,8 @@ public class FeedServletTest extends DrServletTestBase { private static EntityManager em; private DB db; + ListAppender<ILoggingEvent> listAppender; + @BeforeClass public static void init() { emf = Persistence.createEntityManagerFactory("dr-unit-tests"); @@ -87,6 +91,7 @@ public class FeedServletTest extends DrServletTestBase { @Before public void setUp() throws Exception { + listAppender = setTestLogger(FeedServlet.class); feedServlet = new FeedServlet(); db = new DB(); setAuthoriserToReturnRequestIsAuthorized(); @@ -101,6 +106,7 @@ public class FeedServletTest extends DrServletTestBase { when(request.isSecure()).thenReturn(false); feedServlet.doDelete(request, response); verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class))); + verifyEnteringExitCalled(listAppender); } @@ -160,6 +166,7 @@ public class FeedServletTest extends DrServletTestBase { feedServlet.doDelete(request, response); verify(response).setStatus(eq(HttpServletResponse.SC_NO_CONTENT)); reinsertFeedIntoDb(); + verifyEnteringExitCalled(listAppender); } @Test @@ -168,6 +175,7 @@ public class FeedServletTest extends DrServletTestBase { when(request.isSecure()).thenReturn(false); feedServlet.doGet(request, response); verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class))); + verifyEnteringExitCalled(listAppender); } @Test @@ -212,6 +220,7 @@ public class FeedServletTest extends DrServletTestBase { when(response.getOutputStream()).thenReturn(outStream); feedServlet.doGet(request, response); verify(response).setStatus(eq(HttpServletResponse.SC_OK)); + verifyEnteringExitCalled(listAppender); } @@ -221,6 +230,7 @@ public class FeedServletTest extends DrServletTestBase { when(request.isSecure()).thenReturn(false); feedServlet.doPut(request, response); verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class))); + verifyEnteringExitCalled(listAppender); } @Test @@ -387,12 +397,14 @@ public class FeedServletTest extends DrServletTestBase { }; feedServlet.doPut(request, response); verify(response).setStatus(eq(HttpServletResponse.SC_OK)); + verifyEnteringExitCalled(listAppender); } @Test public void Given_Request_Is_HTTP_POST_SC_METHOD_NOT_ALLOWED_Response_Is_Generated() throws Exception { feedServlet.doPost(request, response); verify(response).sendError(eq(HttpServletResponse.SC_METHOD_NOT_ALLOWED), argThat(notNullValue(String.class))); + verifyEnteringExitCalled(listAppender); } @NotNull diff --git a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/InternalServletTest.java b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/InternalServletTest.java index 591dcc3d..b421e99e 100644 --- a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/InternalServletTest.java +++ b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/InternalServletTest.java @@ -41,6 +41,8 @@ import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import ch.qos.logback.classic.spi.ILoggingEvent; +import ch.qos.logback.core.read.ListAppender; import org.apache.commons.lang3.reflect.FieldUtils; import org.junit.Before; import org.junit.Test; @@ -70,6 +72,8 @@ public class InternalServletTest extends DrServletTestBase { @Mock private HttpServletResponse response; + ListAppender<ILoggingEvent> listAppender; + @BeforeClass public static void init() { emf = Persistence.createEntityManagerFactory("dr-unit-tests"); @@ -88,8 +92,9 @@ public class InternalServletTest extends DrServletTestBase { @Before public void setUp() throws Exception { - internalServlet = new InternalServlet(); - setUpValidAuthorisedRequest(); + listAppender = setTestLogger(InternalServlet.class); + internalServlet = new InternalServlet(); + setUpValidAuthorisedRequest(); } @Test @@ -99,6 +104,7 @@ public class InternalServletTest extends DrServletTestBase { internalServlet.doGet(request, response); verify(response) .sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class))); + verifyEnteringExitCalled(listAppender); } @Test @@ -127,6 +133,7 @@ public class InternalServletTest extends DrServletTestBase { when(request.isSecure()).thenReturn(false); internalServlet.doGet(request, response); verify(response).setStatus(eq(HttpServletResponse.SC_OK)); + verifyEnteringExitCalled(listAppender); } @Test @@ -207,6 +214,7 @@ public class InternalServletTest extends DrServletTestBase { internalServlet.doPut(request, response); verify(response) .sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class))); + verifyEnteringExitCalled(listAppender); } @Test @@ -218,6 +226,7 @@ public class InternalServletTest extends DrServletTestBase { setPokerToNotCreateTimers(); internalServlet.doPut(request, response); verify(response).setStatus(eq(HttpServletResponse.SC_OK)); + verifyEnteringExitCalled(listAppender); } @Test @@ -249,6 +258,7 @@ public class InternalServletTest extends DrServletTestBase { internalServlet.doDelete(request, response); verify(response) .sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class))); + verifyEnteringExitCalled(listAppender); } @Test @@ -261,6 +271,7 @@ public class InternalServletTest extends DrServletTestBase { setPokerToNotCreateTimers(); internalServlet.doDelete(request, response); verify(response).setStatus(eq(HttpServletResponse.SC_OK)); + verifyEnteringExitCalled(listAppender); } @Test @@ -291,6 +302,7 @@ public class InternalServletTest extends DrServletTestBase { internalServlet.doPost(request, response); verify(response) .sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class))); + verifyEnteringExitCalled(listAppender); } @Test @@ -302,6 +314,7 @@ public class InternalServletTest extends DrServletTestBase { setPokerToNotCreateTimers(); internalServlet.doPost(request, response); verify(response).setStatus(eq(HttpServletResponse.SC_OK)); + verifyEnteringExitCalled(listAppender); } @Test diff --git a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/LogServletTest.java b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/LogServletTest.java index e411bdc0..03e3d4f2 100755 --- a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/LogServletTest.java +++ b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/LogServletTest.java @@ -23,6 +23,8 @@ package org.onap.dmaap.datarouter.provisioning; +import ch.qos.logback.classic.spi.ILoggingEvent; +import ch.qos.logback.core.read.ListAppender; import org.junit.Before; import org.junit.BeforeClass; import org.junit.AfterClass; @@ -58,6 +60,8 @@ public class LogServletTest extends DrServletTestBase { @Mock private HttpServletResponse response; + ListAppender<ILoggingEvent> listAppender; + @BeforeClass public static void init() { emf = Persistence.createEntityManagerFactory("dr-unit-tests"); @@ -76,6 +80,7 @@ public class LogServletTest extends DrServletTestBase { @Before public void setUp() throws Exception { + listAppender = setTestLogger(LogServlet.class); logServlet = new LogServlet(true); setUpValidParameterValuesForMap(); } @@ -85,6 +90,7 @@ public class LogServletTest extends DrServletTestBase { throws Exception { logServlet.doDelete(request, response); verify(response).sendError(eq(HttpServletResponse.SC_METHOD_NOT_ALLOWED), argThat(notNullValue(String.class))); + verifyEnteringExitCalled(listAppender); } @Test @@ -93,6 +99,7 @@ public class LogServletTest extends DrServletTestBase { when(request.getPathInfo()).thenReturn(null); logServlet.doGet(request, response); verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class))); + verifyEnteringExitCalled(listAppender); } @Test @@ -148,6 +155,7 @@ public class LogServletTest extends DrServletTestBase { throws Exception { logServlet.doGet(request, response); verify(response).setStatus(eq(HttpServletResponse.SC_OK)); + verifyEnteringExitCalled(listAppender); } @Test @@ -155,6 +163,7 @@ public class LogServletTest extends DrServletTestBase { throws Exception { logServlet.doPut(request, response); verify(response).sendError(eq(HttpServletResponse.SC_METHOD_NOT_ALLOWED), argThat(notNullValue(String.class))); + verifyEnteringExitCalled(listAppender); } @Test @@ -162,6 +171,7 @@ public class LogServletTest extends DrServletTestBase { throws Exception { logServlet.doPost(request, response); verify(response).sendError(eq(HttpServletResponse.SC_METHOD_NOT_ALLOWED), argThat(notNullValue(String.class))); + verifyEnteringExitCalled(listAppender); } @Test diff --git a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/PublishServletTest.java b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/PublishServletTest.java index cee09e5c..b99e63c0 100755 --- a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/PublishServletTest.java +++ b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/PublishServletTest.java @@ -23,15 +23,17 @@ package org.onap.dmaap.datarouter.provisioning; +import ch.qos.logback.classic.Logger; +import ch.qos.logback.classic.spi.ILoggingEvent; +import ch.qos.logback.core.read.ListAppender; import org.apache.commons.lang3.reflect.FieldUtils; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; +import org.junit.*; import org.junit.runner.RunWith; import org.mockito.Mock; import org.onap.dmaap.datarouter.provisioning.utils.DB; +import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; +import org.slf4j.LoggerFactory; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; @@ -40,17 +42,25 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.PrintWriter; +import java.net.InetAddress; + import static org.hamcrest.Matchers.notNullValue; +import static org.junit.Assert.assertEquals; import static org.mockito.Matchers.*; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import static org.powermock.api.mockito.PowerMockito.mockStatic; /** * Created by ezcoxem on 21/08/2018. */ @RunWith(PowerMockRunner.class) -public class PublishServletTest { +@PrepareForTest({InetAddress.class }) +public class PublishServletTest extends DrServletTestBase { private PublishServlet publishServlet; @Mock @@ -63,6 +73,8 @@ public class PublishServletTest { private static EntityManager em; private DB db; + ListAppender<ILoggingEvent> listAppender; + @BeforeClass public static void init() { emf = Persistence.createEntityManagerFactory("dr-unit-tests"); @@ -73,7 +85,7 @@ public class PublishServletTest { } @AfterClass - public static void tearDownClass() { + public static void tearDownClass() throws FileNotFoundException { em.clear(); em.close(); emf.close(); @@ -82,6 +94,7 @@ public class PublishServletTest { @Before public void setUp() throws Exception { + listAppender = setTestLogger(PublishServlet.class); publishServlet = new PublishServlet(); db = new DB(); } @@ -93,6 +106,7 @@ public class PublishServletTest { publishServlet.doDelete(request, response); verify(response).sendError(eq(HttpServletResponse.SC_SERVICE_UNAVAILABLE), argThat(notNullValue(String.class))); FieldUtils.writeDeclaredStaticField(BaseServlet.class, "nodes", new String[1], true); + verifyEnteringExitCalled(listAppender); } @Test @@ -135,6 +149,7 @@ public class PublishServletTest { setConditionsForPositiveSuccessFlow(); publishServlet.doDelete(request, response); verify(response).setStatus(eq(HttpServletResponse.SC_MOVED_PERMANENTLY)); + verifyEnteringExitCalled(listAppender); } @Test @@ -144,6 +159,7 @@ public class PublishServletTest { publishServlet.doPut(request, response); verify(response).setStatus(eq(HttpServletResponse.SC_MOVED_PERMANENTLY)); + verifyEnteringExitCalled(listAppender); } @Test @@ -153,15 +169,32 @@ public class PublishServletTest { publishServlet.doPost(request, response); verify(response).setStatus(eq(HttpServletResponse.SC_MOVED_PERMANENTLY)); + verifyEnteringExitCalled(listAppender); } @Test - public void Given_Request_Is_HTTP_GET_And_Request_succeeds() + public void Given_Request_Is_HTTP_GET_And_Request_succeeds_And_RequestId_Header_is_empty() throws Exception { setConditionsForPositiveSuccessFlow(); + mockStatic(InetAddress.class); + publishServlet.doGet(request, response); + verify(response).setStatus(eq(HttpServletResponse.SC_MOVED_PERMANENTLY)); + verifyEnteringExitCalled(listAppender); + assertEquals(null, listAppender.list.get(0).getMDCPropertyMap().get("RequestId")); + assertEquals(null, listAppender.list.get(0).getMDCPropertyMap().get("InvocationId")); + } + @Test + public void Given_Request_Is_HTTP_GET_And_Request_succeeds_And_RequestId_Header_Is_Not_Empty() + throws Exception { + setConditionsForPositiveSuccessFlow(); + when(request.getHeader("X-ONAP-RequestID")).thenReturn("123"); + when(request.getHeader("X-InvocationID")).thenReturn("456"); publishServlet.doGet(request, response); verify(response).setStatus(eq(HttpServletResponse.SC_MOVED_PERMANENTLY)); + verifyEnteringExitCalled(listAppender); + assertEquals("123", listAppender.list.get(0).getMDCPropertyMap().get("RequestId")); + assertEquals("456", listAppender.list.get(0).getMDCPropertyMap().get("InvocationId")); } private void setConditionsForPositiveSuccessFlow() throws Exception { diff --git a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/SubscribeServletTest.java b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/SubscribeServletTest.java index 25341d42..a3431ce9 100755 --- a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/SubscribeServletTest.java +++ b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/SubscribeServletTest.java @@ -22,6 +22,8 @@ ******************************************************************************/ package org.onap.dmaap.datarouter.provisioning; +import ch.qos.logback.classic.spi.ILoggingEvent; +import ch.qos.logback.core.read.ListAppender; import org.apache.commons.lang3.reflect.FieldUtils; import org.jetbrains.annotations.NotNull; import org.json.JSONObject; @@ -61,9 +63,12 @@ public class SubscribeServletTest extends DrServletTestBase { @Mock private HttpServletResponse response; + ListAppender<ILoggingEvent> listAppender; + @Before public void setUp() throws Exception { super.setUp(); + listAppender = setTestLogger(SubscribeServlet.class); subscribeServlet = new SubscribeServlet(); setAuthoriserToReturnRequestIsAuthorized(); setPokerToNotCreateTimersWhenDeleteFeedIsCalled(); @@ -76,6 +81,7 @@ public class SubscribeServletTest extends DrServletTestBase { public void Given_Request_Is_HTTP_DELETE_SC_METHOD_NOT_ALLOWED_Response_Is_Generated() throws Exception { subscribeServlet.doDelete(request, response); verify(response).sendError(eq(HttpServletResponse.SC_METHOD_NOT_ALLOWED), argThat(notNullValue(String.class))); + verifyEnteringExitCalled(listAppender); } @Test @@ -84,6 +90,7 @@ public class SubscribeServletTest extends DrServletTestBase { FieldUtils.writeDeclaredStaticField(BaseServlet.class, "isAddressAuthEnabled", "true", true); subscribeServlet.doGet(request, response); verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class))); + verifyEnteringExitCalled(listAppender); } @Test @@ -127,6 +134,7 @@ public class SubscribeServletTest extends DrServletTestBase { PowerMockito.when(Subscription.getSubscriptionUrlList(anyInt())).thenReturn(list); subscribeServlet.doGet(request, response); verify(response).setStatus(eq(HttpServletResponse.SC_OK)); + verifyEnteringExitCalled(listAppender); } @@ -134,6 +142,7 @@ public class SubscribeServletTest extends DrServletTestBase { public void Given_Request_Is_HTTP_PUT_SC_METHOD_NOT_ALLOWED_Response_Is_Generated() throws Exception { subscribeServlet.doPut(request, response); verify(response).sendError(eq(HttpServletResponse.SC_METHOD_NOT_ALLOWED), argThat(notNullValue(String.class))); + verifyEnteringExitCalled(listAppender); } @Test public void Given_Request_Is_HTTP_POST_And_Is_Not_Secure_When_HTTPS_Is_Required_Then_Forbidden_Response_Is_Generated() throws Exception { @@ -141,6 +150,7 @@ public class SubscribeServletTest extends DrServletTestBase { FieldUtils.writeDeclaredStaticField(BaseServlet.class, "isAddressAuthEnabled", "true", true); subscribeServlet.doPost(request, response); verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class))); + verifyEnteringExitCalled(listAppender); } @Test @@ -253,6 +263,7 @@ public class SubscribeServletTest extends DrServletTestBase { }; subscribeServlet.doPost(request, response); verify(response).setStatus(eq(HttpServletResponse.SC_CREATED)); + verifyEnteringExitCalled(listAppender); } diff --git a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/SubscriptionServletTest.java b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/SubscriptionServletTest.java index 472be512..02d73cc1 100755 --- a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/SubscriptionServletTest.java +++ b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/SubscriptionServletTest.java @@ -22,13 +22,13 @@ ******************************************************************************/ package org.onap.dmaap.datarouter.provisioning; +import ch.qos.logback.classic.Logger; +import ch.qos.logback.classic.spi.ILoggingEvent; +import ch.qos.logback.core.read.ListAppender; import org.apache.commons.lang3.reflect.FieldUtils; import org.jetbrains.annotations.NotNull; import org.json.JSONObject; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; +import org.junit.*; import org.junit.runner.RunWith; import org.mockito.Mock; import org.onap.dmaap.datarouter.authz.AuthorizationResponse; @@ -39,6 +39,7 @@ import org.onap.dmaap.datarouter.provisioning.beans.Subscription; import org.onap.dmaap.datarouter.provisioning.beans.Updateable; import org.onap.dmaap.datarouter.provisioning.utils.DB; import org.powermock.modules.junit4.PowerMockRunner; +import org.slf4j.LoggerFactory; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; @@ -47,17 +48,21 @@ import javax.servlet.ServletInputStream; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.PrintWriter; import java.sql.SQLException; import java.util.HashSet; import java.util.Set; import static org.hamcrest.Matchers.notNullValue; +import static org.junit.Assert.assertEquals; import static org.mockito.Mockito.*; import static org.onap.dmaap.datarouter.provisioning.BaseServlet.BEHALF_HEADER; @RunWith(PowerMockRunner.class) -public class SubscriptionServletTest { +public class SubscriptionServletTest extends DrServletTestBase { private static EntityManagerFactory emf; private static EntityManager em; private SubscriptionServlet subscriptionServlet; @@ -66,11 +71,14 @@ public class SubscriptionServletTest { private final String USER = "user1"; private final String PASSWORD="password1"; + @Mock private HttpServletRequest request; @Mock private HttpServletResponse response; + ListAppender<ILoggingEvent> listAppender; + @BeforeClass public static void init() { emf = Persistence.createEntityManagerFactory("dr-unit-tests"); @@ -81,7 +89,7 @@ public class SubscriptionServletTest { } @AfterClass - public static void tearDownClass() { + public static void tearDownClass() throws FileNotFoundException { em.clear(); em.close(); emf.close(); @@ -89,6 +97,7 @@ public class SubscriptionServletTest { @Before public void setUp() throws Exception { + listAppender = setTestLogger(SubscriptionServlet.class); subscriptionServlet = new SubscriptionServlet(); db = new DB(); setAuthoriserToReturnRequestIsAuthorized(); @@ -102,6 +111,7 @@ public class SubscriptionServletTest { when(request.isSecure()).thenReturn(false); subscriptionServlet.doDelete(request, response); verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class))); + verifyEnteringExitCalled(listAppender); } @Test @@ -147,6 +157,7 @@ public class SubscriptionServletTest { public void Given_Request_Is_HTTP_DELETE_And_Delete_On_Database_Succeeds_A_NO_CONTENT_Response_Is_Generated() throws Exception { subscriptionServlet.doDelete(request, response); verify(response).setStatus(eq(HttpServletResponse.SC_NO_CONTENT)); + verifyEnteringExitCalled(listAppender); insertSubscriptionIntoDb(); } @@ -155,6 +166,7 @@ public class SubscriptionServletTest { when(request.isSecure()).thenReturn(false); subscriptionServlet.doGet(request, response); verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class))); + verifyEnteringExitCalled(listAppender); } @Test @@ -191,6 +203,7 @@ public class SubscriptionServletTest { when(response.getOutputStream()).thenReturn(outStream); subscriptionServlet.doGet(request, response); verify(response).setStatus(eq(HttpServletResponse.SC_OK)); + verifyEnteringExitCalled(listAppender); } @Test @@ -198,6 +211,7 @@ public class SubscriptionServletTest { when(request.isSecure()).thenReturn(false); subscriptionServlet.doPut(request, response); verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class))); + verifyEnteringExitCalled(listAppender); } @Test @@ -328,6 +342,7 @@ public class SubscriptionServletTest { subscriptionServlet.doPut(request, response); verify(response).setStatus(eq(HttpServletResponse.SC_OK)); changeSubscriptionBackToNormal(); + verifyEnteringExitCalled(listAppender); } @Test @@ -335,6 +350,7 @@ public class SubscriptionServletTest { when(request.isSecure()).thenReturn(false); subscriptionServlet.doPost(request, response); verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class))); + verifyEnteringExitCalled(listAppender); } @Test @@ -423,6 +439,7 @@ public class SubscriptionServletTest { }; subscriptionServlet.doPost(request, response); verify(response).setStatus(eq(HttpServletResponse.SC_ACCEPTED)); + verifyEnteringExitCalled(listAppender); } @NotNull diff --git a/datarouter-prov/src/test/resources/logback-test.xml b/datarouter-prov/src/test/resources/logback-test.xml new file mode 100644 index 00000000..c2994ebe --- /dev/null +++ b/datarouter-prov/src/test/resources/logback-test.xml @@ -0,0 +1,122 @@ +<!-- + ============LICENSE_START================================================== + * org.onap.dmaap + * =========================================================================== + * Copyright © 2017 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==================================================== + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * +--> +<configuration scan="true" scanPeriod="3 seconds" debug="true"> + <property name="logDir" value="logs/EELF" /> + <property name="generalLogName" value="application" /> + <property name="errorLogName" value="errors" /> + <property name="defaultPattern" value="%d{MM/dd-HH:mm:ss.SSS}|%logger|%X{RequestId}|%X{InvocationId}|%X{ServiceInstanceId}|%thread|%X{ServiceName}|%X{InstanceUUID}|%.-5level|%X{AlertSeverity}|%X{ServerIPAddress}|%X{ServerFQDN}|%X{RemoteHost}|%X{Timer}|%msg%n" /> + <property name="logDirectory" value="${logDir}" /> + <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> + <encoder> + <pattern>${defaultPattern}</pattern> + </encoder> + </appender> + + <!-- ============================================================================ --> + <!-- EELF Appenders --> + <!-- ============================================================================ --> + + <!-- The EELFAppender is used to record events to the general application + log --> + + + <appender name="EELF" + class="ch.qos.logback.core.rolling.RollingFileAppender"> + <file>${logDirectory}/${generalLogName}.log</file> + <filter class="ch.qos.logback.classic.filter.LevelFilter"> + <level>INFO</level> + <onMatch>ACCEPT</onMatch> + <onMismatch>DENY</onMismatch> + </filter> + <rollingPolicy + class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy"> + <fileNamePattern>${logDirectory}/${generalLogName}.%i.log.zip + </fileNamePattern> + <minIndex>1</minIndex> + <maxIndex>9</maxIndex> + </rollingPolicy> + <triggeringPolicy + class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"> + <maxFileSize>5MB</maxFileSize> + </triggeringPolicy> + <encoder> + <pattern>${defaultPattern}</pattern> + </encoder> + </appender> + + <appender name="asyncEELF" class="ch.qos.logback.classic.AsyncAppender"> + <queueSize>256</queueSize> + <appender-ref ref="EELF" /> + </appender> + + <appender name="EELFError" + class="ch.qos.logback.core.rolling.RollingFileAppender"> + <file>${logDirectory}/${errorLogName}.log</file> + <filter class="ch.qos.logback.classic.filter.LevelFilter"> + <level>ERROR</level> + <onMatch>ACCEPT</onMatch> + <onMismatch>DENY</onMismatch> + </filter> + <rollingPolicy + class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy"> + <fileNamePattern>${logDirectory}/${errorLogName}.%i.log.zip + </fileNamePattern> + <minIndex>1</minIndex> + <maxIndex>9</maxIndex> + </rollingPolicy> + <triggeringPolicy + class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"> + <maxFileSize>5MB</maxFileSize> + </triggeringPolicy> + <encoder> + <pattern>${defaultPattern}</pattern> + </encoder> + </appender> + + <appender name="asyncEELFError" class="ch.qos.logback.classic.AsyncAppender"> + <queueSize>256</queueSize> + <appender-ref ref="EELFError"/> + </appender> + + <!-- ============================================================================ --> + <!-- EELF loggers --> + <!-- ============================================================================ --> + <logger name="com.att.eelf" level="info" additivity="false"> + <appender-ref ref="asyncEELF" /> + </logger> + + <logger name="com.att.eelf.error" level="error" additivity="false"> + <appender-ref ref="asyncEELFError" /> + </logger> + + <logger name="log4j.logger.org.eclipse.jetty" additivity="false" level="info"> + <appender-ref ref="asyncEELFjettyAndNodelog"/> + </logger> + + <root level="INFO"> + <appender-ref ref="asyncEELF" /> + <appender-ref ref="asyncEELFError" /> + <appender-ref ref="asyncEELFjettyAndNodelog" /> + </root> + +</configuration> |