Retrieve All anchors for a given Dataspace 98/115998/11
authorputhuparambil.aditya <aditya.puthuparambil@bell.ca>
Thu, 10 Dec 2020 16:49:53 +0000 (16:49 +0000)
committerputhuparambil.aditya <aditya.puthuparambil@bell.ca>
Thu, 10 Dec 2020 16:55:54 +0000 (16:55 +0000)
Issue-ID: CPS-8
Signed-off-by: puthuparambil.aditya <aditya.puthuparambil@bell.ca>
Change-Id: Idb2e4f83d390f078345e556d89781e0bf4a9a41f

cps-rest/docs/api/swagger/openapi.yml
cps-rest/src/main/java/org/onap/cps/rest/controller/CpsRestController.java
cps-rest/src/main/java/org/onap/cps/rest/exceptions/CpsRestExceptionHandler.java
cps-ri/pom.xml
cps-ri/src/main/java/org/onap/cps/spi/impl/CpsAdminPersistenceServiceImpl.java
cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentRepository.java
cps-service/src/main/java/org/onap/cps/api/CpsAdminService.java
cps-service/src/main/java/org/onap/cps/api/impl/CpsAdminServiceImpl.java
cps-service/src/main/java/org/onap/cps/spi/CpsAdminPersistenceService.java
cps-service/src/test/groovy/org/onap/cps/api/impl/CpsAdminServiceImplSpec.groovy

index 56a012f..441d5e5 100755 (executable)
@@ -61,12 +61,17 @@ paths:
         401:
           description: Unauthorized
           content: {}
+        400:
+          description: Bad Request
+          content: {}
         403:
           description: Forbidden
-          content: {}
         404:
           description: Not Found
           content: {}
+        204:
+          description: No Content
+          content: {}
     post:
       tags:
         - cps-rest
index 30d3e24..32ea35c 100755 (executable)
@@ -26,6 +26,7 @@ import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.OutputStream;
+import java.util.Collection;
 import javax.validation.Valid;
 import org.modelmapper.ModelMapper;
 import org.onap.cps.api.CpService;
@@ -107,8 +108,10 @@ public class CpsRestController implements CpsRestApi {
     }
 
     @Override
+
     public ResponseEntity<Object> getAnchors(final String dataspaceName) {
-        return null;
+        final Collection<Anchor> anchorDetails = cpsAdminService.getAnchors(dataspaceName);
+        return new ResponseEntity<>(anchorDetails, HttpStatus.OK);
     }
 
     @Override
index fc0164f..00e72a1 100644 (file)
@@ -23,6 +23,7 @@ import org.apache.commons.lang3.exception.ExceptionUtils;
 import org.onap.cps.rest.controller.CpsRestController;
 import org.onap.cps.rest.model.ErrorMessage;
 import org.onap.cps.spi.exceptions.AnchorAlreadyDefinedException;
+import org.onap.cps.spi.exceptions.CpsAdminException;
 import org.onap.cps.spi.exceptions.CpsException;
 import org.onap.cps.spi.exceptions.DataValidationException;
 import org.onap.cps.spi.exceptions.ModelValidationException;
@@ -48,7 +49,7 @@ public class CpsRestExceptionHandler {
     }
 
     @ExceptionHandler({ModelValidationException.class, DataValidationException.class,
-        SchemaSetAlreadyDefinedException.class, AnchorAlreadyDefinedException.class})
+        SchemaSetAlreadyDefinedException.class, AnchorAlreadyDefinedException.class, CpsAdminException.class})
     public static ResponseEntity<Object> handleBadRequestExceptions(final CpsException exception) {
         return buildErrorResponse(HttpStatus.BAD_REQUEST, exception.getMessage(), extractDetails(exception));
     }
index b5fe933..89b19d5 100644 (file)
             <groupId>org.projectlombok</groupId>\r
             <artifactId>lombok</artifactId>\r
         </dependency>\r
+        <dependency>\r
+            <groupId>org.modelmapper</groupId>\r
+            <artifactId>modelmapper</artifactId>\r
+        </dependency>\r
         <!-- Test dependencies -->\r
         <dependency>\r
             <groupId>org.springframework.boot</groupId>\r
index f119507..48e7303 100644 (file)
@@ -1,6 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2020 Nordix Foundation. All rights reserved.
+ *  Modifications Copyright (C) 2020 Bell Canada. 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.
 
 package org.onap.cps.spi.impl;
 
+import java.lang.reflect.Type;
+import java.util.Collection;
+import org.modelmapper.ModelMapper;
+import org.modelmapper.TypeToken;
 import org.onap.cps.spi.CpsAdminPersistenceService;
 import org.onap.cps.spi.entities.Dataspace;
 import org.onap.cps.spi.entities.Fragment;
@@ -61,4 +66,12 @@ public class CpsAdminPersistenceServiceImpl implements CpsAdminPersistenceServic
             throw new AnchorAlreadyDefinedException(anchor.getDataspaceName(), anchorName, ex);
         }
     }
+
+    @Override
+    public Collection<Anchor> getAnchors(final String dataspaceName) {
+        final Dataspace dataspace = dataspaceRepository.getByName(dataspaceName);
+        final Collection<Fragment> fragments = fragmentRepository.findFragmentsThatAreAnchorsByDataspace(dataspace);
+        final Type anchorListType = new TypeToken<Collection<Anchor>>() {}.getType();
+        return new ModelMapper().map(fragments, anchorListType);
+    }
 }
