Delete schema set - persistence layer

Issue-ID: CPS-121
Change-Id: I6fc8343969971b76d7f78ad202dd8ec1058c03fb
Signed-off-by: Ruslan Kashapov <ruslan.kashapov@pantheon.tech>
diff --git a/cps-ri/src/test/java/org/onap/cps/spi/impl/CpsModulePersistenceServiceTest.java b/cps-ri/src/test/java/org/onap/cps/spi/impl/CpsModulePersistenceServiceTest.java
index 8455e57..050ee26 100755
--- a/cps-ri/src/test/java/org/onap/cps/spi/impl/CpsModulePersistenceServiceTest.java
+++ b/cps-ri/src/test/java/org/onap/cps/spi/impl/CpsModulePersistenceServiceTest.java
@@ -20,7 +20,11 @@
 package org.onap.cps.spi.impl;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_ALLOWED;
+import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_PROHIBITED;
 
 import com.google.common.collect.ImmutableMap;
 import java.util.Map;
@@ -36,8 +40,13 @@
 import org.onap.cps.spi.entities.YangResourceEntity;
 import org.onap.cps.spi.exceptions.DataspaceNotFoundException;
 import org.onap.cps.spi.exceptions.SchemaSetAlreadyDefinedException;
+import org.onap.cps.spi.exceptions.SchemaSetInUseException;
+import org.onap.cps.spi.exceptions.SchemaSetNotFoundException;
+import org.onap.cps.spi.repository.AnchorRepository;
 import org.onap.cps.spi.repository.DataspaceRepository;
+import org.onap.cps.spi.repository.FragmentRepository;
 import org.onap.cps.spi.repository.SchemaSetRepository;
+import org.onap.cps.spi.repository.YangResourceRepository;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.test.context.SpringBootTest;
 import org.springframework.test.context.jdbc.Sql;
@@ -57,6 +66,8 @@
     private static final String DATASPACE_NAME_INVALID = "DATASPACE-X";
     private static final String SCHEMA_SET_NAME = "SCHEMA-SET-001";
     private static final String SCHEMA_SET_NAME_NEW = "SCHEMA-SET-NEW";
+    private static final String SCHEMA_SET_NAME_NO_ANCHORS = "SCHEMA-SET-100";
+    private static final String SCHEMA_SET_NAME_WITH_ANCHORS_AND_DATA = "SCHEMA-SET-101";
 
     private static final String EXISTING_RESOURCE_NAME = "module1@2020-02-02.yang";
     private static final String EXISTING_RESOURCE_CONTENT = "CONTENT-001";
@@ -68,6 +79,13 @@
     private static final String NEW_RESOURCE_CHECKSUM = "c94d40a1350eb1c0b1c1949eac84fc59";
     private static final Long NEW_RESOURCE_ABSTRACT_ID = 0L;
 
+    private static final Long SHARED_RESOURCE_ID1 = 3003L;
+    private static final Long SHARED_RESOURCE_ID2 = 3004L;
+    private static final Long ORPHAN_RESOURCE_ID = 3100L;
+    private static final Integer REMOVED_ANCHOR_ID1 = 6001;
+    private static final Integer REMOVED_ANCHOR_ID2 = 6002;
+    private static final Long REMOVED_FRAGMENT_ID = 7001L;
+
     @ClassRule
     public static DatabaseTestContainer testContainer = DatabaseTestContainer.getInstance();
 
@@ -78,11 +96,19 @@
     private CpsAdminPersistenceService cpsAdminPersistenceService;
 
     @Autowired
-    private DataspaceRepository dataspaceRepository;
+    DataspaceRepository dataspaceRepository;
 
     @Autowired
     private SchemaSetRepository schemaSetRepository;
 
+    @Autowired
+    private YangResourceRepository yangResourceRepository;
+
+    @Autowired
+    private AnchorRepository anchorRepository;
+
+    @Autowired
+    private FragmentRepository fragmentRepository;
 
     @Test(expected = DataspaceNotFoundException.class)
     @Sql(CLEAR_DATA)
@@ -92,14 +118,14 @@
     }
 
     @Test(expected = SchemaSetAlreadyDefinedException.class)
