Aaf cleanup in TopicService implementation

Change-Id: Ica0ed712223f246a6134c03ca12f3de05a3e01ff
Issue-ID: DMAAP-1217
Signed-off-by: piotr.karas <piotr.karas@nokia.com>
diff --git a/src/main/java/org/onap/dmaap/dbcapi/service/AafTopicSetupService.java b/src/main/java/org/onap/dmaap/dbcapi/service/AafTopicSetupService.java
index 031f594..a1fc89e 100644
--- a/src/main/java/org/onap/dmaap/dbcapi/service/AafTopicSetupService.java
+++ b/src/main/java/org/onap/dmaap/dbcapi/service/AafTopicSetupService.java
@@ -77,6 +77,10 @@
         return okStatus();
     }
 
+    ApiError aafTopicCleanup(Topic topic) {
+        return okStatus();
+    }
+
     private String getTopicsNsRoot() throws TopicSetupException {
         String nsr = dmaapService.getDmaap().getTopicNsRoot();
         if (nsr == null) {
diff --git a/src/main/java/org/onap/dmaap/dbcapi/service/TopicService.java b/src/main/java/org/onap/dmaap/dbcapi/service/TopicService.java
index 3386b97..2065754 100644
--- a/src/main/java/org/onap/dmaap/dbcapi/service/TopicService.java
+++ b/src/main/java/org/onap/dmaap/dbcapi/service/TopicService.java
@@ -246,11 +246,16 @@
             apiError.setFields("fqtn");
             return null;
         }
+
+        ApiError topicSetupError = aafTopicSetupService.aafTopicCleanup(topic);
+        updateApiError(apiError, topicSetupError);
+        if (apiError.getCode() >= 400) {
+            return null;
+        }
+
         ArrayList<MR_Client> clients = new ArrayList<MR_Client>(clientService.getAllMrClients(pubId));
         for (Iterator<MR_Client> it = clients.iterator(); it.hasNext(); ) {
             MR_Client c = it.next();
-
-
             clientService.removeMr_Client(c.getMrClientId(), false, apiError);
             if (!apiError.is2xx()) {
                 return null;
diff --git a/src/test/java/org/onap/dmaap/dbcapi/service/TopicServiceTest.java b/src/test/java/org/onap/dmaap/dbcapi/service/TopicServiceTest.java
index f73c297..a37ce02 100644
--- a/src/test/java/org/onap/dmaap/dbcapi/service/TopicServiceTest.java
+++ b/src/test/java/org/onap/dmaap/dbcapi/service/TopicServiceTest.java
@@ -42,12 +42,16 @@
 
 import static com.google.common.collect.Iterables.getOnlyElement;
 import static com.google.common.collect.Lists.newArrayList;
+import static javax.ws.rs.core.Response.Status.NOT_FOUND;
+import static javax.ws.rs.core.Response.Status.OK;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertSame;
 import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
 import static org.mockito.BDDMockito.given;
+import static org.mockito.BDDMockito.then;
 import static org.mockito.Matchers.any;
 import static org.mockito.Mockito.verifyZeroInteractions;
 import static org.onap.dmaap.dbcapi.model.ReplicationType.REPLICATION_GLOBAL_TO_FQDN;
@@ -75,6 +79,7 @@
     public void setUp() throws Exception {
         given(dmaapConfig.getProperty("MR.globalHost", "global.host.not.set")).willReturn(GLOBAL_MR_HOST);
         given(aafTopicSetupService.aafTopicSetup(any(Topic.class))).willReturn(new ApiError(200, "OK"));
+        given(aafTopicSetupService.aafTopicCleanup(any(Topic.class))).willReturn(new ApiError(200, "OK"));
         createTopicService();
     }
 
@@ -129,7 +134,7 @@
         Topic topic = topicService.getTopic(TOPIC_FQTN, apiError);
 
         assertThat(topic, hasCorrectFqtn(TOPIC_FQTN));
-        assertEquals(Response.Status.OK.getStatusCode(), apiError.getCode());
+        assertEquals(OK.getStatusCode(), apiError.getCode());
     }
 
     @Test
@@ -151,7 +156,7 @@
         Topic topic = topicService.getTopic("not_existing", apiError);
 
         assertNull(topic);
-        assertEquals(Response.Status.NOT_FOUND.getStatusCode(), apiError.getCode());
+        assertEquals(NOT_FOUND.getStatusCode(), apiError.getCode());
     }
 
     @Test
@@ -162,7 +167,7 @@
         Topic addedTopic = topicService.addTopic(newTopic, apiError, true);
 
         assertSame(newTopic, addedTopic);
-        assertEquals(Response.Status.OK.getStatusCode(), apiError.getCode());
+        assertEquals(OK.getStatusCode(), apiError.getCode());
         assertNotNull(topicService.getTopic(addedTopic.getFqtn(), new ApiError()));
     }
 
