diff --git a/admin/kernel-masterdata-service/src/main/java/io/mosip/kernel/masterdata/constant/SchemaErrorCode.java b/admin/kernel-masterdata-service/src/main/java/io/mosip/kernel/masterdata/constant/SchemaErrorCode.java index 758761105b2..020e367c627 100644 --- a/admin/kernel-masterdata-service/src/main/java/io/mosip/kernel/masterdata/constant/SchemaErrorCode.java +++ b/admin/kernel-masterdata-service/src/main/java/io/mosip/kernel/masterdata/constant/SchemaErrorCode.java @@ -32,6 +32,9 @@ public enum SchemaErrorCode { SUB_TYPE_REQUIRED_EXCEPTION("KER-SCH-017", "SubType is required for field %s"), BIO_ATTRIBUTES_REQUIRED_EXCEPTION("KER-SCH-018", "BioAttributes are required for field %s"), BIO_ATTRIBUTES_DUPLICATED_EXCEPTION("KER-SCH-019", "Same BioAttributes used in field with same SubType : %s"), + + PUBLISHED_SCHEMA_EXCEPTION("KER-SCH-022", "Published identity schema can't be deleted"), + DYNAMIC_FIELD_VALUE_JSON_INVALID("KER-DYN-001", "Dynamic field is invalid, must contain code and value keys"); private final String errorCode; diff --git a/admin/kernel-masterdata-service/src/main/java/io/mosip/kernel/masterdata/service/impl/IdentitySchemaServiceImpl.java b/admin/kernel-masterdata-service/src/main/java/io/mosip/kernel/masterdata/service/impl/IdentitySchemaServiceImpl.java index d5cc8992eca..a656c2fb050 100644 --- a/admin/kernel-masterdata-service/src/main/java/io/mosip/kernel/masterdata/service/impl/IdentitySchemaServiceImpl.java +++ b/admin/kernel-masterdata-service/src/main/java/io/mosip/kernel/masterdata/service/impl/IdentitySchemaServiceImpl.java @@ -276,14 +276,16 @@ public IdSchemaResponseDto updateSchema(String id, IdentitySchemaDto dto) { @Transactional public String deleteSchema(String id) { try { - int updatedRows = identitySchemaRepository.deleteIdentitySchema(id, MetaDataUtils.getCurrentDateTime(), - MetaDataUtils.getContextUser()); - - if (updatedRows < 1) { + IdentitySchema entity = identitySchemaRepository.findIdentitySchemaById(id); + if (entity == null) { throw new RequestException(SchemaErrorCode.SCHEMA_NOT_FOUND_EXCEPTION.getErrorCode(), SchemaErrorCode.SCHEMA_NOT_FOUND_EXCEPTION.getErrorMessage()); + } else if (STATUS_PUBLISHED.equalsIgnoreCase(entity.getStatus())) { + throw new RequestException(SchemaErrorCode.PUBLISHED_SCHEMA_EXCEPTION.getErrorCode(), + SchemaErrorCode.PUBLISHED_SCHEMA_EXCEPTION.getErrorMessage()); } - + identitySchemaRepository.deleteIdentitySchema(id, MetaDataUtils.getCurrentDateTime(), + MetaDataUtils.getContextUser()); } catch (DataAccessException | DataAccessLayerException e) { LOGGER.error("Error while deleting identity schema : " , ExceptionUtils.neutralizeParam(id), e); throw new MasterDataServiceException(SchemaErrorCode.SCHEMA_UPDATE_EXCEPTION.getErrorCode(), diff --git a/admin/kernel-masterdata-service/src/test/java/io/mosip/kernel/masterdata/test/service/SchemaServiceTest.java b/admin/kernel-masterdata-service/src/test/java/io/mosip/kernel/masterdata/test/service/SchemaServiceTest.java index 0c60f6508bc..5ebbf39c5c9 100644 --- a/admin/kernel-masterdata-service/src/test/java/io/mosip/kernel/masterdata/test/service/SchemaServiceTest.java +++ b/admin/kernel-masterdata-service/src/test/java/io/mosip/kernel/masterdata/test/service/SchemaServiceTest.java @@ -306,15 +306,15 @@ public void testPublishIdentitySchemaWithException() throws Exception { @Test @WithUserDetails("global-admin") - public void testDeleteIdentitySchema() throws Exception { - Mockito.when(identitySchemaRepository.deleteIdentitySchema(Mockito.anyString(), Mockito.any(LocalDateTime.class), - Mockito.anyString())).thenReturn(1); - identitySchemaService.deleteSchema("test-test"); + public void deleteIdentitySchema_withValidId_recordDeleted() throws Exception { + String id="123456789"; + Mockito.when(identitySchemaRepository.findIdentitySchemaById(id)).thenReturn(draftSchema); + assertEquals(id,identitySchemaService.deleteSchema(id)); } @Test(expected = RequestException.class) @WithUserDetails("global-admin") - public void testDeleteIdentitySchemaFailed() throws Exception { + public void deleteIdentitySchema_invalidId_failedToDelete() throws Exception { Mockito.when(identitySchemaRepository.deleteIdentitySchema(Mockito.anyString(), Mockito.any(LocalDateTime.class), Mockito.anyString())).thenReturn(0); identitySchemaService.deleteSchema("test-test"); @@ -322,9 +322,17 @@ public void testDeleteIdentitySchemaFailed() throws Exception { @Test(expected = MasterDataServiceException.class) @WithUserDetails("global-admin") - public void testDeleteIdentitySchemaFailedUpdate() throws Exception { - Mockito.when(identitySchemaRepository.deleteIdentitySchema(Mockito.anyString(), Mockito.any(LocalDateTime.class), - Mockito.anyString())).thenThrow(DataAccessLayerException.class); + public void deleteIdentitySchema_withDbException_failedToDelete() throws Exception { + Mockito.when(identitySchemaRepository.findIdentitySchemaById( + Mockito.anyString())).thenThrow(DataAccessLayerException.class); + identitySchemaService.deleteSchema("test-test"); + } + + @Test(expected = RequestException.class) + @WithUserDetails("global-admin") + public void deleteIdentitySchema_publishedSchema_Failed() throws Exception { + Mockito.when(identitySchemaRepository.findIdentitySchemaById( + Mockito.anyString())).thenReturn(publishedSchema); identitySchemaService.deleteSchema("test-test"); }