Rework CpsModulePersistenceService 68/116668/9
authorClaudio David Gasparini <claudio.gasparini@pantheon.tech>
Thu, 7 Jan 2021 11:21:38 +0000 (12:21 +0100)
committerClaudio David Gasparini <claudio.gasparini@pantheon.tech>
Mon, 11 Jan 2021 09:01:27 +0000 (10:01 +0100)
- remove getModuleReferences
- provide service returning YangTextSchemaSourceSet

Issue-ID: CPS-21
Signed-off-by: Claudio David Gasparini <claudio.gasparini@pantheon.tech>
Change-Id: I61cf6450c8ed8a5154990ea19467e016dc1155b4

15 files changed:
cps-parent/pom.xml
cps-rest/docs/api/swagger/openapi.yml
cps-rest/src/main/java/org/onap/cps/rest/controller/AdminRestController.java
cps-rest/src/test/groovy/org/onap/cps/rest/controller/AdminRestControllerSpec.groovy
cps-ri/src/main/java/org/onap/cps/spi/impl/CpsModulePersistenceServiceImpl.java
cps-ri/src/test/java/org/onap/cps/spi/impl/CpsModulePersistenceServiceTest.java
cps-service/src/main/java/org/onap/cps/api/CpsAdminService.java
cps-service/src/main/java/org/onap/cps/api/CpsModuleService.java
cps-service/src/main/java/org/onap/cps/api/impl/CpsModuleServiceImpl.java
cps-service/src/main/java/org/onap/cps/spi/CpsAdminPersistenceService.java
cps-service/src/main/java/org/onap/cps/spi/CpsModulePersistenceService.java
cps-service/src/main/java/org/onap/cps/spi/model/ModuleReference.java
cps-service/src/main/java/org/onap/cps/spi/model/SchemaSet.java [new file with mode: 0644]
cps-service/src/main/java/org/onap/cps/yang/YangTextSchemaSourceSetBuilder.java
cps-service/src/test/groovy/org/onap/cps/api/impl/CpsModuleServiceImplSpec.groovy

index 55db1a1..97dba3a 100644 (file)
@@ -20,7 +20,7 @@
         <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.35</minimum-coverage>
+        <minimum-coverage>0.44</minimum-coverage>
         <nexusproxy>https://nexus.onap.org</nexusproxy>
         <oparent.version>3.1.0</oparent.version>
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
index d2c720e..2acd2b1 100755 (executable)
@@ -67,6 +67,41 @@ paths:
         403:
           description: Forbidden
           content: {}
+  /v1/dataspaces/{dataspace-name}/schema-sets/{schema-set-name}:
+    get:
+      tags:
+        - cps-admin
+      summary: Read a schema set given a schema set and a dataspace
+      operationId: getSchemaSet
+      parameters:
+        - name: dataspace-name
+          in: path
+          description: dataspace-name
+          required: true
+          schema:
+            type: string
+        - name: schema-set-name
+          in: path
+          description: schema-name
+          required: true
+          schema:
+            type: string
+      responses:
+        200:
+          description: OK
+          content:
+            application/json:
+              schema:
+                type: object
+        401:
+          description: Unauthorized
+          content: {}
+        403:
+          description: Forbidden
+          content: {}
+        404:
+          description: Not Found
+          content: {}
   /v1/dataspaces/{dataspace-name}/schema-sets:
     post:
       tags:
index 9549580..407a767 100644 (file)
@@ -28,6 +28,7 @@ import org.onap.cps.api.CpsAdminService;
 import org.onap.cps.api.CpsModuleService;
 import org.onap.cps.rest.api.CpsAdminApi;
 import org.onap.cps.spi.model.Anchor;
+import org.onap.cps.spi.model.SchemaSet;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
@@ -59,6 +60,12 @@ public class AdminRestController implements CpsAdminApi {
         return new ResponseEntity<>(schemaSetName, HttpStatus.CREATED);
     }
 
