diff options
author | 2020-12-14 15:20:58 +0000 | |
---|---|---|
committer | 2020-12-14 15:20:58 +0000 | |
commit | c040bcb4ae61177b1aa691f059d19c96c6cfff7b (patch) | |
tree | 7535cf652f46cb0564db24f478575f3eaba99618 /cps-ri/src | |
parent | a0da7f6b3e04ee6f06f4580f8a14e60f3438d5e7 (diff) | |
parent | 4ab9d0edb5bc0c5251175078a4b8933a29e73744 (diff) |
Merge "Yang resource name persistence"
Diffstat (limited to 'cps-ri/src')
5 files changed, 53 insertions, 35 deletions
diff --git a/cps-ri/src/main/java/org/onap/cps/spi/entities/YangResource.java b/cps-ri/src/main/java/org/onap/cps/spi/entities/YangResource.java index 862b7aea6..b8a58d098 100644 --- a/cps-ri/src/main/java/org/onap/cps/spi/entities/YangResource.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/entities/YangResource.java @@ -55,6 +55,10 @@ public class YangResource implements Serializable { @NotNull @Column + private String name; + + @NotNull + @Column private String content; @ManyToMany(mappedBy = "yangResources") diff --git a/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsModulePersistenceServiceImpl.java b/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsModulePersistenceServiceImpl.java index e63ae5962..3067f4834 100644 --- a/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsModulePersistenceServiceImpl.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsModulePersistenceServiceImpl.java @@ -21,6 +21,7 @@ package org.onap.cps.spi.impl; import com.google.common.collect.ImmutableSet; +import java.util.Collection; import java.util.List; import java.util.Map; import java.util.Set; @@ -61,10 +62,10 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ @Override @Transactional public void storeSchemaSet(final String dataspaceName, final String schemaSetName, - final Set<String> yangResourcesAsStrings) { + final Map<String, String> yangResourcesNameToContentMap) { final Dataspace dataspace = dataspaceRepository.getByName(dataspaceName); - final Set<YangResource> yangResources = synchronizeYangResources(yangResourcesAsStrings); + final Set<YangResource> yangResources = synchronizeYangResources(yangResourcesNameToContentMap); final SchemaSet schemaSet = new SchemaSet(); schemaSet.setName(schemaSetName); schemaSet.setDataspace(dataspace); @@ -76,24 +77,25 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ } } - private Set<YangResource> synchronizeYangResources(final Set<String> yangResourcesAsStrings) { - final Map<String, String> checksumToContentMap = yangResourcesAsStrings.stream() - .collect(Collectors.toMap( - content -> DigestUtils.md5DigestAsHex(content.getBytes()), - content -> content) - ); - - final List<YangResource> existingYangResources = - yangResourceRepository.findAllByChecksumIn(checksumToContentMap.keySet()); - existingYangResources.forEach(yangFile -> checksumToContentMap.remove(yangFile.getChecksum())); - - final List<YangResource> newYangResources = checksumToContentMap.entrySet().stream() + private Set<YangResource> synchronizeYangResources(final Map<String, String> yangResourcesNameToContentMap) { + final Map<String, YangResource> checksumToEntityMap = yangResourcesNameToContentMap.entrySet().stream() .map(entry -> { final YangResource yangResource = new YangResource(); - yangResource.setChecksum(entry.getKey()); + yangResource.setName(entry.getKey()); yangResource.setContent(entry.getValue()); + yangResource.setChecksum(DigestUtils.md5DigestAsHex(entry.getValue().getBytes())); return yangResource; - }).collect(Collectors.toList()); + }) + .collect(Collectors.toMap( + YangResource::getChecksum, + entity -> entity + )); + + final List<YangResource> existingYangResources = + yangResourceRepository.findAllByChecksumIn(checksumToEntityMap.keySet()); + existingYangResources.forEach(yangFile -> checksumToEntityMap.remove(yangFile.getChecksum())); + + final Collection<YangResource> newYangResources = checksumToEntityMap.values(); if (!newYangResources.isEmpty()) { yangResourceRepository.saveAll(newYangResources); } diff --git a/cps-ri/src/main/resources/schema.sql b/cps-ri/src/main/resources/schema.sql index d47f261ad..2dce470ae 100755 --- a/cps-ri/src/main/resources/schema.sql +++ b/cps-ri/src/main/resources/schema.sql @@ -29,8 +29,9 @@ CREATE TABLE IF NOT EXISTS SCHEMA_SET CREATE TABLE IF NOT EXISTS YANG_RESOURCE
(
ID SERIAL PRIMARY KEY,
- CHECKSUM TEXT NOT NULL,
+ NAME TEXT NOT NULL,
CONTENT TEXT NOT NULL,
+ CHECKSUM TEXT NOT NULL,
UNIQUE (CHECKSUM)
);
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 a93d277ea..46f017bed 100644 --- 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 @@ -22,7 +22,8 @@ package org.onap.cps.spi.impl; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; -import com.google.common.collect.ImmutableSet; +import com.google.common.collect.ImmutableMap; +import java.util.Map; import java.util.Set; import org.junit.ClassRule; import org.junit.Test; @@ -55,8 +56,13 @@ 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 OLD_RESOURCE_CONTENT = "CONTENT-001"; - private static final String OLD_RESOURCE_CHECKSUM = "877e65a9f36d54e7702c3f073f6bc42b"; + + private static final String EXISTING_RESOURCE_NAME = "module1@2020-02-02.yang"; + private static final String EXISTING_RESOURCE_CONTENT = "CONTENT-001"; + private static final String EXISTING_RESOURCE_CHECKSUM = "877e65a9f36d54e7702c3f073f6bc42b"; + private static final Long EXISTING_RESOURCE_ID = 3001L; + + private static final String NEW_RESOURCE_NAME = "new-module@2020-02-02.yang"; private static final String NEW_RESOURCE_CONTENT = "CONTENT-NEW"; private static final String NEW_RESOURCE_CHECKSUM = "c94d40a1350eb1c0b1c1949eac84fc59"; private static final Long NEW_RESOURCE_ABSTRACT_ID = 0L; @@ -80,34 +86,38 @@ public class CpsModulePersistenceServiceTest { @Test(expected = DataspaceNotFoundException.class) @Sql(CLEAR_DATA) public void testStoreSchemaSetToInvalidDataspace() { - cpsModulePersistenceService - .storeSchemaSet(DATASPACE_NAME_INVALID, SCHEMA_SET_NAME_NEW, toSet(NEW_RESOURCE_CONTENT)); + cpsModulePersistenceService.storeSchemaSet(DATASPACE_NAME_INVALID, SCHEMA_SET_NAME_NEW, + toMap(NEW_RESOURCE_NAME, NEW_RESOURCE_CONTENT)); } @Test(expected = SchemaSetAlreadyDefinedException.class) @SqlGroup({@Sql(CLEAR_DATA), @Sql(SET_DATA)}) public void testStoreDuplicateSchemaSet() { - cpsModulePersistenceService.storeSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME, toSet(NEW_RESOURCE_CONTENT)); + cpsModulePersistenceService.storeSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME, + toMap(NEW_RESOURCE_NAME, NEW_RESOURCE_CONTENT)); } @Test @SqlGroup({@Sql(CLEAR_DATA), @Sql(SET_DATA)}) public void testStoreSchemaSetWithNewYangResource() { - cpsModulePersistenceService.storeSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_NEW, toSet(NEW_RESOURCE_CONTENT)); + cpsModulePersistenceService.storeSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_NEW, + toMap(NEW_RESOURCE_NAME, NEW_RESOURCE_CONTENT)); assertSchemaSetPersisted(DATASPACE_NAME, SCHEMA_SET_NAME_NEW, - NEW_RESOURCE_ABSTRACT_ID, NEW_RESOURCE_CONTENT, NEW_RESOURCE_CHECKSUM); + NEW_RESOURCE_ABSTRACT_ID, NEW_RESOURCE_NAME, NEW_RESOURCE_CONTENT, NEW_RESOURCE_CHECKSUM); } @Test @SqlGroup({@Sql(CLEAR_DATA), @Sql(SET_DATA)}) public void testStoreSchemaSetWithExistingYangResourceReuse() { - cpsModulePersistenceService.storeSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_NEW, toSet(OLD_RESOURCE_CONTENT)); + cpsModulePersistenceService.storeSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_NEW, + toMap(NEW_RESOURCE_NAME, EXISTING_RESOURCE_CONTENT)); assertSchemaSetPersisted(DATASPACE_NAME, SCHEMA_SET_NAME_NEW, - 3001L, OLD_RESOURCE_CONTENT, OLD_RESOURCE_CHECKSUM); + EXISTING_RESOURCE_ID, EXISTING_RESOURCE_NAME, EXISTING_RESOURCE_CONTENT, EXISTING_RESOURCE_CHECKSUM); } private void assertSchemaSetPersisted(final String expectedDataspaceName, final String expectedSchemaSetName, - final Long expectedYangResourceId, final String expectedYangResourceContent, + final Long expectedYangResourceId, final String expectedYangResourceName, + final String expectedYangResourceContent, final String expectedYangResourceChecksum) { // assert the schema set is persisted @@ -127,12 +137,13 @@ public class CpsModulePersistenceServiceTest { // existing resource with known id assertEquals(expectedYangResourceId, yangResource.getId()); } + assertEquals(expectedYangResourceName, yangResource.getName()); assertEquals(expectedYangResourceContent, yangResource.getContent()); assertEquals(expectedYangResourceChecksum, yangResource.getChecksum()); } - private static Set<String> toSet(final String... elements) { - return ImmutableSet.<String>builder().add(elements).build(); + private static Map<String, String> toMap(final String key, final String value) { + return ImmutableMap.<String, String>builder().put(key, value).build(); } private SchemaSet getSchemaSetFromDatabase(final String dataspaceName, final String schemaSetName) { diff --git a/cps-ri/src/test/resources/data/schemaset.sql b/cps-ri/src/test/resources/data/schemaset.sql index 23788c3c0..e3fc69b08 100644 --- a/cps-ri/src/test/resources/data/schemaset.sql +++ b/cps-ri/src/test/resources/data/schemaset.sql @@ -4,11 +4,11 @@ INSERT INTO DATASPACE (ID, NAME) VALUES INSERT INTO SCHEMA_SET (ID, NAME, DATASPACE_ID) VALUES (2001, 'SCHEMA-SET-001', 1001), (2002, 'SCHEMA-SET-002', 1001); -INSERT INTO YANG_RESOURCE (ID, CONTENT, CHECKSUM) VALUES - (3001, 'CONTENT-001', '877e65a9f36d54e7702c3f073f6bc42b'), - (3002, 'CONTENT-002', '88892586b1f23fe8c1595759784a18f8'), - (3003, 'CONTENT-003', 'fc5740499a09a48e0c95d6fc45d4bde8'), - (3004, 'CONTENT-004', '3801280fe532f5cbf535695cf6122026'); +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'); INSERT INTO SCHEMA_SET_YANG_RESOURCES (SCHEMA_SET_ID, YANG_RESOURCE_ID) VALUES (2001, 3001), (2001, 3002), (2002, 3003), (2002, 3004); |