diff options
author | esobmar <mariusz.sobucki@ericsson.com> | 2018-09-04 18:06:59 +0100 |
---|---|---|
committer | esobmar <mariusz.sobucki@ericsson.com> | 2018-09-05 15:02:51 +0100 |
commit | 517ccb3875a6951bbe701f24ff120b7a8e158791 (patch) | |
tree | 366c8f1ca21e6e5ac7bb21f190531e08488c4f91 /datarouter-prov | |
parent | a640609ea3ba7cf2adc2f593d9ead3b5e229649b (diff) |
Unit Tests InternalServletTest
Change-Id: I81a2912f8ec5834117dccded71aa719dc0c2a46f
Signed-off-by: Mariusz Sobucki <mariusz.sobucki@ericsson.com>
Issue-ID: DMAAP-101
Diffstat (limited to 'datarouter-prov')
-rw-r--r-- | datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/InternalServletTest.java | 121 | ||||
-rwxr-xr-x | datarouter-prov/src/test/resources/h2Database.properties | 4 |
2 files changed, 42 insertions, 83 deletions
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 5f6b7ae3..591dcc3d 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 @@ -33,43 +33,35 @@ import static org.onap.dmaap.datarouter.provisioning.BaseServlet.BEHALF_HEADER; import java.io.File; import java.net.InetAddress; -import java.util.HashMap; -import java.util.Map; +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.Persistence; import javax.servlet.ServletInputStream; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; + import org.apache.commons.lang3.reflect.FieldUtils; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.junit.BeforeClass; +import org.junit.AfterClass; import org.mockito.Mock; -import org.onap.dmaap.datarouter.authz.AuthorizationResponse; -import org.onap.dmaap.datarouter.authz.Authorizer; + import org.onap.dmaap.datarouter.provisioning.beans.Deleteable; -import org.onap.dmaap.datarouter.provisioning.beans.Feed; import org.onap.dmaap.datarouter.provisioning.beans.Insertable; import org.onap.dmaap.datarouter.provisioning.beans.LogRecord; -import org.onap.dmaap.datarouter.provisioning.beans.NodeClass; -import org.onap.dmaap.datarouter.provisioning.beans.Parameters; -import org.onap.dmaap.datarouter.provisioning.beans.Subscription; import org.onap.dmaap.datarouter.provisioning.beans.Updateable; -import org.onap.dmaap.datarouter.provisioning.utils.LogfileLoader; -import org.onap.dmaap.datarouter.provisioning.utils.RLEBitSet; 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; @RunWith(PowerMockRunner.class) @PrepareForTest(LogRecord.class) -@SuppressStaticInitializationFor({"org.onap.dmaap.datarouter.provisioning.beans.Feed", - "org.onap.dmaap.datarouter.provisioning.beans.Parameters", - "org.onap.dmaap.datarouter.provisioning.beans.NodeClass", - "org.onap.dmaap.datarouter.provisioning.beans.Subscription", - "org.onap.dmaap.datarouter.provisioning.utils.LogfileLoader"}) public class InternalServletTest extends DrServletTestBase { - + private static EntityManagerFactory emf; + private static EntityManager em; private InternalServlet internalServlet; @Mock @@ -78,11 +70,25 @@ public class InternalServletTest extends DrServletTestBase { @Mock private HttpServletResponse response; + @BeforeClass + public static void init() { + emf = Persistence.createEntityManagerFactory("dr-unit-tests"); + em = emf.createEntityManager(); + System.setProperty( + "org.onap.dmaap.datarouter.provserver.properties", + "src/test/resources/h2Database.properties"); + } + + @AfterClass + public static void tearDownClass() { + em.clear(); + em.close(); + emf.close(); + } + @Before public void setUp() throws Exception { - super.setUp(); internalServlet = new InternalServlet(); - setAuthoriserToReturnRequestIsAuthorized(); setUpValidAuthorisedRequest(); } @@ -90,8 +96,6 @@ public class InternalServletTest extends DrServletTestBase { public void Given_Request_Is_HTTP_GET_And_Address_Not_Authorized_When_HTTPS_Is_Required_Then_Forbidden_Response_Is_Generated() throws Exception { when(request.getRemoteAddr()).thenReturn("127.100.0.3"); - FieldUtils.writeDeclaredStaticField(BaseServlet.class, "isAddressAuthEnabled", "true", true); - internalServlet.doGet(request, response); verify(response) .sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class))); @@ -108,7 +112,7 @@ public class InternalServletTest extends DrServletTestBase { } @Test - public void Given_Request_Is_HTTP_GET_With_Halt_In_Endpoint_Request_Succeeds() throws Exception { + public void Given_Request_Is_HTTP_GET_With_Halt_In_Endpoint_Then_Request_Succeeds() throws Exception { when(request.getPathInfo()).thenReturn("/halt"); when(request.isSecure()).thenReturn(false); when(request.getRemoteAddr()).thenReturn("127.0.0.1"); @@ -117,7 +121,7 @@ public class InternalServletTest extends DrServletTestBase { } @Test - public void Given_Request_Is_HTTP_GET_With_FetchProv_In_Endpoint_Request_Succeeds() + public void Given_Request_Is_HTTP_GET_With_FetchProv_In_Endpoint_Then_Request_Succeeds() throws Exception { when(request.getPathInfo()).thenReturn("/fetchProv"); when(request.isSecure()).thenReturn(false); @@ -126,7 +130,7 @@ public class InternalServletTest extends DrServletTestBase { } @Test - public void Given_Request_Is_HTTP_GET_With_Prov_In_Endpoint_Request_Succeeds() throws Exception { + public void Given_Request_Is_HTTP_GET_With_Prov_In_Endpoint_Then_Request_Succeeds() throws Exception { when(request.getPathInfo()).thenReturn("/prov"); when(request.getQueryString()).thenReturn(null); setPokerToNotCreateTimers(); @@ -137,7 +141,7 @@ public class InternalServletTest extends DrServletTestBase { } @Test - public void Given_Request_Is_HTTP_GET_With_Logs_In_Endpoint_Request_Succeeds() throws Exception { + public void Given_Request_Is_HTTP_GET_With_Logs_In_Endpoint_Then_Request_Succeeds() throws Exception { when(request.getPathInfo()).thenReturn("/logs/"); ServletOutputStream outStream = mock(ServletOutputStream.class); when(response.getOutputStream()).thenReturn(outStream); @@ -146,7 +150,7 @@ public class InternalServletTest extends DrServletTestBase { } @Test - public void Given_Request_Is_HTTP_GET_Starts_With_Logs_In_Endpoint_Request_Succeeds() + public void Given_Request_Is_HTTP_GET_Starts_With_Logs_In_Endpoint_Then_Request_Succeeds() throws Exception { when(request.getPathInfo()).thenReturn("/logs/TestFile"); internalServlet.doGet(request, response); @@ -168,9 +172,8 @@ public class InternalServletTest extends DrServletTestBase { } @Test - public void Given_Request_Is_HTTP_GET_With_Api_In_Endpoint_Request_Succeeds() throws Exception { - when(request.getPathInfo()).thenReturn("/api/Key"); - setParametersToNotContactDb(false); + public void Given_Request_Is_HTTP_GET_With_Api_In_Endpoint_Then_Request_Succeeds() throws Exception { + when(request.getPathInfo()).thenReturn("/api/DELIVERY_MAX_RETRY_INTERVAL"); ServletOutputStream outStream = mock(ServletOutputStream.class); when(response.getOutputStream()).thenReturn(outStream); internalServlet.doGet(request, response); @@ -178,10 +181,9 @@ public class InternalServletTest extends DrServletTestBase { } @Test - public void Given_Request_Is_HTTP_GET_With_Drlogs_In_Endpoint_Request_Succeeds() + public void Given_Request_Is_HTTP_GET_With_Drlogs_In_Endpoint_Then_Request_Succeeds() throws Exception { when(request.getPathInfo()).thenReturn("/drlogs/"); - mockLogfileLoader(); ServletOutputStream outStream = mock(ServletOutputStream.class); when(response.getOutputStream()).thenReturn(outStream); internalServlet.doGet(request, response); @@ -189,7 +191,7 @@ public class InternalServletTest extends DrServletTestBase { } @Test - public void Given_Request_Is_HTTP_GET_Incorrect_Endpoint_Then_No_Content_Response_Is_Generated() + public void Given_Request_Is_HTTP_GET_With_Incorrect_Endpoint_Then_No_Content_Response_Is_Generated() throws Exception { when(request.getPathInfo()).thenReturn("/incorrect/"); internalServlet.doGet(request, response); @@ -209,13 +211,11 @@ public class InternalServletTest extends DrServletTestBase { @Test public void Given_Request_Is_HTTP_PUT_With_Api_In_Endpoint_Request_Succeeds() throws Exception { - when(request.getPathInfo()).thenReturn("/api/Key"); - setParametersToNotContactDb(false); + when(request.getPathInfo()).thenReturn("/api/NODES"); String[] values = {"V", "a", "l", "u", "e", "s"}; when(request.getParameterValues(anyString())).thenReturn(values); internalServlet = internalServerSuccess(); setPokerToNotCreateTimers(); - mockProvisioningParametersChanged(); internalServlet.doPut(request, response); verify(response).setStatus(eq(HttpServletResponse.SC_OK)); } @@ -223,8 +223,7 @@ public class InternalServletTest extends DrServletTestBase { @Test public void Given_Request_Is_HTTP_PUT_With_Api_In_Endpoint_And_Update_Fails_Then_Internal_Server_Error_Is_Generated() throws Exception { - when(request.getPathInfo()).thenReturn("/api/Key"); - setParametersToNotContactDb(false); + when(request.getPathInfo()).thenReturn("/api/NODES"); String[] values = {"V", "a", "l", "u", "e", "s"}; when(request.getParameterValues(anyString())).thenReturn(values); internalServlet = internalServerFailure(); @@ -255,13 +254,11 @@ public class InternalServletTest extends DrServletTestBase { @Test public void Given_Request_Is_HTTP_DELETE_With_Api_In_Endpoint_Request_Succeeds() throws Exception { - when(request.getPathInfo()).thenReturn("/api/Key"); - setParametersToNotContactDb(false); + when(request.getPathInfo()).thenReturn("/api/NODES"); String[] values = {"V", "a", "l", "u", "e", "s"}; when(request.getParameterValues(anyString())).thenReturn(values); internalServlet = internalServerSuccess(); setPokerToNotCreateTimers(); - mockProvisioningParametersChanged(); internalServlet.doDelete(request, response); verify(response).setStatus(eq(HttpServletResponse.SC_OK)); } @@ -269,8 +266,7 @@ public class InternalServletTest extends DrServletTestBase { @Test public void Given_Request_Is_HTTP_DELETE_With_Api_In_Endpoint_And_Delete_Fails_Then_Internal_Server_Error_Is_Generated() throws Exception { - when(request.getPathInfo()).thenReturn("/api/Key"); - setParametersToNotContactDb(false); + when(request.getPathInfo()).thenReturn("/api/NODES"); String[] values = {"V", "a", "l", "u", "e", "s"}; when(request.getParameterValues(anyString())).thenReturn(values); internalServlet = internalServerFailure(); @@ -293,20 +289,17 @@ public class InternalServletTest extends DrServletTestBase { throws Exception { when(request.getRemoteAddr()).thenReturn("127.100.0.3"); internalServlet.doPost(request, response); - FieldUtils.writeDeclaredStaticField(BaseServlet.class, "isAddressAuthEnabled", "true", true); verify(response) .sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class))); } @Test public void Given_Request_Is_HTTP_POST_With_Api_In_Endpoint_Request_Succeeds() throws Exception { - when(request.getPathInfo()).thenReturn("/api/Key"); - setParametersToNotContactDb(true); + when(request.getPathInfo()).thenReturn("/api/key"); String[] values = {"V", "a", "l", "u", "e", "s"}; when(request.getParameterValues(anyString())).thenReturn(values); internalServlet = internalServerSuccess(); setPokerToNotCreateTimers(); - mockProvisioningParametersChanged(); internalServlet.doPost(request, response); verify(response).setStatus(eq(HttpServletResponse.SC_OK)); } @@ -315,7 +308,6 @@ public class InternalServletTest extends DrServletTestBase { public void Given_Request_Is_HTTP_POST_With_Api_In_Endpoint_And_Insert_Fails_Then_Internal_Server_Error_Is_Generated() throws Exception { when(request.getPathInfo()).thenReturn("/api/Key"); - setParametersToNotContactDb(true); String[] values = {"V", "a", "l", "u", "e", "s"}; when(request.getParameterValues(anyString())).thenReturn(values); internalServlet = internalServerFailure(); @@ -352,7 +344,6 @@ public class InternalServletTest extends DrServletTestBase { File testDir = new File("unit-test-logs/spool"); testDir.mkdirs(); testDir.deleteOnExit(); - mockLogfileLoader(); internalServlet.doPost(request, response); verify(response).setStatus(eq(HttpServletResponse.SC_CREATED)); } @@ -386,14 +377,6 @@ public class InternalServletTest extends DrServletTestBase { .sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class))); } - private void setAuthoriserToReturnRequestIsAuthorized() throws IllegalAccessException { - AuthorizationResponse authResponse = mock(AuthorizationResponse.class); - Authorizer authorizer = mock(Authorizer.class); - FieldUtils.writeDeclaredStaticField(BaseServlet.class, "authz", authorizer, true); - when(authorizer.decide(request)).thenReturn(authResponse); - when(authResponse.isAuthorized()).thenReturn(true); - } - private void setUpValidAuthorisedRequest() throws Exception { setUpValidSecurityOnHttpRequest(); setBehalfHeader("Stub_Value"); @@ -424,16 +407,6 @@ public class InternalServletTest extends DrServletTestBase { FieldUtils.writeDeclaredStaticField(Poker.class, "poker", poker, true); } - private void setParametersToNotContactDb(boolean isPost) { - PowerMockito.mockStatic(Parameters.class); - Parameters parameters = mock(Parameters.class); - if (isPost) { - PowerMockito.when(Parameters.getParameter(anyString())).thenReturn(null); - } else { - PowerMockito.when(Parameters.getParameter(anyString())).thenReturn(parameters); - } - } - private InternalServlet internalServerSuccess() { InternalServlet internalServlet = new InternalServlet() { @@ -469,20 +442,4 @@ public class InternalServletTest extends DrServletTestBase { }; return internalServlet; } - - private void mockProvisioningParametersChanged() throws IllegalAccessException { - PowerMockito.mockStatic(Feed.class); - PowerMockito.mockStatic(Subscription.class); - PowerMockito.when(Feed.countActiveFeeds()).thenReturn(0); - PowerMockito.when(Subscription.countActiveSubscriptions()).thenReturn(0); - Map<String, Integer> map = new HashMap<>(); - FieldUtils.writeDeclaredStaticField(NodeClass.class, "map", map, true); - } - - private void mockLogfileLoader() { - PowerMockito.mockStatic(LogfileLoader.class); - LogfileLoader logfileLoader = mock(LogfileLoader.class); - when(logfileLoader.getBitSet()).thenReturn(new RLEBitSet()); - PowerMockito.when(LogfileLoader.getLoader()).thenReturn(logfileLoader); - } } diff --git a/datarouter-prov/src/test/resources/h2Database.properties b/datarouter-prov/src/test/resources/h2Database.properties index 11f13810..336af0e2 100755 --- a/datarouter-prov/src/test/resources/h2Database.properties +++ b/datarouter-prov/src/test/resources/h2Database.properties @@ -25,4 +25,6 @@ org.onap.dmaap.datarouter.db.driver = org.h2.Driver org.onap.dmaap.datarouter.db.url = jdbc:h2:mem:test;DB_CLOSE_DELAY=-1 org.onap.dmaap.datarouter.provserver.isaddressauthenabled = true -org.onap.dmaap.datarouter.provserver.https.relaxation = false
\ No newline at end of file +org.onap.dmaap.datarouter.provserver.https.relaxation = false +org.onap.dmaap.datarouter.provserver.accesslog.dir = unit-test-logs +org.onap.dmaap.datarouter.provserver.spooldir = unit-test-logs/spool
\ No newline at end of file |