+    @Override
+    public ResponseEntity<Object> getSchemaSet(final String dataspaceName, final String schemaSetName) {
+        final SchemaSet schemaSet = cpsModuleService.getSchemaSet(dataspaceName, schemaSetName);
+        return new ResponseEntity<>(schemaSet, HttpStatus.OK);
+    }
+
     /**
      * Create a new anchor.
      *
index 9919649..078253a 100644 (file)
@@ -25,9 +25,9 @@ import org.onap.cps.api.CpsAdminService
 import org.onap.cps.api.CpsModuleService
 import org.onap.cps.spi.model.Anchor
 import org.onap.cps.spi.exceptions.DataspaceAlreadyDefinedException
+import org.onap.cps.spi.model.SchemaSet
 import org.spockframework.spring.SpringBean
 import org.springframework.beans.factory.annotation.Autowired
-import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
 import org.springframework.http.HttpStatus
 import org.springframework.http.MediaType
@@ -57,6 +57,8 @@ class AdminRestControllerSpec extends Specification {
     MockMvc mvc
 
     def anchorsEndpoint = '/v1/dataspaces/my_dataspace/anchors'
+    def schemaSetsEndpoint = '/v1/dataspaces/test-dataspace/schema-sets'
+    def schemaSetEndpoint = schemaSetsEndpoint + '/my_schema_set'
 
     def anchor = new Anchor(name: 'my_anchor')
     def anchorList = [anchor]
@@ -66,7 +68,7 @@ class AdminRestControllerSpec extends Specification {
             def response = performCreateDataspaceRequest("new-dataspace")
         then: 'Service method is invoked with expected parameters'
             1 * mockCpsAdminService.createDataspace("new-dataspace")
-        and:
+        and: 'Dataspace is create successfully'
             response.status == HttpStatus.CREATED.value()
     }
 
@@ -76,7 +78,7 @@ class AdminRestControllerSpec extends Specification {
             mockCpsAdminService.createDataspace("existing-dataspace") >> { throw thrownException }
         when:
             def response = performCreateDataspaceRequest("existing-dataspace")
-        then:
+        then: 'Dataspace creation fails'
             response.status == HttpStatus.BAD_REQUEST.value()
     }
 
@@ -99,7 +101,7 @@ class AdminRestControllerSpec extends Specification {
             def multipartFile = createMultipartFile("filename.doc", "content")
         when:
             def response = performCreateSchemaSetRequest(multipartFile)
-        then:
+        then: 'Create schema fails'
             response.status == HttpStatus.BAD_REQUEST.value()
     }
 
@@ -118,13 +120,24 @@ class AdminRestControllerSpec extends Specification {
     def performCreateSchemaSetRequest(multipartFile) {
         return mvc.perform(
                 MockMvcRequestBuilders
-                        .multipart('/v1/dataspaces/test-dataspace/schema-sets')
+                        .multipart(schemaSetsEndpoint)
                         .file(multipartFile)
                         .param('schemaSetName', 'test-schema-set')
         ).andReturn().response
     }
 
-    def 'when createAnchor API is called, the response status is 201. '() {
+    def 'Get existing schema set'() {
+        given:
+            mockCpsModuleService.getSchemaSet('test-dataspace', 'my_schema_set') >>
+                    new SchemaSet(name: 'my_schema_set', dataspaceName: 'test-dataspace')
+        when: 'get schema set API is invoked'
+            def response = mvc.perform(get(schemaSetEndpoint)).andReturn().response
+        then: 'the correct schema set is returned'
+            response.status == HttpStatus.OK.value()
+            response.getContentAsString().contains('my_schema_set')
+    }
+
+    def 'Create Anchor'() {
         given:
             def requestParams = new LinkedMultiValueMap<>()
             requestParams.add('schema-set-name', 'my_schema-set')
@@ -132,19 +145,19 @@ class AdminRestControllerSpec extends Specification {
         when: 'post is invoked'
             def response = mvc.perform(post(anchorsEndpoint).contentType(MediaType.APPLICATION_JSON)
                     .params(requestParams as MultiValueMap)).andReturn().response
-        then: 'Status is 201 and the response is the name of the created anchor -> my_anchor'
+        then: 'Anchor is created successfully'
             1 * mockCpsAdminService.createAnchor('my_dataspace', 'my_schema-set', 'my_anchor')
-            assert response.status == HttpStatus.CREATED.value()
-            assert response.getContentAsString().contains('my_anchor')
+            response.status == HttpStatus.CREATED.value()
+            response.getContentAsString().contains('my_anchor')
     }
 
-    def 'when get all anchors for a dataspace API is called, the response status is 200 '() {
+    def 'Get existing anchor'() {
         given:
             mockCpsAdminService.getAnchors('my_dataspace') >> anchorList
         when: 'get all anchors API is invoked'
             def response = mvc.perform(get(anchorsEndpoint)).andReturn().response
-        then: 'Status is 200 and the response is Collection of Anchors containing anchor name -> my_anchor'
-            assert response.status == HttpStatus.OK.value()
-            assert response.getContentAsString().contains('my_anchor')
+        then: 'the correct anchor is returned'
+            response.status == HttpStatus.OK.value()
+            response.getContentAsString().contains('my_anchor')
     }
 }
index 3e3a4b4..23a3843 100644 (file)
@@ -32,11 +32,9 @@ import org.onap.cps.spi.entities.Dataspace;
 import org.onap.cps.spi.entities.SchemaSet;
 import org.onap.cps.spi.entities.YangResource;
 import org.onap.cps.spi.exceptions.SchemaSetAlreadyDefinedException;
-import org.onap.cps.spi.model.ModuleReference;
 import org.onap.cps.spi.repository.DataspaceRepository;
 import org.onap.cps.spi.repository.SchemaSetRepository;
 import org.onap.cps.spi.repository.YangResourceRepository;
-import org.onap.cps.yang.YangTextSchemaSourceSetBuilder;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.dao.DataIntegrityViolationException;
 import org.springframework.stereotype.Component;
@@ -102,11 +100,10 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ
     }
 
     @Override
-    public Collection<ModuleReference> getModuleReferences(final String dataspaceName, final String schemaSetName) {
+    public Map<String, String> getYangSchemaResources(final String dataspaceName, final String schemaSetName) {
         final Dataspace dataspace = dataspaceRepository.getByName(dataspaceName);
         final SchemaSet schemaSet = schemaSetRepository.getByDataspaceAndName(dataspace, schemaSetName);
-        final Map<String, String> yangResourceNameToContent = schemaSet.getYangResources().stream().collect(
+        return schemaSet.getYangResources().stream().collect(
             Collectors.toMap(YangResource::getName, YangResource::getContent));
-        return YangTextSchemaSourceSetBuilder.of(yangResourceNameToContent).getModuleReferences();
     }
 }
index 46f017b..77c8003 100644 (file)
@@ -100,10 +100,13 @@ public class CpsModulePersistenceServiceTest {
     @Test
     @SqlGroup({@Sql(CLEAR_DATA), @Sql(SET_DATA)})
     public void testStoreSchemaSetWithNewYangResource() {
+        final Map<String, String> yangResourcesNameToContentMap = toMap(NEW_RESOURCE_NAME, NEW_RESOURCE_CONTENT);
         cpsModulePersistenceService.storeSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_NEW,
-            toMap(NEW_RESOURCE_NAME, NEW_RESOURCE_CONTENT));
+            yangResourcesNameToContentMap);
         assertSchemaSetPersisted(DATASPACE_NAME, SCHEMA_SET_NAME_NEW,
             NEW_RESOURCE_ABSTRACT_ID, NEW_RESOURCE_NAME, NEW_RESOURCE_CONTENT, NEW_RESOURCE_CHECKSUM);
+        assertEquals(yangResourcesNameToContentMap,
+                cpsModulePersistenceService.getYangSchemaResources(DATASPACE_NAME, SCHEMA_SET_NAME_NEW));
     }
 
     @Test
index 581550f..853bd95 100644 (file)
@@ -50,7 +50,7 @@ public interface CpsAdminService {
     void createAnchor(@NonNull String dataspaceName, @NonNull String schemaSetName, @NonNull String anchorName);
 
     /**
-     * Read all anchors in the given dataspace.
+     * Read all anchors in the given dataspace.
      *
      * @param dataspaceName dataspace name
      * @return a collection of anchors
index e7b02fb..5f63f97 100644 (file)
@@ -21,8 +21,7 @@ package org.onap.cps.api;
 
 import java.util.Map;
 import org.checkerframework.checker.nullness.qual.NonNull;
-import org.onap.cps.spi.exceptions.CpsException;
-import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.onap.cps.spi.model.SchemaSet;
 
 /**
  * Responsible for managing module sets.
@@ -40,5 +39,12 @@ public interface CpsModuleService {
     void createSchemaSet(@NonNull String dataspaceName, @NonNull String schemaSetName,
                          @NonNull Map<String, String> yangResourcesNameToContentMap);
 
-    
+    /**
+     * Read schema set in the given dataspace.
+     *
+     * @param dataspaceName dataspace name
+     * @param schemaSetName schema set name
+     * @return a SchemaSet
+     */
+    SchemaSet getSchemaSet(@NonNull String dataspaceName, @NonNull String schemaSetName);
 }