-    @SqlGroup({@Sql(CLEAR_DATA), @Sql(SET_DATA)})
+    @Sql({CLEAR_DATA, SET_DATA})
     public void testStoreDuplicateSchemaSet() {
         cpsModulePersistenceService.storeSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME,
             toMap(NEW_RESOURCE_NAME, NEW_RESOURCE_CONTENT));
     }
 
     @Test
-    @SqlGroup({@Sql(CLEAR_DATA), @Sql(SET_DATA)})
+    @Sql({CLEAR_DATA, SET_DATA})
     public void testStoreSchemaSetWithNewYangResource() {
         final Map<String, String> yangResourcesNameToContentMap = toMap(NEW_RESOURCE_NAME, NEW_RESOURCE_CONTENT);
         cpsModulePersistenceService.storeSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_NEW,
@@ -107,11 +133,11 @@
         assertSchemaSetPersisted(DATASPACE_NAME, SCHEMA_SET_NAME_NEW,
             NEW_RESOURCE_ABSTRACT_ID, NEW_RESOURCE_NAME, NEW_RESOURCE_CONTENT, NEW_RESOURCE_CHECKSUM);
         assertEquals(yangResourcesNameToContentMap,
-                cpsModulePersistenceService.getYangSchemaResources(DATASPACE_NAME, SCHEMA_SET_NAME_NEW));
+            cpsModulePersistenceService.getYangSchemaResources(DATASPACE_NAME, SCHEMA_SET_NAME_NEW));
     }
 
     @Test
-    @SqlGroup({@Sql(CLEAR_DATA), @Sql(SET_DATA)})
+    @Sql({CLEAR_DATA, SET_DATA})
     public void testGetYangResourcesWithAnchorName() {
         final Map<String, String> yangResourcesNameToContentMap =
             toMap(NEW_RESOURCE_NAME, NEW_RESOURCE_CONTENT);
@@ -124,7 +150,7 @@
     }
 
     @Test
-    @SqlGroup({@Sql(CLEAR_DATA), @Sql(SET_DATA)})
+    @Sql({CLEAR_DATA, SET_DATA})
     public void testStoreSchemaSetWithExistingYangResourceReuse() {
         cpsModulePersistenceService.storeSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_NEW,
             toMap(NEW_RESOURCE_NAME, EXISTING_RESOURCE_CONTENT));
@@ -133,9 +159,9 @@
     }
 
     private void assertSchemaSetPersisted(final String expectedDataspaceName, final String expectedSchemaSetName,
-                                          final Long expectedYangResourceId, final String expectedYangResourceName,
-                                          final String expectedYangResourceContent,
-                                          final String expectedYangResourceChecksum) {
+        final Long expectedYangResourceId, final String expectedYangResourceName,
+        final String expectedYangResourceContent,
+        final String expectedYangResourceChecksum) {
 
         // assert the schema set is persisted
         final SchemaSetEntity schemaSetEntity = getSchemaSetFromDatabase(expectedDataspaceName, expectedSchemaSetName);
@@ -159,6 +185,63 @@
         assertEquals(expectedYangResourceChecksum, yangResourceEntity.getChecksum());
     }
 
