diff options
Diffstat (limited to 'cps-ri')
17 files changed, 210 insertions, 495 deletions
diff --git a/cps-ri/pom.xml b/cps-ri/pom.xml index f065421ec9..eae383490c 100644 --- a/cps-ri/pom.xml +++ b/cps-ri/pom.xml @@ -3,7 +3,7 @@ ============LICENSE_START=======================================================
Copyright (C) 2020-2021 Pantheon.tech
Modifications Copyright (C) 2020-2021 Bell Canada
- Modifications Copyright (C) 2020-2024 Nordix Foundation
+ Modifications Copyright (C) 2020-2025 Nordix Foundation
================================================================================
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
@@ -26,14 +26,14 @@ <parent>
<groupId>org.onap.cps</groupId>
<artifactId>cps-parent</artifactId>
- <version>3.6.0-SNAPSHOT</version>
+ <version>3.6.2-SNAPSHOT</version>
<relativePath>../cps-parent/pom.xml</relativePath>
</parent>
<artifactId>cps-ri</artifactId>
<properties>
- <minimum-coverage>0.29</minimum-coverage>
+ <minimum-coverage>0.31</minimum-coverage>
<!-- Additional coverage is provided by integration-test module -->
</properties>
diff --git a/cps-ri/src/main/java/org/onap/cps/ri/CpsAdminPersistenceServiceImpl.java b/cps-ri/src/main/java/org/onap/cps/ri/CpsAdminPersistenceServiceImpl.java index 494d6919da..588a639ab8 100755 --- a/cps-ri/src/main/java/org/onap/cps/ri/CpsAdminPersistenceServiceImpl.java +++ b/cps-ri/src/main/java/org/onap/cps/ri/CpsAdminPersistenceServiceImpl.java @@ -1,6 +1,6 @@ /* * ============LICENSE_START======================================================= - * Copyright (C) 2020-2024 Nordix Foundation. + * Copyright (C) 2020-2025 Nordix Foundation. * Modifications Copyright (C) 2020-2022 Bell Canada. * Modifications Copyright (C) 2021 Pantheon.tech * Modifications Copyright (C) 2022 TechMahindra Ltd. @@ -91,10 +91,10 @@ public class CpsAdminPersistenceServiceImpl implements CpsAdminPersistenceServic @Override public void createAnchor(final String dataspaceName, final String schemaSetName, final String anchorName) { - final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName); - final var schemaSetEntity = - schemaSetRepository.getByDataspaceAndName(dataspaceEntity, schemaSetName); - final var anchorEntity = AnchorEntity.builder() + final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName); + final SchemaSetEntity schemaSetEntity = schemaSetRepository + .getByDataspaceAndName(dataspaceEntity, schemaSetName); + final AnchorEntity anchorEntity = AnchorEntity.builder() .name(anchorName) .dataspace(dataspaceEntity) .schemaSet(schemaSetEntity) @@ -114,7 +114,7 @@ public class CpsAdminPersistenceServiceImpl implements CpsAdminPersistenceServic @Override public Collection<Anchor> getAnchors(final String dataspaceName) { - final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName); + final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName); final Collection<AnchorEntity> anchorEntities = anchorRepository.findAllByDataspace(dataspaceEntity); return anchorEntities.stream().map(CpsAdminPersistenceServiceImpl::toAnchor).collect(Collectors.toSet()); } @@ -154,14 +154,14 @@ public class CpsAdminPersistenceServiceImpl implements CpsAdminPersistenceServic @Transactional @Override public void deleteAnchor(final String dataspaceName, final String anchorName) { - final var anchorEntity = getAnchorEntity(dataspaceName, anchorName); + final AnchorEntity anchorEntity = getAnchorEntity(dataspaceName, anchorName); anchorRepository.delete(anchorEntity); } @Transactional @Override public void deleteAnchors(final String dataspaceName, final Collection<String> anchorNames) { - final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName); + final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName); anchorRepository.deleteAllByDataspaceAndNameIn(dataspaceEntity, anchorNames); } @@ -178,7 +178,7 @@ public class CpsAdminPersistenceServiceImpl implements CpsAdminPersistenceServic } private AnchorEntity getAnchorEntity(final String dataspaceName, final String anchorName) { - final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName); + final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName); return anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName); } diff --git a/cps-ri/src/main/java/org/onap/cps/ri/CpsDataPersistenceServiceImpl.java b/cps-ri/src/main/java/org/onap/cps/ri/CpsDataPersistenceServiceImpl.java index 52fd7f2be1..a510d308d6 100644 --- a/cps-ri/src/main/java/org/onap/cps/ri/CpsDataPersistenceServiceImpl.java +++ b/cps-ri/src/main/java/org/onap/cps/ri/CpsDataPersistenceServiceImpl.java @@ -1,6 +1,6 @@ /* * ============LICENSE_START======================================================= - * Copyright (C) 2021-2024 Nordix Foundation + * Copyright (C) 2021-2025 Nordix Foundation * Modifications Copyright (C) 2021 Pantheon.tech * Modifications Copyright (C) 2020-2022 Bell Canada. * Modifications Copyright (C) 2022-2023 TechMahindra Ltd. @@ -23,6 +23,7 @@ package org.onap.cps.ri; +import static org.onap.cps.api.CpsQueryService.NO_LIMIT; import static org.onap.cps.api.parameters.PaginationOption.NO_PAGINATION; import com.google.common.collect.ImmutableSet; @@ -37,6 +38,7 @@ import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Set; import java.util.TreeMap; import java.util.stream.Collectors; @@ -222,14 +224,44 @@ public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService description = "Time taken to query data nodes") public List<DataNode> queryDataNodes(final String dataspaceName, final String anchorName, final String cpsPath, final FetchDescendantsOption fetchDescendantsOption) { + return queryDataNodes(dataspaceName, anchorName, cpsPath, fetchDescendantsOption, NO_LIMIT); + } + + @Override + public List<DataNode> queryDataNodes(final String dataspaceName, + final String anchorName, + final String cpsPath, + final FetchDescendantsOption fetchDescendantsOption, + final int queryResultLimit) { final AnchorEntity anchorEntity = getAnchorEntity(dataspaceName, anchorName); final CpsPathQuery cpsPathQuery = getCpsPathQuery(cpsPath); final Collection<FragmentEntity> fragmentEntities = - fragmentRepository.findByAnchorAndCpsPath(anchorEntity, cpsPathQuery); + fragmentRepository.findByAnchorAndCpsPath(anchorEntity, cpsPathQuery, queryResultLimit); return createDataNodesFromFragmentEntities(fetchDescendantsOption, fragmentEntities); } @Override + public <T> Set<T> queryDataLeaf(final String dataspaceName, final String anchorName, final String cpsPath, + final int queryResultLimit, final Class<T> targetClass) { + final CpsPathQuery cpsPathQuery = getCpsPathQuery(cpsPath); + if (!cpsPathQuery.hasAttributeAxis()) { + throw new IllegalArgumentException( + "Only Cps Path Queries with attribute-axis are supported by queryDataLeaf"); + } + + final String attributeName = cpsPathQuery.getAttributeAxisAttributeName(); + final List<DataNode> dataNodes = queryDataNodes(dataspaceName, anchorName, cpsPath, + FetchDescendantsOption.OMIT_DESCENDANTS, queryResultLimit); + return dataNodes.stream() + .map(dataNode -> { + final Object attributeValue = dataNode.getLeaves().get(attributeName); + return targetClass.isInstance(attributeValue) ? targetClass.cast(attributeValue) : null; + }) + .filter(Objects::nonNull) + .collect(Collectors.toSet()); + } + + @Override @Timed(value = "cps.data.persistence.service.datanode.query.anchors", description = "Time taken to query data nodes across all anchors or list of anchors") public List<DataNode> queryDataNodesAcrossAnchors(final String dataspaceName, final String cpsPath, diff --git a/cps-ri/src/main/java/org/onap/cps/ri/CpsModulePersistenceServiceImpl.java b/cps-ri/src/main/java/org/onap/cps/ri/CpsModulePersistenceServiceImpl.java index aaf6165471..412c6f9c0b 100755 --- a/cps-ri/src/main/java/org/onap/cps/ri/CpsModulePersistenceServiceImpl.java +++ b/cps-ri/src/main/java/org/onap/cps/ri/CpsModulePersistenceServiceImpl.java @@ -24,6 +24,7 @@ package org.onap.cps.ri; import static com.google.common.base.Preconditions.checkNotNull; +import static org.opendaylight.yangtools.yang.common.YangConstants.RFC6020_YANG_FILE_EXTENSION; import com.google.common.base.MoreObjects; import com.google.common.collect.ImmutableSet; @@ -36,7 +37,6 @@ import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; -import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Optional; @@ -103,10 +103,9 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ @Override public Collection<ModuleReference> getYangResourceModuleReferences(final String dataspaceName) { - final Set<YangResourceModuleReference> yangResourceModuleReferenceList = + final Collection<YangResourceModuleReference> yangResourceModuleReferences = yangResourceRepository.findAllModuleReferencesByDataspace(dataspaceName); - return yangResourceModuleReferenceList.stream().map(CpsModulePersistenceServiceImpl::toModuleReference) - .collect(Collectors.toList()); + return yangResourceModuleReferences.stream().map(CpsModulePersistenceServiceImpl::toModuleReference).toList(); } @Override @@ -150,19 +149,24 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ @Override @Transactional - public void storeSchemaSet(final String dataspaceName, final String schemaSetName, - final Map<String, String> moduleReferenceNameToContentMap) { - final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName); - final Set<YangResourceEntity> yangResourceEntities = synchronizeYangResources(moduleReferenceNameToContentMap); - final SchemaSetEntity schemaSetEntity = new SchemaSetEntity(); - schemaSetEntity.setName(schemaSetName); - schemaSetEntity.setDataspace(dataspaceEntity); - schemaSetEntity.setYangResources(yangResourceEntities); - try { - schemaSetRepository.save(schemaSetEntity); - } catch (final DataIntegrityViolationException e) { - throw AlreadyDefinedException.forSchemaSet(schemaSetName, dataspaceName, e); - } + @Timed(value = "cps.module.persistence.schemaset.create", + description = "Time taken to store a schemaset (list of module references)") + public void createSchemaSet(final String dataspaceName, final String schemaSetName, + final Map<String, String> yangResourceContentPerName) { + final Set<YangResourceEntity> yangResourceEntities = synchronizeYangResources(yangResourceContentPerName); + createAndSaveSchemaSetEntity(dataspaceName, schemaSetName, yangResourceEntities); + } + + @Override + @Transactional + @Timed(value = "cps.module.persistence.schemaset.createFromNewAndExistingModules", + description = "Time taken to store a schemaset (from new and existing)") + public void createSchemaSetFromNewAndExistingModules(final String dataspaceName, final String schemaSetName, + final Map<String, String> newYangResourceContentPerName, + final Collection<ModuleReference> allModuleReferences) { + synchronizeYangResources(newYangResourceContentPerName); + final Set<YangResourceEntity> allYangResourceEntities = getYangResourceEntities(allModuleReferences); + createAndSaveSchemaSetEntity(dataspaceName, schemaSetName, allYangResourceEntities); } @Override @@ -181,22 +185,6 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ @Override @Transactional - @Timed(value = "cps.module.persistence.schemaset.store", - description = "Time taken to store a schemaset (list of module references)") - public void storeSchemaSetFromModules(final String dataspaceName, final String schemaSetName, - final Map<String, String> newModuleNameToContentMap, - final Collection<ModuleReference> allModuleReferences) { - storeSchemaSet(dataspaceName, schemaSetName, newModuleNameToContentMap); - final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName); - final SchemaSetEntity schemaSetEntity = - schemaSetRepository.getByDataspaceAndName(dataspaceEntity, schemaSetName); - final List<Integer> allYangResourceIds = - yangResourceRepository.getResourceIdsByModuleReferences(allModuleReferences); - yangResourceRepository.insertSchemaSetIdYangResourceId(schemaSetEntity.getId(), allYangResourceIds); - } - - @Override - @Transactional public void deleteSchemaSet(final String dataspaceName, final String schemaSetName) { final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName); final SchemaSetEntity schemaSetEntity = @@ -213,14 +201,16 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ @Override @Transactional - public void updateSchemaSetFromModules(final String dataspaceName, final String schemaSetName, - final Map<String, String> newModuleNameToContentMap, - final Collection<ModuleReference> allModuleReferences) { + public void updateSchemaSetFromNewAndExistingModules(final String dataspaceName, final String schemaSetName, + final Map<String, String> newYangResourcesPerName, + final Collection<ModuleReference> allModuleReferences) { final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName); final SchemaSetEntity schemaSetEntity = schemaSetRepository.getByDataspaceAndName(dataspaceEntity, schemaSetName); - storeAndLinkNewModules(newModuleNameToContentMap, schemaSetEntity); - updateAllModuleReferences(allModuleReferences, schemaSetEntity.getId()); + synchronizeYangResources(newYangResourcesPerName); + final Set<YangResourceEntity> allYangResourceEntities = getYangResourceEntities(allModuleReferences); + schemaSetEntity.setYangResources(allYangResourceEntities); + schemaSetRepository.save(schemaSetEntity); } @Override @@ -237,48 +227,66 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ return moduleReferenceRepository.identifyNewModuleReferences(moduleReferencesToCheck); } - private Set<YangResourceEntity> synchronizeYangResources( - final Map<String, String> moduleReferenceNameToContentMap) { - final Map<String, YangResourceEntity> checksumToEntityMap = moduleReferenceNameToContentMap.entrySet().stream() - .map(entry -> { - final String checksum = DigestUtils.sha256Hex(entry.getValue().getBytes(StandardCharsets.UTF_8)); - final Map<String, String> moduleNameAndRevisionMap = createModuleNameAndRevisionMap(entry.getKey(), - entry.getValue()); - final YangResourceEntity yangResourceEntity = new YangResourceEntity(); - yangResourceEntity.setFileName(entry.getKey()); - yangResourceEntity.setContent(entry.getValue()); - yangResourceEntity.setModuleName(moduleNameAndRevisionMap.get("moduleName")); - yangResourceEntity.setRevision(moduleNameAndRevisionMap.get("revision")); - yangResourceEntity.setChecksum(checksum); - return yangResourceEntity; - }) - .collect(Collectors.toMap( - YangResourceEntity::getChecksum, - entity -> entity - )); + private Set<YangResourceEntity> synchronizeYangResources(final Map<String, String> yangResourceContentPerName) { + final Map<String, YangResourceEntity> yangResourceEntitiesPerChecksum = + getYangResourceEntityPerChecksum(yangResourceContentPerName); final List<YangResourceEntity> existingYangResourceEntities = - yangResourceRepository.findAllByChecksumIn(checksumToEntityMap.keySet()); - existingYangResourceEntities.forEach(yangFile -> checksumToEntityMap.remove(yangFile.getChecksum())); + yangResourceRepository.findAllByChecksumIn(yangResourceEntitiesPerChecksum.keySet()); + + existingYangResourceEntities.forEach(exist -> yangResourceEntitiesPerChecksum.remove(exist.getChecksum())); + final Collection<YangResourceEntity> newYangResourceEntities = yangResourceEntitiesPerChecksum.values(); - final Collection<YangResourceEntity> newYangResourceEntities = checksumToEntityMap.values(); if (!newYangResourceEntities.isEmpty()) { try { yangResourceRepository.saveAll(newYangResourceEntities); } catch (final DataIntegrityViolationException dataIntegrityViolationException) { - // Throw a CPS duplicated Yang resource exception if the cause of the error is a yang checksum - // database constraint violation. If it is not, then throw the original exception - final Optional<DuplicatedYangResourceException> convertedException = - convertToDuplicatedYangResourceException( - dataIntegrityViolationException, newYangResourceEntities); - throw convertedException.isPresent() ? convertedException.get() : dataIntegrityViolationException; + convertExceptionIfNeeded(dataIntegrityViolationException, newYangResourceEntities); } } + // return ALL yang resourceEntities return ImmutableSet.<YangResourceEntity>builder() - .addAll(existingYangResourceEntities) - .addAll(newYangResourceEntities) - .build(); + .addAll(existingYangResourceEntities) + .addAll(newYangResourceEntities) + .build(); + } + + private static Map<String, YangResourceEntity> getYangResourceEntityPerChecksum( + final Map<String, String> yangResourceContentPerName) { + return yangResourceContentPerName.entrySet().stream().map(entry -> { + final String checksum = DigestUtils.sha256Hex(entry.getValue().getBytes(StandardCharsets.UTF_8)); + final Map<String, String> moduleNameAndRevisionMap = createModuleNameAndRevisionMap(entry.getKey(), + entry.getValue()); + final YangResourceEntity yangResourceEntity = new YangResourceEntity(); + yangResourceEntity.setContent(entry.getValue()); + final String moduleName = moduleNameAndRevisionMap.get("moduleName"); + final String revision = moduleNameAndRevisionMap.get("revision"); + yangResourceEntity.setModuleName(moduleName); + yangResourceEntity.setRevision(revision); + yangResourceEntity.setFileName(moduleName + "@" + revision + RFC6020_YANG_FILE_EXTENSION); + yangResourceEntity.setChecksum(checksum); + return yangResourceEntity; + }) + .collect(Collectors.toMap( + YangResourceEntity::getChecksum, + entity -> entity + )); + } + + private void createAndSaveSchemaSetEntity(final String dataspaceName, + final String schemaSetName, + final Set<YangResourceEntity> yangResourceEntities) { + final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName); + final SchemaSetEntity schemaSetEntity = new SchemaSetEntity(); + schemaSetEntity.setName(schemaSetName); + schemaSetEntity.setDataspace(dataspaceEntity); + schemaSetEntity.setYangResources(yangResourceEntities); + try { + schemaSetRepository.save(schemaSetEntity); + } catch (final DataIntegrityViolationException e) { + throw AlreadyDefinedException.forSchemaSet(schemaSetName, dataspaceName, e); + } } private static Map<String, String> createModuleNameAndRevisionMap(final String sourceName, final String source) { @@ -323,6 +331,14 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ return RevisionSourceIdentifier.create(sourceName); } + private void convertExceptionIfNeeded(final DataIntegrityViolationException dataIntegrityViolationException, + final Collection<YangResourceEntity> newYangResourceEntities) { + final Optional<DuplicatedYangResourceException> convertedException = + convertToDuplicatedYangResourceException( + dataIntegrityViolationException, newYangResourceEntities); + throw convertedException.isPresent() ? convertedException.get() : dataIntegrityViolationException; + } + /** * Convert the specified data integrity violation exception into a CPS duplicated Yang resource exception * if the cause of the error is a yang checksum database constraint violation. @@ -372,6 +388,13 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ return "no checksum found"; } + private Set<YangResourceEntity> getYangResourceEntities(final Collection<ModuleReference> moduleReferences) { + return moduleReferences.stream().map(moduleReference -> + yangResourceRepository + .findByModuleNameAndRevision(moduleReference.getModuleName(), moduleReference.getRevision())) + .collect(Collectors.toSet()); + } + private static ModuleReference toModuleReference( final YangResourceModuleReference yangResourceModuleReference) { return ModuleReference.builder() @@ -392,20 +415,4 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ .dataspaceName(schemaSetEntity.getDataspace().getName()).build(); } - private void storeAndLinkNewModules(final Map<String, String> newModuleNameToContentMap, - final SchemaSetEntity schemaSetEntity) { - final Set<YangResourceEntity> yangResourceEntities - = new HashSet<>(synchronizeYangResources(newModuleNameToContentMap)); - schemaSetEntity.setYangResources(yangResourceEntities); - schemaSetRepository.save(schemaSetEntity); - } - - private void updateAllModuleReferences(final Collection<ModuleReference> allModuleReferences, - final Integer schemaSetEntityId) { - yangResourceRepository.deleteSchemaSetYangResourceForSchemaSetId(schemaSetEntityId); - final List<Integer> allYangResourceIds = - yangResourceRepository.getResourceIdsByModuleReferences(allModuleReferences); - yangResourceRepository.insertSchemaSetIdYangResourceId(schemaSetEntityId, allYangResourceIds); - } - } diff --git a/cps-ri/src/main/java/org/onap/cps/ri/repository/FragmentQueryBuilder.java b/cps-ri/src/main/java/org/onap/cps/ri/repository/FragmentQueryBuilder.java index 3f3ca79900..5563ba6d50 100644 --- a/cps-ri/src/main/java/org/onap/cps/ri/repository/FragmentQueryBuilder.java +++ b/cps-ri/src/main/java/org/onap/cps/ri/repository/FragmentQueryBuilder.java @@ -1,6 +1,6 @@ /* * ============LICENSE_START======================================================= - * Copyright (C) 2022-2024 Nordix Foundation + * Copyright (C) 2022-2025 Nordix Foundation * Modifications Copyright (C) 2023 TechMahindra Ltd. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -51,13 +51,18 @@ public class FragmentQueryBuilder { private EntityManager entityManager; /** - * Create a sql query to retrieve by anchor(id) and cps path. + * Create a sql query to retrieve by anchor(id) and cps path with an optional queryResultLimit on results. * * @param anchorEntity the anchor * @param cpsPathQuery the cps path query to be transformed into a sql query + * @param queryResultLimit queryResultLimit number of returned entities + * (if the queryResultLimit is less than 1 the method returns all related entities) + * * @return a executable query object */ - public Query getQueryForAnchorAndCpsPath(final AnchorEntity anchorEntity, final CpsPathQuery cpsPathQuery) { + public Query getQueryForAnchorAndCpsPath(final AnchorEntity anchorEntity, + final CpsPathQuery cpsPathQuery, + final int queryResultLimit) { final StringBuilder sqlStringBuilder = new StringBuilder(); final Map<String, Object> queryParameters = new HashMap<>(); @@ -65,6 +70,7 @@ public class FragmentQueryBuilder { addWhereClauseForAnchor(anchorEntity, sqlStringBuilder, queryParameters); addNodeSearchConditions(cpsPathQuery, sqlStringBuilder, queryParameters, false); addSearchSuffix(cpsPathQuery, sqlStringBuilder, queryParameters); + addLimitClause(sqlStringBuilder, queryParameters, queryResultLimit); return getQuery(sqlStringBuilder.toString(), queryParameters, FragmentEntity.class); } @@ -219,6 +225,15 @@ public class FragmentQueryBuilder { } } + private static void addLimitClause(final StringBuilder sqlStringBuilder, + final Map<String, Object> queryParameters, + final int queryResultLimit) { + if (queryResultLimit > 0) { + sqlStringBuilder.append(" LIMIT :queryResultLimit"); + queryParameters.put("queryResultLimit", queryResultLimit); + } + } + private static Integer getTextValueAsInt(final CpsPathQuery cpsPathQuery) { try { return Integer.parseInt(cpsPathQuery.getTextFunctionConditionValue()); diff --git a/cps-ri/src/main/java/org/onap/cps/ri/repository/FragmentRepositoryCpsPathQuery.java b/cps-ri/src/main/java/org/onap/cps/ri/repository/FragmentRepositoryCpsPathQuery.java index 9c1929eaf7..50c7494b29 100644 --- a/cps-ri/src/main/java/org/onap/cps/ri/repository/FragmentRepositoryCpsPathQuery.java +++ b/cps-ri/src/main/java/org/onap/cps/ri/repository/FragmentRepositoryCpsPathQuery.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2021-2023 Nordix Foundation. + * Copyright (C) 2021-2025 Nordix Foundation. * Modifications Copyright (C) 2023 TechMahindra Ltd. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -29,7 +29,9 @@ import org.onap.cps.ri.models.DataspaceEntity; import org.onap.cps.ri.models.FragmentEntity; public interface FragmentRepositoryCpsPathQuery { - List<FragmentEntity> findByAnchorAndCpsPath(AnchorEntity anchorEntity, CpsPathQuery cpsPathQuery); + + List<FragmentEntity> findByAnchorAndCpsPath(AnchorEntity anchorEntity, CpsPathQuery cpsPathQuery, + int queryResultLimit); List<FragmentEntity> findByDataspaceAndCpsPath(DataspaceEntity dataspaceEntity, CpsPathQuery cpsPathQuery, List<Long> anchorIds); diff --git a/cps-ri/src/main/java/org/onap/cps/ri/repository/FragmentRepositoryCpsPathQueryImpl.java b/cps-ri/src/main/java/org/onap/cps/ri/repository/FragmentRepositoryCpsPathQueryImpl.java index e8c2725670..80fbe9b6cd 100644 --- a/cps-ri/src/main/java/org/onap/cps/ri/repository/FragmentRepositoryCpsPathQueryImpl.java +++ b/cps-ri/src/main/java/org/onap/cps/ri/repository/FragmentRepositoryCpsPathQueryImpl.java @@ -41,10 +41,15 @@ public class FragmentRepositoryCpsPathQueryImpl implements FragmentRepositoryCps @Override @Transactional public List<FragmentEntity> findByAnchorAndCpsPath(final AnchorEntity anchorEntity, - final CpsPathQuery cpsPathQuery) { - final Query query = fragmentQueryBuilder.getQueryForAnchorAndCpsPath(anchorEntity, cpsPathQuery); + final CpsPathQuery cpsPathQuery, + final int queryResultLimit) { + final Query query = fragmentQueryBuilder + .getQueryForAnchorAndCpsPath(anchorEntity, cpsPathQuery, queryResultLimit); final List<FragmentEntity> fragmentEntities = query.getResultList(); log.debug("Fetched {} fragment entities by anchor and cps path.", fragmentEntities.size()); + if (queryResultLimit > 0) { + log.debug("Result limited to {} entries", queryResultLimit); + } return fragmentEntities; } diff --git a/cps-ri/src/main/java/org/onap/cps/ri/repository/SchemaSetYangResourceRepository.java b/cps-ri/src/main/java/org/onap/cps/ri/repository/SchemaSetYangResourceRepository.java deleted file mode 100644 index 410dcc2e26..0000000000 --- a/cps-ri/src/main/java/org/onap/cps/ri/repository/SchemaSetYangResourceRepository.java +++ /dev/null @@ -1,36 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2021-2025 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.ri.repository; - -import java.util.List; - -public interface SchemaSetYangResourceRepository { - - - /** - * Link yang resources (ids) with a schema set (id). - * - * @param schemaSetId the schema set id - * @param yangResourceIds list of yang resource ids - */ - void insertSchemaSetIdYangResourceId(final Integer schemaSetId, final List<Integer> yangResourceIds); - -} diff --git a/cps-ri/src/main/java/org/onap/cps/ri/repository/SchemaSetYangResourceRepositoryImpl.java b/cps-ri/src/main/java/org/onap/cps/ri/repository/SchemaSetYangResourceRepositoryImpl.java deleted file mode 100644 index 989809af5b..0000000000 --- a/cps-ri/src/main/java/org/onap/cps/ri/repository/SchemaSetYangResourceRepositoryImpl.java +++ /dev/null @@ -1,58 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2021-2023 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.ri.repository; - -import jakarta.persistence.EntityManager; -import jakarta.persistence.PersistenceContext; -import java.sql.PreparedStatement; -import java.util.List; -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<Integer> 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 int yangResourceId : yangResourceIds) { - preparedStatement.setInt(1, schemaSetId); - preparedStatement.setInt(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/ri/repository/YangResourceNativeRepository.java b/cps-ri/src/main/java/org/onap/cps/ri/repository/YangResourceNativeRepository.java deleted file mode 100644 index 2875511c1e..0000000000 --- a/cps-ri/src/main/java/org/onap/cps/ri/repository/YangResourceNativeRepository.java +++ /dev/null @@ -1,31 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2022-2023 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.ri.repository; - -import java.util.Collection; -import java.util.List; -import org.onap.cps.api.model.ModuleReference; - -public interface YangResourceNativeRepository { - - List<Integer> getResourceIdsByModuleReferences(Collection<ModuleReference> moduleReferences); - -} diff --git a/cps-ri/src/main/java/org/onap/cps/ri/repository/YangResourceNativeRepositoryImpl.java b/cps-ri/src/main/java/org/onap/cps/ri/repository/YangResourceNativeRepositoryImpl.java deleted file mode 100644 index 34f1ee362a..0000000000 --- a/cps-ri/src/main/java/org/onap/cps/ri/repository/YangResourceNativeRepositoryImpl.java +++ /dev/null @@ -1,69 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2022-2023 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.ri.repository; - -import jakarta.persistence.EntityManager; -import jakarta.persistence.PersistenceContext; -import jakarta.persistence.Query; -import java.util.Collection; -import java.util.Collections; -import java.util.List; -import java.util.StringJoiner; -import lombok.extern.slf4j.Slf4j; -import org.hibernate.type.StandardBasicTypes; -import org.onap.cps.api.model.ModuleReference; -import org.springframework.stereotype.Repository; -import org.springframework.transaction.annotation.Transactional; - -@Slf4j -@Repository -public class YangResourceNativeRepositoryImpl implements YangResourceNativeRepository { - - @PersistenceContext - private EntityManager entityManager; - - @Override - @Transactional - public List<Integer> getResourceIdsByModuleReferences(final Collection<ModuleReference> moduleReferences) { - if (moduleReferences.isEmpty()) { - return Collections.emptyList(); - } - final Query query = entityManager.createNativeQuery(getCombinedSelectSqlQuery(moduleReferences)) - .unwrap(org.hibernate.query.NativeQuery.class) - .addScalar("id", StandardBasicTypes.INTEGER); - final List<Integer> yangResourceIds = query.getResultList(); - if (yangResourceIds.size() != moduleReferences.size()) { - log.warn("ModuleReferences size : {} and QueryResult size : {}", moduleReferences.size(), - yangResourceIds.size()); - } - return yangResourceIds; - } - - private String getCombinedSelectSqlQuery(final Collection<ModuleReference> moduleReferences) { - final StringJoiner sqlQueryJoiner = new StringJoiner(" UNION ALL "); - moduleReferences.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/ri/repository/YangResourceRepository.java b/cps-ri/src/main/java/org/onap/cps/ri/repository/YangResourceRepository.java index 628502f846..e36e376bc6 100644 --- a/cps-ri/src/main/java/org/onap/cps/ri/repository/YangResourceRepository.java +++ b/cps-ri/src/main/java/org/onap/cps/ri/repository/YangResourceRepository.java @@ -32,8 +32,10 @@ import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; @Repository -public interface YangResourceRepository extends JpaRepository<YangResourceEntity, Integer>, - YangResourceNativeRepository, SchemaSetYangResourceRepository { +public interface YangResourceRepository extends JpaRepository<YangResourceEntity, Integer> { + + YangResourceEntity findByModuleNameAndRevision(@Param("moduleName") String moduleName, + @Param("revision") String revision); List<YangResourceEntity> findAllByChecksumIn(Collection<String> checksums); @@ -88,10 +90,6 @@ public interface YangResourceRepository extends JpaRepository<YangResourceEntity @Param("moduleName") String moduleName, @Param("revision") String revision); @Modifying - @Query(value = "DELETE FROM schema_set_yang_resources WHERE schema_set_id = :schemaSetId", nativeQuery = true) - void deleteSchemaSetYangResourceForSchemaSetId(@Param("schemaSetId") int schemaSetId); - - @Modifying @Query(value = """ DELETE FROM yang_resource WHERE NOT EXISTS (SELECT 1 FROM schema_set_yang_resources WHERE schema_set_yang_resources.yang_resource_id = yang_resource.id) diff --git a/cps-ri/src/main/java/org/onap/cps/ri/utils/CpsSessionFactory.java b/cps-ri/src/main/java/org/onap/cps/ri/utils/CpsSessionFactory.java index c0291176f4..d4cd366157 100644 --- a/cps-ri/src/main/java/org/onap/cps/ri/utils/CpsSessionFactory.java +++ b/cps-ri/src/main/java/org/onap/cps/ri/utils/CpsSessionFactory.java @@ -1,6 +1,6 @@ /* * ============LICENSE_START======================================================= - * Copyright (C) 2022 Nordix Foundation + * Copyright (C) 2022-2025 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,22 +20,20 @@ package org.onap.cps.ri.utils; +import lombok.RequiredArgsConstructor; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.SessionFactory; -import org.onap.cps.ri.models.AnchorEntity; -import org.onap.cps.ri.models.DataspaceEntity; -import org.onap.cps.ri.models.SchemaSetEntity; -import org.onap.cps.ri.models.YangResourceEntity; import org.springframework.beans.factory.config.ConfigurableBeanFactory; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; @Component @Scope(ConfigurableBeanFactory.SCOPE_SINGLETON) +@RequiredArgsConstructor public class CpsSessionFactory { - private SessionFactory sessionFactory = null; + private final SessionFactory sessionFactory; /** * Open a session from session factory. @@ -44,7 +42,7 @@ public class CpsSessionFactory { * @throws HibernateException hibernate exception */ public Session openSession() throws HibernateException { - return getSessionFactory().openSession(); + return sessionFactory.openSession(); } /** @@ -53,18 +51,6 @@ public class CpsSessionFactory { * @throws HibernateException hibernate exception */ public void closeSessionFactory() throws HibernateException { - getSessionFactory().close(); - } - - private SessionFactory getSessionFactory() { - if (sessionFactory == null) { - sessionFactory = new org.hibernate.cfg.Configuration().configure("hibernate.cfg.xml") - .addAnnotatedClass(AnchorEntity.class) - .addAnnotatedClass(DataspaceEntity.class) - .addAnnotatedClass(SchemaSetEntity.class) - .addAnnotatedClass(YangResourceEntity.class) - .buildSessionFactory(); - } - return sessionFactory; + sessionFactory.close(); } } diff --git a/cps-ri/src/main/java/org/onap/cps/ri/utils/CpsValidatorImpl.java b/cps-ri/src/main/java/org/onap/cps/ri/utils/CpsValidatorImpl.java deleted file mode 100644 index 9e89c8aed9..0000000000 --- a/cps-ri/src/main/java/org/onap/cps/ri/utils/CpsValidatorImpl.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * Copyright (C) 2022-2023 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.ri.utils; - -import com.google.common.collect.Lists; -import java.util.Arrays; -import java.util.Collection; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.onap.cps.api.exceptions.DataValidationException; -import org.onap.cps.api.parameters.PaginationOption; -import org.onap.cps.utils.CpsValidator; -import org.springframework.stereotype.Component; - -@Slf4j -@Component -@RequiredArgsConstructor -public class CpsValidatorImpl implements CpsValidator { - - private static final char[] UNSUPPORTED_NAME_CHARACTERS = "!\" #$%&'()*+,./\\:;<=>?@[]^`{|}~".toCharArray(); - - @Override - public void validateNameCharacters(final String... names) { - validateNameCharacters(Arrays.asList(names)); - } - - @Override - public void validateNameCharacters(final Iterable<String> names) { - for (final String name : names) { - final Collection<Character> charactersOfName = Lists.charactersOf(name); - for (final char unsupportedCharacter : UNSUPPORTED_NAME_CHARACTERS) { - if (charactersOfName.contains(unsupportedCharacter)) { - throw new DataValidationException("Name or ID Validation Error.", - name + " invalid token encountered at position " - + (name.indexOf(unsupportedCharacter) + 1)); - } - } - } - } - - @Override - public void validatePaginationOption(final PaginationOption paginationOption) { - if (PaginationOption.NO_PAGINATION == paginationOption) { - return; - } - - if (!paginationOption.isValidPaginationOption()) { - throw new DataValidationException("Pagination validation error.", - "Invalid page index or size"); - } - } -} diff --git a/cps-ri/src/main/resources/hibernate.cfg.xml b/cps-ri/src/main/resources/hibernate.cfg.xml deleted file mode 100644 index 1b822b9de9..0000000000 --- a/cps-ri/src/main/resources/hibernate.cfg.xml +++ /dev/null @@ -1,16 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<!DOCTYPE hibernate-configuration PUBLIC - "-//Hibernate/Hibernate Configuration DTD 3.0//EN" - "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> - -<hibernate-configuration> - <session-factory> - <property name="hibernate.connection.driver_class">org.postgresql.Driver</property> - <property name="hibernate.connection.url">jdbc:postgresql://${DB_HOST}:${DB_PORT:5432}/cpsdb</property> - <property name="hibernate.connection.username">${DB_USERNAME}</property> - <property name="hibernate.connection.password">${DB_PASSWORD}</property> - <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property> - <property name="show_sql">true</property> - <property name="hibernate.hbm2ddl.auto">update</property> - </session-factory> -</hibernate-configuration>
\ No newline at end of file diff --git a/cps-ri/src/test/groovy/org/onap/cps/ri/CpsModulePersistenceServiceImplSpec.groovy b/cps-ri/src/test/groovy/org/onap/cps/ri/CpsModulePersistenceServiceImplSpec.groovy index 2915bc8e8c..4bf8c7ca09 100644 --- a/cps-ri/src/test/groovy/org/onap/cps/ri/CpsModulePersistenceServiceImplSpec.groovy +++ b/cps-ri/src/test/groovy/org/onap/cps/ri/CpsModulePersistenceServiceImplSpec.groovy @@ -1,7 +1,7 @@ /* * ============LICENSE_START======================================================= * Copyright (c) 2021 Bell Canada. - * Modifications Copyright (C) 2022-2023 Nordix Foundation + * Modifications Copyright (C) 2022-2025 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,6 +20,7 @@ package org.onap.cps.ri import org.hibernate.exception.ConstraintViolationException +import org.onap.cps.ri.models.DataspaceEntity import org.onap.cps.ri.models.SchemaSetEntity import org.onap.cps.ri.repository.DataspaceRepository import org.onap.cps.ri.repository.ModuleReferenceRepository @@ -30,7 +31,6 @@ import org.onap.cps.api.exceptions.DuplicatedYangResourceException import org.onap.cps.api.model.ModuleReference import org.springframework.dao.DataIntegrityViolationException import spock.lang.Specification - import java.sql.SQLException /** @@ -78,8 +78,8 @@ class CpsModulePersistenceServiceImplSpec extends Specification { and: 'persisting yang resource raises db constraint exception (in case of concurrent requests for example)' mockYangResourceRepository.saveAll(_) >> { throw dbException } when: 'attempt to store schema set ' - def newYangResourcesNameToContentMap = [(yangResourceName):yangResourceContent] - objectUnderTest.storeSchemaSet('my-dataspace', 'my-schema-set', newYangResourcesNameToContentMap) + def newYangResourceContentPerName = [(yangResourceName):yangResourceContent] + objectUnderTest.createSchemaSet('my-dataspace', 'my-schema-set', newYangResourceContentPerName) then: 'an #expectedThrownException is thrown' def e = thrown(expectedThrownException) assert e.getMessage().contains(expectedThrownExceptionMessage) @@ -96,9 +96,37 @@ class CpsModulePersistenceServiceImplSpec extends Specification { def schemaSetEntity = new SchemaSetEntity(id: 1) mockSchemaSetRepository.getByDataspaceAndName(_, _) >> schemaSetEntity when: 'schema set update is requested' - objectUnderTest.updateSchemaSetFromModules('my-dataspace', 'my-schemaset', [:], [new ModuleReference('some module name', 'some revision name')]) + objectUnderTest.updateSchemaSetFromNewAndExistingModules('my-dataspace', 'my-schemaset', [:], [new ModuleReference('some module name', 'some revision name')]) then: 'no exception is thrown ' noExceptionThrown() } + def 'Get yang schema resources.' () { + given: 'mocked methods for dataspace and schema set repositories' + mockDataspaceRepository.getByName('someDataspaceName') >> new DataspaceEntity() + mockSchemaSetRepository.getByDataspaceAndName(_,_) >> new SchemaSetEntity(yangResources: []) + when: 'the get yang schema resources method is called' + def result = objectUnderTest.getYangSchemaResources('someDataspaceName', 'someSchemaSetName') + then: 'an empty map is returned' + assert result.isEmpty() + } + + def 'Get yang module references with just dataspace name.' () { + given: 'mocked method return yang resource repository' + mockYangResourceRepository.findAllModuleReferencesByDataspace('someDataspaceName') >> [] + when: 'the get yang resource module reference method is called with 1 parameter' + def result = objectUnderTest.getYangResourceModuleReferences('someDataspaceName') + then: 'an empty collection is returned' + assert result.isEmpty() + } + + def 'Get yang module references with dataspace name and anchor.' () { + given: 'mocked method return yang resource repository' + mockYangResourceRepository.findAllModuleReferencesByDataspaceAndAnchor('someDataspaceName', 'someAnchorName') >> [] + when: 'the get yang resource module reference method is called with 2 parameters' + def result = objectUnderTest.getYangResourceModuleReferences('someDataspaceName','someAnchorName') + then: 'an empty collection is returned' + assert result.isEmpty() + } + } diff --git a/cps-ri/src/test/groovy/org/onap/cps/ri/utils/CpsValidatorImplSpec.groovy b/cps-ri/src/test/groovy/org/onap/cps/ri/utils/CpsValidatorImplSpec.groovy deleted file mode 100644 index f7c4798772..0000000000 --- a/cps-ri/src/test/groovy/org/onap/cps/ri/utils/CpsValidatorImplSpec.groovy +++ /dev/null @@ -1,78 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * Copyright (C) 2022-2023 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.ri.utils - - -import org.onap.cps.api.parameters.PaginationOption -import org.onap.cps.api.exceptions.DataValidationException -import spock.lang.Specification - -class CpsValidatorImplSpec extends Specification { - - def objectUnderTest = new CpsValidatorImpl() - - def 'Validating a valid string.'() { - when: 'the string is validated using a valid name' - objectUnderTest.validateNameCharacters('name-with-no-spaces') - then: 'no exception is thrown' - noExceptionThrown() - } - - def 'Validating an invalid string.'() { - when: 'the string is validated using an invalid name' - objectUnderTest.validateNameCharacters(name) - then: 'a data validation exception is thrown' - def exceptionThrown = thrown(DataValidationException) - and: 'the error was encountered at the following index in #scenario' - assert exceptionThrown.getDetails().contains(expectedErrorMessage) - where: 'the following names are used' - scenario | name || expectedErrorMessage - 'position 5' | 'name with spaces' || 'name with spaces invalid token encountered at position 5' - 'position 9' | 'nameWith Space' || 'nameWith Space invalid token encountered at position 9' - } - - def 'Validating a list of valid names.'() { - given: 'a list of valid names' - def names = ['valid-name', 'another-valid-name'] - when: 'a list of strings is validated' - objectUnderTest.validateNameCharacters(names) - then: 'no exception is thrown' - noExceptionThrown() - } - - def 'Validating a list of names with invalid names.'() { - given: 'a list of names with an invalid name' - def names = ['valid-name', 'name with spaces'] - when: 'a list of strings is validated' - objectUnderTest.validateNameCharacters(names) - then: 'a data validation exception is thrown' - thrown(DataValidationException) - } - - def 'Validate Pagination option with invalid page index and size.'() { - when: 'the pagination option is validated using invalid options' - objectUnderTest.validatePaginationOption(new PaginationOption(-5, -2)) - then: 'a data validation exception is thrown' - def exceptionThrown = thrown(DataValidationException) - and: 'the error was encountered at the following index in #scenario' - assert exceptionThrown.getDetails().contains("Invalid page index or size") - } -} |