index 8a437db..04a6fe1 100644 (file)
 
 package org.onap.cps.api.impl;
 
-
 import java.util.Map;
 import org.onap.cps.api.CpsModuleService;
 import org.onap.cps.spi.CpsModulePersistenceService;
+import org.onap.cps.spi.model.SchemaSet;
+import org.onap.cps.yang.YangTextSchemaSourceSet;
 import org.onap.cps.yang.YangTextSchemaSourceSetBuilder;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
@@ -42,4 +43,16 @@ public class CpsModuleServiceImpl implements CpsModuleService {
             .storeSchemaSet(dataspaceName, schemaSetName, yangResourcesNameToContentMap);
     }
 
+    @Override
+    public SchemaSet getSchemaSet(final String dataspaceName, final String schemaSetName) {
+        final Map<String, String> yangResourceNameToContent =
+                cpsModulePersistenceService.getYangSchemaResources(dataspaceName, schemaSetName);
+        final YangTextSchemaSourceSet yangTextSchemaSourceSet = YangTextSchemaSourceSetBuilder
+                                                                        .of(yangResourceNameToContent);
+        return SchemaSet.builder()
+                       .name(schemaSetName)
+                       .dataspaceName(dataspaceName)
+                       .moduleReferences(yangTextSchemaSourceSet.getModuleReferences())
+                .build();
+    }
 }
