summaryrefslogtreecommitdiffstats
path: root/cps-ri/src/test/java
diff options
context:
space:
mode:
Diffstat (limited to 'cps-ri/src/test/java')
-rwxr-xr-xcps-ri/src/test/java/org/onap/cps/spi/impl/CpsModulePersistenceServiceTest.java101
1 files changed, 92 insertions, 9 deletions
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 8455e57517..050ee265a5 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.SchemaSetEntity;
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 @@ public class CpsModulePersistenceServiceTest {
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 @@ public class CpsModulePersistenceServiceTest {
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 @@ public class CpsModulePersistenceServiceTest {
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 @@ public class CpsModulePersistenceServiceTest {
}
@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 @@ public class CpsModulePersistenceServiceTest {
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 @@ public class CpsModulePersistenceServiceTest {
}
@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 @@ public class CpsModulePersistenceServiceTest {
}
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 @@ public class CpsModulePersistenceServiceTest {
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();
}