@@ -187,7 +192,7 @@
         Topic secondAddedTopic = topicService.addTopic(addedTopic, apiError, true);
 
         assertSame(addedTopic, secondAddedTopic);
-        assertEquals(Response.Status.OK.getStatusCode(), apiError.getCode());
+        assertEquals(OK.getStatusCode(), apiError.getCode());
         assertNotNull(topicService.getTopic(secondAddedTopic.getFqtn(), new ApiError()));
     }
 
@@ -200,7 +205,7 @@
         ApiError apiError = new ApiError();
         Topic addedTopic = topicService.addTopic(newTopic, apiError, true);
 
-        assertEquals(Response.Status.OK.getStatusCode(), apiError.getCode());
+        assertEquals(OK.getStatusCode(), apiError.getCode());
         assertEquals(GLOBAL_MR_HOST, addedTopic.getGlobalMrURL());
     }
 
@@ -218,6 +223,63 @@
         assertNull(addedTopic);
     }
 
+    @Test
+    public void removeTopic_shouldFailIfTopicDoesNotExist() {
+        ApiError apiError = new ApiError();
+
+        Topic removedTopic = topicService.removeTopic("not_existing_fqtn", apiError);
+
+        assertNull(removedTopic);
+        assertEquals(NOT_FOUND.getStatusCode(), apiError.getCode());
+        assertTrue(topicService.getTopics().containsKey(TOPIC_FQTN));
+    }
+
+    @Test
+    public void removeTopic_shouldExecuteAafCleanup() {
+        ApiError apiError = new ApiError();
+
+        Topic removedTopic = topicService.removeTopic(TOPIC_FQTN, apiError);
+
+        then(aafTopicSetupService).should().aafTopicCleanup(removedTopic);
+        assertEquals(OK.getStatusCode(), apiError.getCode());
+    }
+
+    @Test
+    public void removeTopic_shouldRemoveEachMrClientAssignedToTopic() {
+        ApiError apiError = new ApiError();
+        MR_Client mrClient = new MR_Client();
+        mrClient.setMrClientId("mrClientId");
+
+        given(clientService.getAllMrClients(TOPIC_FQTN)).willReturn(newArrayList(mrClient));
+
+        topicService.removeTopic(TOPIC_FQTN, apiError);
+
+        then(clientService).should().removeMr_Client(mrClient.getMrClientId(), false, apiError);
+        assertEquals(OK.getStatusCode(), apiError.getCode());
+    }
+
+    @Test
+    public void removeTopic_shouldRemoveTopicFromCache() {
+        ApiError apiError = new ApiError();
+
+        topicService.removeTopic(TOPIC_FQTN, apiError);
+
+        assertTrue(topicService.getTopics().isEmpty());
+        assertEquals(OK.getStatusCode(), apiError.getCode());
+    }
+
+    @Test
+    public void removeTopic_shouldFailIfAafCleanupWasFailed() {
+        ApiError apiError = new ApiError();
+        given(aafTopicSetupService.aafTopicCleanup(any(Topic.class))).willReturn(new ApiError(404, "sth went wrong"));
+
+        Topic removedTopic = topicService.removeTopic(TOPIC_FQTN, apiError);
+
+        assertNull(removedTopic);
+        assertEquals(404, apiError.getCode());
+        assertTrue(topicService.getTopics().containsKey(TOPIC_FQTN));
+    }
+
     private void createTopicService() {
         Map<String, Topic> mrTopics = new HashMap<>();
         mrTopics.put(TOPIC_FQTN, createTopic(TOPIC_FQTN));