+    @Test
+    @Sql({CLEAR_DATA, SET_DATA})
+    public void testStrictDeleteSchemaSetNoAnchors() {
+        cpsModulePersistenceService.deleteSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_NO_ANCHORS,
+            CASCADE_DELETE_PROHIBITED);
+
+        // validate schema set removed
+        final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(DATASPACE_NAME);
+        assertFalse(schemaSetRepository
+            .findByDataspaceAndName(dataspaceEntity, SCHEMA_SET_NAME_NO_ANCHORS).isPresent());
+
+        // validate shared resource remain, but orphan one is removed
+        assertTrue(yangResourceRepository.findById(SHARED_RESOURCE_ID1).isPresent());
+        assertFalse(yangResourceRepository.findById(ORPHAN_RESOURCE_ID).isPresent());
+    }
+
+
+    @Test
+    @Sql({CLEAR_DATA, SET_DATA})
+    public void testFullDeleteSchemaSetWithAnchorsAndData() {
+        cpsModulePersistenceService
+            .deleteSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_WITH_ANCHORS_AND_DATA, CASCADE_DELETE_ALLOWED);
+
+        // validate schema set removed
+        final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(DATASPACE_NAME);
+        assertFalse(schemaSetRepository
+            .findByDataspaceAndName(dataspaceEntity, SCHEMA_SET_NAME_WITH_ANCHORS_AND_DATA).isPresent());
+
+        // validate shared resources remain
+        assertTrue(yangResourceRepository.findById(SHARED_RESOURCE_ID1).isPresent());
+        assertTrue(yangResourceRepository.findById(SHARED_RESOURCE_ID2).isPresent());
+
+        // validate associated anchors and data are removed
+        assertFalse(anchorRepository.findById(REMOVED_ANCHOR_ID1).isPresent());
+        assertFalse(anchorRepository.findById(REMOVED_ANCHOR_ID2).isPresent());
+        assertFalse(fragmentRepository.findById(REMOVED_FRAGMENT_ID).isPresent());
+    }
+
+    @Test(expected = DataspaceNotFoundException.class)
+    @Sql(CLEAR_DATA)
+    public void testDeleteSchemaSetWithinInvalidDataspace() {
+        cpsModulePersistenceService.deleteSchemaSet(DATASPACE_NAME_INVALID, SCHEMA_SET_NAME, CASCADE_DELETE_ALLOWED);
+    }
+
+    @Test(expected = SchemaSetNotFoundException.class)
+    @Sql({CLEAR_DATA, SET_DATA})
+    public void testDeleteNonExistingSchemaSet() {
+        cpsModulePersistenceService.deleteSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_NEW, CASCADE_DELETE_ALLOWED);
+    }
+
+    @Test(expected = SchemaSetInUseException.class)
+    @Sql({CLEAR_DATA, SET_DATA})
+    public void testStrictDeleteSchemaSetInUse() {
+        cpsModulePersistenceService
+            .deleteSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_WITH_ANCHORS_AND_DATA, CASCADE_DELETE_PROHIBITED);
+    }
+
     private static Map<String, String> toMap(final String key, final String value) {
         return ImmutableMap.<String, String>builder().put(key, value).build();
     }
diff --git a/cps-ri/src/test/resources/data/schemaset.sql b/cps-ri/src/test/resources/data/schemaset.sql
index e3fc69b..0ec1ec3 100644
--- a/cps-ri/src/test/resources/data/schemaset.sql
+++ b/cps-ri/src/test/resources/data/schemaset.sql
@@ -2,13 +2,26 @@
     (1001, 'DATASPACE-001'), (1002, 'DATASPACE-002');
 
 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),
+    (2100, 'SCHEMA-SET-100', 1001), -- for removal, not referenced by anchors
+    (2101, 'SCHEMA-SET-101', 1001); -- for removal, having anchor and data associated
 
 INSERT INTO YANG_RESOURCE (ID, NAME, CONTENT, CHECKSUM) VALUES
     (3001, 'module1@2020-02-02.yang', 'CONTENT-001', '877e65a9f36d54e7702c3f073f6bc42b'),
     (3002, 'module2@2020-02-02.yang', 'CONTENT-002', '88892586b1f23fe8c1595759784a18f8'),
     (3003, 'module3@2020-02-02.yang', 'CONTENT-003', 'fc5740499a09a48e0c95d6fc45d4bde8'),
-    (3004, 'module4@2020-02-02.yang', 'CONTENT-004', '3801280fe532f5cbf535695cf6122026');
+    (3004, 'module4@2020-02-02.yang', 'CONTENT-004', '3801280fe532f5cbf535695cf6122026'),
+    (3100, 'orphan@2020-02-02.yang', 'ORPHAN', 'checksum'); -- for auto-removal as orphan
 
 INSERT INTO SCHEMA_SET_YANG_RESOURCES (SCHEMA_SET_ID, YANG_RESOURCE_ID) VALUES
-    (2001, 3001), (2001, 3002), (2002, 3003), (2002, 3004);
+    (2001, 3001), (2001, 3002),
+    (2002, 3003), (2002, 3004),
+    (2100, 3003), (2100, 3100), -- orphan removal case
+    (2101, 3003), (2101, 3004);
+
+INSERT INTO ANCHOR (ID, NAME, DATASPACE_ID, SCHEMA_SET_ID) VALUES -- anchors for removal
+    (6001, 'ANCHOR1', 1001, 2101),
+    (6002, 'ANCHOR2', 1001, 2101);
+
+INSERT INTO FRAGMENT (ID, XPATH, ANCHOR_ID, DATASPACE_ID) VALUES
+    (7001, '/XPATH', 6001, 1001);