use delete level property in AafTopicSetupService
[dmaap/dbcapi.git] / src / test / java / org / onap / dmaap / dbcapi / service / AafTopicSetupServiceTest.java
index a250c90..0ca406a 100644 (file)
@@ -35,11 +35,13 @@ import org.onap.dmaap.dbcapi.aaf.DmaapPerm;
 import org.onap.dmaap.dbcapi.model.ApiError;
 import org.onap.dmaap.dbcapi.model.Dmaap;
 import org.onap.dmaap.dbcapi.model.Topic;
+import org.onap.dmaap.dbcapi.util.DmaapConfig;
 
 import java.util.List;
 
 import static com.google.common.collect.Lists.newArrayList;
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
 import static org.mockito.BDDMockito.given;
 
@@ -49,6 +51,7 @@ public class AafTopicSetupServiceTest {
     private static final int INTERNAL_SERVER_ERROR = 500;
     private static final int NOT_FOUND = 404;
     private static final int CREATED = 201;
+    private static final int OK = 200;
     private static final String TOPIC_NS_ROOT = "org.onap.dmaap.mr";
     private static final String TOPIC_PERM = "org.onap.dmaap.mr.topic";
     private static final String TOPIC_FQTN = "org.onap.dmaap.mr.sample_topic";
@@ -56,6 +59,8 @@ public class AafTopicSetupServiceTest {
     private AafServiceStub aafService = new AafServiceStub();
     @Mock
     private DmaapService dmaapService;
+    @Mock
+    private DmaapConfig dmaapConfig;
     private AafTopicSetupService aafTopicSetupService;
 
     @Before
@@ -65,7 +70,9 @@ public class AafTopicSetupServiceTest {
         dmaap.setTopicNsRoot(TOPIC_NS_ROOT);
         given(dmaapService.getDmaap()).willReturn(dmaap);
         given(dmaapService.getTopicPerm()).willReturn(TOPIC_PERM);
-        aafTopicSetupService = new AafTopicSetupService(aafService, dmaapService, true);
+        given(dmaapConfig.getProperty("aaf.CreateTopicRoles", "true")).willReturn("true");
+        given(dmaapConfig.getProperty("MR.ClientDeleteLevel", "0")).willReturn("2");
+        aafTopicSetupService = new AafTopicSetupService(aafService, dmaapService, dmaapConfig);
     }
 
     @Test
@@ -157,14 +164,14 @@ public class AafTopicSetupServiceTest {
 
     @Test
     public void shouldCreateOnlyPermissionsWhenCreateTopicRolesIsFalse() {
-        aafTopicSetupService = new AafTopicSetupService(aafService, dmaapService, false);
+        given(dmaapConfig.getProperty("aaf.CreateTopicRoles", "true")).willReturn("false");
 
         aafTopicSetupService.aafTopicSetup(givenTopic(TOPIC_FQTN));
 
         aafService.shouldAddPerm(new DmaapPerm(TOPIC_PERM, ":topic." + TOPIC_FQTN, "pub"));
         aafService.shouldAddPerm(new DmaapPerm(TOPIC_PERM, ":topic." + TOPIC_FQTN, "sub"));
         aafService.shouldAddPerm(new DmaapPerm(TOPIC_PERM, ":topic." + TOPIC_FQTN, "view"));
-        aafService.shouldHaveNoRolesAndGrants();
+        aafService.shouldHaveNoNamespaceRolesAndGrantsAdded();
     }
 
     @Test
@@ -176,7 +183,7 @@ public class AafTopicSetupServiceTest {
         aafService.shouldAddPerm(new DmaapPerm(TOPIC_PERM, ":topic." + topicFqtn, "pub"));
         aafService.shouldAddPerm(new DmaapPerm(TOPIC_PERM, ":topic." + topicFqtn, "sub"));
         aafService.shouldAddPerm(new DmaapPerm(TOPIC_PERM, ":topic." + topicFqtn, "view"));
-        aafService.shouldHaveNoRolesAndGrants();
+        aafService.shouldHaveNoNamespaceRolesAndGrantsAdded();
     }
 
     @Test
@@ -226,6 +233,92 @@ public class AafTopicSetupServiceTest {
         assertErrorStatus(apiError, NOT_FOUND);
     }
 
+    @Test
+    @Parameters({"200", "404"})
+    public void shouldremovePublisherSubscriberViewerPermissions(int aafServiceReturnedCode) {
+        aafService.givenReturnCode(aafServiceReturnedCode);
+
+        aafTopicSetupService.aafTopicCleanup(givenTopic(TOPIC_FQTN));
+
+        aafService.shouldRemovePerm(new DmaapPerm(TOPIC_PERM, ":topic." + TOPIC_FQTN, "pub"));
+        aafService.shouldRemovePerm(new DmaapPerm(TOPIC_PERM, ":topic." + TOPIC_FQTN, "sub"));
+        aafService.shouldRemovePerm(new DmaapPerm(TOPIC_PERM, ":topic." + TOPIC_FQTN, "view"));
+    }
+
+    @Test
+    @Parameters({"200", "404"})
+    public void shouldRemoveNamespace(int aafServiceReturnedCode) {
+        aafService.givenReturnCode(aafServiceReturnedCode);
+        Topic topic = givenTopic(TOPIC_FQTN);
+
+        aafTopicSetupService.aafTopicCleanup(topic);
+
+        AafNamespace namespace = new AafNamespace(TOPIC_FQTN, IDENTITY);
+        aafService.shouldRemoveNamespace(namespace);
+    }
+
+    @Test
+    public void shouldRemoveOnlyPermissionsWhenCreateTopicRolesIsFalse() {
+        given(dmaapConfig.getProperty("aaf.CreateTopicRoles", "true")).willReturn("false");
+
+        aafTopicSetupService.aafTopicCleanup(givenTopic(TOPIC_FQTN));
+
+        aafService.shouldRemovePerm(new DmaapPerm(TOPIC_PERM, ":topic." + TOPIC_FQTN, "pub"));
+        aafService.shouldRemovePerm(new DmaapPerm(TOPIC_PERM, ":topic." + TOPIC_FQTN, "sub"));
+        aafService.shouldRemovePerm(new DmaapPerm(TOPIC_PERM, ":topic." + TOPIC_FQTN, "view"));
+        aafService.shouldNotRemoveNamespace();
+    }
+
+    @Test
+    public void shouldRemoveOnlyPermissionsWhenTopicFqtnDoesntStartWithNsRoot() {
+
+        String topicFqtn = "sample_topic";
+        aafTopicSetupService.aafTopicCleanup(givenTopic(topicFqtn));
+
+        aafService.shouldRemovePerm(new DmaapPerm(TOPIC_PERM, ":topic." + topicFqtn, "pub"));
+        aafService.shouldRemovePerm(new DmaapPerm(TOPIC_PERM, ":topic." + topicFqtn, "sub"));
+        aafService.shouldRemovePerm(new DmaapPerm(TOPIC_PERM, ":topic." + topicFqtn, "view"));
+        aafService.shouldNotRemoveNamespace();
+    }
+
+    @Test
+    public void shouldHandleExceptionWhenPermissionRemovalWasFailed() {
+        aafService.givenRemovePermStatus(INTERNAL_SERVER_ERROR);
+
+        ApiError apiError = aafTopicSetupService.aafTopicCleanup(givenTopic(TOPIC_FQTN));
+
+        assertErrorStatus(apiError, INTERNAL_SERVER_ERROR);
+    }
+
+    @Test
+    public void shouldHandleExceptionWhenNamespaceRemovalWasFailed() {
+        aafService.givenRemoveNamespaceStatus(INTERNAL_SERVER_ERROR);
+
+        ApiError apiError = aafTopicSetupService.aafTopicCleanup(givenTopic(TOPIC_FQTN));
+
+        assertErrorStatus(apiError, INTERNAL_SERVER_ERROR);
+    }
+
+    @Test
+    public void shouldNotPerformCleanupWhenDeleteLevelIsLessThanTwo() {
+        given(dmaapConfig.getProperty("MR.ClientDeleteLevel", "0")).willReturn("0");
+
+        ApiError apiError = aafTopicSetupService.aafTopicCleanup(givenTopic(TOPIC_FQTN));
+
+        aafService.shouldNotPerformCleanup();
+        assertOkStatus(apiError);
+    }
+
+    @Test
+    public void shouldNotPerformCleanupWhenDeleteLevelIsNotNumericValue() {
+        given(dmaapConfig.getProperty("MR.ClientDeleteLevel", "0")).willReturn("not number");
+
+        ApiError apiError = aafTopicSetupService.aafTopicCleanup(givenTopic(TOPIC_FQTN));
+
+        aafService.shouldNotPerformCleanup();
+        assertOkStatus(apiError);
+    }
+
     private Topic givenTopic(String topicFqtn) {
         Topic topic = new Topic();
         topic.setFqtn(topicFqtn);
@@ -243,14 +336,18 @@ public class AafTopicSetupServiceTest {
 
     private class AafServiceStub implements AafService {
 
-        private AafNamespace namespace;
-        private List<DmaapPerm> perms = newArrayList();
-        private List<AafRole> roles = newArrayList();
-        private List<DmaapGrant> grants = newArrayList();
+        private AafNamespace addedNamespace;
+        private AafNamespace removedNamespace;
+        private List<DmaapPerm> addedPerms = newArrayList();
+        private List<DmaapPerm> removedPerms = newArrayList();
+        private List<AafRole> addedRoles = newArrayList();
+        private List<DmaapGrant> addedGrants = newArrayList();
         private int addNamespaceStatus = CREATED;
         private int addGrantStatus = CREATED;
         private int addRoleStatus = CREATED;
         private int addPermStatus = CREATED;
+        private int removePermStatus = OK;
+        private int removeNamespaceStatus = OK;
 
         @Override
         public String getIdentity() {
@@ -259,49 +356,62 @@ public class AafTopicSetupServiceTest {
 
         @Override
         public int addPerm(DmaapPerm perm) {
-            this.perms.add(perm);
+            this.addedPerms.add(perm);
             return addPermStatus;
         }
 
         @Override
-        public int addGrant(DmaapGrant grant) {
-            grants.add(grant);
-            return addGrantStatus;
+        public int delPerm(DmaapPerm perm, boolean force) {
+            removedPerms.add(perm);
+            return removePermStatus;
         }
 
         @Override
-        public int addUserRole(AafUserRole ur) {
-            throw new UnsupportedOperationException();
+        public int addGrant(DmaapGrant grant) {
+            addedGrants.add(grant);
+            return addGrantStatus;
         }
 
         @Override
-        public int delGrant(DmaapGrant grant) {
+        public int addUserRole(AafUserRole ur) {
             throw new UnsupportedOperationException();
         }
 
         @Override
         public int addRole(AafRole role) {
-            this.roles.add(role);
+            this.addedRoles.add(role);
             return addRoleStatus;
         }
 
         @Override
         public int addNamespace(AafNamespace namespace) {
-            this.namespace = namespace;
+            this.addedNamespace = namespace;
             return addNamespaceStatus;
         }
 
+        @Override
+        public int delNamespace(AafNamespace namespace, boolean force) {
+            this.removedNamespace = namespace;
+            return removeNamespaceStatus;
+        }
+
         void givenReturnCode(int status) {
             this.addNamespaceStatus = status;
             this.addGrantStatus = status;
             this.addRoleStatus = status;
             this.addPermStatus = status;
+            this.removePermStatus = status;
+            this.removeNamespaceStatus = status;
         }
 
         void givenAddNamespaceStatus(int addNamespaceStatus) {
             this.addNamespaceStatus = addNamespaceStatus;
         }
 
+        void givenRemoveNamespaceStatus(int removeNamespaceStatus) {
+            this.removeNamespaceStatus = removeNamespaceStatus;
+        }
+
         void givenAddGrantStatus(int addGrantStatus) {
             this.addGrantStatus = addGrantStatus;
         }
@@ -314,25 +424,47 @@ public class AafTopicSetupServiceTest {
             this.addPermStatus = addPermStatus;
         }
 
+        void givenRemovePermStatus(int removePermStatus) {
+            this.removePermStatus = removePermStatus;
+        }
+
         void shouldAddPerm(DmaapPerm perm) {
-            assertTrue(perms.contains(perm));
+            assertTrue(addedPerms.contains(perm));
+        }
+
+        void shouldRemovePerm(DmaapPerm perm) {
+            assertTrue(removedPerms.contains(perm));
         }
 
         void shouldAddNamespace(AafNamespace namespace) {
-            assertEquals(namespace, this.namespace);
+            assertEquals(namespace, this.addedNamespace);
+        }
+
+        void shouldRemoveNamespace(AafNamespace namespace) {
+            assertEquals(namespace, this.removedNamespace);
         }
 
         void shouldAddRole(AafRole role) {
-            assertTrue(roles.contains(role));
+            assertTrue(addedRoles.contains(role));
         }
 
         void shouldAddGrant(DmaapGrant grant) {
-            assertTrue(grants.contains(grant));
+            assertTrue(addedGrants.contains(grant));
+        }
+
+        void shouldHaveNoNamespaceRolesAndGrantsAdded() {
+            assertNull(this.addedNamespace);
+            assertTrue(this.addedGrants.isEmpty());
+            assertTrue(this.addedRoles.isEmpty());
+        }
+
+        void shouldNotRemoveNamespace() {
+            assertNull(this.removedNamespace);
         }
 
-        void shouldHaveNoRolesAndGrants() {
-            assertTrue(this.grants.isEmpty());
-            assertTrue(this.roles.isEmpty());
+        void shouldNotPerformCleanup() {
+            shouldNotRemoveNamespace();
+            assertTrue(removedPerms.isEmpty());
         }
     }
 }
\ No newline at end of file