diff options
11 files changed, 226 insertions, 97 deletions
diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/CmHandleQueries.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/CmHandleQueries.java index 245161747d..9655612e70 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/CmHandleQueries.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/CmHandleQueries.java @@ -92,7 +92,7 @@ public class CmHandleQueries { final Map<String, NcmpServiceCmHandle> firstQuery, final Map<String, NcmpServiceCmHandle> secondQuery) { if (firstQuery == NO_QUERY_TO_EXECUTE && secondQuery == NO_QUERY_TO_EXECUTE) { - return Collections.emptyMap(); + return NO_QUERY_TO_EXECUTE; } else if (firstQuery == NO_QUERY_TO_EXECUTE) { return secondQuery; } else if (secondQuery == NO_QUERY_TO_EXECUTE) { diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/inventory/CmHandleQueriesSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/inventory/CmHandleQueriesSpec.groovy index ff173300a6..21aa1643d0 100644 --- a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/inventory/CmHandleQueriesSpec.groovy +++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/inventory/CmHandleQueriesSpec.groovy @@ -82,7 +82,7 @@ class CmHandleQueriesSpec extends Specification { 'the first query contains entries and second query is null' | ['PNFDemo': pnfDemoCmHandle, 'PNFDemo2': pnfDemo2CmHandle] | null || ['PNFDemo': pnfDemoCmHandle, 'PNFDemo2': pnfDemo2CmHandle] 'the second query contains entries and first query is null' | null | ['PNFDemo': pnfDemoCmHandle, 'PNFDemo3': pnfDemo3CmHandle] || ['PNFDemo': pnfDemoCmHandle, 'PNFDemo3': pnfDemo3CmHandle] 'both queries are empty' | [:] | [:] || [:] - 'both queries are null' | null | null || [:] + 'both queries are null' | null | null || null } def 'Get Cm Handles By State'() { 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 647d6cdefb..e9e945a49e 100755 --- 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 @@ -30,7 +30,6 @@ import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; -import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.List; @@ -40,13 +39,14 @@ import java.util.Set; import java.util.regex.Pattern; import java.util.stream.Collectors; import javax.transaction.Transactional; -import lombok.AllArgsConstructor; +import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.lang3.StringUtils; import org.hibernate.exception.ConstraintViolationException; import org.onap.cps.spi.CpsAdminPersistenceService; import org.onap.cps.spi.CpsModulePersistenceService; +import org.onap.cps.spi.entities.DataspaceEntity; import org.onap.cps.spi.entities.SchemaSetEntity; import org.onap.cps.spi.entities.YangResourceEntity; import org.onap.cps.spi.entities.YangResourceModuleReference; @@ -72,7 +72,7 @@ import org.springframework.stereotype.Component; @Slf4j @Component -@AllArgsConstructor +@RequiredArgsConstructor public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceService { private static final String YANG_RESOURCE_CHECKSUM_CONSTRAINT_NAME = "yang_resource_checksum_key"; @@ -80,15 +80,15 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ private static final Pattern RFC6020_RECOMMENDED_FILENAME_PATTERN = Pattern .compile("([\\w-]+)@(\\d{4}-\\d{2}-\\d{2})(?:\\.yang)?", Pattern.CASE_INSENSITIVE); - private YangResourceRepository yangResourceRepository; + private final YangResourceRepository yangResourceRepository; - private SchemaSetRepository schemaSetRepository; + private final SchemaSetRepository schemaSetRepository; - private DataspaceRepository dataspaceRepository; + private final DataspaceRepository dataspaceRepository; - private CpsAdminPersistenceService cpsAdminPersistenceService; + private final CpsAdminPersistenceService cpsAdminPersistenceService; - private ModuleReferenceRepository moduleReferenceRepository; + private final ModuleReferenceRepository moduleReferenceRepository; @Override public Map<String, String> getYangSchemaResources(final String dataspaceName, final String schemaSetName) { @@ -164,13 +164,11 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ final Map<String, String> newModuleNameToContentMap, final Collection<ModuleReference> moduleReferences) { storeSchemaSet(dataspaceName, schemaSetName, newModuleNameToContentMap); - final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName); - final var schemaSetEntity = + final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName); + final SchemaSetEntity schemaSetEntity = schemaSetRepository.getByDataspaceAndName(dataspaceEntity, schemaSetName); - final List<Long> listOfYangResourceIds = new ArrayList<>(); - moduleReferences.forEach(moduleReference -> - listOfYangResourceIds.add(yangResourceRepository.getIdByModuleNameAndRevision( - moduleReference.getModuleName(), moduleReference.getRevision()))); + final List<Long> listOfYangResourceIds = + yangResourceRepository.getResourceIdsByModuleReferences(moduleReferences); yangResourceRepository.insertSchemaSetIdYangResourceId(schemaSetEntity.getId(), listOfYangResourceIds); } diff --git a/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentRepositoryCpsPathQueryImpl.java b/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentRepositoryCpsPathQueryImpl.java index 37202498fb..f07f7f8d5b 100644 --- a/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentRepositoryCpsPathQueryImpl.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentRepositoryCpsPathQueryImpl.java @@ -20,12 +20,14 @@ package org.onap.cps.spi.repository; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.persistence.Query; +import javax.transaction.Transactional; import lombok.RequiredArgsConstructor; import org.onap.cps.cpspath.parser.CpsPathPrefixType; import org.onap.cps.cpspath.parser.CpsPathQuery; @@ -43,8 +45,9 @@ public class FragmentRepositoryCpsPathQueryImpl implements FragmentRepositoryCps private final JsonObjectMapper jsonObjectMapper; @Override + @Transactional public List<FragmentEntity> findByAnchorAndCpsPath(final int anchorId, final CpsPathQuery cpsPathQuery) { - final var sqlStringBuilder = new StringBuilder("SELECT * FROM FRAGMENT WHERE anchor_id = :anchorId"); + final StringBuilder sqlStringBuilder = new StringBuilder("SELECT * FROM FRAGMENT WHERE anchor_id = :anchorId"); final Map<String, Object> queryParameters = new HashMap<>(); queryParameters.put("anchorId", anchorId); sqlStringBuilder.append(" AND xpath SIMILAR TO :xpathRegex"); @@ -57,13 +60,23 @@ public class FragmentRepositoryCpsPathQueryImpl implements FragmentRepositoryCps } addTextFunctionCondition(cpsPathQuery, sqlStringBuilder, queryParameters); - final var query = entityManager.createNativeQuery(sqlStringBuilder.toString(), FragmentEntity.class); + final Query query = entityManager.createNativeQuery(sqlStringBuilder.toString(), FragmentEntity.class); setQueryParameters(query, queryParameters); - return query.getResultList(); + return getFragmentEntitiesAsStream(query); + } + + private List<FragmentEntity> getFragmentEntitiesAsStream(final Query query) { + final List<FragmentEntity> fragmentEntities = new ArrayList<>(); + query.getResultStream().forEach(fragmentEntity -> { + fragmentEntities.add((FragmentEntity) fragmentEntity); + entityManager.detach(fragmentEntity); + }); + + return fragmentEntities; } private static String getSimilarToXpathSqlRegex(final CpsPathQuery cpsPathQuery) { - final var xpathRegexBuilder = new StringBuilder(); + final StringBuilder xpathRegexBuilder = new StringBuilder(); if (CpsPathPrefixType.ABSOLUTE.equals(cpsPathQuery.getCpsPathPrefixType())) { xpathRegexBuilder.append(escapeXpath(cpsPathQuery.getXpathPrefix())); } else { @@ -96,7 +109,7 @@ public class FragmentRepositoryCpsPathQueryImpl implements FragmentRepositoryCps .append(" OR attributes @> jsonb_build_object(:textLeafName, json_build_array(:textValue))"); queryParameters.put("textLeafName", cpsPathQuery.getTextFunctionConditionLeafName()); queryParameters.put("textValue", cpsPathQuery.getTextFunctionConditionValue()); - final var textValueAsInt = getTextValueAsInt(cpsPathQuery); + final Integer textValueAsInt = getTextValueAsInt(cpsPathQuery); if (textValueAsInt != null) { sqlStringBuilder.append(" OR attributes @> jsonb_build_object(:textLeafName, :textValueAsInt)"); sqlStringBuilder diff --git a/cps-ri/src/main/java/org/onap/cps/spi/repository/SchemaSetYangResourceRepositoryImpl.java b/cps-ri/src/main/java/org/onap/cps/spi/repository/SchemaSetYangResourceRepositoryImpl.java index 04eaa453ef..c87e15adbf 100644 --- a/cps-ri/src/main/java/org/onap/cps/spi/repository/SchemaSetYangResourceRepositoryImpl.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/repository/SchemaSetYangResourceRepositoryImpl.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2021 Nordix Foundation. + * Copyright (C) 2021-2022 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,26 +20,40 @@ package org.onap.cps.spi.repository; +import java.sql.PreparedStatement; import java.util.List; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; +import org.hibernate.Session; import org.springframework.transaction.annotation.Transactional; + @Transactional public class SchemaSetYangResourceRepositoryImpl implements SchemaSetYangResourceRepository { + private static final int MAX_INSERT_BATCH_SIZE = 100; + @PersistenceContext private EntityManager entityManager; @Override - public void insertSchemaSetIdYangResourceId(final Integer schemaSetId, final List<Long> yangResourceId) { - final var query = "INSERT INTO SCHEMA_SET_YANG_RESOURCES (SCHEMA_SET_ID, YANG_RESOURCE_ID) " - + "VALUES ( :schemaSetId, :yangResourceId)"; - yangResourceId.forEach(id -> - entityManager.createNativeQuery(query) - .setParameter("schemaSetId", schemaSetId) - .setParameter("yangResourceId", id) - .executeUpdate() - ); + public void insertSchemaSetIdYangResourceId(final Integer schemaSetId, final List<Long> yangResourceIds) { + final Session session = entityManager.unwrap(Session.class); + session.doWork(connection -> { + try (PreparedStatement preparedStatement = connection.prepareStatement( + "INSERT INTO SCHEMA_SET_YANG_RESOURCES (SCHEMA_SET_ID, YANG_RESOURCE_ID) VALUES ( ?, ?)")) { + int sqlQueryCount = 1; + for (final long yangResourceId : yangResourceIds) { + preparedStatement.setInt(1, schemaSetId); + preparedStatement.setLong(2, yangResourceId); + preparedStatement.addBatch(); + if (sqlQueryCount % MAX_INSERT_BATCH_SIZE == 0 || sqlQueryCount == yangResourceIds.size()) { + preparedStatement.executeBatch(); + } + sqlQueryCount++; + } + } + }); } } + diff --git a/cps-ri/src/main/java/org/onap/cps/spi/repository/YangResourceNativeRepository.java b/cps-ri/src/main/java/org/onap/cps/spi/repository/YangResourceNativeRepository.java new file mode 100644 index 0000000000..335c971f97 --- /dev/null +++ b/cps-ri/src/main/java/org/onap/cps/spi/repository/YangResourceNativeRepository.java @@ -0,0 +1,31 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2022 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.cps.spi.repository; + +import java.util.Collection; +import java.util.List; +import org.onap.cps.spi.model.ModuleReference; + +public interface YangResourceNativeRepository { + + List<Long> getResourceIdsByModuleReferences(Collection<ModuleReference> moduleReferences); + +} diff --git a/cps-ri/src/main/java/org/onap/cps/spi/repository/YangResourceNativeRepositoryImpl.java b/cps-ri/src/main/java/org/onap/cps/spi/repository/YangResourceNativeRepositoryImpl.java new file mode 100644 index 0000000000..e21fecb2b1 --- /dev/null +++ b/cps-ri/src/main/java/org/onap/cps/spi/repository/YangResourceNativeRepositoryImpl.java @@ -0,0 +1,58 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2022 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.cps.spi.repository; + +import java.util.Collection; +import java.util.List; +import java.util.StringJoiner; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import javax.persistence.Query; +import org.hibernate.type.StandardBasicTypes; +import org.onap.cps.spi.model.ModuleReference; +import org.springframework.stereotype.Repository; +import org.springframework.transaction.annotation.Transactional; + +@Repository +public class YangResourceNativeRepositoryImpl implements YangResourceNativeRepository { + + @PersistenceContext + private EntityManager entityManager; + + @Override + @Transactional + public List<Long> getResourceIdsByModuleReferences(final Collection<ModuleReference> moduleReferences) { + final Query query = entityManager.createNativeQuery(getCombinedSelectSqlQuery(moduleReferences)) + .unwrap(org.hibernate.query.NativeQuery.class) + .addScalar("id", StandardBasicTypes.LONG); + return query.getResultList(); + } + + private String getCombinedSelectSqlQuery(final Collection<ModuleReference> moduleReferences) { + final StringJoiner sqlQueryJoiner = new StringJoiner(" UNION ALL "); + moduleReferences.stream().forEach(moduleReference -> { + sqlQueryJoiner.add(String.format("SELECT id FROM yang_resource WHERE module_name='%s' and revision='%s'", + moduleReference.getModuleName(), + moduleReference.getRevision())); + }); + return sqlQueryJoiner.toString(); + } +} diff --git a/cps-ri/src/main/java/org/onap/cps/spi/repository/YangResourceRepository.java b/cps-ri/src/main/java/org/onap/cps/spi/repository/YangResourceRepository.java index 98306d8684..6ca4fff4f4 100644 --- a/cps-ri/src/main/java/org/onap/cps/spi/repository/YangResourceRepository.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/repository/YangResourceRepository.java @@ -34,7 +34,7 @@ import org.springframework.stereotype.Repository; @Repository public interface YangResourceRepository extends JpaRepository<YangResourceEntity, Long>, - SchemaSetYangResourceRepository { + YangResourceNativeRepository, SchemaSetYangResourceRepository { List<YangResourceEntity> findAllByChecksumIn(@NotNull Set<String> checksum); @@ -91,10 +91,6 @@ public interface YangResourceRepository extends JpaRepository<YangResourceEntity Set<YangResourceModuleReference> findAllModuleReferencesByDataspaceAndModuleNames( @Param("dataspaceName") String dataspaceName, @Param("moduleNames") Collection<String> moduleNames); - - @Query(value = "SELECT id FROM yang_resource WHERE module_name=:name and revision=:revision", nativeQuery = true) - Long getIdByModuleNameAndRevision(@Param("name") String moduleName, @Param("revision") String revision); - @Modifying @Query(value = "DELETE FROM yang_resource yr WHERE NOT EXISTS " + "(SELECT 1 FROM schema_set_yang_resources ssyr WHERE ssyr.yang_resource_id = yr.id)", nativeQuery = true) diff --git a/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsModulePersistenceServiceIntegrationSpec.groovy b/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsModulePersistenceServiceIntegrationSpec.groovy index 3249d51ad1..eac28873e7 100644 --- a/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsModulePersistenceServiceIntegrationSpec.groovy +++ b/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsModulePersistenceServiceIntegrationSpec.groovy @@ -30,6 +30,7 @@ import org.onap.cps.spi.model.ModuleDefinition import org.onap.cps.spi.model.ModuleReference import org.onap.cps.spi.repository.AnchorRepository import org.onap.cps.spi.repository.SchemaSetRepository +import org.onap.cps.spi.repository.SchemaSetYangResourceRepositoryImpl import org.springframework.beans.factory.annotation.Autowired import org.springframework.test.context.jdbc.Sql @@ -47,13 +48,13 @@ class CpsModulePersistenceServiceIntegrationSpec extends CpsPersistenceSpecBase @Autowired CpsAdminPersistenceService cpsAdminPersistenceService - static final String SET_DATA = '/data/schemaset.sql' - static final String EXISTING_SCHEMA_SET_NAME = SCHEMA_SET_NAME1 - static final String SCHEMA_SET_NAME_NO_ANCHORS = 'SCHEMA-SET-100' - static final String SCHEMA_SET_NAME_NEW = 'SCHEMA-SET-NEW' + final static String SET_DATA = '/data/schemaset.sql' - static final String NEW_RESOURCE_NAME = 'some new resource' - static final String NEW_RESOURCE_CONTENT = 'module stores {\n' + + def static EXISTING_SCHEMA_SET_NAME = SCHEMA_SET_NAME1 + def SCHEMA_SET_NAME_NO_ANCHORS = 'SCHEMA-SET-100' + def NEW_SCHEMA_SET_NAME = 'SCHEMA-SET-NEW' + def NEW_RESOURCE_NAME = 'some new resource' + def NEW_RESOURCE_CONTENT = 'module stores {\n' + ' yang-version 1.1;\n' + ' namespace "org:onap:ccsdk:sample";\n' + '\n' + @@ -64,12 +65,9 @@ class CpsModulePersistenceServiceIntegrationSpec extends CpsPersistenceSpecBase ' "Sample Model";\n' + ' }' + '}' - static final String NEW_RESOURCE_CHECKSUM = 'b13faef573ed1374139d02c40d8ce09c80ea1dc70e63e464c1ed61568d48d539' - static final String NEW_RESOURCE_MODULE_NAME = 'stores' - static final String NEW_RESOURCE_REVISION = '2020-09-15' - static final ModuleReference newModuleReference = ModuleReference.builder().moduleName(NEW_RESOURCE_MODULE_NAME) - .revision(NEW_RESOURCE_REVISION).build() - + def NEW_RESOURCE_CHECKSUM = 'b13faef573ed1374139d02c40d8ce09c80ea1dc70e63e464c1ed61568d48d539' + def NEW_RESOURCE_MODULE_NAME = 'stores' + def NEW_RESOURCE_REVISION = '2020-09-15' def newYangResourcesNameToContentMap = [(NEW_RESOURCE_NAME):NEW_RESOURCE_CONTENT] def dataspaceEntity @@ -91,37 +89,62 @@ class CpsModulePersistenceServiceIntegrationSpec extends CpsPersistenceSpecBase } @Sql([CLEAR_DATA, SET_DATA]) - def 'Store new schema set.'() { - when: 'a new schemaset is stored' - objectUnderTest.storeSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_NEW, newYangResourcesNameToContentMap) + def 'Store new schema set with one module'() { + when: 'a new schema set with one module is stored' + objectUnderTest.storeSchemaSet(DATASPACE_NAME, NEW_SCHEMA_SET_NAME, newYangResourcesNameToContentMap) then: 'the schema set is persisted correctly' - assertSchemaSetPersisted(DATASPACE_NAME, SCHEMA_SET_NAME_NEW, NEW_RESOURCE_NAME, - NEW_RESOURCE_CONTENT, NEW_RESOURCE_CHECKSUM, NEW_RESOURCE_MODULE_NAME, NEW_RESOURCE_REVISION) + assertSchemaSetWithOneModuleIsPersistedCorrectly(NEW_RESOURCE_NAME, + NEW_RESOURCE_MODULE_NAME, NEW_RESOURCE_REVISION, NEW_RESOURCE_CONTENT, NEW_RESOURCE_CHECKSUM) + } + + @Sql([CLEAR_DATA, SET_DATA]) + def 'Store new schema set with multiple modules.'() { + given: 'a new schema set with #numberOfModules modules' + def numberOfModules = 2 + String schemaSetName = "NewSchemaWith${numberOfModules}Modules" + def newYangResourcesNameToContentMap = [:] + (1..numberOfModules).each { + def uniqueRevision = String.valueOf(2000 + it) + '-01-01' + def uniqueContent = NEW_RESOURCE_CONTENT.replace(NEW_RESOURCE_REVISION, uniqueRevision) + newYangResourcesNameToContentMap.put(uniqueRevision, uniqueContent) + } + when: 'the new schema set is stored' + objectUnderTest.storeSchemaSet(DATASPACE_NAME, schemaSetName, newYangResourcesNameToContentMap) + then: 'the correct number of modules are persisted' + assert getYangResourceEntities(schemaSetName).size() == numberOfModules } @Sql([CLEAR_DATA, SET_DATA]) def 'Store and retrieve new schema set from new modules and existing modules.'() { - given: 'map of new modules, a list of existing modules, module reference' - def mapOfNewModules = [newModule1: 'module newmodule { yang-version 1.1; revision "2021-10-12" { } }'] - def moduleReferenceForExistingModule = new ModuleReference("test","2021-10-12") - def listOfExistingModulesModuleReference = [moduleReferenceForExistingModule] - def mapOfExistingModule = [test: 'module test { yang-version 1.1; revision "2021-10-12" { } }'] - objectUnderTest.storeSchemaSet(DATASPACE_NAME, "someSchemaSetName", mapOfExistingModule) + given: 'a new module' + def mapOfNewModules = [newModule: 'module newmodule { yang-version 1.1; revision "2022-08-19" { } }'] + and: 'there are more existing modules in the db than the batch size (100)' + def listOfExistingModulesModuleReference = [] + def mapOfExistingModule = [:] + def numberOfModules = 1 + SchemaSetYangResourceRepositoryImpl.MAX_INSERT_BATCH_SIZE + (1..numberOfModules).each { + def uniqueRevision = String.valueOf(2000 + it) + '-01-01' + def uniqueContent = "module test { yang-version 1.1; revision \"${uniqueRevision}\" { } }".toString() + mapOfNewModules.put( 'test' + it, uniqueContent) + listOfExistingModulesModuleReference.add(new ModuleReference('test',uniqueRevision)) + } + objectUnderTest.storeSchemaSet(DATASPACE_NAME, 'existing schema set ', mapOfExistingModule) when: 'a new schema set is created from these new modules and existing modules' - objectUnderTest.storeSchemaSetFromModules(DATASPACE_NAME, "newSchemaSetName" , mapOfNewModules, listOfExistingModulesModuleReference) + objectUnderTest.storeSchemaSetFromModules(DATASPACE_NAME, NEW_SCHEMA_SET_NAME , mapOfNewModules, listOfExistingModulesModuleReference) then: 'the schema set can be retrieved' - def expectedYangResourcesMapAfterSchemaSetHasBeenCreated = mapOfNewModules + mapOfExistingModule def actualYangResourcesMapAfterSchemaSetHasBeenCreated = - objectUnderTest.getYangSchemaResources(DATASPACE_NAME, "newSchemaSetName") - actualYangResourcesMapAfterSchemaSetHasBeenCreated == expectedYangResourcesMapAfterSchemaSetHasBeenCreated + objectUnderTest.getYangSchemaResources(DATASPACE_NAME, NEW_SCHEMA_SET_NAME) + and: 'it has all the new and existing modules' + def expectedYangResourcesMapAfterSchemaSetHasBeenCreated = mapOfNewModules + mapOfExistingModule + assert actualYangResourcesMapAfterSchemaSetHasBeenCreated == expectedYangResourcesMapAfterSchemaSetHasBeenCreated } @Sql([CLEAR_DATA, SET_DATA]) def 'Retrieving schema set (resources) by anchor.'() { given: 'a new schema set is stored' - objectUnderTest.storeSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_NEW, newYangResourcesNameToContentMap) + objectUnderTest.storeSchemaSet(DATASPACE_NAME, NEW_SCHEMA_SET_NAME, newYangResourcesNameToContentMap) and: 'an anchor is created with that schema set' - cpsAdminPersistenceService.createAnchor(DATASPACE_NAME, SCHEMA_SET_NAME_NEW, ANCHOR_NAME1) + cpsAdminPersistenceService.createAnchor(DATASPACE_NAME, NEW_SCHEMA_SET_NAME, ANCHOR_NAME1) when: 'the schema set resources for that anchor is retrieved' def result = objectUnderTest.getYangSchemaSetResources(DATASPACE_NAME, ANCHOR_NAME1) then: 'the correct resources are returned' @@ -157,15 +180,13 @@ class CpsModulePersistenceServiceIntegrationSpec extends CpsPersistenceSpecBase def existingResourceContent = 'module test { yang-version 1.1; revision "2020-09-15"; }' def newYangResourcesNameToContentMap = [(NEW_RESOURCE_NAME):existingResourceContent] when: 'the schema set with duplicate resource is stored' - objectUnderTest.storeSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_NEW, newYangResourcesNameToContentMap) - then: 'the schema persisted (re)uses the existing name and has the same checksum' - def existingResourceName = 'module1@2020-02-02.yang' + objectUnderTest.storeSchemaSet(DATASPACE_NAME, NEW_SCHEMA_SET_NAME, newYangResourcesNameToContentMap) + then: 'the schema persisted has the new name and has the same checksum' def existingResourceChecksum = 'bea1afcc3d1517e7bf8cae151b3b6bfbd46db77a81754acdcb776a50368efa0a' - def existingResourceModelName = 'test' + def existingResourceModuleName = 'test' def existingResourceRevision = '2020-09-15' - assertSchemaSetPersisted(DATASPACE_NAME, SCHEMA_SET_NAME_NEW, existingResourceName, - existingResourceContent, existingResourceChecksum, - existingResourceModelName, existingResourceRevision) + assertSchemaSetWithOneModuleIsPersistedCorrectly(NEW_RESOURCE_NAME, existingResourceModuleName, + existingResourceRevision, existingResourceContent, existingResourceChecksum) } @Sql([CLEAR_DATA, SET_DATA]) @@ -223,31 +244,31 @@ class CpsModulePersistenceServiceIntegrationSpec extends CpsPersistenceSpecBase result.sort() == [new ModuleDefinition('MODULE-NAME-004', 'REVISION-004', 'CONTENT-004')] } - def assertSchemaSetPersisted(expectedDataspaceName, - expectedSchemaSetName, - expectedYangResourceName, - expectedYangResourceContent, - expectedYangResourceChecksum, - expectedYangResourceModuleName, - expectedYangResourceRevision) { - // assert the schema set is persisted - def schemaSetEntity = schemaSetRepository - .findByDataspaceAndName(dataspaceEntity, expectedSchemaSetName).orElseThrow() - assert schemaSetEntity.name == expectedSchemaSetName - assert schemaSetEntity.dataspace.name == expectedDataspaceName + def assertSchemaSetWithOneModuleIsPersistedCorrectly(expectedYangResourceName, + expectedYangResourceModuleName, + expectedYangResourceRevision, + expectedYangResourceContent, + expectedYangResourceChecksum) { // assert the attached yang resource is persisted - def yangResourceEntities = schemaSetEntity.getYangResources() - yangResourceEntities.size() == 1 + def yangResourceEntities = getYangResourceEntities(NEW_SCHEMA_SET_NAME) + assert yangResourceEntities.size() == 1 // assert the attached yang resource content YangResourceEntity yangResourceEntity = yangResourceEntities.iterator().next() assert yangResourceEntity.id != null - yangResourceEntity.fileName == expectedYangResourceName - yangResourceEntity.content == expectedYangResourceContent - yangResourceEntity.checksum == expectedYangResourceChecksum - yangResourceEntity.moduleName == expectedYangResourceModuleName - yangResourceEntity.revision == expectedYangResourceRevision + assert yangResourceEntity.fileName == expectedYangResourceName + assert yangResourceEntity.moduleName == expectedYangResourceModuleName + assert yangResourceEntity.revision == expectedYangResourceRevision + assert yangResourceEntity.content == expectedYangResourceContent + assert yangResourceEntity.checksum == expectedYangResourceChecksum + return true + } + + def getYangResourceEntities(schemaSetName) { + def schemaSetEntity = schemaSetRepository + .findByDataspaceAndName(dataspaceEntity, schemaSetName).orElseThrow() + return schemaSetEntity.getYangResources() } def toModuleReference(moduleReferenceAsMap) { diff --git a/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsPersistenceSpecBase.groovy b/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsPersistenceSpecBase.groovy index 8e13be2ae1..d6f10d809d 100644 --- a/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsPersistenceSpecBase.groovy +++ b/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsPersistenceSpecBase.groovy @@ -61,7 +61,6 @@ class CpsPersistenceSpecBase extends Specification { static final String CLEAR_DATA = '/data/clear-all.sql' static final String DATASPACE_NAME = 'DATASPACE-001' - static final String DATASPACE_NAME2 = 'DATASPACE-002' static final String SCHEMA_SET_NAME1 = 'SCHEMA-SET-001' static final String SCHEMA_SET_NAME2 = 'SCHEMA-SET-002' static final String ANCHOR_NAME1 = 'ANCHOR-001' diff --git a/cps-ri/src/test/resources/data/clear-all.sql b/cps-ri/src/test/resources/data/clear-all.sql index 8a5e8444e7..07c8a7aab5 100644 --- a/cps-ri/src/test/resources/data/clear-all.sql +++ b/cps-ri/src/test/resources/data/clear-all.sql @@ -1,7 +1,7 @@ /* ============LICENSE_START======================================================= Copyright (C) 2020-2021 Pantheon.tech - Modifications Copyright (C) 2020 Nordix Foundation. + Modifications Copyright (C) 2020,2022 Nordix Foundation. Modifications Copyright (C) 2020 Bell Canada. ================================================================================ Licensed under the Apache License, Version 2.0 (the "License"); @@ -23,7 +23,6 @@ DELETE FROM FRAGMENT; DELETE FROM ANCHOR; DELETE FROM DATASPACE; --- following tables are cleared by CASCADE constraint: --- SCHEMA_SET --- SCHEMA_SET_YANG_RESOURCES -DELETE FROM YANG_RESOURCE; +DELETE FROM YANG_RESOURCE +-- following tables are cleared by CASCADE constraint: SCHEMA_SET, SCHEMA_SET_YANG_RESOURCES + |