diff options
author | Kyle Stewart <kyle.stewart@ericsson.com> | 2018-08-24 15:51:03 +0100 |
---|---|---|
committer | Kyle Stewart <kyle.stewart@ericsson.com> | 2018-08-27 12:21:03 +0100 |
commit | 1bb36e52875a835f57652ad64b3cfac0b93bc9a1 (patch) | |
tree | f232af2675a7b69689ac12a006b88805fa4fecb9 /datarouter-prov/src/test/java/org | |
parent | cb1693e59584bb3e1b897ed9431478ae30c666cc (diff) |
Test case fixes
Change-Id: I697684f7cda881c749fddb02821ade7084d0e0a3
Issue-ID: DMAAP-101
Signed-off-by: Kyle Stewart <kyle.stewart@ericsson.com>
Diffstat (limited to 'datarouter-prov/src/test/java/org')
-rwxr-xr-x[-rw-r--r--] | datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/InternalServletTest.java | 773 | ||||
-rwxr-xr-x[-rw-r--r--] | datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/StatisticsServletTest.java | 180 | ||||
-rwxr-xr-x[-rw-r--r--] | datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/SubscribeServletTest.java | 2 |
3 files changed, 512 insertions, 443 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 104d137a..f8342449 100644..100755 --- 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 @@ -22,6 +22,22 @@ ******************************************************************************/ package org.onap.dmaap.datarouter.provisioning; +import static org.hamcrest.Matchers.notNullValue; +import static org.mockito.Matchers.anyString; +import static org.mockito.Matchers.argThat; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import static org.onap.dmaap.datarouter.provisioning.BaseServlet.BEHALF_HEADER; + +import java.net.InetAddress; +import java.util.HashMap; +import java.util.Map; +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; @@ -29,377 +45,406 @@ import org.junit.runner.RunWith; 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.*; +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; -import javax.servlet.ServletInputStream; -import javax.servlet.ServletOutputStream; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import java.net.InetAddress; -import java.util.HashMap; -import java.util.Map; - -import static org.hamcrest.Matchers.notNullValue; -import static org.mockito.Matchers.*; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; -import static org.onap.dmaap.datarouter.provisioning.BaseServlet.BEHALF_HEADER; - @RunWith(PowerMockRunner.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"}) +@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 InternalServlet internalServlet; - @Mock - private HttpServletRequest request; - - @Mock - private HttpServletResponse response; - - @Before - public void setUp() throws Exception { - super.setUp(); - internalServlet = new InternalServlet(); - setAuthoriserToReturnRequestIsAuthorized(); - setUpValidAuthorisedRequest(); - } - - @Test - 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"); - internalServlet.doGet(request, response); - verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class))); - } - - @Test - public void Given_Request_Is_HTTP_GET_With_Halt_In_Endpoint_But_Not_Sent_From_Localhost_Then_Forbidden_Response_Is_Generated() throws Exception { - when(request.getPathInfo()).thenReturn("/halt"); - when(request.isSecure()).thenReturn(false); - when(request.getRemoteAddr()).thenReturn("127.100.0.3"); - internalServlet.doGet(request, response); - verify(response).setStatus(eq(HttpServletResponse.SC_FORBIDDEN)); - } - - @Test - public void Given_Request_Is_HTTP_GET_With_Halt_In_Endpoint_Request_Succeeds() throws Exception { - when(request.getPathInfo()).thenReturn("/halt"); - when(request.isSecure()).thenReturn(false); - when(request.getRemoteAddr()).thenReturn("127.0.0.1"); - internalServlet.doGet(request, response); - verify(response).setStatus(eq(HttpServletResponse.SC_OK)); - } - - @Test - public void Given_Request_Is_HTTP_GET_With_FetchProv_In_Endpoint_Request_Succeeds() throws Exception { - when(request.getPathInfo()).thenReturn("/fetchProv"); - when(request.isSecure()).thenReturn(false); - internalServlet.doGet(request, response); - verify(response).setStatus(eq(HttpServletResponse.SC_OK)); - } - - @Test - public void Given_Request_Is_HTTP_GET_With_Prov_In_Endpoint_Request_Succeeds() throws Exception { - when(request.getPathInfo()).thenReturn("/prov"); - when(request.getQueryString()).thenReturn(null); - setPokerToNotCreateTimers(); - ServletOutputStream outStream = mock(ServletOutputStream.class); - when(response.getOutputStream()).thenReturn(outStream); - internalServlet.doGet(request, response); - verify(response).setStatus(eq(HttpServletResponse.SC_OK)); - } - - @Test - public void Given_Request_Is_HTTP_GET_With_Logs_In_Endpoint_Request_Succeeds() throws Exception { - when(request.getPathInfo()).thenReturn("/logs/"); - ServletOutputStream outStream = mock(ServletOutputStream.class); - when(response.getOutputStream()).thenReturn(outStream); - internalServlet.doGet(request, response); - verify(response).setStatus(eq(HttpServletResponse.SC_OK)); - } - - @Test - public void Given_Request_Is_HTTP_GET_Starts_With_Logs_In_Endpoint_Request_Succeeds() throws Exception { - when(request.getPathInfo()).thenReturn("/logs/TestFile"); - internalServlet.doGet(request, response); - verify(response).sendError(eq(HttpServletResponse.SC_NO_CONTENT), argThat(notNullValue(String.class))); - } - - @Test - public void Given_Request_Is_HTTP_GET_With_Api_In_Endpoint_Request_Succeeds() throws Exception { - when(request.getPathInfo()).thenReturn("/api/Key"); - setParametersToNotContactDb(false); - ServletOutputStream outStream = mock(ServletOutputStream.class); - when(response.getOutputStream()).thenReturn(outStream); - internalServlet.doGet(request, response); - verify(response).setStatus(eq(HttpServletResponse.SC_OK)); - } - - @Test - public void Given_Request_Is_HTTP_GET_With_Drlogs_In_Endpoint_Request_Succeeds() throws Exception { - when(request.getPathInfo()).thenReturn("/drlogs/"); - PowerMockito.mockStatic(LogfileLoader.class); - LogfileLoader logfileLoader = mock(LogfileLoader.class); - when(logfileLoader.getBitSet()).thenReturn(new RLEBitSet()); - PowerMockito.when(LogfileLoader.getLoader()).thenReturn(logfileLoader); - ServletOutputStream outStream = mock(ServletOutputStream.class); - when(response.getOutputStream()).thenReturn(outStream); - internalServlet.doGet(request, response); - verify(response).setStatus(eq(HttpServletResponse.SC_OK)); - } - - @Test - public void Given_Request_Is_HTTP_GET_Incorrect_Endpoint_Then_No_Content_Response_Is_Generated() throws Exception { - when(request.getPathInfo()).thenReturn("/incorrect/"); - internalServlet.doGet(request, response); - verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class))); - } - - @Test - public void Given_Request_Is_HTTP_PUT_And_Address_Not_Authorized_When_HTTPS_Is_Required_Then_Forbidden_Response_Is_Generated() throws Exception { - when(request.getRemoteAddr()).thenReturn("127.100.0.3"); - internalServlet.doPut(request, response); - verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class))); - } - - @Test - public void Given_Request_Is_HTTP_PUT_With_Api_In_Endpoint_Request_Succeeds() throws Exception { - when(request.getPathInfo()).thenReturn("/api/Key"); - setParametersToNotContactDb(false); - 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)); - } - - @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); - String[] values = {"V", "a", "l", "u", "e", "s"}; - when(request.getParameterValues(anyString())).thenReturn(values); - internalServlet = internalServerFailure(); - internalServlet.doPut(request, response); - verify(response).sendError(eq(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), argThat(notNullValue(String.class))); - } - - @Test - public void Given_Request_Is_HTTP_PUT_With_Incorrect_Endpoint_Then_Not_Found_Error_Is_Generated() throws Exception { - when(request.getPathInfo()).thenReturn("/incorrect"); - internalServlet.doPut(request, response); - verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class))); - } - - @Test - public void Given_Request_Is_HTTP_DELETE_And_Address_Not_Authorized_When_HTTPS_Is_Required_Then_Forbidden_Response_Is_Generated() throws Exception { - when(request.getRemoteAddr()).thenReturn("127.100.0.3"); - internalServlet.doDelete(request, response); - verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class))); - } - - @Test - public void Given_Request_Is_HTTP_DELETE_With_Api_In_Endpoint_Request_Succeeds() throws Exception { - when(request.getPathInfo()).thenReturn("/api/Key"); - setParametersToNotContactDb(false); - 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)); - } - - @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); - String[] values = {"V", "a", "l", "u", "e", "s"}; - when(request.getParameterValues(anyString())).thenReturn(values); - internalServlet = internalServerFailure(); - internalServlet.doDelete(request, response); - verify(response).sendError(eq(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), argThat(notNullValue(String.class))); - } - - @Test - public void Given_Request_Is_HTTP_DELETE_With_Incorrect_Endpoint_Then_Not_Found_Error_Is_Generated() throws Exception { - when(request.getPathInfo()).thenReturn("/incorrect"); - internalServlet.doDelete(request, response); - verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class))); - } - - @Test - public void Given_Request_Is_HTTP_POST_And_Address_Not_Authorized_When_HTTPS_Is_Required_Then_Forbidden_Response_Is_Generated() throws Exception { - when(request.getRemoteAddr()).thenReturn("127.100.0.3"); - internalServlet.doPost(request, response); - 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); - 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)); - } - - @Test - 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(); - internalServlet.doPost(request, response); - verify(response).sendError(eq(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), argThat(notNullValue(String.class))); - } - - @Test - public void Given_Request_Is_HTTP_POST_To_Logs_And_Content_Header_Is_Not_Supported_Type_Then_Unsupported_Media_Type_Response_Is_Generated() throws Exception { - when(request.getHeader("Content-Type")).thenReturn("stub_contentType"); - when(request.getPathInfo()).thenReturn("/logs/"); - internalServlet.doPost(request, response); - verify(response).setStatus(eq(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE)); - } - - @Test - public void Given_Request_Is_HTTP_POST_To_Logs_And_Content_Encoding_Is_Not_Supported_Type_Then_Unsupported_Media_Type_Response_Is_Generated() throws Exception { - when(request.getHeader("Content-Encoding")).thenReturn("not-supported"); - when(request.getPathInfo()).thenReturn("/logs/"); - internalServlet.doPost(request, response); - verify(response).setStatus(eq(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE)); - } - - @Test - public void Given_Request_Is_HTTP_POST_To_Drlogs_And_Then_Unsupported_Media_Type_Response_Is_Generated() throws Exception { - when(request.getHeader("Content-Type")).thenReturn("stub_contentType"); - when(request.getPathInfo()).thenReturn("/drlogs/"); - internalServlet.doPost(request, response); - verify(response).setStatus(eq(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE)); - } - - @Test - public void Given_Request_Is_HTTP_POST_To_Drlogs_And_Request_Succeeds() throws Exception { - when(request.getPathInfo()).thenReturn("/drlogs/"); - ServletInputStream inStream = mock(ServletInputStream.class); - when(inStream.read()).thenReturn(1, -1); - when(request.getInputStream()).thenReturn(inStream); - internalServlet.doPost(request, response); - verify(response).setStatus(eq(HttpServletResponse.SC_OK)); - } - - @Test - public void Given_Request_Is_HTTP_POST_With_Incorrect_Endpoint_Then_Not_Found_Error_Is_Generated() throws Exception { - when(request.getPathInfo()).thenReturn("/incorrect/"); - internalServlet.doPost(request, response); - verify(response).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"); - setValidPathInfoInHttpHeader(); - when(request.getHeader("Content-Type")).thenReturn("text/plain"); - when(request.getHeader("Content-Encoding")).thenReturn("gzip"); - } - - private void setUpValidSecurityOnHttpRequest() throws Exception { - when(request.isSecure()).thenReturn(true); - when(request.getRemoteAddr()).thenReturn(InetAddress.getLocalHost().getHostAddress()); - InetAddress[] nodeAddresses = new InetAddress[1]; - nodeAddresses[0] = InetAddress.getLocalHost(); - FieldUtils.writeDeclaredStaticField(BaseServlet.class, "nodeAddresses", nodeAddresses, true); - FieldUtils.writeDeclaredStaticField(BaseServlet.class, "requireCert", false, true); - } - - private void setBehalfHeader(String headerValue) { - when(request.getHeader(BEHALF_HEADER)).thenReturn(headerValue); - } - - private void setValidPathInfoInHttpHeader() { - when(request.getPathInfo()).thenReturn("/123"); - } - - private void setPokerToNotCreateTimers() throws Exception { - Poker poker = mock(Poker.class); - 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() { - - protected boolean doUpdate(Updateable bean) { - return true; - } - - protected boolean doDelete(Deleteable bean) { - return true; - } - - protected boolean doInsert(Insertable bean) { - return true; - } - }; - return internalServlet; - } - - private InternalServlet internalServerFailure() { - InternalServlet internalServlet = new InternalServlet() { - - protected boolean doUpdate(Updateable bean) { - return false; - } - - protected boolean doDelete(Deleteable bean) { - return false; - } - - protected boolean doInsert(Insertable bean) { - return false; - } - }; - 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 InternalServlet internalServlet; + + @Mock + private HttpServletRequest request; + + @Mock + private HttpServletResponse response; + + @Before + public void setUp() throws Exception { + super.setUp(); + internalServlet = new InternalServlet(); + setAuthoriserToReturnRequestIsAuthorized(); + setUpValidAuthorisedRequest(); + } + + @Test + 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"); + internalServlet.doGet(request, response); + verify(response) + .sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class))); + } + + @Test + public void Given_Request_Is_HTTP_GET_With_Halt_In_Endpoint_But_Not_Sent_From_Localhost_Then_Forbidden_Response_Is_Generated() + throws Exception { + when(request.getPathInfo()).thenReturn("/halt"); + when(request.isSecure()).thenReturn(false); + when(request.getRemoteAddr()).thenReturn("127.100.0.3"); + internalServlet.doGet(request, response); + verify(response).setStatus(eq(HttpServletResponse.SC_FORBIDDEN)); + } + + @Test + public void Given_Request_Is_HTTP_GET_With_Halt_In_Endpoint_Request_Succeeds() throws Exception { + when(request.getPathInfo()).thenReturn("/halt"); + when(request.isSecure()).thenReturn(false); + when(request.getRemoteAddr()).thenReturn("127.0.0.1"); + internalServlet.doGet(request, response); + verify(response).setStatus(eq(HttpServletResponse.SC_OK)); + } + + @Test + public void Given_Request_Is_HTTP_GET_With_FetchProv_In_Endpoint_Request_Succeeds() + throws Exception { + when(request.getPathInfo()).thenReturn("/fetchProv"); + when(request.isSecure()).thenReturn(false); + internalServlet.doGet(request, response); + verify(response).setStatus(eq(HttpServletResponse.SC_OK)); + } + + @Test + public void Given_Request_Is_HTTP_GET_With_Prov_In_Endpoint_Request_Succeeds() throws Exception { + when(request.getPathInfo()).thenReturn("/prov"); + when(request.getQueryString()).thenReturn(null); + setPokerToNotCreateTimers(); + ServletOutputStream outStream = mock(ServletOutputStream.class); + when(response.getOutputStream()).thenReturn(outStream); + internalServlet.doGet(request, response); + verify(response).setStatus(eq(HttpServletResponse.SC_OK)); + } + + @Test + public void Given_Request_Is_HTTP_GET_With_Logs_In_Endpoint_Request_Succeeds() throws Exception { + when(request.getPathInfo()).thenReturn("/logs/"); + ServletOutputStream outStream = mock(ServletOutputStream.class); + when(response.getOutputStream()).thenReturn(outStream); + internalServlet.doGet(request, response); + verify(response).setStatus(eq(HttpServletResponse.SC_OK)); + } + + @Test + public void Given_Request_Is_HTTP_GET_Starts_With_Logs_In_Endpoint_Request_Succeeds() + throws Exception { + when(request.getPathInfo()).thenReturn("/logs/TestFile"); + internalServlet.doGet(request, response); + verify(response) + .sendError(eq(HttpServletResponse.SC_NO_CONTENT), argThat(notNullValue(String.class))); + } + + @Test + public void Given_Request_Is_HTTP_GET_With_Api_In_Endpoint_Request_Succeeds() throws Exception { + when(request.getPathInfo()).thenReturn("/api/Key"); + setParametersToNotContactDb(false); + ServletOutputStream outStream = mock(ServletOutputStream.class); + when(response.getOutputStream()).thenReturn(outStream); + internalServlet.doGet(request, response); + verify(response).setStatus(eq(HttpServletResponse.SC_OK)); + } + + @Test + public void Given_Request_Is_HTTP_GET_With_Drlogs_In_Endpoint_Request_Succeeds() + throws Exception { + when(request.getPathInfo()).thenReturn("/drlogs/"); + PowerMockito.mockStatic(LogfileLoader.class); + LogfileLoader logfileLoader = mock(LogfileLoader.class); + when(logfileLoader.getBitSet()).thenReturn(new RLEBitSet()); + PowerMockito.when(LogfileLoader.getLoader()).thenReturn(logfileLoader); + ServletOutputStream outStream = mock(ServletOutputStream.class); + when(response.getOutputStream()).thenReturn(outStream); + internalServlet.doGet(request, response); + verify(response).setStatus(eq(HttpServletResponse.SC_OK)); + } + + @Test + public void Given_Request_Is_HTTP_GET_Incorrect_Endpoint_Then_No_Content_Response_Is_Generated() + throws Exception { + when(request.getPathInfo()).thenReturn("/incorrect/"); + internalServlet.doGet(request, response); + verify(response) + .sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class))); + } + + @Test + public void Given_Request_Is_HTTP_PUT_And_Address_Not_Authorized_When_HTTPS_Is_Required_Then_Forbidden_Response_Is_Generated() + throws Exception { + when(request.getRemoteAddr()).thenReturn("127.100.0.3"); + internalServlet.doPut(request, response); + verify(response) + .sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class))); + } + + @Test + public void Given_Request_Is_HTTP_PUT_With_Api_In_Endpoint_Request_Succeeds() throws Exception { + when(request.getPathInfo()).thenReturn("/api/Key"); + setParametersToNotContactDb(false); + 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)); + } + + @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); + String[] values = {"V", "a", "l", "u", "e", "s"}; + when(request.getParameterValues(anyString())).thenReturn(values); + internalServlet = internalServerFailure(); + internalServlet.doPut(request, response); + verify(response).sendError(eq(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), + argThat(notNullValue(String.class))); + } + + @Test + public void Given_Request_Is_HTTP_PUT_With_Incorrect_Endpoint_Then_Not_Found_Error_Is_Generated() + throws Exception { + when(request.getPathInfo()).thenReturn("/incorrect"); + internalServlet.doPut(request, response); + verify(response) + .sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class))); + } + + @Test + public void Given_Request_Is_HTTP_DELETE_And_Address_Not_Authorized_When_HTTPS_Is_Required_Then_Forbidden_Response_Is_Generated() + throws Exception { + when(request.getRemoteAddr()).thenReturn("127.100.0.3"); + internalServlet.doDelete(request, response); + verify(response) + .sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class))); + } + + @Test + public void Given_Request_Is_HTTP_DELETE_With_Api_In_Endpoint_Request_Succeeds() + throws Exception { + when(request.getPathInfo()).thenReturn("/api/Key"); + setParametersToNotContactDb(false); + 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)); + } + + @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); + String[] values = {"V", "a", "l", "u", "e", "s"}; + when(request.getParameterValues(anyString())).thenReturn(values); + internalServlet = internalServerFailure(); + internalServlet.doDelete(request, response); + verify(response).sendError(eq(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), + argThat(notNullValue(String.class))); + } + + @Test + public void Given_Request_Is_HTTP_DELETE_With_Incorrect_Endpoint_Then_Not_Found_Error_Is_Generated() + throws Exception { + when(request.getPathInfo()).thenReturn("/incorrect"); + internalServlet.doDelete(request, response); + verify(response) + .sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class))); + } + + @Test + public void Given_Request_Is_HTTP_POST_And_Address_Not_Authorized_When_HTTPS_Is_Required_Then_Forbidden_Response_Is_Generated() + throws Exception { + when(request.getRemoteAddr()).thenReturn("127.100.0.3"); + internalServlet.doPost(request, response); + 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); + 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)); + } + + @Test + 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(); + internalServlet.doPost(request, response); + verify(response).sendError(eq(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), + argThat(notNullValue(String.class))); + } + + @Test + public void Given_Request_Is_HTTP_POST_To_Logs_And_Content_Header_Is_Not_Supported_Type_Then_Unsupported_Media_Type_Response_Is_Generated() + throws Exception { + when(request.getHeader("Content-Type")).thenReturn("stub_contentType"); + when(request.getPathInfo()).thenReturn("/logs/"); + internalServlet.doPost(request, response); + verify(response).setStatus(eq(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE)); + } + + @Test + public void Given_Request_Is_HTTP_POST_To_Logs_And_Content_Encoding_Is_Not_Supported_Type_Then_Unsupported_Media_Type_Response_Is_Generated() + throws Exception { + when(request.getHeader("Content-Encoding")).thenReturn("not-supported"); + when(request.getPathInfo()).thenReturn("/logs/"); + internalServlet.doPost(request, response); + verify(response).setStatus(eq(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE)); + } + + @Test + public void Given_Request_Is_HTTP_POST_To_Drlogs_And_Then_Unsupported_Media_Type_Response_Is_Generated() + throws Exception { + when(request.getHeader("Content-Type")).thenReturn("stub_contentType"); + when(request.getPathInfo()).thenReturn("/drlogs/"); + internalServlet.doPost(request, response); + verify(response).setStatus(eq(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE)); + } + + @Test + public void Given_Request_Is_HTTP_POST_To_Drlogs_And_Request_Succeeds() throws Exception { + when(request.getPathInfo()).thenReturn("/drlogs/"); + ServletInputStream inStream = mock(ServletInputStream.class); + when(inStream.read()).thenReturn(1, -1); + when(request.getInputStream()).thenReturn(inStream); + PowerMockito.mockStatic(LogRecord.class); + internalServlet.doPost(request, response); + verify(response).setStatus(eq(HttpServletResponse.SC_OK)); + } + + @Test + public void Given_Request_Is_HTTP_POST_With_Incorrect_Endpoint_Then_Not_Found_Error_Is_Generated() + throws Exception { + when(request.getPathInfo()).thenReturn("/incorrect/"); + internalServlet.doPost(request, response); + verify(response) + .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"); + setValidPathInfoInHttpHeader(); + when(request.getHeader("Content-Type")).thenReturn("text/plain"); + when(request.getHeader("Content-Encoding")).thenReturn("gzip"); + } + + private void setUpValidSecurityOnHttpRequest() throws Exception { + when(request.isSecure()).thenReturn(true); + when(request.getRemoteAddr()).thenReturn(InetAddress.getLocalHost().getHostAddress()); + InetAddress[] nodeAddresses = new InetAddress[1]; + nodeAddresses[0] = InetAddress.getLocalHost(); + FieldUtils.writeDeclaredStaticField(BaseServlet.class, "nodeAddresses", nodeAddresses, true); + FieldUtils.writeDeclaredStaticField(BaseServlet.class, "requireCert", false, true); + } + + private void setBehalfHeader(String headerValue) { + when(request.getHeader(BEHALF_HEADER)).thenReturn(headerValue); + } + + private void setValidPathInfoInHttpHeader() { + when(request.getPathInfo()).thenReturn("/123"); + } + + private void setPokerToNotCreateTimers() throws Exception { + Poker poker = mock(Poker.class); + 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() { + + protected boolean doUpdate(Updateable bean) { + return true; + } + + protected boolean doDelete(Deleteable bean) { + return true; + } + + protected boolean doInsert(Insertable bean) { + return true; + } + }; + return internalServlet; + } + + private InternalServlet internalServerFailure() { + InternalServlet internalServlet = new InternalServlet() { + + protected boolean doUpdate(Updateable bean) { + return false; + } + + protected boolean doDelete(Deleteable bean) { + return false; + } + + protected boolean doInsert(Insertable bean) { + return false; + } + }; + 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); + } } diff --git a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/StatisticsServletTest.java b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/StatisticsServletTest.java index e42a8a8c..0a9632eb 100644..100755 --- a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/StatisticsServletTest.java +++ b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/StatisticsServletTest.java @@ -22,100 +22,124 @@ ******************************************************************************/ package org.onap.dmaap.datarouter.provisioning; +import static org.hamcrest.Matchers.notNullValue; +import static org.mockito.Matchers.anyInt; +import static org.mockito.Matchers.anyObject; +import static org.mockito.Matchers.anyString; +import static org.mockito.Matchers.argThat; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.doCallRealMethod; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import javax.servlet.ServletOutputStream; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; -import javax.servlet.ServletOutputStream; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import static org.hamcrest.Matchers.notNullValue; -import static org.mockito.Matchers.*; -import static org.mockito.Mockito.*; @RunWith(PowerMockRunner.class) -public class StatisticsServletTest extends DrServletTestBase{ - private StatisticsServlet statisticsServlet; +@PrepareForTest(StatisticsServlet.class) +public class StatisticsServletTest extends DrServletTestBase { + + private StatisticsServlet statisticsServlet; - @Mock - private HttpServletRequest request; + @Mock + private HttpServletRequest request; - @Mock - private HttpServletResponse response; + @Mock + private HttpServletResponse response; - @Before - public void setUp() throws Exception{ - super.setUp(); - statisticsServlet = new StatisticsServlet(); - buildRequestParameters(); - } + @Before + public void setUp() throws Exception { + super.setUp(); + statisticsServlet = new StatisticsServlet(); + buildRequestParameters(); + } - @Test - public void Given_Request_Is_HTTP_DELETE_SC_METHOD_NOT_ALLOWED_Response_Is_Generated() throws Exception { - statisticsServlet.doDelete(request, response); - verify(response).sendError(eq(HttpServletResponse.SC_METHOD_NOT_ALLOWED), argThat(notNullValue(String.class))); - } + @Test + public void Given_Request_Is_HTTP_DELETE_SC_METHOD_NOT_ALLOWED_Response_Is_Generated() + throws Exception { + statisticsServlet.doDelete(request, response); + verify(response).sendError(eq(HttpServletResponse.SC_METHOD_NOT_ALLOWED), + argThat(notNullValue(String.class))); + } - @Test - public void Given_Request_Is_HTTP_PUT_SC_METHOD_NOT_ALLOWED_Response_Is_Generated() throws Exception { - statisticsServlet.doPut(request, response); - verify(response).sendError(eq(HttpServletResponse.SC_METHOD_NOT_ALLOWED), argThat(notNullValue(String.class))); - } + @Test + public void Given_Request_Is_HTTP_PUT_SC_METHOD_NOT_ALLOWED_Response_Is_Generated() + throws Exception { + statisticsServlet.doPut(request, response); + verify(response).sendError(eq(HttpServletResponse.SC_METHOD_NOT_ALLOWED), + argThat(notNullValue(String.class))); + } - @Test - public void Given_Request_Is_HTTP_POST_SC_METHOD_NOT_ALLOWED_Response_Is_Generated() throws Exception { - statisticsServlet.doPost(request, response); - verify(response).sendError(eq(HttpServletResponse.SC_METHOD_NOT_ALLOWED), argThat(notNullValue(String.class))); - } + @Test + public void Given_Request_Is_HTTP_POST_SC_METHOD_NOT_ALLOWED_Response_Is_Generated() + throws Exception { + statisticsServlet.doPost(request, response); + verify(response).sendError(eq(HttpServletResponse.SC_METHOD_NOT_ALLOWED), + argThat(notNullValue(String.class))); + } - @Test - public void Given_Request_Is_HTTP_GET_With_Incorrect_Parameters_Then_Bad_Request_Response_Is_Generated() throws Exception { - when(request.getParameter("type")).thenReturn("get"); - statisticsServlet.doGet(request, response); - verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class))); - } + @Test + public void Given_Request_Is_HTTP_GET_With_Incorrect_Parameters_Then_Bad_Request_Response_Is_Generated() + throws Exception { + when(request.getParameter("type")).thenReturn("get"); + statisticsServlet.doGet(request, response); + verify(response) + .sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class))); + } - @Test - public void Given_Request_Is_HTTP_GET_With_GroupId_But_No_FeedId_Parameters_Then_Request_Succeeds() throws Exception { - ServletOutputStream outStream = mock(ServletOutputStream.class); - when(response.getOutputStream()).thenReturn(outStream); - statisticsServlet = mock(StatisticsServlet.class); - doCallRealMethod().when(statisticsServlet).doGet(request, response); - doNothing().when(statisticsServlet).rsToCSV(anyObject(), anyObject()); - when(statisticsServlet.getFeedIdsByGroupId(anyInt())).thenReturn(new StringBuffer("1")); - statisticsServlet.doGet(request, response); - verify(response).setStatus(eq(HttpServletResponse.SC_OK)); - } + @Test + public void Given_Request_Is_HTTP_GET_With_GroupId_But_No_FeedId_Parameters_Then_Request_Succeeds() + throws Exception { + ServletOutputStream outStream = mock(ServletOutputStream.class); + when(response.getOutputStream()).thenReturn(outStream); + statisticsServlet = PowerMockito.mock(StatisticsServlet.class); + PowerMockito.doReturn(null).when(statisticsServlet, "getRecordsForSQL", anyString()); + doNothing().when(statisticsServlet).rsToCSV(anyObject(), anyObject()); + doCallRealMethod().when(statisticsServlet).doGet(request, response); + when(statisticsServlet.getFeedIdsByGroupId(anyInt())).thenReturn(new StringBuffer("1")); + statisticsServlet.doGet(request, response); + verify(response).setStatus(eq(HttpServletResponse.SC_OK)); + } - @Test - public void Given_Request_Is_HTTP_GET_With_GroupId_And_FeedId_Parameters_Then_Request_Succeeds() throws Exception { - when(request.getParameter("feedid")).thenReturn("1"); - ServletOutputStream outStream = mock(ServletOutputStream.class); - when(response.getOutputStream()).thenReturn(outStream); - statisticsServlet = mock(StatisticsServlet.class); - doCallRealMethod().when(statisticsServlet).doGet(request, response); - doCallRealMethod().when(statisticsServlet).queryGeneretor(anyObject()); - doNothing().when(statisticsServlet).rsToCSV(anyObject(), anyObject()); - when(statisticsServlet.getFeedIdsByGroupId(anyInt())).thenReturn(new StringBuffer("1")); - statisticsServlet.doGet(request, response); - verify(response).setStatus(eq(HttpServletResponse.SC_OK)); - } + @Test + public void Given_Request_Is_HTTP_GET_With_GroupId_And_FeedId_Parameters_Then_Request_Succeeds() + throws Exception { + when(request.getParameter("feedid")).thenReturn("1"); + ServletOutputStream outStream = mock(ServletOutputStream.class); + when(response.getOutputStream()).thenReturn(outStream); + statisticsServlet = PowerMockito.mock(StatisticsServlet.class); + PowerMockito.doReturn(null).when(statisticsServlet, "getRecordsForSQL", anyString()); + doNothing().when(statisticsServlet).rsToCSV(anyObject(), anyObject()); + doCallRealMethod().when(statisticsServlet).doGet(request, response); + doCallRealMethod().when(statisticsServlet).queryGeneretor(anyObject()); + when(statisticsServlet.getFeedIdsByGroupId(anyInt())).thenReturn(new StringBuffer("1")); + statisticsServlet.doGet(request, response); + verify(response).setStatus(eq(HttpServletResponse.SC_OK)); + } - private void buildRequestParameters() { - when(request.getParameter("type")).thenReturn("exp"); - when(request.getParameter("publishId")).thenReturn("ID"); - when(request.getParameter("statusCode")).thenReturn("success"); - when(request.getParameter("expiryReason")).thenReturn("other"); - when(request.getParameter("start")).thenReturn("0"); - when(request.getParameter("end")).thenReturn("0"); - when(request.getParameter("output_type")).thenReturn("csv"); - when(request.getParameter("start_time")).thenReturn("13"); - when(request.getParameter("end_time")).thenReturn("15"); - when(request.getParameter("time")).thenReturn("10"); - when(request.getParameter("groupid")).thenReturn("1"); - when(request.getParameter("subid")).thenReturn("1"); - } + private void buildRequestParameters() { + when(request.getParameter("type")).thenReturn("exp"); + when(request.getParameter("publishId")).thenReturn("ID"); + when(request.getParameter("statusCode")).thenReturn("success"); + when(request.getParameter("expiryReason")).thenReturn("other"); + when(request.getParameter("start")).thenReturn("0"); + when(request.getParameter("end")).thenReturn("0"); + when(request.getParameter("output_type")).thenReturn("csv"); + when(request.getParameter("start_time")).thenReturn("13"); + when(request.getParameter("end_time")).thenReturn("15"); + when(request.getParameter("time")).thenReturn("10"); + when(request.getParameter("groupid")).thenReturn("1"); + when(request.getParameter("subid")).thenReturn("1"); + } } 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 c663451b..9009b976 100644..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 @@ -269,7 +269,7 @@ public class SubscribeServletTest extends DrServletTestBase { authAddressesAndNetworks.add(("127.0.0.1")); FieldUtils.writeDeclaredStaticField(BaseServlet.class, "authorizedAddressesAndNetworks", authAddressesAndNetworks, true); FieldUtils.writeDeclaredStaticField(BaseServlet.class, "requireCert", false, true); - FieldUtils.writeDeclaredStaticField(BaseServlet.class, "maxSubs", 1, true); + FieldUtils.writeDeclaredStaticField(BaseServlet.class, "maxSubs", 100, true); } private void setBehalfHeader(String headerValue) { |