index ba83f15..7ae7c13 100755 (executable)
@@ -1,6 +1,7 @@
 /*-\r
  * ============LICENSE_START=======================================================\r
  *  Copyright (C) 2020 Nordix Foundation. All rights reserved.\r
+ *  Modifications Copyright (C) 2020 Bell Canada. 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
 \r
 package org.onap.cps.spi.repository;\r
 \r
+import java.util.Collection;\r
+import org.onap.cps.spi.entities.Dataspace;\r
 import org.onap.cps.spi.entities.Fragment;\r
 import org.springframework.data.jpa.repository.JpaRepository;\r
+import org.springframework.data.jpa.repository.Query;\r
+import org.springframework.data.repository.query.Param;\r
 import org.springframework.stereotype.Repository;\r
 \r
 @Repository\r
 public interface FragmentRepository extends JpaRepository<Fragment, Integer> {\r
+\r
+    default Collection<Fragment> findFragmentsThatAreAnchorsByDataspace(Dataspace dataspace) {\r
+        return findFragmentsByDataspaceAndParentFragmentIsNull(dataspace);\r
+    }\r
+\r
+    Collection<Fragment> findFragmentsByDataspaceAndParentFragmentIsNull(Dataspace dataspace);\r
 }
\ No newline at end of file
index 98ea8eb..a2c05bf 100644 (file)
@@ -20,6 +20,7 @@
 
 package org.onap.cps.api;
 
+import java.util.Collection;
 import org.onap.cps.spi.exceptions.CpsException;
 import org.onap.cps.spi.model.Anchor;
 
@@ -36,4 +37,12 @@ public interface CpsAdminService {
      * @throws CpsException if input data is invalid.
      */
     String createAnchor(Anchor anchor);
+
+    /**
+     * Read all anchors in the given a dataspace.
+     *
+     * @param dataspaceName dataspace name
+     * @return a collection of anchors
+     */
+    Collection<Anchor> getAnchors(String dataspaceName);
 }
index b4deef6..5d9bc01 100644 (file)
@@ -20,6 +20,7 @@
 
 package org.onap.cps.api.impl;
 
+import java.util.Collection;
 import org.onap.cps.api.CpsAdminService;
 import org.onap.cps.spi.CpsAdminPersistenceService;
 import org.onap.cps.spi.model.Anchor;
@@ -36,4 +37,9 @@ public class CpsAdminServiceImpl implements CpsAdminService {
     public String createAnchor(final Anchor anchor) {
         return cpsAdminPersistenceService.createAnchor(anchor);
     }
+
+    @Override
+    public Collection<Anchor> getAnchors(final String dataspaceName) {
+        return cpsAdminPersistenceService.getAnchors(dataspaceName);
+    }
 }
index 6709c1f..4e88d49 100644 (file)
@@ -1,6 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2020 Nordix Foundation. All rights reserved.
+ *  Modifications Copyright (C) 2020 Bell Canada. 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.
@@ -20,6 +21,7 @@
 
 package org.onap.cps.spi;
 
+import java.util.Collection;
 import org.onap.cps.spi.model.Anchor;
 
 /*
@@ -34,4 +36,12 @@ public interface CpsAdminPersistenceService {
      * @return the anchor name.
      */
     String createAnchor(Anchor anchor);
+
+    /**
+     * Read all anchors in the given a dataspace.
+     *
+     * @param dataspaceName dataspace name
+     * @return a collection of anchors
+     */
+    Collection<Anchor> getAnchors(String dataspaceName);
 }
index 65a8e71..d31a2f7 100644 (file)
@@ -20,8 +20,8 @@
 
 package org.onap.cps.api.impl
 
-
 import org.onap.cps.spi.CpsAdminPersistenceService
+import org.onap.cps.spi.exceptions.DataspaceNotFoundException
 import org.onap.cps.spi.model.Anchor
 import spock.lang.Specification
 
@@ -52,4 +52,24 @@ class CpsAdminServiceImplSpec extends Specification {
             def exceptionThrownInServiceLayer = thrown(Exception)
             exceptionThrownInServiceLayer == exceptionThrownInPersistenceLayer
     }
+
+    def 'Retrieve all anchors for an existing dataspace'() {
+        given: 'that the dataspace exist and an anchor is associated with the dataspace'
+            Collection<Anchor> anchorCollection = Arrays.asList(anchor)
+            mockCpsAdminPersistenceService.getAnchors('dummyDataspace') >> { anchorCollection }
+        expect: 'we try to retrieve an anchor, a collection of anchor is returned by the service'
+            objectUnderTest.getAnchors('dummyDataspace') == anchorCollection
+    }
+
+    def 'Retrieve all anchors for a non existing dataspace'() {
+        given: 'that the dataspace does not exist, service throws an exception'
+            def exceptionThrownInPersistenceLayer = new DataspaceNotFoundException(_ as String)
+            mockCpsAdminPersistenceService.getAnchors('dummyDataspace') >>
+                    { throw exceptionThrownInPersistenceLayer }
+        when: 'we try to retrieve a anchor with a non-existant dataspace'
+            objectUnderTest.getAnchors('dummyDataspace')
+        then: 'the same exception is thrown by CPS'
+            def exceptionThrownInServiceLayer = thrown(Exception)
+            exceptionThrownInServiceLayer == exceptionThrownInPersistenceLayer
+    }
 }