From 56e040334f4225439948fd08ca43bac9c5c9d280 Mon Sep 17 00:00:00 2001 From: azizbekxm Date: Tue, 19 Dec 2023 18:55:39 +0500 Subject: [PATCH] [MODCON-127] - Covered with Unit Tests --- .../service/impl/TenantServiceImpl.java | 8 +-- .../controller/TenantControllerTest.java | 19 +++--- .../controller/UserTenantControllerTest.java | 4 +- .../consortia/service/TenantServiceTest.java | 67 ++++++++++++------- .../folio/consortia/utils/EntityUtils.java | 17 +++++ 5 files changed, 78 insertions(+), 37 deletions(-) diff --git a/src/main/java/org/folio/consortia/service/impl/TenantServiceImpl.java b/src/main/java/org/folio/consortia/service/impl/TenantServiceImpl.java index 4b57c51f..d7599801 100644 --- a/src/main/java/org/folio/consortia/service/impl/TenantServiceImpl.java +++ b/src/main/java/org/folio/consortia/service/impl/TenantServiceImpl.java @@ -197,7 +197,7 @@ public void delete(UUID consortiumId, String tenantId) { throw new ResourceNotFoundException("id", tenantId); } if (tenant.get().getIsCentral()) { - throw new IllegalArgumentException(String.format("central tenant %s cannot be deleted", tenantId)); + throw new IllegalArgumentException(String.format("central tenant '%s' cannot be deleted", tenantId)); } var softDeletedTenant = tenant.get(); @@ -206,9 +206,9 @@ public void delete(UUID consortiumId, String tenantId) { cleanupService.clearPublicationTables(); tenantRepository.save(softDeletedTenant); -// try (var ignored = new FolioExecutionContextSetter(contextHelper.getSystemUserFolioExecutionContext(tenantId))) { -// userTenantsClient.deleteUserTenants(); -// } + try (var ignored = new FolioExecutionContextSetter(contextHelper.getSystemUserFolioExecutionContext(tenantId))) { + userTenantsClient.deleteUserTenants(); + } } private Tenant saveTenant(UUID consortiumId, Tenant tenantDto, SetupStatusEnum setupStatus) { diff --git a/src/test/java/org/folio/consortia/controller/TenantControllerTest.java b/src/test/java/org/folio/consortia/controller/TenantControllerTest.java index 443f3dd4..945ce452 100644 --- a/src/test/java/org/folio/consortia/controller/TenantControllerTest.java +++ b/src/test/java/org/folio/consortia/controller/TenantControllerTest.java @@ -379,20 +379,23 @@ void shouldThrowNotFoundErrorWhileUpdateTenant(String contentString) throws Exce jsonPath("$.errors[0].code", is("NOT_FOUND_ERROR"))); } - @ParameterizedTest - @ValueSource(strings = {TENANT_REQUEST_BODY}) - void shouldThrownHasActiveAffiliationExceptionWhileDeletingTenant(String contentString) throws Exception { + @Test + void shouldThrownExceptionWhenDeletingCentralTenant() throws Exception { var headers = defaultHeaders(); - when(tenantRepository.existsById(any())).thenReturn(true); + String tenantId = "diku"; + var centralTenant = createTenantEntity(tenantId); + centralTenant.setIsCentral(true); + + when(tenantRepository.findById(any())).thenReturn(Optional.of(centralTenant)); when(consortiumRepository.existsById(any())).thenReturn(true); - when(userTenantRepository.existsByTenantId("diku1234")).thenReturn(true); this.mockMvc.perform( - delete("/consortia/7698e46-c3e3-11ed-afa1-0242ac120002/tenants/diku1234") - .headers(headers).content(contentString)) + delete("/consortia/7698e46-c3e3-11ed-afa1-0242ac120002/tenants/diku") + .headers(headers)) .andExpectAll( status().is4xxClientError(), - jsonPath("$.errors[0].code", is("VALIDATION_ERROR"))); + jsonPath("$.errors[0].code", is("VALIDATION_ERROR")), + jsonPath("$.errors[0].message", is("central tenant 'diku' cannot be deleted"))); } @Test diff --git a/src/test/java/org/folio/consortia/controller/UserTenantControllerTest.java b/src/test/java/org/folio/consortia/controller/UserTenantControllerTest.java index 7a0420a3..8757ca2e 100644 --- a/src/test/java/org/folio/consortia/controller/UserTenantControllerTest.java +++ b/src/test/java/org/folio/consortia/controller/UserTenantControllerTest.java @@ -59,6 +59,7 @@ class UserTenantControllerTest extends BaseIT { private static final String PERMISSION_EXCEPTION_MSG = "[403 Forbidden] during [GET] to " + "[http://users/8c54ff1e-5954-4227-8402-9a5dd061a350] [UsersClient#getUserById(String)]: " + "[Access for user 'ss_admin' (b82b46b6-9a6e-46f0-b986-5c643d9ba036) requires permission: users.item.get]"; + @Mock private UserTenantService userTenantService; @InjectMocks @@ -122,7 +123,7 @@ void shouldGetUserTenantList() throws Exception { Page userTenantPage = new PageImpl<>(List.of(createUserTenantEntity(consortiumId))); when(consortiumRepository.existsById(consortiumId)).thenReturn(true); - when(userTenantRepository.findAll(PageRequest.of(1, 2))).thenReturn(userTenantPage); + when(userTenantRepository.getAll(PageRequest.of(1, 2))).thenReturn(userTenantPage); this.mockMvc.perform( get("/consortia/7698e46-c3e3-11ed-afa1-0242ac120002/user-tenants?limit=2&offset=1") @@ -223,6 +224,7 @@ private Response createForbiddenResponse(String message) { .request(request) .build(); } + private Response createUnknownResponse(String message) { Request request = Request.create(Request.HttpMethod.GET, "", Map.of(), null, Charset.defaultCharset(), new RequestTemplate()); diff --git a/src/test/java/org/folio/consortia/service/TenantServiceTest.java b/src/test/java/org/folio/consortia/service/TenantServiceTest.java index b576df5f..9432d577 100644 --- a/src/test/java/org/folio/consortia/service/TenantServiceTest.java +++ b/src/test/java/org/folio/consortia/service/TenantServiceTest.java @@ -18,6 +18,7 @@ import static org.mockito.Mockito.never; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoInteractions; import static org.mockito.Mockito.when; import java.util.ArrayList; @@ -28,6 +29,9 @@ import java.util.Optional; import java.util.UUID; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; + import org.folio.consortia.client.ConsortiaConfigurationClient; import org.folio.consortia.client.PermissionsClient; import org.folio.consortia.client.SyncPrimaryAffiliationClient; @@ -56,7 +60,6 @@ import org.junit.jupiter.api.Test; import org.mockito.InjectMocks; import org.mockito.Mock; -import org.mockito.Mockito; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration; import org.springframework.boot.autoconfigure.domain.EntityScan; @@ -65,9 +68,6 @@ import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.PageRequest; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; - @SpringBootTest @EnableAutoConfiguration(exclude = BatchAutoConfiguration.class) @EntityScan(basePackageClasses = TenantEntity.class) @@ -139,7 +139,7 @@ void shouldGetTenantList() { .thenReturn(new PageImpl<>(tenantEntityList, PageRequest.of(offset, limit), tenantEntityList.size())); var tenantCollection = tenantService.get(consortiumId, 0, 10); - Assertions.assertEquals(2, tenantCollection.getTotalRecords()); + assertEquals(2, tenantCollection.getTotalRecords()); } @Test @@ -156,7 +156,7 @@ void shouldGetAllTenantList() { when(tenantRepository.findByConsortiumId(consortiumId)).thenReturn(tenantEntityList); var allTenants = tenantService.getAll(consortiumId); - Assertions.assertEquals(2, allTenants.getTotalRecords()); + assertEquals(2, allTenants.getTotalRecords()); } @Test @@ -198,7 +198,7 @@ void shouldSaveNotCentralTenantWithNewUserAndPermissions() { verify(userService, times(1)).getByUsername(any()); verify(lockService).lockTenantSetupWithinTransaction(); - Assertions.assertEquals(tenant, tenant1); + assertEquals(tenant, tenant1); } @Test @@ -244,7 +244,7 @@ void shouldSaveCentralTenantWithExistingAndPermissions() throws JsonProcessingEx verify(userService, never()).createUser(any()); verify(permissionUserService, never()).createWithPermissionsFromFile(any(), any()); - Assertions.assertEquals(tenant, tenant1); + assertEquals(tenant, tenant1); } @@ -272,47 +272,66 @@ void shouldUpdateTenant() { void shouldDeleteTenant() { UUID consortiumId = UUID.randomUUID(); String tenantId = "diku"; + var tenant = createTenantEntity(tenantId); + var deletingTenant = createTenantEntity(tenantId); + deletingTenant.setIsDeleted(true); doNothing().when(consortiumService).checkConsortiumExistsOrThrow(consortiumId); - when(tenantRepository.existsById(any())).thenReturn(true); doNothing().when(cleanupService).clearPublicationTables(); - doNothing().when(tenantRepository).deleteById(tenantId); + doReturn(deletingTenant).when(tenantRepository).save(deletingTenant); + when(tenantRepository.findById(tenant.getId())).thenReturn(Optional.of(tenant)); + doReturn(folioExecutionContext).when(contextHelper).getSystemUserFolioExecutionContext(anyString()); + when(folioExecutionContext.getTenantId()).thenReturn("diku"); + Map> okapiHeaders = new HashMap<>(); + okapiHeaders.put(XOkapiHeaders.TENANT, List.of("diku")); + when(folioExecutionContext.getOkapiHeaders()).thenReturn(okapiHeaders); tenantService.delete(consortiumId, tenantId); // Assert - Mockito.verify(consortiumService).checkConsortiumExistsOrThrow(consortiumId); - Mockito.verify(tenantRepository).existsById(tenantId); - Mockito.verify(tenantRepository).deleteById(tenantId); + verify(consortiumService).checkConsortiumExistsOrThrow(consortiumId); + verify(tenantRepository).findById(tenantId); + verify(tenantRepository).save(deletingTenant); + verify(cleanupService).clearPublicationTables(); + verify(userTenantsClient).deleteUserTenants(); } - @Test() - void testDeleteWithAssociation() { + @Test + void testDeleteNonexistentTenant() { UUID consortiumId = UUID.randomUUID(); String tenantId = "123"; // Mock repository method calls - Mockito.when(tenantRepository.existsById(tenantId)).thenReturn(true); - Mockito.when(userTenantRepository.existsByTenantId(tenantId)).thenReturn(true); + when(tenantRepository.existsById(tenantId)).thenReturn(false); // Call the method - assertThrows(IllegalArgumentException.class, () -> + assertThrows(ResourceNotFoundException.class, () -> tenantService.delete(consortiumId, tenantId)); } @Test - void testDeleteNonexistentTenant() { + void shouldThrowErrorWhenDeletingCentralTenant(){ UUID consortiumId = UUID.randomUUID(); - String tenantId = "123"; + String tenantId = "college"; + var tenant = createTenantEntity(tenantId); + tenant.setIsCentral(true); + var deletingTenant = createTenantEntity(tenantId); + deletingTenant.setIsDeleted(true); - // Mock repository method calls - when(tenantRepository.existsById(tenantId)).thenReturn(false); + doNothing().when(consortiumService).checkConsortiumExistsOrThrow(consortiumId); + when(tenantRepository.findById(tenant.getId())).thenReturn(Optional.of(tenant)); - // Call the method - assertThrows(ResourceNotFoundException.class, () -> + // Assert + assertThrows(java.lang.IllegalArgumentException.class, () -> tenantService.delete(consortiumId, tenantId)); + + verify(consortiumService).checkConsortiumExistsOrThrow(consortiumId); + verify(tenantRepository).findById(tenantId); + verifyNoInteractions(cleanupService); + verifyNoInteractions(userTenantsClient); } + @Test void shouldThrowExceptionWhileSavingLocalTenantWithoutAdminUserId() { TenantDetailsEntity tenantDetailsEntity = createTenantDetailsEntity("TestID", "TestName1"); diff --git a/src/test/java/org/folio/consortia/utils/EntityUtils.java b/src/test/java/org/folio/consortia/utils/EntityUtils.java index 7ca5faeb..4071f3cb 100644 --- a/src/test/java/org/folio/consortia/utils/EntityUtils.java +++ b/src/test/java/org/folio/consortia/utils/EntityUtils.java @@ -74,6 +74,7 @@ public static TenantEntity createTenantEntity(String id, String name, String cod tenantEntity.setName(name); tenantEntity.setIsCentral(isCentral); tenantEntity.setConsortiumId(UUID.randomUUID()); + tenantEntity.setIsDeleted(false); return tenantEntity; } @@ -84,6 +85,7 @@ public static TenantEntity createTenantEntity() { tenantEntity.setName("testtenant1"); tenantEntity.setIsCentral(false); tenantEntity.setConsortiumId(UUID.randomUUID()); + tenantEntity.setIsDeleted(false); return tenantEntity; } @@ -93,6 +95,17 @@ public static TenantEntity createTenantEntity(String id, String name) { tenantEntity.setCode("ABC"); tenantEntity.setName(name); tenantEntity.setIsCentral(false); + tenantEntity.setIsDeleted(false); + return tenantEntity; + } + + public static TenantEntity createTenantEntity(String id) { + TenantEntity tenantEntity = new TenantEntity(); + tenantEntity.setId(id); + tenantEntity.setCode("ABC"); + tenantEntity.setName(id); + tenantEntity.setIsCentral(false); + tenantEntity.setIsDeleted(false); return tenantEntity; } @@ -102,6 +115,7 @@ public static TenantDetailsEntity createTenantDetailsEntity() { tenantDetailsEntity.setCode("ABC"); tenantDetailsEntity.setName("testtenant1"); tenantDetailsEntity.setIsCentral(false); + tenantDetailsEntity.setIsDeleted(false); tenantDetailsEntity.setConsortiumId(UUID.randomUUID()); tenantDetailsEntity.setSetupStatus(SetupStatusEnum.COMPLETED); return tenantDetailsEntity; @@ -113,6 +127,7 @@ public static TenantDetailsEntity createTenantDetailsEntity(String id, String na tenantDetailsEntity.setCode("ABC"); tenantDetailsEntity.setName(name); tenantDetailsEntity.setIsCentral(false); + tenantDetailsEntity.setIsDeleted(false); tenantDetailsEntity.setSetupStatus(SetupStatusEnum.IN_PROGRESS); return tenantDetailsEntity; } @@ -123,6 +138,7 @@ public static Tenant createTenant(String id, String name) { tenant.setName(name); tenant.setIsCentral(false); tenant.setCode("ABC"); + tenant.setIsDeleted(false); return tenant; } @@ -131,6 +147,7 @@ public static Tenant createTenant(String id, String name, boolean isCentral) { tenant.setId(id); tenant.setName(name); tenant.setIsCentral(isCentral); + tenant.setIsDeleted(false); tenant.setCode("ABC"); return tenant; }