<base.image>openjdk:11-jre-slim</base.image>
<java.version>11</java.version>
<jib-maven-plugin.version>2.6.0</jib-maven-plugin.version>
- <minimum-coverage>0.76</minimum-coverage>
+ <minimum-coverage>0.80</minimum-coverage>
<nexusproxy>https://nexus.onap.org</nexusproxy>
<oparent.version>3.1.0</oparent.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
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())
.schemaSetName(anchorEntity.getSchemaSet().getName())
.build();
}
-}
+}
\ No newline at end of file
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;
@Autowired
private DataspaceRepository dataspaceRepository;
+ @Autowired
+ private CpsAdminPersistenceService cpsAdminPersistenceService;
+
@Override
@Transactional
public void storeSchemaSet(final String dataspaceName, final String schemaSetName,
return schemaSetEntity.getYangResources().stream().collect(
Collectors.toMap(YangResourceEntity::getName, YangResourceEntity::getContent));
}
+
+ @Override
+ public Map<String, String> getYangSchemaSetResources(final String dataspaceName,
+ final String anchorName) {
+ final Anchor anchor = cpsAdminPersistenceService.getAnchor(dataspaceName, anchorName);
+ return getYangSchemaResources(dataspaceName, anchor.getSchemaSetName());
+ }
}
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<AnchorEntity, Integer> {
Optional<AnchorEntity> 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<AnchorEntity> findAllByDataspace(@NotNull DataspaceEntity dataspaceEntity);
-}
+}
\ No newline at end of file
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;
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();
@Autowired
private DataspaceRepository dataspaceRepository;
- @Autowired
- private SchemaSetRepository schemaSetRepository;
-
@Test
@SqlGroup({@Sql(CLEAR_DATA), @Sql(SET_DATA)})
public void testCreateDataspace() {
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)
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);
+ }
+
}
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;
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;
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";
private CpsModulePersistenceService cpsModulePersistenceService;
@Autowired
- private DataspaceRepository dataspaceRepository;
+ private CpsAdminPersistenceService cpsAdminPersistenceService;
@Autowired
- private YangResourceRepository yangResourceRepository;
+ private DataspaceRepository dataspaceRepository;
@Autowired
private SchemaSetRepository schemaSetRepository;
cpsModulePersistenceService.getYangSchemaResources(DATASPACE_NAME, SCHEMA_SET_NAME_NEW));
}
+ @Test
+ @SqlGroup({@Sql(CLEAR_DATA), @Sql(SET_DATA)})
+ public void testGetYangResourcesWithAnchorName() {
+ final Map<String, String> 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() {
*/
@NonNull
Collection<Anchor> 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
* @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<String, String> yangResourcesNameToContentMap);
+ @NonNull Map<String, String> 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<String, String> getYangSchemaResources(@NonNull String namespace, @NonNull String schemaSetName);
-}
+ Map<String, String> 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<String, String> getYangSchemaSetResources(@NonNull String dataspaceName,
+ @NonNull String anchorName);
+}
\ No newline at end of file
--- /dev/null
+/*-\r
+ * ============LICENSE_START=======================================================\r
+ * Copyright (C) 2021 Nordix Foundation. All rights reserved.\r
+ * ================================================================================\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ *\r
+ * SPDX-License-Identifier: Apache-2.0\r
+ * ============LICENSE_END=========================================================\r
+ */\r
+\r
+package org.onap.cps.spi.exceptions;\r
+\r
+public class AnchorNotFoundException extends CpsAdminException {\r
+\r
+ private static final long serialVersionUID = -1821064664642194882L;\r
+\r
+ /**\r
+ * Constructor.\r
+ *\r
+ * @param anchorName the name of the anchor\r
+ * @param dataspaceName the dataspace name\r
+ */\r
+ public AnchorNotFoundException(final String anchorName, final String dataspaceName) {\r
+ super("Anchor not found",\r
+ String.format("Anchor with name %s does not exist in dataspace %s.", anchorName,\r
+ dataspaceName));\r
+ }\r
+}
\ No newline at end of file
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<Anchor> 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
}
-
}
(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