From a16c3fb5f7f0bbe7bda9e4f21f71a3e958ed523f Mon Sep 17 00:00:00 2001 From: "Rishi.Chail" Date: Wed, 6 Jan 2021 13:09:34 +0000 Subject: Retrieve the SchemaSet resources for an Anchor Issue-ID: CPS-135 Signed-off-by: Rishi.Chail Change-Id: Ib7c17c5180212c7dd3b96cf27797c84166041cf9 --- cps-parent/pom.xml | 2 +- .../spi/impl/CpsAdminPersistenceServiceImpl.java | 10 +++++- .../spi/impl/CpsModulePersistenceServiceImpl.java | 12 +++++++ .../onap/cps/spi/repository/AnchorRepository.java | 9 ++++- .../spi/impl/CpsAdminPersistenceServiceTest.java | 32 ++++++++++++++---- .../spi/impl/CpsModulePersistenceServiceTest.java | 20 ++++++++++-- .../onap/cps/spi/CpsAdminPersistenceService.java | 12 ++++++- .../onap/cps/spi/CpsModulePersistenceService.java | 22 ++++++++++--- .../spi/exceptions/AnchorNotFoundException.java | 38 ++++++++++++++++++++++ .../cps/api/impl/CpsAdminServiceImplSpec.groovy | 9 +++-- .../cps/spi/exceptions/CpsExceptionsSpec.groovy | 8 ++++- 11 files changed, 150 insertions(+), 24 deletions(-) mode change 100644 => 100755 cps-ri/src/main/java/org/onap/cps/spi/impl/CpsAdminPersistenceServiceImpl.java mode change 100644 => 100755 cps-ri/src/main/java/org/onap/cps/spi/impl/CpsModulePersistenceServiceImpl.java mode change 100644 => 100755 cps-ri/src/main/java/org/onap/cps/spi/repository/AnchorRepository.java mode change 100644 => 100755 cps-ri/src/test/java/org/onap/cps/spi/impl/CpsAdminPersistenceServiceTest.java mode change 100644 => 100755 cps-ri/src/test/java/org/onap/cps/spi/impl/CpsModulePersistenceServiceTest.java mode change 100644 => 100755 cps-service/src/main/java/org/onap/cps/spi/CpsAdminPersistenceService.java mode change 100644 => 100755 cps-service/src/main/java/org/onap/cps/spi/CpsModulePersistenceService.java create mode 100755 cps-service/src/main/java/org/onap/cps/spi/exceptions/AnchorNotFoundException.java mode change 100644 => 100755 cps-service/src/test/groovy/org/onap/cps/api/impl/CpsAdminServiceImplSpec.groovy mode change 100644 => 100755 cps-service/src/test/groovy/org/onap/cps/spi/exceptions/CpsExceptionsSpec.groovy diff --git a/cps-parent/pom.xml b/cps-parent/pom.xml index 125c843114..01eacb3023 100755 --- a/cps-parent/pom.xml +++ b/cps-parent/pom.xml @@ -20,7 +20,7 @@ openjdk:11-jre-slim 11 2.6.0 - 0.76 + 0.80 https://nexus.onap.org 3.1.0 UTF-8 diff --git a/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsAdminPersistenceServiceImpl.java b/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsAdminPersistenceServiceImpl.java old mode 100644 new mode 100755 index dfe3ecc119..ddbfeb2283 --- a/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsAdminPersistenceServiceImpl.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsAdminPersistenceServiceImpl.java @@ -82,6 +82,14 @@ public class CpsAdminPersistenceServiceImpl implements CpsAdminPersistenceServic return anchorEntities.stream().map(CpsAdminPersistenceServiceImpl::toAnchor).collect(Collectors.toList()); } + @Override + public Anchor getAnchor(final String dataspaceName, final String anchorName) { + final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName); + final AnchorEntity anchorEntity = + anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName); + return toAnchor(anchorEntity); + } + private static Anchor toAnchor(final AnchorEntity anchorEntity) { return Anchor.builder() .name(anchorEntity.getName()) @@ -89,4 +97,4 @@ public class CpsAdminPersistenceServiceImpl implements CpsAdminPersistenceServic .schemaSetName(anchorEntity.getSchemaSet().getName()) .build(); } -} +} \ No newline at end of file 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 old mode 100644 new mode 100755 index cbc945da60..0a870bac14 --- 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 @@ -28,11 +28,13 @@ import java.util.Map; import java.util.Set; import java.util.stream.Collectors; import javax.transaction.Transactional; +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.exceptions.SchemaSetAlreadyDefinedException; +import org.onap.cps.spi.model.Anchor; import org.onap.cps.spi.repository.DataspaceRepository; import org.onap.cps.spi.repository.SchemaSetRepository; import org.onap.cps.spi.repository.YangResourceRepository; @@ -53,6 +55,9 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ @Autowired private DataspaceRepository dataspaceRepository; + @Autowired + private CpsAdminPersistenceService cpsAdminPersistenceService; + @Override @Transactional public void storeSchemaSet(final String dataspaceName, final String schemaSetName, @@ -109,4 +114,11 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ return schemaSetEntity.getYangResources().stream().collect( Collectors.toMap(YangResourceEntity::getName, YangResourceEntity::getContent)); } + + @Override + public Map getYangSchemaSetResources(final String dataspaceName, + final String anchorName) { + final Anchor anchor = cpsAdminPersistenceService.getAnchor(dataspaceName, anchorName); + return getYangSchemaResources(dataspaceName, anchor.getSchemaSetName()); + } } diff --git a/cps-ri/src/main/java/org/onap/cps/spi/repository/AnchorRepository.java b/cps-ri/src/main/java/org/onap/cps/spi/repository/AnchorRepository.java old mode 100644 new mode 100755 index 318b4daa6b..cc9c2e09c7 --- a/cps-ri/src/main/java/org/onap/cps/spi/repository/AnchorRepository.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/repository/AnchorRepository.java @@ -24,11 +24,18 @@ import java.util.Optional; import javax.validation.constraints.NotNull; import org.onap.cps.spi.entities.AnchorEntity; import org.onap.cps.spi.entities.DataspaceEntity; +import org.onap.cps.spi.exceptions.AnchorNotFoundException; import org.springframework.data.jpa.repository.JpaRepository; public interface AnchorRepository extends JpaRepository { Optional findByDataspaceAndName(@NotNull DataspaceEntity dataspaceEntity, @NotNull String name); + default AnchorEntity getByDataspaceAndName(@NotNull DataspaceEntity dataspace, + @NotNull String anchorName) { + return findByDataspaceAndName(dataspace, anchorName) + .orElseThrow(() -> new AnchorNotFoundException(anchorName, dataspace.getName())); + } + Collection findAllByDataspace(@NotNull DataspaceEntity dataspaceEntity); -} +} \ No newline at end of file diff --git a/cps-ri/src/test/java/org/onap/cps/spi/impl/CpsAdminPersistenceServiceTest.java b/cps-ri/src/test/java/org/onap/cps/spi/impl/CpsAdminPersistenceServiceTest.java old mode 100644 new mode 100755 index 02487078d0..981f065282 --- a/cps-ri/src/test/java/org/onap/cps/spi/impl/CpsAdminPersistenceServiceTest.java +++ b/cps-ri/src/test/java/org/onap/cps/spi/impl/CpsAdminPersistenceServiceTest.java @@ -32,13 +32,13 @@ import org.onap.cps.spi.CpsAdminPersistenceService; import org.onap.cps.spi.entities.AnchorEntity; import org.onap.cps.spi.entities.DataspaceEntity; import org.onap.cps.spi.exceptions.AnchorAlreadyDefinedException; +import org.onap.cps.spi.exceptions.AnchorNotFoundException; import org.onap.cps.spi.exceptions.DataspaceAlreadyDefinedException; import org.onap.cps.spi.exceptions.DataspaceNotFoundException; import org.onap.cps.spi.exceptions.SchemaSetNotFoundException; import org.onap.cps.spi.model.Anchor; import org.onap.cps.spi.repository.AnchorRepository; import org.onap.cps.spi.repository.DataspaceRepository; -import org.onap.cps.spi.repository.SchemaSetRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.jdbc.Sql; @@ -61,6 +61,7 @@ public class CpsAdminPersistenceServiceTest { private static final String ANCHOR_NAME1 = "ANCHOR-001"; private static final String ANCHOR_NAME2 = "ANCHOR-002"; private static final String ANCHOR_NAME_NEW = "ANCHOR-NEW"; + private static final String NON_EXISTING_ANCHOR_NAME = "NON EXISTING ANCHOR"; @ClassRule public static DatabaseTestContainer databaseTestContainer = DatabaseTestContainer.getInstance(); @@ -74,9 +75,6 @@ public class CpsAdminPersistenceServiceTest { @Autowired private DataspaceRepository dataspaceRepository; - @Autowired - private SchemaSetRepository schemaSetRepository; - @Test @SqlGroup({@Sql(CLEAR_DATA), @Sql(SET_DATA)}) public void testCreateDataspace() { @@ -138,10 +136,10 @@ public class CpsAdminPersistenceServiceTest { assertEquals(2, anchors.size()); assertTrue(anchors.contains( Anchor.builder().name(ANCHOR_NAME1).schemaSetName(SCHEMA_SET_NAME1).dataspaceName(DATASPACE_NAME).build() - )); + )); assertTrue(anchors.contains( Anchor.builder().name(ANCHOR_NAME2).schemaSetName(SCHEMA_SET_NAME2).dataspaceName(DATASPACE_NAME).build() - )); + )); } @Test(expected = DataspaceNotFoundException.class) @@ -159,4 +157,26 @@ public class CpsAdminPersistenceServiceTest { assertTrue(anchors.isEmpty()); } + @Test + @SqlGroup({@Sql(CLEAR_DATA), @Sql(SET_DATA)}) + public void testGetAnchorByDataspaceAndAnchorName() { + final Anchor anchor = cpsAdminPersistenceService.getAnchor(DATASPACE_NAME, ANCHOR_NAME1); + + assertNotNull(anchor); + assertEquals(ANCHOR_NAME1, anchor.getName()); + assertEquals(DATASPACE_NAME, anchor.getDataspaceName()); + } + + @Test(expected = DataspaceNotFoundException.class) + @SqlGroup({@Sql(CLEAR_DATA), @Sql(SET_DATA)}) + public void testGetAnchorFromNonExistingDataspace() { + cpsAdminPersistenceService.getAnchor(NON_EXISTING_DATASPACE_NAME, ANCHOR_NAME1); + } + + @Test(expected = AnchorNotFoundException.class) + @SqlGroup({@Sql(CLEAR_DATA), @Sql(SET_DATA)}) + public void testGetAnchorByNonExistingAnchorName() { + cpsAdminPersistenceService.getAnchor(DATASPACE_NAME, NON_EXISTING_ANCHOR_NAME); + } + } 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 old mode 100644 new mode 100755 index e813d26bf5..8455e57517 --- 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 @@ -29,6 +29,7 @@ import org.junit.ClassRule; import org.junit.Test; import org.junit.runner.RunWith; import org.onap.cps.DatabaseTestContainer; +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; @@ -37,7 +38,6 @@ import org.onap.cps.spi.exceptions.DataspaceNotFoundException; import org.onap.cps.spi.exceptions.SchemaSetAlreadyDefinedException; import org.onap.cps.spi.repository.DataspaceRepository; 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; @@ -52,6 +52,7 @@ public class CpsModulePersistenceServiceTest { private static final String CLEAR_DATA = "/data/clear-all.sql"; private static final String SET_DATA = "/data/schemaset.sql"; + private static final String ANCHOR_NAME = "ANCHOR-001"; private static final String DATASPACE_NAME = "DATASPACE-001"; private static final String DATASPACE_NAME_INVALID = "DATASPACE-X"; private static final String SCHEMA_SET_NAME = "SCHEMA-SET-001"; @@ -74,10 +75,10 @@ public class CpsModulePersistenceServiceTest { private CpsModulePersistenceService cpsModulePersistenceService; @Autowired - private DataspaceRepository dataspaceRepository; + private CpsAdminPersistenceService cpsAdminPersistenceService; @Autowired - private YangResourceRepository yangResourceRepository; + private DataspaceRepository dataspaceRepository; @Autowired private SchemaSetRepository schemaSetRepository; @@ -109,6 +110,19 @@ public class CpsModulePersistenceServiceTest { cpsModulePersistenceService.getYangSchemaResources(DATASPACE_NAME, SCHEMA_SET_NAME_NEW)); } + @Test + @SqlGroup({@Sql(CLEAR_DATA), @Sql(SET_DATA)}) + public void testGetYangResourcesWithAnchorName() { + final Map yangResourcesNameToContentMap = + toMap(NEW_RESOURCE_NAME, NEW_RESOURCE_CONTENT); + cpsModulePersistenceService.storeSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_NEW, + yangResourcesNameToContentMap); + + cpsAdminPersistenceService.createAnchor(DATASPACE_NAME, SCHEMA_SET_NAME_NEW, ANCHOR_NAME); + assertEquals(yangResourcesNameToContentMap, + cpsModulePersistenceService.getYangSchemaSetResources(DATASPACE_NAME, ANCHOR_NAME)); + } + @Test @SqlGroup({@Sql(CLEAR_DATA), @Sql(SET_DATA)}) public void testStoreSchemaSetWithExistingYangResourceReuse() { diff --git a/cps-service/src/main/java/org/onap/cps/spi/CpsAdminPersistenceService.java b/cps-service/src/main/java/org/onap/cps/spi/CpsAdminPersistenceService.java old mode 100644 new mode 100755 index 5643aedb72..06c04ceb62 --- a/cps-service/src/main/java/org/onap/cps/spi/CpsAdminPersistenceService.java +++ b/cps-service/src/main/java/org/onap/cps/spi/CpsAdminPersistenceService.java @@ -57,4 +57,14 @@ public interface CpsAdminPersistenceService { */ @NonNull Collection getAnchors(@NonNull String dataspaceName); -} + + /** + * Get an anchor in the given dataspace using the anchor name. + * + * @param dataspaceName dataspace name + * @param anchorName anchor name + * @return an anchor + */ + @NonNull + Anchor getAnchor(@NonNull String dataspaceName, @NonNull String anchorName); +} \ No newline at end of file diff --git a/cps-service/src/main/java/org/onap/cps/spi/CpsModulePersistenceService.java b/cps-service/src/main/java/org/onap/cps/spi/CpsModulePersistenceService.java old mode 100644 new mode 100755 index a82d69acbd..dc4e26b913 --- a/cps-service/src/main/java/org/onap/cps/spi/CpsModulePersistenceService.java +++ b/cps-service/src/main/java/org/onap/cps/spi/CpsModulePersistenceService.java @@ -36,15 +36,27 @@ public interface CpsModulePersistenceService { * @param yangResourcesNameToContentMap YANG resources (files) map where key is a name and value is content */ void storeSchemaSet(@NonNull String dataspaceName, @NonNull String schemaSetName, - @NonNull Map yangResourcesNameToContentMap); + @NonNull Map yangResourcesNameToContentMap); /** - * Returns YANG resources per specific namespace / schemaSetName. + * Returns YANG resources per specific dataspace / schemaSetName. * - * @param namespace module namespace + * @param dataspaceName dataspace name * @param schemaSetName schema set name * @return YANG resources (files) map where key is a name and value is content */ @NonNull - Map getYangSchemaResources(@NonNull String namespace, @NonNull String schemaSetName); -} + Map getYangSchemaResources(@NonNull String dataspaceName, + @NonNull String schemaSetName); + + /** + * Returns YANG resources per specific dataspace / anchorName. + * + * @param dataspaceName dataspace name + * @param anchorName anchor name + * @return YANG resources (files) map where key is a name and value is content + */ + @NonNull + Map getYangSchemaSetResources(@NonNull String dataspaceName, + @NonNull String anchorName); +} \ No newline at end of file diff --git a/cps-service/src/main/java/org/onap/cps/spi/exceptions/AnchorNotFoundException.java b/cps-service/src/main/java/org/onap/cps/spi/exceptions/AnchorNotFoundException.java new file mode 100755 index 0000000000..847959fc05 --- /dev/null +++ b/cps-service/src/main/java/org/onap/cps/spi/exceptions/AnchorNotFoundException.java @@ -0,0 +1,38 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation. All rights reserved. + * ================================================================================ + * 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.exceptions; + +public class AnchorNotFoundException extends CpsAdminException { + + private static final long serialVersionUID = -1821064664642194882L; + + /** + * Constructor. + * + * @param anchorName the name of the anchor + * @param dataspaceName the dataspace name + */ + public AnchorNotFoundException(final String anchorName, final String dataspaceName) { + super("Anchor not found", + String.format("Anchor with name %s does not exist in dataspace %s.", anchorName, + dataspaceName)); + } +} \ No newline at end of file diff --git a/cps-service/src/test/groovy/org/onap/cps/api/impl/CpsAdminServiceImplSpec.groovy b/cps-service/src/test/groovy/org/onap/cps/api/impl/CpsAdminServiceImplSpec.groovy old mode 100644 new mode 100755 index 022282493b..5aaa340271 --- a/cps-service/src/test/groovy/org/onap/cps/api/impl/CpsAdminServiceImplSpec.groovy +++ b/cps-service/src/test/groovy/org/onap/cps/api/impl/CpsAdminServiceImplSpec.groovy @@ -41,17 +41,16 @@ class CpsAdminServiceImplSpec extends Specification { def 'Create anchor method invokes persistence service'() { when: 'Create anchor method is invoked' - objectUnderTest.createAnchor('dummyDataspace', 'dummySchemaSet', 'dummyAnchorName') + objectUnderTest.createAnchor('someDataspace', 'someSchemaSet', 'someAnchorName') then: 'The persistence service method is invoked with same parameters' - 1 * mockCpsAdminPersistenceService.createAnchor('dummyDataspace', 'dummySchemaSet', 'dummyAnchorName') + 1 * mockCpsAdminPersistenceService.createAnchor('someDataspace', 'someSchemaSet', 'someAnchorName') } def 'Retrieve all anchors for dataspace'() { given: 'that anchor is associated with the dataspace' Collection anchorCollection = Arrays.asList(new Anchor()) - mockCpsAdminPersistenceService.getAnchors('dummyDataspace') >> { anchorCollection } + mockCpsAdminPersistenceService.getAnchors('someDataspace') >> { anchorCollection } expect: 'the collection provided by persistence service is returned as result' - objectUnderTest.getAnchors('dummyDataspace') == anchorCollection + objectUnderTest.getAnchors('someDataspace') == anchorCollection } - } diff --git a/cps-service/src/test/groovy/org/onap/cps/spi/exceptions/CpsExceptionsSpec.groovy b/cps-service/src/test/groovy/org/onap/cps/spi/exceptions/CpsExceptionsSpec.groovy old mode 100644 new mode 100755 index 067556dd58..e00a640d47 --- a/cps-service/src/test/groovy/org/onap/cps/spi/exceptions/CpsExceptionsSpec.groovy +++ b/cps-service/src/test/groovy/org/onap/cps/spi/exceptions/CpsExceptionsSpec.groovy @@ -104,4 +104,10 @@ class CpsExceptionsSpec extends Specification { (new SchemaSetNotFoundException(dataspaceName,schemaSetName)).details == "Schema Set with name ${schemaSetName} was not found for dataspace ${dataspaceName}." } -} + + def 'Creating a exception that an anchor cannot be found.'() { + expect: 'the exception details contains the correct message with dataspace and anchor name' + (new AnchorNotFoundException(anchorName, dataspaceName)).details + == "Anchor with name ${anchorName} does not exist in dataspace ${dataspaceName}." + } +} \ No newline at end of file -- cgit 1.2.3-korg