Add optional API for PM Mapper
[dmaap/datarouter.git] / datarouter-prov / src / test / java / org / onap / dmaap / datarouter / provisioning / SubscriptionServletTest.java
index 472be51..43973d5 100755 (executable)
  ******************************************************************************/
 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;
-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;
@@ -47,6 +46,7 @@ import javax.servlet.ServletInputStream;
 import javax.servlet.ServletOutputStream;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
+import java.io.FileNotFoundException;
 import java.sql.SQLException;
 import java.util.HashSet;
 import java.util.Set;
@@ -57,7 +57,7 @@ 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 +66,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 +84,7 @@ public class SubscriptionServletTest {
     }
 
     @AfterClass
-    public static void tearDownClass() {
+    public static void tearDownClass() throws FileNotFoundException {
         em.clear();
         em.close();
         emf.close();
@@ -89,6 +92,7 @@ public class SubscriptionServletTest {
 
     @Before
     public void setUp() throws Exception {
+        listAppender = setTestLogger(SubscriptionServlet.class);
         subscriptionServlet = new SubscriptionServlet();
         db = new DB();
         setAuthoriserToReturnRequestIsAuthorized();
@@ -102,6 +106,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 +152,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 +161,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 +198,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 +206,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
@@ -237,7 +246,7 @@ public class SubscriptionServletTest {
 
     @Test
     public void Given_Request_Is_HTTP_PUT_And_Request_Contains_Badly_Formed_JSON_Then_Bad_Request_Response_Is_Generated() throws Exception {
-        when(request.getHeader("Content-Type")).thenReturn("application/vnd.att-dr.subscription; version=1.0");
+        when(request.getHeader("Content-Type")).thenReturn("application/vnd.dmaap-dr.subscription; version=1.0");
         ServletInputStream inStream = mock(ServletInputStream.class);
         when(request.getInputStream()).thenReturn(inStream);
         subscriptionServlet.doPut(request, response);
@@ -246,7 +255,7 @@ public class SubscriptionServletTest {
 
     @Test
     public void Given_Request_Is_HTTP_PUT_And_Subscription_Object_Is_Invalid_Bad_Request_Response_Is_Generated() throws Exception {
-        when(request.getHeader("Content-Type")).thenReturn("application/vnd.att-dr.subscription; version=1.0");
+        when(request.getHeader("Content-Type")).thenReturn("application/vnd.dmaap-dr.subscription; version=1.0");
         SubscriptionServlet subscriptionServlet = new SubscriptionServlet() {
             protected JSONObject getJSONfromInput(HttpServletRequest req) {
                 JSONObject jo = new JSONObject();
@@ -259,8 +268,8 @@ public class SubscriptionServletTest {
 
     @Test
     public void Given_Request_Is_HTTP_PUT_And_Subscriber_Modified_By_Different_Creator_Then_Bad_Request_Is_Generated() throws Exception {
-        when(request.getHeader("X-ATT-DR-ON-BEHALF-OF-GROUP")).thenReturn(null);
-        when(request.getHeader("Content-Type")).thenReturn("application/vnd.att-dr.subscription; version=1.0");
+        when(request.getHeader("X-DMAAP-DR-ON-BEHALF-OF-GROUP")).thenReturn(null);
+        when(request.getHeader("Content-Type")).thenReturn("application/vnd.dmaap-dr.subscription; version=1.0");
         JSONObject JSObject = buildRequestJsonObject();
         SubscriptionServlet subscriptionServlet = new SubscriptionServlet() {
             protected JSONObject getJSONfromInput(HttpServletRequest req) {
@@ -269,6 +278,7 @@ public class SubscriptionServletTest {
                 jo.put("version", "2.0");
                 jo.put("metadataOnly", true);
                 jo.put("suspend", true);
+                jo.put("privilegedSubscriber", true);
                 jo.put("delivery", JSObject);
                 jo.put("subscriber", "differentSubscriber");
                 jo.put("sync", true);
@@ -281,8 +291,8 @@ public class SubscriptionServletTest {
 
     @Test
     public void Given_Request_Is_HTTP_PUT_And_Update_Fails() throws Exception {
-        when(request.getHeader("X-ATT-DR-ON-BEHALF-OF-GROUP")).thenReturn("stub_subjectGroup");
-        when(request.getHeader("Content-Type")).thenReturn("application/vnd.att-dr.subscription; version=1.0");
+        when(request.getHeader("X-DMAAP-DR-ON-BEHALF-OF-GROUP")).thenReturn("stub_subjectGroup");
+        when(request.getHeader("Content-Type")).thenReturn("application/vnd.dmaap-dr.subscription; version=1.0");
         JSONObject JSObject = buildRequestJsonObject();
         SubscriptionServlet subscriptionServlet = new SubscriptionServlet() {
             protected JSONObject getJSONfromInput(HttpServletRequest req) {
@@ -291,6 +301,7 @@ public class SubscriptionServletTest {
                 jo.put("version", "2.0");
                 jo.put("metadataOnly", true);
                 jo.put("suspend", true);
+                jo.put("privilegedSubscriber", true);
                 jo.put("delivery", JSObject);
                 jo.put("sync", true);
                 return jo;
@@ -309,8 +320,8 @@ public class SubscriptionServletTest {
     public void Given_Request_Is_HTTP_PUT_And_Update_Succeeds() throws Exception {
         ServletOutputStream outStream = mock(ServletOutputStream.class);
         when(response.getOutputStream()).thenReturn(outStream);
-        when(request.getHeader("X-ATT-DR-ON-BEHALF-OF-GROUP")).thenReturn("stub_subjectGroup");
-        when(request.getHeader("Content-Type")).thenReturn("application/vnd.att-dr.subscription; version=1.0");
+        when(request.getHeader("X-DMAAP-DR-ON-BEHALF-OF-GROUP")).thenReturn("stub_subjectGroup");
+        when(request.getHeader("Content-Type")).thenReturn("application/vnd.dmaap-dr.subscription; version=1.0");
         JSONObject JSObject = buildRequestJsonObject();
         SubscriptionServlet subscriptionServlet = new SubscriptionServlet() {
             protected JSONObject getJSONfromInput(HttpServletRequest req) {
@@ -319,6 +330,7 @@ public class SubscriptionServletTest {
                 jo.put("version", "2.0");
                 jo.put("metadataOnly", true);
                 jo.put("suspend", true);
+                jo.put("privilegedSubscriber", true);
                 jo.put("delivery", JSObject);
                 jo.put("sync", true);
                 jo.put("changeowner", true);
@@ -328,6 +340,7 @@ public class SubscriptionServletTest {
         subscriptionServlet.doPut(request, response);
         verify(response).setStatus(eq(HttpServletResponse.SC_OK));
         changeSubscriptionBackToNormal();
+        verifyEnteringExitCalled(listAppender);
     }
 
     @Test
@@ -335,6 +348,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
@@ -367,7 +381,7 @@ public class SubscriptionServletTest {
 
     @Test
     public void Given_Request_Is_HTTP_POST_And_Request_Is_Not_Authorized_Then_Forbidden_Response_Is_Generated() throws Exception {
-        when(request.getHeader(anyString())).thenReturn("application/vnd.att-dr.subscription-control");
+        when(request.getHeader(anyString())).thenReturn("application/vnd.dmaap-dr.subscription-control");
         setAuthoriserToReturnRequestNotAuthorized();
         subscriptionServlet.doPost(request, response);
         verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class)));
@@ -375,7 +389,7 @@ public class SubscriptionServletTest {
 
     @Test
     public void Given_Request_Is_HTTP_POST_And_Request_Contains_Badly_Formed_JSON_Then_Bad_Request_Response_Is_Generated() throws Exception {
-        when(request.getHeader("Content-Type")).thenReturn("application/vnd.att-dr.subscription-control; version=1.0");
+        when(request.getHeader("Content-Type")).thenReturn("application/vnd.dmaap-dr.subscription-control; version=1.0");
         ServletInputStream inStream = mock(ServletInputStream.class);
         when(request.getInputStream()).thenReturn(inStream);
         subscriptionServlet.doPost(request, response);
@@ -384,8 +398,8 @@ public class SubscriptionServletTest {
 
     @Test
     public void Given_Request_Is_HTTP_POST_And_Post_Fails() throws Exception {
-        when(request.getHeader("X-ATT-DR-ON-BEHALF-OF-GROUP")).thenReturn("stub_subjectGroup");
-        when(request.getHeader("Content-Type")).thenReturn("application/vnd.att-dr.subscription-control; version=1.0");
+        when(request.getHeader("X-DMAAP-DR-ON-BEHALF-OF-GROUP")).thenReturn("stub_subjectGroup");
+        when(request.getHeader("Content-Type")).thenReturn("application/vnd.dmaap-dr.subscription-control; version=1.0");
         JSONObject JSObject = buildRequestJsonObject();
         SubscriptionServlet subscriptionServlet = new SubscriptionServlet() {
             protected JSONObject getJSONfromInput(HttpServletRequest req) {
@@ -406,8 +420,8 @@ public class SubscriptionServletTest {
     public void Given_Request_Is_HTTP_POST_And_Post_Succeeds() throws Exception {
         ServletOutputStream outStream = mock(ServletOutputStream.class);
         when(response.getOutputStream()).thenReturn(outStream);
-        when(request.getHeader("X-ATT-DR-ON-BEHALF-OF-GROUP")).thenReturn("stub_subjectGroup");
-        when(request.getHeader("Content-Type")).thenReturn("application/vnd.att-dr.subscription-control; version=1.0");
+        when(request.getHeader("X-DMAAP-DR-ON-BEHALF-OF-GROUP")).thenReturn("stub_subjectGroup");
+        when(request.getHeader("Content-Type")).thenReturn("application/vnd.dmaap-dr.subscription-control; version=1.0");
         JSONObject JSObject = buildRequestJsonObject();
         SubscriptionServlet subscriptionServlet = new SubscriptionServlet() {
             protected JSONObject getJSONfromInput(HttpServletRequest req) {
@@ -423,6 +437,7 @@ public class SubscriptionServletTest {
         };
         subscriptionServlet.doPost(request, response);
         verify(response).setStatus(eq(HttpServletResponse.SC_ACCEPTED));
+        verifyEnteringExitCalled(listAppender);
     }
 
     @NotNull
@@ -488,6 +503,7 @@ public class SubscriptionServletTest {
         subscription.setGroupid(1);
         subscription.setMetadataOnly(false);
         subscription.setSuspended(false);
+        subscription.setPrivilegedSubscriber(false);
         subscription.doInsert(db.getConnection());
     }
 
@@ -501,6 +517,7 @@ public class SubscriptionServletTest {
         subscription.setGroupid(1);
         subscription.setMetadataOnly(false);
         subscription.setSuspended(false);
+        subscription.setPrivilegedSubscriber(false);
         subscription.changeOwnerShip();
         subscription.doUpdate(db.getConnection());
     }