index ba53003..5643aed 100644 (file)
@@ -57,5 +57,4 @@ public interface CpsAdminPersistenceService {
      */
     @NonNull
     Collection<Anchor> getAnchors(@NonNull String dataspaceName);
-
 }
index 63f1f36..a82d69a 100644 (file)
@@ -20,9 +20,8 @@
 
 package org.onap.cps.spi;
 
-import java.util.Collection;
 import java.util.Map;
-import org.onap.cps.spi.model.ModuleReference;
+import org.checkerframework.checker.nullness.qual.NonNull;
 
 /**
  * Service to manage modules.
@@ -36,14 +35,16 @@ public interface CpsModulePersistenceService {
      * @param schemaSetName                 schema set name
      * @param yangResourcesNameToContentMap YANG resources (files) map where key is a name and value is content
      */
-    void storeSchemaSet(String dataspaceName, String schemaSetName, Map<String, String> yangResourcesNameToContentMap);
+    void storeSchemaSet(@NonNull String dataspaceName, @NonNull String schemaSetName,
+            @NonNull Map<String, String> yangResourcesNameToContentMap);
 
     /**
-     * Returns Modules references per specific namespace / schemaSetName.
+     * Returns YANG resources per specific namespace / schemaSetName.
      *
      * @param namespace     module namespace
      * @param schemaSetName schema set name
-     * @return collection of ModuleRef
+     * @return YANG resources (files) map where key is a name and value is content
      */
