From faddcb0a0242d5799bebca064bdc161d6f5b5590 Mon Sep 17 00:00:00 2001 From: pkjacob <121239864+pkjacob@users.noreply.github.com> Date: Tue, 10 Dec 2024 05:29:17 -0500 Subject: [PATCH] MODLD-613: Delete kafka topics after deleting tenant (#71) * MODLD-613: Delete kafka topics after deleting tenant * rename LinkedTenantService -> LinkedDataTenantService --------- Co-authored-by: PBobylev --- ...vice.java => LinkedDataTenantService.java} | 14 ++++++++++---- .../tenant/worker/KafkaAdminWorker.java | 5 +++++ .../tenant/worker/TenantServiceWorker.java | 4 ++++ ....java => LinkedDataTenantServiceTest.java} | 19 ++++++++++++++++--- .../tenant/worker/KafkaAdminWorkerTest.java | 12 ++++++++++++ 5 files changed, 47 insertions(+), 7 deletions(-) rename src/main/java/org/folio/linked/data/service/tenant/{LinkedTenantService.java => LinkedDataTenantService.java} (73%) rename src/test/java/org/folio/linked/data/service/tenant/{LinkedTenantServiceTest.java => LinkedDataTenantServiceTest.java} (81%) diff --git a/src/main/java/org/folio/linked/data/service/tenant/LinkedTenantService.java b/src/main/java/org/folio/linked/data/service/tenant/LinkedDataTenantService.java similarity index 73% rename from src/main/java/org/folio/linked/data/service/tenant/LinkedTenantService.java rename to src/main/java/org/folio/linked/data/service/tenant/LinkedDataTenantService.java index 269b589d..daf284d5 100644 --- a/src/main/java/org/folio/linked/data/service/tenant/LinkedTenantService.java +++ b/src/main/java/org/folio/linked/data/service/tenant/LinkedDataTenantService.java @@ -19,12 +19,12 @@ @Primary @Service @Profile("!" + STANDALONE_PROFILE) -public class LinkedTenantService extends TenantService { +public class LinkedDataTenantService extends TenantService { private final Collection workers; @Autowired - public LinkedTenantService( + public LinkedDataTenantService( JdbcTemplate jdbcTemplate, FolioExecutionContext context, FolioSpringLiquibase folioSpringLiquibase, @@ -36,13 +36,19 @@ public LinkedTenantService( @Override public void beforeTenantUpdate(TenantAttributes tenantAttributes) { - log.debug("Start before actions for the tenant [{}]", context.getTenantId()); + log.debug("Start before update actions for the tenant [{}]", context.getTenantId()); workers.forEach(worker -> worker.beforeTenantUpdate(context.getTenantId(), tenantAttributes)); } @Override public void afterTenantUpdate(TenantAttributes tenantAttributes) { - log.info("Start after actions for the tenant [{}]", context.getTenantId()); + log.info("Start after update actions for the tenant [{}]", context.getTenantId()); workers.forEach(worker -> worker.afterTenantUpdate(context.getTenantId(), tenantAttributes)); } + + @Override + public void afterTenantDeletion(TenantAttributes tenantAttributes) { + log.info("Start after delete actions for the tenant [{}]", context.getTenantId()); + workers.forEach(worker -> worker.afterTenantDeletion(context.getTenantId())); + } } diff --git a/src/main/java/org/folio/linked/data/service/tenant/worker/KafkaAdminWorker.java b/src/main/java/org/folio/linked/data/service/tenant/worker/KafkaAdminWorker.java index f3827e95..b5b27427 100644 --- a/src/main/java/org/folio/linked/data/service/tenant/worker/KafkaAdminWorker.java +++ b/src/main/java/org/folio/linked/data/service/tenant/worker/KafkaAdminWorker.java @@ -20,4 +20,9 @@ public void afterTenantUpdate(String tenantId, TenantAttributes tenantAttributes kafkaAdminService.createTopics(tenantId); kafkaAdminService.restartEventListeners(); } + + @Override + public void afterTenantDeletion(String tenantId) { + kafkaAdminService.deleteTopics(tenantId); + } } diff --git a/src/main/java/org/folio/linked/data/service/tenant/worker/TenantServiceWorker.java b/src/main/java/org/folio/linked/data/service/tenant/worker/TenantServiceWorker.java index e7dd7053..5afd054f 100644 --- a/src/main/java/org/folio/linked/data/service/tenant/worker/TenantServiceWorker.java +++ b/src/main/java/org/folio/linked/data/service/tenant/worker/TenantServiceWorker.java @@ -11,4 +11,8 @@ default void beforeTenantUpdate(String tenantId, TenantAttributes tenantAttribut default void afterTenantUpdate(String tenantId, TenantAttributes tenantAttributes) { // no action by default } + + default void afterTenantDeletion(String tenantId) { + // no action by default + } } diff --git a/src/test/java/org/folio/linked/data/service/tenant/LinkedTenantServiceTest.java b/src/test/java/org/folio/linked/data/service/tenant/LinkedDataTenantServiceTest.java similarity index 81% rename from src/test/java/org/folio/linked/data/service/tenant/LinkedTenantServiceTest.java rename to src/test/java/org/folio/linked/data/service/tenant/LinkedDataTenantServiceTest.java index 70f7f8ff..39ab55d0 100644 --- a/src/test/java/org/folio/linked/data/service/tenant/LinkedTenantServiceTest.java +++ b/src/test/java/org/folio/linked/data/service/tenant/LinkedDataTenantServiceTest.java @@ -19,7 +19,7 @@ @UnitTest @ExtendWith(MockitoExtension.class) -class LinkedTenantServiceTest { +class LinkedDataTenantServiceTest { @Mock private JdbcTemplate jdbcTemplate; @@ -30,12 +30,12 @@ class LinkedTenantServiceTest { @Mock private TenantServiceWorker testWorker; - private LinkedTenantService tenantService; + private LinkedDataTenantService tenantService; private final String tenantId = "tenant-01"; @BeforeEach void init() { - tenantService = new LinkedTenantService( + tenantService = new LinkedDataTenantService( jdbcTemplate, context, folioSpringLiquibase, @@ -69,4 +69,17 @@ void shouldCallWorker_afterTenantUpdate() { verify(testWorker) .afterTenantUpdate(tenantId, attributes); } + + @Test + void shouldCallWorker_afterTenantDeletion() { + //given + var attributes = mock(TenantAttributes.class); + + //when + tenantService.afterTenantDeletion(attributes); + + //then + verify(testWorker) + .afterTenantDeletion(tenantId); + } } diff --git a/src/test/java/org/folio/linked/data/service/tenant/worker/KafkaAdminWorkerTest.java b/src/test/java/org/folio/linked/data/service/tenant/worker/KafkaAdminWorkerTest.java index d34d0929..09b80f9c 100644 --- a/src/test/java/org/folio/linked/data/service/tenant/worker/KafkaAdminWorkerTest.java +++ b/src/test/java/org/folio/linked/data/service/tenant/worker/KafkaAdminWorkerTest.java @@ -34,4 +34,16 @@ void shouldCreateTopics() { verify(kafkaAdminService).createTopics(tenantId); verify(kafkaAdminService).restartEventListeners(); } + + @Test + void shouldDeleteTopics() { + // given + var tenantId = "tenant-01"; + + // when + kafkaAdminWorker.afterTenantDeletion(tenantId); + + // then + verify(kafkaAdminService).deleteTopics(tenantId); + } }