diff options
Diffstat (limited to 'cps-ri/src')
5 files changed, 53 insertions, 8 deletions
diff --git a/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsAdminPersistenceServiceImpl.java b/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsAdminPersistenceServiceImpl.java index b1bd03c255..9c69006ece 100755 --- a/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsAdminPersistenceServiceImpl.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsAdminPersistenceServiceImpl.java @@ -32,6 +32,7 @@ import org.onap.cps.spi.entities.AnchorEntity; import org.onap.cps.spi.entities.DataspaceEntity; import org.onap.cps.spi.entities.YangResourceModuleReference; import org.onap.cps.spi.exceptions.AlreadyDefinedException; +import org.onap.cps.spi.exceptions.DataspaceInUseException; import org.onap.cps.spi.exceptions.ModuleNamesNotFoundException; import org.onap.cps.spi.model.Anchor; import org.onap.cps.spi.repository.AnchorRepository; @@ -71,6 +72,22 @@ public class CpsAdminPersistenceServiceImpl implements CpsAdminPersistenceServic } @Override + public void deleteDataspace(final String dataspaceName) { + final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName); + final int numberOfAssociatedAnchors = anchorRepository.countByDataspace(dataspaceEntity); + if (numberOfAssociatedAnchors != 0) { + throw new DataspaceInUseException(dataspaceName, + String.format("Dataspace contains %d anchor(s)", numberOfAssociatedAnchors)); + } + final int numberOfAssociatedSchemaSets = schemaSetRepository.countByDataspace(dataspaceEntity); + if (numberOfAssociatedSchemaSets != 0) { + throw new DataspaceInUseException(dataspaceName, + String.format("Dataspace contains %d schemaset(s)", numberOfAssociatedSchemaSets)); + } + dataspaceRepository.delete(dataspaceEntity); + } + + @Override public void createAnchor(final String dataspaceName, final String schemaSetName, final String anchorName) { final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName); final var schemaSetEntity = diff --git a/cps-ri/src/main/java/org/onap/cps/spi/repository/AnchorRepository.java b/cps-ri/src/main/java/org/onap/cps/spi/repository/AnchorRepository.java index 5870fd9e9b..471f1758cd 100755 --- a/cps-ri/src/main/java/org/onap/cps/spi/repository/AnchorRepository.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/repository/AnchorRepository.java @@ -45,6 +45,8 @@ public interface AnchorRepository extends JpaRepository<AnchorEntity, Integer> { Collection<AnchorEntity> findAllBySchemaSet(@NotNull SchemaSetEntity schemaSetEntity); + Integer countByDataspace(@NotNull DataspaceEntity dataspaceEntity); + @Query(value = "SELECT anchor.* FROM yang_resource\n" + "JOIN schema_set_yang_resources ON schema_set_yang_resources.yang_resource_id = yang_resource.id\n" + "JOIN schema_set ON schema_set.id = schema_set_yang_resources.schema_set_id\n" diff --git a/cps-ri/src/main/java/org/onap/cps/spi/repository/SchemaSetRepository.java b/cps-ri/src/main/java/org/onap/cps/spi/repository/SchemaSetRepository.java index 7b56f9323d..a15ce622c2 100644 --- a/cps-ri/src/main/java/org/onap/cps/spi/repository/SchemaSetRepository.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/repository/SchemaSetRepository.java @@ -19,7 +19,6 @@ package org.onap.cps.spi.repository; -import java.util.List; import java.util.Optional; import javax.validation.constraints.NotNull; import org.onap.cps.spi.entities.DataspaceEntity; @@ -31,11 +30,11 @@ import org.springframework.stereotype.Repository; @Repository public interface SchemaSetRepository extends JpaRepository<SchemaSetEntity, Integer> { - List<SchemaSetEntity> findAllByDataspace(@NotNull DataspaceEntity dataspaceEntity); - Optional<SchemaSetEntity> findByDataspaceAndName(@NotNull DataspaceEntity dataspaceEntity, @NotNull String schemaSetName); + Integer countByDataspace(@NotNull DataspaceEntity dataspaceEntity); + /** * Gets a schema set by dataspace and schema set name. * diff --git a/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsAdminPersistenceServiceSpec.groovy b/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsAdminPersistenceServiceSpec.groovy index a0df2b169d..4b5b116f48 100644 --- a/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsAdminPersistenceServiceSpec.groovy +++ b/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsAdminPersistenceServiceSpec.groovy @@ -24,6 +24,7 @@ package org.onap.cps.spi.impl import org.onap.cps.spi.CpsAdminPersistenceService import org.onap.cps.spi.exceptions.AlreadyDefinedException import org.onap.cps.spi.exceptions.AnchorNotFoundException +import org.onap.cps.spi.exceptions.DataspaceInUseException import org.onap.cps.spi.exceptions.DataspaceNotFoundException import org.onap.cps.spi.exceptions.SchemaSetNotFoundException import org.onap.cps.spi.exceptions.ModuleNamesNotFoundException @@ -36,10 +37,9 @@ class CpsAdminPersistenceServiceSpec extends CpsPersistenceSpecBase { @Autowired CpsAdminPersistenceService objectUnderTest - static final String SET_DATA = '/data/anchor.sql' static final String SAMPLE_DATA_FOR_ANCHORS_WITH_MODULES = '/data/anchors-schemaset-modules.sql' - static final String EMPTY_DATASPACE_NAME = 'DATASPACE-002' + static final String DATASPACE_WITH_NO_DATA = 'DATASPACE-002' static final Integer DELETED_ANCHOR_ID = 3001 static final Long DELETED_FRAGMENT_ID = 4001 @@ -111,7 +111,7 @@ class CpsAdminPersistenceServiceSpec extends CpsPersistenceSpecBase { dataspaceName || expectedAnchors DATASPACE_NAME || [Anchor.builder().name(ANCHOR_NAME1).schemaSetName(SCHEMA_SET_NAME1).dataspaceName(DATASPACE_NAME).build(), Anchor.builder().name(ANCHOR_NAME2).schemaSetName(SCHEMA_SET_NAME2).dataspaceName(DATASPACE_NAME).build()] - EMPTY_DATASPACE_NAME || [] + DATASPACE_WITH_NO_DATA || [] } @Sql(CLEAR_DATA) @@ -173,4 +173,27 @@ class CpsAdminPersistenceServiceSpec extends CpsPersistenceSpecBase { def buildAnchor(def anchorName, def dataspaceName, def SchemaSetName) { return Anchor.builder().name(anchorName).dataspaceName(dataspaceName).schemaSetName(SchemaSetName).build() } + + @Sql([CLEAR_DATA, SET_DATA]) + def 'Delete dataspace.'() { + when: 'delete dataspace action is invoked' + objectUnderTest.deleteDataspace(DATASPACE_WITH_NO_DATA) + then: 'dataspace is deleted' + assert dataspaceRepository.findByName(DATASPACE_WITH_NO_DATA).isEmpty(); + } + + @Sql([CLEAR_DATA, SET_DATA]) + def 'Delete dataspace when #scenario.'() { + when: 'delete dataspace action is invoked' + objectUnderTest.deleteDataspace(dataspaceName) + then: 'the correct exception is thrown with the relevant details' + def thrownException = thrown(expectedException) + thrownException.details.contains(expectedMessageDetails) + where: 'the following data is used' + scenario | dataspaceName || expectedException | expectedMessageDetails + 'dataspace name does not exist' | 'unknown' || DataspaceNotFoundException | 'unknown does not exist' + 'dataspace contains an anchor' | 'DATASPACE-001' || DataspaceInUseException | 'contains 2 anchor(s)' + 'dataspace contains schemasets' | 'DATASPACE-003' || DataspaceInUseException | 'contains 1 schemaset(s)' + } + } diff --git a/cps-ri/src/test/resources/data/anchor.sql b/cps-ri/src/test/resources/data/anchor.sql index dbf1a6a47d..c9240f7fd2 100644 --- a/cps-ri/src/test/resources/data/anchor.sql +++ b/cps-ri/src/test/resources/data/anchor.sql @@ -21,10 +21,14 @@ */ INSERT INTO DATASPACE (ID, NAME) VALUES - (1001, 'DATASPACE-001'), (1002, 'DATASPACE-002'); + (1001, 'DATASPACE-001'), + (1002, 'DATASPACE-002'), + (1003, 'DATASPACE-003'); INSERT INTO SCHEMA_SET (ID, NAME, DATASPACE_ID) VALUES - (2001, 'SCHEMA-SET-001', 1001), (2002, 'SCHEMA-SET-002', 1001); + (2001, 'SCHEMA-SET-001', 1001), + (2002, 'SCHEMA-SET-002', 1001), + (2003, 'SCHEMA-SET-002', 1003); INSERT INTO ANCHOR (ID, NAME, DATASPACE_ID, SCHEMA_SET_ID) VALUES (3001, 'ANCHOR-001', 1001, 2001), |