-    Collection<ModuleReference> getModuleReferences(String namespace, String schemaSetName);
+    @NonNull
+    Map<String, String> getYangSchemaResources(@NonNull String namespace, @NonNull String schemaSetName);
 }
index 5af355d..aa198a9 100644 (file)
@@ -31,6 +31,7 @@ import lombok.NoArgsConstructor;
 @AllArgsConstructor
 public class ModuleReference {
 
+    private String name;
     private String namespace;
     private String revision;
 }
diff --git a/cps-service/src/main/java/org/onap/cps/spi/model/SchemaSet.java b/cps-service/src/main/java/org/onap/cps/spi/model/SchemaSet.java
new file mode 100644 (file)
index 0000000..caf7800
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+ *  ============LICENSE_START=======================================================
+ *  Copyright (C) 2020 Pantheon.tech
+ *  ================================================================================
+ *  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.model;
+
+import java.io.Serializable;
+import java.util.List;
+import lombok.AllArgsConstructor;
+import lombok.Builder;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+@Data
+@Builder
+@NoArgsConstructor
+@AllArgsConstructor
+public class SchemaSet implements Serializable {
+
+    private static final long serialVersionUID = 1464791260718603291L;
+    private String name;
+    private String dataspaceName;
+    private List<ModuleReference> moduleReferences;
+}
index 6d82544..ae0f2cd 100644 (file)
@@ -49,11 +49,6 @@ public final class YangTextSchemaSourceSetBuilder {
 
     private final ImmutableMap.Builder<String, String> yangModelMap = new ImmutableMap.Builder<>();
 
-    public YangTextSchemaSourceSetBuilder put(final String fileName, final String content) {
-        this.yangModelMap.put(fileName, content);
-        return this;
-    }
-
     public YangTextSchemaSourceSetBuilder putAll(final Map<String, String> yangResourceNameToContent) {
         this.yangModelMap.putAll(yangResourceNameToContent);
         return this;
@@ -95,6 +90,7 @@ public final class YangTextSchemaSourceSetBuilder {
 
         private static ModuleReference toModuleReference(final Module module) {
             return ModuleReference.builder()
+                .name(module.getName())
                 .namespace(module.getNamespace().toString())
                 .revision(module.getRevision().map(Revision::toString).orElse(null))
                 .build();
index a93411b..ebe4fe7 100644 (file)
 package org.onap.cps.api.impl
 
 import org.onap.cps.TestUtils
-import org.onap.cps.spi.CpsModulePersistenceService
+import org.onap.cps.spi.CpsModulePersistenceService;
 import org.onap.cps.spi.exceptions.ModelValidationException
-import org.onap.cps.utils.YangUtils
-import org.opendaylight.yangtools.yang.common.Revision
-import org.opendaylight.yangtools.yang.model.api.SchemaContext
+import org.onap.cps.spi.model.ModuleReference
 import spock.lang.Specification
 
 class CpsModuleServiceImplSpec extends Specification {
@@ -54,4 +52,15 @@ class CpsModuleServiceImplSpec extends Specification {
             thrown(ModelValidationException.class)
     }
 
+    def 'Get schema set by name and namespace.'() {
+        given: 'an already present schema set'
+            def yangResourcesNameToContentMap = TestUtils.getYangResourcesAsMap('bookstore.yang')
+            mockModuleStoreService.getYangSchemaResources('someDataspace', 'someSchemaSet') >> yangResourcesNameToContentMap
+        when: 'get schema set method is invoked'
+            def result = objectUnderTest.getSchemaSet('someDataspace', 'someSchemaSet')
+        then: 'the correct schema set is returned'
+            result.getName().contains('someSchemaSet')
+            result.getDataspaceName().contains('someDataspace')
+            result.getModuleReferences().contains(new ModuleReference('stores', 'org:onap:ccsdk:sample', '2020-09-15'))
+    }
 }