From 098a93c84aae8d8d74e7589518a576b84c38ca27 Mon Sep 17 00:00:00 2001 From: ToineSiebelink Date: Tue, 8 Dec 2020 12:08:31 +0000 Subject: [PATCH] Improve CPS Exception handling - Removing unnecessary level of CPS(Validation)Exception - Moved exception inside spi package - Rename exceptions are per agreed new exception hierarchy - Use new exceptions - Added SchemaSetAlreadDefined Exception - Added AnchorAlreadyDefined Exception - Ensure unique serialVersionUIDs - Fixed CpsAdmin(Persistence)ServiceImplSpec tests (there was a mix-up about what this class was testing) - Replaced exception builder with dedicated exception (constructors) Issue-ID: CPS-8 Change-Id: I9cb80af4951e69ee7b0e99303853955de9d4399f Signed-off-by: ToineSiebelink --- .../cps/rest/controller/CpsRestController.java | 14 ++-- .../rest/exceptions/CpsRestExceptionHandler.java | 34 ++++---- .../exceptions/CpsRestExceptionHandlerSpec.groovy | 75 ++++++++++++----- .../spi/impl/CpsAdminPersistenceServiceImpl.java | 21 ++--- .../spi/impl/CpsModulePersistenceServiceImpl.java | 10 +-- .../cps/spi/repository/DataspaceRepository.java | 16 ++-- .../onap/cps/spi/repository/ModuleRepository.java | 26 +++--- .../java/org/onap/cps/api/CpsAdminService.java | 4 +- .../java/org/onap/cps/api/CpsModuleService.java | 4 +- .../onap/cps/api/impl/CpsModuleServiceImpl.java | 8 +- .../onap/cps/exceptions/CpsExceptionBuilder.java | 94 ---------------------- .../exceptions/AnchorAlreadyDefinedException.java | 37 +++++++++ .../{ => spi}/exceptions/CpsAdminException.java | 28 +++---- .../cps/{ => spi}/exceptions/CpsException.java | 22 +++-- .../exceptions/DataValidationException.java} | 37 ++------- .../exceptions/DataspaceNotFoundException.java | 26 +----- .../exceptions/ModelValidationException.java} | 32 ++------ .../exceptions/NotFoundInDataspaceException.java | 34 ++++++++ .../SchemaSetAlreadyDefinedException.java | 39 +++++++++ .../impl/CpsAdminPersistenceServiceImplSpec.groovy | 86 -------------------- .../cps/api/impl/CpsAdminServiceImplSpec.groovy | 55 +++++++++++++ .../CpsModulePersistenceServiceImplSpec.groovy | 4 +- 22 files changed, 334 insertions(+), 372 deletions(-) delete mode 100644 cps-service/src/main/java/org/onap/cps/exceptions/CpsExceptionBuilder.java create mode 100644 cps-service/src/main/java/org/onap/cps/spi/exceptions/AnchorAlreadyDefinedException.java rename cps-service/src/main/java/org/onap/cps/{ => spi}/exceptions/CpsAdminException.java (73%) rename cps-service/src/main/java/org/onap/cps/{ => spi}/exceptions/CpsException.java (75%) rename cps-service/src/main/java/org/onap/cps/{exceptions/CpsNotFoundException.java => spi/exceptions/DataValidationException.java} (54%) rename cps-service/src/main/java/org/onap/cps/{ => spi}/exceptions/DataspaceNotFoundException.java (65%) rename cps-service/src/main/java/org/onap/cps/{exceptions/CpsValidationException.java => spi/exceptions/ModelValidationException.java} (59%) create mode 100644 cps-service/src/main/java/org/onap/cps/spi/exceptions/NotFoundInDataspaceException.java create mode 100644 cps-service/src/main/java/org/onap/cps/spi/exceptions/SchemaSetAlreadyDefinedException.java delete mode 100644 cps-service/src/test/groovy/org/onap/cps/api/impl/CpsAdminPersistenceServiceImplSpec.groovy create mode 100644 cps-service/src/test/groovy/org/onap/cps/api/impl/CpsAdminServiceImplSpec.groovy diff --git a/cps-rest/src/main/java/org/onap/cps/rest/controller/CpsRestController.java b/cps-rest/src/main/java/org/onap/cps/rest/controller/CpsRestController.java index 0821dca2b..30d3e24bb 100755 --- a/cps-rest/src/main/java/org/onap/cps/rest/controller/CpsRestController.java +++ b/cps-rest/src/main/java/org/onap/cps/rest/controller/CpsRestController.java @@ -31,9 +31,9 @@ import org.modelmapper.ModelMapper; import org.onap.cps.api.CpService; import org.onap.cps.api.CpsAdminService; import org.onap.cps.api.CpsModuleService; -import org.onap.cps.exceptions.CpsException; -import org.onap.cps.exceptions.CpsValidationException; import org.onap.cps.rest.api.CpsRestApi; +import org.onap.cps.spi.exceptions.CpsException; +import org.onap.cps.spi.exceptions.DataValidationException; import org.onap.cps.spi.model.Anchor; import org.opendaylight.yangtools.yang.model.api.SchemaContext; import org.springframework.beans.factory.annotation.Autowired; @@ -75,7 +75,7 @@ public class CpsRestController implements CpsRestApi { final Anchor anchorDetails = modelMapper.map(anchor, Anchor.class); anchorDetails.setDataspaceName(dataspaceName); final String anchorName = cpsAdminService.createAnchor(anchorDetails); - return new ResponseEntity(anchorName, HttpStatus.CREATED); + return new ResponseEntity<>(anchorName, HttpStatus.CREATED); } @Override @@ -154,7 +154,7 @@ public class CpsRestController implements CpsRestApi { @GetMapping("/json-object/{id}") public final ResponseEntity getJsonObjectById( @PathVariable("id") final int jsonObjectId) { - return new ResponseEntity(cpService.getJsonById(jsonObjectId), HttpStatus.OK); + return new ResponseEntity<>(cpService.getJsonById(jsonObjectId), HttpStatus.OK); } /** @@ -175,7 +175,7 @@ public class CpsRestController implements CpsRestApi { final Gson gson = new Gson(); gson.fromJson(getJsonString(multipartFile), Object.class); } catch (final JsonSyntaxException e) { - throw new CpsValidationException("Not a valid JSON file.", e); + throw new DataValidationException("Not a valid JSON file.", e.getMessage(), e); } } @@ -183,7 +183,7 @@ public class CpsRestController implements CpsRestApi { try { final File file = File.createTempFile("tempFile", ".yang"); file.deleteOnExit(); - try (OutputStream outputStream = new FileOutputStream(file)) { + try (final OutputStream outputStream = new FileOutputStream(file)) { outputStream.write(multipartFile.getBytes()); } return file; @@ -200,4 +200,4 @@ public class CpsRestController implements CpsRestApi { throw new CpsException(e); } } -} \ No newline at end of file +} diff --git a/cps-rest/src/main/java/org/onap/cps/rest/exceptions/CpsRestExceptionHandler.java b/cps-rest/src/main/java/org/onap/cps/rest/exceptions/CpsRestExceptionHandler.java index 94226b78c..fc0164f5e 100644 --- a/cps-rest/src/main/java/org/onap/cps/rest/exceptions/CpsRestExceptionHandler.java +++ b/cps-rest/src/main/java/org/onap/cps/rest/exceptions/CpsRestExceptionHandler.java @@ -20,11 +20,14 @@ package org.onap.cps.rest.exceptions; import org.apache.commons.lang3.exception.ExceptionUtils; -import org.onap.cps.exceptions.CpsException; -import org.onap.cps.exceptions.CpsNotFoundException; -import org.onap.cps.exceptions.CpsValidationException; 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.CpsException; +import org.onap.cps.spi.exceptions.DataValidationException; +import org.onap.cps.spi.exceptions.ModelValidationException; +import org.onap.cps.spi.exceptions.NotFoundInDataspaceException; +import org.onap.cps.spi.exceptions.SchemaSetAlreadyDefinedException; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.ExceptionHandler; @@ -39,32 +42,33 @@ public class CpsRestExceptionHandler { * @param exception the exception to handle * @return response with response code 500. */ - @ExceptionHandler - public ResponseEntity handleInternalErrorException(final Exception exception) { + @ExceptionHandler public static ResponseEntity handleInternalServerErrorExceptions( + final Exception exception) { return buildErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, exception); } - @ExceptionHandler({CpsException.class}) - public ResponseEntity handleCpsException(final CpsException exception) { - return buildErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, exception.getMessage(), extractDetails(exception)); - } - - @ExceptionHandler({CpsValidationException.class}) - public ResponseEntity handleCpsValidationException(final CpsException exception) { + @ExceptionHandler({ModelValidationException.class, DataValidationException.class, + SchemaSetAlreadyDefinedException.class, AnchorAlreadyDefinedException.class}) + public static ResponseEntity handleBadRequestExceptions(final CpsException exception) { return buildErrorResponse(HttpStatus.BAD_REQUEST, exception.getMessage(), extractDetails(exception)); } - @ExceptionHandler({CpsNotFoundException.class}) - public ResponseEntity handleCpsNotFoundException(final CpsException exception) { + @ExceptionHandler({NotFoundInDataspaceException.class}) + public static ResponseEntity handleNotFoundExceptions(final CpsException exception) { return buildErrorResponse(HttpStatus.NOT_FOUND, exception.getMessage(), extractDetails(exception)); } + @ExceptionHandler({CpsException.class}) + public static ResponseEntity handleAnyOtherCpsExceptions(final CpsException exception) { + return buildErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, exception.getMessage(), extractDetails(exception)); + } + private static ResponseEntity buildErrorResponse(final HttpStatus status, final Exception exception) { return buildErrorResponse(status, exception.getMessage(), ExceptionUtils.getStackTrace(exception)); } private static ResponseEntity buildErrorResponse(final HttpStatus status, final String message, - final String details) { + final String details) { final ErrorMessage errorMessage = new ErrorMessage(); errorMessage.setStatus(status.toString()); errorMessage.setMessage(message); diff --git a/cps-rest/src/test/groovy/org/onap/cps/rest/exceptions/CpsRestExceptionHandlerSpec.groovy b/cps-rest/src/test/groovy/org/onap/cps/rest/exceptions/CpsRestExceptionHandlerSpec.groovy index dd1b956d8..d951cbeb6 100644 --- a/cps-rest/src/test/groovy/org/onap/cps/rest/exceptions/CpsRestExceptionHandlerSpec.groovy +++ b/cps-rest/src/test/groovy/org/onap/cps/rest/exceptions/CpsRestExceptionHandlerSpec.groovy @@ -21,11 +21,16 @@ package org.onap.cps.rest.exceptions import groovy.json.JsonSlurper import org.onap.cps.api.CpService -import org.onap.cps.exceptions.CpsException -import org.onap.cps.exceptions.CpsNotFoundException -import org.onap.cps.exceptions.CpsValidationException +import org.onap.cps.spi.exceptions.AnchorAlreadyDefinedException +import org.onap.cps.spi.exceptions.CpsException +import org.onap.cps.spi.exceptions.DataValidationException +import org.onap.cps.spi.exceptions.NotFoundInDataspaceException +import org.onap.cps.spi.exceptions.ModelValidationException import org.onap.cps.rest.controller.CpsRestController +import org.onap.cps.spi.exceptions.SchemaSetAlreadyDefinedException +import spock.lang.Shared import spock.lang.Specification +import spock.lang.Unroll import static org.springframework.http.HttpStatus.BAD_REQUEST import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR @@ -35,9 +40,18 @@ import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standal class CpsRestExceptionHandlerSpec extends Specification { - def cpsRestController = new CpsRestController(); + @Shared + def errorMessage = 'some error message' + @Shared + def errorDetails = 'some error details' + @Shared + def dataspaceName = 'MyDataSpace' + @Shared + def existingObjectName = 'MyAdminObject' + + def cpsRestController = new CpsRestController() def mockCpService = Mock(CpService.class) - def objectUnderTest = new CpsRestExceptionHandler(); + def objectUnderTest = new CpsRestExceptionHandler() def mockMvc = standaloneSetup(cpsRestController).setControllerAdvice(objectUnderTest).build() def setup() { @@ -47,48 +61,67 @@ class CpsRestExceptionHandlerSpec extends Specification { def 'Get request with runtime exception returns HTTP Status Internal Server Error'() { when: 'runtime exception is thrown by the service' - def errorMessage = 'runtime error message' setupTestException(new IllegalStateException(errorMessage)) def response = performTestRequest() - then: 'an HTTP Internal Server Error response is returned with the correct message' + then: 'an HTTP Internal Server Error response is returned with correct message and details' assertTestResponse(response, INTERNAL_SERVER_ERROR, errorMessage, null) } def 'Get request with generic CPS exception returns HTTP Status Internal Server Error'() { when: 'generic CPS exception is thrown by the service' - def errorMessage = 'cps generic error message' - def errorDetails = 'cps generic error details' setupTestException(new CpsException(errorMessage, errorDetails)) def response = performTestRequest() - then: 'an HTTP Internal Server Error response is returned with the correct message' + then: 'an HTTP Internal Server Error response is returned with correct message and details' assertTestResponse(response, INTERNAL_SERVER_ERROR, errorMessage, errorDetails) } def 'Get request with no data found CPS exception returns HTTP Status Not Found'() { when: 'no data found CPS exception is thrown by the service' - def errorMessage = 'cps no data error message' - def errorDetails = 'cps no data error details' - setupTestException(new CpsNotFoundException(errorMessage, errorDetails)) + def dataspaceName = 'MyDataSpace' + def descriptionOfObject = 'Description' + setupTestException(new NotFoundInDataspaceException(dataspaceName, descriptionOfObject)) def response = performTestRequest() - then: 'an HTTP Not Found response is returned with the correct message' - assertTestResponse(response, NOT_FOUND, errorMessage, errorDetails) + then: 'an HTTP Not Found response is returned with correct message and details' + assertTestResponse(response, NOT_FOUND, 'Object not found', + 'Description does not exist in dataspace MyDataSpace.') } - def 'Get request with CPS validation exception returns HTTP Status Bad Request'() { + @Unroll + def 'request with an expectedObjectTypeInMessage object already defined exception returns HTTP Status Bad Request'() { + + when: 'no data found CPS exception is thrown by the service' + setupTestException(exceptionThrown) + def response = performTestRequest() + + then: 'an HTTP Bad Request response is returned with correct message an details' + assertTestResponse(response, BAD_REQUEST, + "Duplicate ${expectedObjectTypeInMessage}", + "${expectedObjectTypeInMessage} with name ${existingObjectName} " + + 'already exists for dataspace MyDataSpace.') + where: 'the following exceptions are thrown' + exceptionThrown || expectedObjectTypeInMessage + new SchemaSetAlreadyDefinedException(dataspaceName, existingObjectName, null) || 'Schema Set' + new AnchorAlreadyDefinedException(dataspaceName, existingObjectName, null) || 'Anchor' + } + + @Unroll + def 'Get request with a #exceptionThrown.class.simpleName returns HTTP Status Bad Request'() { when: 'CPS validation exception is thrown by the service' - def errorMessage = 'cps validation error message' - def errorDetails = 'cps validation error details' - setupTestException(new CpsValidationException(errorMessage, errorDetails)) + setupTestException(exceptionThrown) def response = performTestRequest() - then: 'an HTTP Bad Request response is returned with the correct message' + then: 'an HTTP Bad Request response is returned with correct message and details' assertTestResponse(response, BAD_REQUEST, errorMessage, errorDetails) + + where: 'the following exceptions are thrown' + exceptionThrown << [new ModelValidationException(errorMessage, errorDetails, null), + new DataValidationException(errorMessage, errorDetails, null)] } /* @@ -111,5 +144,5 @@ class CpsRestExceptionHandlerSpec extends Specification { assert content['message'] == expectedErrorMessage assert expectedErrorDetails == null || content['details'] == expectedErrorDetails } - + } 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 index 684750c45..f11950721 100644 --- 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 @@ -20,12 +20,11 @@ package org.onap.cps.spi.impl; -import org.onap.cps.exceptions.CpsNotFoundException; -import org.onap.cps.exceptions.CpsValidationException; import org.onap.cps.spi.CpsAdminPersistenceService; import org.onap.cps.spi.entities.Dataspace; import org.onap.cps.spi.entities.Fragment; import org.onap.cps.spi.entities.Module; +import org.onap.cps.spi.exceptions.AnchorAlreadyDefinedException; import org.onap.cps.spi.model.Anchor; import org.onap.cps.spi.repository.DataspaceRepository; import org.onap.cps.spi.repository.FragmentRepository; @@ -51,23 +50,15 @@ public class CpsAdminPersistenceServiceImpl implements CpsAdminPersistenceServic final String anchorName = anchor.getAnchorName(); try { final Dataspace dataspace = dataspaceRepository.getByName(anchor.getDataspaceName()); - final Module module = - moduleRepository.getByDataspaceAndNamespaceAndRevision(dataspace, - anchor.getNamespace(), anchor.getRevision()); - - final Fragment fragment = Fragment.builder().xpath(anchorName) - .anchorName(anchorName) - .dataspace(dataspace).module(module).build(); + final Module module = moduleRepository + .getByDataspaceAndNamespaceAndRevision(dataspace, anchor.getNamespace(), anchor.getRevision()); + final Fragment fragment = + Fragment.builder().xpath(anchorName).anchorName(anchorName).dataspace(dataspace).module(module).build(); fragmentRepository.save(fragment); return anchorName; - } catch (final CpsNotFoundException ex) { - throw new CpsValidationException("Validation Error", - "Dataspace and/or Module do not exist."); } catch (final DataIntegrityViolationException ex) { - throw new CpsValidationException("Duplication Error", - String.format("Anchor with name %s already exist in dataspace %s.", - anchorName, anchor.getDataspaceName())); + throw new AnchorAlreadyDefinedException(anchor.getDataspaceName(), anchorName, ex); } } } 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 index 52f803457..e63ae5962 100644 --- 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 @@ -20,9 +20,6 @@ package org.onap.cps.spi.impl; -import static org.onap.cps.exceptions.CpsExceptionBuilder.duplicateSchemaSetException; -import static org.onap.cps.exceptions.CpsExceptionBuilder.invalidDataspaceException; - import com.google.common.collect.ImmutableSet; import java.util.List; import java.util.Map; @@ -33,6 +30,7 @@ import org.onap.cps.spi.CpsModulePersistenceService; 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.repository.DataspaceRepository; import org.onap.cps.spi.repository.SchemaSetRepository; import org.onap.cps.spi.repository.YangResourceRepository; @@ -65,9 +63,7 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ public void storeSchemaSet(final String dataspaceName, final String schemaSetName, final Set yangResourcesAsStrings) { - final Dataspace dataspace = dataspaceRepository.findByName(dataspaceName) - .orElseThrow(() -> invalidDataspaceException(dataspaceName)); - + final Dataspace dataspace = dataspaceRepository.getByName(dataspaceName); final Set yangResources = synchronizeYangResources(yangResourcesAsStrings); final SchemaSet schemaSet = new SchemaSet(); schemaSet.setName(schemaSetName); @@ -76,7 +72,7 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ try { schemaSetRepository.save(schemaSet); } catch (final DataIntegrityViolationException e) { - throw duplicateSchemaSetException(dataspaceName, schemaSetName); + throw new SchemaSetAlreadyDefinedException(dataspaceName, schemaSetName, e); } } diff --git a/cps-ri/src/main/java/org/onap/cps/spi/repository/DataspaceRepository.java b/cps-ri/src/main/java/org/onap/cps/spi/repository/DataspaceRepository.java index 4b649167f..ce231c9c5 100755 --- a/cps-ri/src/main/java/org/onap/cps/spi/repository/DataspaceRepository.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/repository/DataspaceRepository.java @@ -21,8 +21,8 @@ package org.onap.cps.spi.repository; import java.util.Optional; import javax.validation.constraints.NotNull; -import org.onap.cps.exceptions.DataspaceNotFoundException; import org.onap.cps.spi.entities.Dataspace; +import org.onap.cps.spi.exceptions.DataspaceNotFoundException; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @@ -32,8 +32,14 @@ public interface DataspaceRepository extends JpaRepository { Optional findByName(@NotNull String name); - default Dataspace getByName(@NotNull String name) { - return findByName(name).orElseThrow( - () -> new DataspaceNotFoundException("Dataspace " + name + " does not exist.")); + /** + * Get a dataspace by name. + * throws a DataspaceNotFoundException if it does not exist + * + * @param name the name of the dataspace + * @return the Dataspace found + */ + default Dataspace getByName(@NotNull final String name) { + return findByName(name).orElseThrow(() -> new DataspaceNotFoundException(name)); } -} \ No newline at end of file +} diff --git a/cps-ri/src/main/java/org/onap/cps/spi/repository/ModuleRepository.java b/cps-ri/src/main/java/org/onap/cps/spi/repository/ModuleRepository.java index d3a8bc1f0..cab7e1930 100755 --- a/cps-ri/src/main/java/org/onap/cps/spi/repository/ModuleRepository.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/repository/ModuleRepository.java @@ -22,9 +22,9 @@ package org.onap.cps.spi.repository; import java.util.Optional; import javax.validation.constraints.NotNull; -import org.onap.cps.exceptions.CpsNotFoundException; import org.onap.cps.spi.entities.Dataspace; import org.onap.cps.spi.entities.Module; +import org.onap.cps.spi.exceptions.NotFoundInDataspaceException; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @@ -32,24 +32,24 @@ import org.springframework.stereotype.Repository; public interface ModuleRepository extends JpaRepository { Optional findByDataspaceAndNamespaceAndRevision(@NotNull Dataspace dataspace, - @NotNull String namespace, - @NotNull String revision); + @NotNull String namespace, + @NotNull String revision); /** * This method gets a Module by dataspace, namespace and revision. * * @param dataspace the dataspace * @param namespace the namespace - * @param revision the revision + * @param revision the revision * @return the Module - * @throws CpsNotFoundException if Module not found + * @throws NotFoundInDataspaceException if Module not found */ - default Module getByDataspaceAndNamespaceAndRevision(@NotNull Dataspace dataspace, @NotNull String namespace, - @NotNull String revision) { - return findByDataspaceAndNamespaceAndRevision(dataspace, namespace, - revision) - .orElseThrow(() -> new CpsNotFoundException("Validation Error", String.format( - "Module with dataspace %s, revision %s does not exist in namespace %s.", - dataspace.getName(), revision, namespace))); + default Module getByDataspaceAndNamespaceAndRevision(@NotNull final Dataspace dataspace, + @NotNull final String namespace, + @NotNull final String revision) { + return findByDataspaceAndNamespaceAndRevision(dataspace, namespace, revision) + .orElseThrow(() -> new NotFoundInDataspaceException("Validation Error", String.format( + "Module with dataspace %s, revision %s does not exist in namespace %s.", + dataspace.getName(), revision, namespace))); } -} \ No newline at end of file +} diff --git a/cps-service/src/main/java/org/onap/cps/api/CpsAdminService.java b/cps-service/src/main/java/org/onap/cps/api/CpsAdminService.java index 406655e5e..98ea8ebd8 100644 --- a/cps-service/src/main/java/org/onap/cps/api/CpsAdminService.java +++ b/cps-service/src/main/java/org/onap/cps/api/CpsAdminService.java @@ -20,7 +20,7 @@ package org.onap.cps.api; -import org.onap.cps.exceptions.CpsValidationException; +import org.onap.cps.spi.exceptions.CpsException; import org.onap.cps.spi.model.Anchor; /** @@ -33,7 +33,7 @@ public interface CpsAdminService { * * @param anchor the anchor details object. * @return the anchor name. - * @throws CpsValidationException if input data is invalid. + * @throws CpsException if input data is invalid. */ String createAnchor(Anchor anchor); } diff --git a/cps-service/src/main/java/org/onap/cps/api/CpsModuleService.java b/cps-service/src/main/java/org/onap/cps/api/CpsModuleService.java index 02553d074..94ebea2fc 100644 --- a/cps-service/src/main/java/org/onap/cps/api/CpsModuleService.java +++ b/cps-service/src/main/java/org/onap/cps/api/CpsModuleService.java @@ -20,7 +20,7 @@ package org.onap.cps.api; import java.io.File; -import org.onap.cps.exceptions.CpsValidationException; +import org.onap.cps.spi.exceptions.CpsException; import org.opendaylight.yangtools.yang.model.api.SchemaContext; /** @@ -49,7 +49,7 @@ public interface CpsModuleService { * * @param schemaContext the schema context * @param dataspaceName the dataspace name - * @throws CpsValidationException if input data already exists. + * @throws CpsException if input data already exists. */ void storeSchemaContext(SchemaContext schemaContext, String dataspaceName); } diff --git a/cps-service/src/main/java/org/onap/cps/api/impl/CpsModuleServiceImpl.java b/cps-service/src/main/java/org/onap/cps/api/impl/CpsModuleServiceImpl.java index 87ffdd3d9..c7fa04759 100644 --- a/cps-service/src/main/java/org/onap/cps/api/impl/CpsModuleServiceImpl.java +++ b/cps-service/src/main/java/org/onap/cps/api/impl/CpsModuleServiceImpl.java @@ -26,9 +26,9 @@ import java.io.FileWriter; import java.io.IOException; import java.util.Optional; import org.onap.cps.api.CpsModuleService; -import org.onap.cps.exceptions.CpsException; -import org.onap.cps.exceptions.CpsValidationException; import org.onap.cps.spi.CpsModulePersistenceService; +import org.onap.cps.spi.exceptions.CpsException; +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.Module; @@ -47,7 +47,7 @@ public class CpsModuleServiceImpl implements CpsModuleService { public SchemaContext parseAndValidateModel(final String yangModelContent) { try { final File tempFile = File.createTempFile("yang", ".yang"); - try (BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile))) { + try (final BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile))) { writer.write(yangModelContent); } return parseAndValidateModel(tempFile); @@ -61,7 +61,7 @@ public class CpsModuleServiceImpl implements CpsModuleService { try { return YangUtils.parseYangModelFile(yangModelFile); } catch (final YangParserException e) { - throw new CpsValidationException("Yang file validation failed", e.getMessage()); + throw new ModelValidationException("Yang file validation failed", e.getMessage(), e); } catch (final IOException e) { throw new CpsException(e); } diff --git a/cps-service/src/main/java/org/onap/cps/exceptions/CpsExceptionBuilder.java b/cps-service/src/main/java/org/onap/cps/exceptions/CpsExceptionBuilder.java deleted file mode 100644 index 2acbb9232..000000000 --- a/cps-service/src/main/java/org/onap/cps/exceptions/CpsExceptionBuilder.java +++ /dev/null @@ -1,94 +0,0 @@ -/* - * ============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.exceptions; - -import lombok.AccessLevel; -import lombok.NoArgsConstructor; - -/** - * Utility class. - * Serves error message consistency for same error cases occurred in different CPS modules. - */ -@NoArgsConstructor(access = AccessLevel.PRIVATE) -public class CpsExceptionBuilder { - - private static final String SCHEMA_SET_IS_INVALID = "Schema Set is invalid."; - - /** - * Generates validation error exception for case when requested dataspace is absent. - * - * @param dataspaceName dataspace name - */ - public static CpsException invalidDataspaceException(final String dataspaceName) { - return new CpsValidationException("Dataspace is invalid.", - String.format("Dataspace with name %s does not exist.", dataspaceName)); - } - - /** - * Generates validation error exception for case when requested schema set is absent for existing dataspace. - * - * @param dataspaceName dataspace name - * @param schemaSetName schema set name - */ - public static CpsException invalidSchemaSetException(final String dataspaceName, final String schemaSetName) { - return new CpsValidationException(SCHEMA_SET_IS_INVALID, - String.format("Schema Set with name %s was not found for dataspace %s.", schemaSetName, dataspaceName)); - } - - /** - * Returns validation error exception for case when SchemaSet contains no files. - */ - public static CpsException emptySchemaSetException() { - return new CpsValidationException(SCHEMA_SET_IS_INVALID, "Schema Set has no YANG resources to store"); - } - - /** - * Generates validation error exception for case when SchemaSet with same name already exists in the dataspace. - * - * @param dataspaceName dataspace name - * @param schemaSetName schema set name - */ - public static CpsException duplicateSchemaSetException(final String dataspaceName, final String schemaSetName) { - return new CpsValidationException(SCHEMA_SET_IS_INVALID, - String.format("Schema Set with name %s already exists for dataspace %s.", schemaSetName, dataspaceName)); - } - - /** - * Generates no data found exception for case when requested dataspace is absent. - * - * @param dataspaceName dataspace name - */ - public static CpsException dataspaceNotFoundException(final String dataspaceName) { - return new CpsNotFoundException("Dataspace was not found.", - String.format("Dataspace with name %s does not exist.", dataspaceName)); - } - - /** - * Generates no data found exception for case when requested SchemaSet is absent for existing dataspace. - * - * @param dataspaceName dataspace name - * @param schemaSetName schema set name - */ - public static CpsException schemaSetNotFoundException(final String dataspaceName, final String schemaSetName) { - return new CpsNotFoundException("Schema Set was not found.", - String.format("Schema Set with name %s was not found for dataspace %s.", schemaSetName, dataspaceName)); - } - -} diff --git a/cps-service/src/main/java/org/onap/cps/spi/exceptions/AnchorAlreadyDefinedException.java b/cps-service/src/main/java/org/onap/cps/spi/exceptions/AnchorAlreadyDefinedException.java new file mode 100644 index 000000000..efdb86e97 --- /dev/null +++ b/cps-service/src/main/java/org/onap/cps/spi/exceptions/AnchorAlreadyDefinedException.java @@ -0,0 +1,37 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2020 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.spi.exceptions; + +public class AnchorAlreadyDefinedException extends CpsAdminException { + + private static final long serialVersionUID = 5744381546778730691L; + + /** + * Constructor. + * + * @param dataspaceName the name dataspace + * @param anchorName the name of the schema set + * @param cause the cause of the exception + */ + public AnchorAlreadyDefinedException(final String dataspaceName, final String anchorName, final Throwable cause) { + super("Duplicate Anchor", + String.format("Anchor with name %s already exists for dataspace %s.", anchorName, dataspaceName), cause); + } +} diff --git a/cps-service/src/main/java/org/onap/cps/exceptions/CpsAdminException.java b/cps-service/src/main/java/org/onap/cps/spi/exceptions/CpsAdminException.java similarity index 73% rename from cps-service/src/main/java/org/onap/cps/exceptions/CpsAdminException.java rename to cps-service/src/main/java/org/onap/cps/spi/exceptions/CpsAdminException.java index a08891337..6d6a6c31f 100644 --- a/cps-service/src/main/java/org/onap/cps/exceptions/CpsAdminException.java +++ b/cps-service/src/main/java/org/onap/cps/spi/exceptions/CpsAdminException.java @@ -17,34 +17,23 @@ * ============LICENSE_END========================================================= */ -package org.onap.cps.exceptions; - -import lombok.Getter; +package org.onap.cps.spi.exceptions; /** * CPS Admin exception. */ -public class CpsAdminException extends CpsValidationException { +public class CpsAdminException extends CpsException { private static final long serialVersionUID = 5573438585188332404L; - /** - * Constructor. - * - * @param cause the cause of the exception - */ - public CpsAdminException(final Throwable cause) { - super(cause.getMessage(), cause); - } - /** * Constructor. * * @param message the error message - * @param cause the cause of the exception + * @param details the error details */ - public CpsAdminException(final String message, final Throwable cause) { - super(message, cause); + public CpsAdminException(final String message, final String details) { + super(message, details); } /** @@ -52,9 +41,10 @@ public class CpsAdminException extends CpsValidationException { * * @param message the error message * @param details the error details + * @param cause the error cause */ - public CpsAdminException(final String message, final String details) { - super(message, details); - this.details = details; + public CpsAdminException(final String message, final String details, final Throwable cause) { + super(message, details, cause); } + } diff --git a/cps-service/src/main/java/org/onap/cps/exceptions/CpsException.java b/cps-service/src/main/java/org/onap/cps/spi/exceptions/CpsException.java similarity index 75% rename from cps-service/src/main/java/org/onap/cps/exceptions/CpsException.java rename to cps-service/src/main/java/org/onap/cps/spi/exceptions/CpsException.java index acdada14a..406ade688 100644 --- a/cps-service/src/main/java/org/onap/cps/exceptions/CpsException.java +++ b/cps-service/src/main/java/org/onap/cps/spi/exceptions/CpsException.java @@ -17,7 +17,7 @@ * ============LICENSE_END========================================================= */ -package org.onap.cps.exceptions; +package org.onap.cps.spi.exceptions; import lombok.Getter; @@ -26,10 +26,9 @@ import lombok.Getter; */ public class CpsException extends RuntimeException { - private static final long serialVersionUID = 5573438585188332404L; + private static final long serialVersionUID = 1592619410918497467L; - @Getter - String details; + @Getter String details; /** * Constructor. @@ -46,7 +45,7 @@ public class CpsException extends RuntimeException { * @param message the error message * @param cause the cause of the exception */ - public CpsException(final String message, final Throwable cause) { + public CpsException(final String message, final Throwable cause) { super(message, cause); } @@ -60,4 +59,17 @@ public class CpsException extends RuntimeException { super(message); this.details = details; } + + /** + * Constructor. + * + * @param message the error message + * @param details the error details + * @param cause the cause of the exception + */ + public CpsException(final String message, final String details, final Throwable cause) { + super(message, cause); + this.details = details; + } + } diff --git a/cps-service/src/main/java/org/onap/cps/exceptions/CpsNotFoundException.java b/cps-service/src/main/java/org/onap/cps/spi/exceptions/DataValidationException.java similarity index 54% rename from cps-service/src/main/java/org/onap/cps/exceptions/CpsNotFoundException.java rename to cps-service/src/main/java/org/onap/cps/spi/exceptions/DataValidationException.java index e66c3bd3b..e6af60760 100644 --- a/cps-service/src/main/java/org/onap/cps/exceptions/CpsNotFoundException.java +++ b/cps-service/src/main/java/org/onap/cps/spi/exceptions/DataValidationException.java @@ -1,6 +1,7 @@ /* * ============LICENSE_START======================================================= - * Copyright (C) 2020 Pantheon.tech + * Copyright (C) 2020 Nordix Foundation + * 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. @@ -17,42 +18,20 @@ * ============LICENSE_END========================================================= */ -package org.onap.cps.exceptions; +package org.onap.cps.spi.exceptions; +public class DataValidationException extends CpsException { -/** - * CP Service exception. Indicates the requested data being absent. - */ -public class CpsNotFoundException extends CpsException { - - private static final long serialVersionUID = -1852996415384288431L; - - /** - * Constructor. - * - * @param cause the cause of the exception - */ - public CpsNotFoundException(final Throwable cause) { - super(cause.getMessage(), cause); - } - - /** - * Constructor. - * - * @param message the error message - * @param cause the cause of the exception - */ - public CpsNotFoundException(final String message, final Throwable cause) { - super(message, cause); - } + private static final long serialVersionUID = 7747941311132087621L; /** * Constructor. * * @param message the error message * @param details the error details + * @param cause the error cause */ - public CpsNotFoundException(final String message, final String details) { - super(message, details); + public DataValidationException(final String message, final String details, final Throwable cause) { + super(message, details, cause); } } diff --git a/cps-service/src/main/java/org/onap/cps/exceptions/DataspaceNotFoundException.java b/cps-service/src/main/java/org/onap/cps/spi/exceptions/DataspaceNotFoundException.java similarity index 65% rename from cps-service/src/main/java/org/onap/cps/exceptions/DataspaceNotFoundException.java rename to cps-service/src/main/java/org/onap/cps/spi/exceptions/DataspaceNotFoundException.java index 1b09cdd1e..58fcde3d1 100644 --- a/cps-service/src/main/java/org/onap/cps/exceptions/DataspaceNotFoundException.java +++ b/cps-service/src/main/java/org/onap/cps/spi/exceptions/DataspaceNotFoundException.java @@ -17,7 +17,7 @@ * ============LICENSE_END========================================================= */ -package org.onap.cps.exceptions; +package org.onap.cps.spi.exceptions; /** @@ -30,28 +30,10 @@ public class DataspaceNotFoundException extends CpsAdminException { /** * Constructor. * - * @param cause the cause of the exception + * @param dataspaceName the name of the dataspace */ - public DataspaceNotFoundException(final Throwable cause) { - super(cause.getMessage(), cause); - } - /** - * Constructor. - * - * @param message the error message - * @param cause the cause of the exception - */ - public DataspaceNotFoundException(final String message, final Throwable cause) { - super(message, cause); - } - - /** - * Constructor. - * - * @param details the error details - */ - public DataspaceNotFoundException(final String details) { - super("Dataspace Not Found", details); + public DataspaceNotFoundException(final String dataspaceName) { + super("Dataspace not found", String.format("Dataspace with name %s does not exist.", dataspaceName)); } } diff --git a/cps-service/src/main/java/org/onap/cps/exceptions/CpsValidationException.java b/cps-service/src/main/java/org/onap/cps/spi/exceptions/ModelValidationException.java similarity index 59% rename from cps-service/src/main/java/org/onap/cps/exceptions/CpsValidationException.java rename to cps-service/src/main/java/org/onap/cps/spi/exceptions/ModelValidationException.java index 8e049c66e..04a8836ac 100644 --- a/cps-service/src/main/java/org/onap/cps/exceptions/CpsValidationException.java +++ b/cps-service/src/main/java/org/onap/cps/spi/exceptions/ModelValidationException.java @@ -1,6 +1,6 @@ /* * ============LICENSE_START======================================================= - * Copyright (C) 2020 Pantheon.tech + * 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. @@ -17,39 +17,23 @@ * ============LICENSE_END========================================================= */ -package org.onap.cps.exceptions; +package org.onap.cps.spi.exceptions; /** - * CP Service exception. Indicates the parameter validation failure. + * Yang Model Validation exception. */ -public class CpsValidationException extends CpsException { +public class ModelValidationException extends CpsException { - /** - * Constructor. - * - * @param cause the cause of the exception - */ - public CpsValidationException(final Throwable cause) { - super(cause.getMessage(), cause); - } - - /** - * Constructor. - * - * @param message the error message - * @param cause the cause of the exception - */ - public CpsValidationException(final String message, final Throwable cause) { - super(message, cause); - } + private static final long serialVersionUID = 650368325928748496L; /** * Constructor. * * @param message the error message * @param details the error details + * @param cause the cause of the exception */ - public CpsValidationException(final String message, final String details) { - super(message, details); + public ModelValidationException(final String message, final String details, final Throwable cause) { + super(message, details, cause); } } diff --git a/cps-service/src/main/java/org/onap/cps/spi/exceptions/NotFoundInDataspaceException.java b/cps-service/src/main/java/org/onap/cps/spi/exceptions/NotFoundInDataspaceException.java new file mode 100644 index 000000000..66cce1bfc --- /dev/null +++ b/cps-service/src/main/java/org/onap/cps/spi/exceptions/NotFoundInDataspaceException.java @@ -0,0 +1,34 @@ +/* + * ============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.exceptions; + + +/** + * CP Service exception. Indicates the requested data being absent in a given dataspace + */ +public class NotFoundInDataspaceException extends CpsException { + + private static final long serialVersionUID = 3838769447129047713L; + + public NotFoundInDataspaceException(final String dataspaceName, final String descriptionOfObject) { + super("Object not found", + String.format("%s does not exist in dataspace %s.", descriptionOfObject, dataspaceName)); + } +} diff --git a/cps-service/src/main/java/org/onap/cps/spi/exceptions/SchemaSetAlreadyDefinedException.java b/cps-service/src/main/java/org/onap/cps/spi/exceptions/SchemaSetAlreadyDefinedException.java new file mode 100644 index 000000000..b3766726f --- /dev/null +++ b/cps-service/src/main/java/org/onap/cps/spi/exceptions/SchemaSetAlreadyDefinedException.java @@ -0,0 +1,39 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2020 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.spi.exceptions; + +public class SchemaSetAlreadyDefinedException extends CpsAdminException { + + private static final long serialVersionUID = 501929839139881112L; + + /** + * Constructor. + * + * @param dataspaceName the name dataspace + * @param schemaSetName the name of the schema set + * @param cause the cause of the exception + */ + public SchemaSetAlreadyDefinedException(final String dataspaceName, final String schemaSetName, + final Throwable cause) { + super("Duplicate Schema Set", + String.format("Schema Set with name %s already exists for dataspace %s.", schemaSetName, dataspaceName), + cause); + } +} diff --git a/cps-service/src/test/groovy/org/onap/cps/api/impl/CpsAdminPersistenceServiceImplSpec.groovy b/cps-service/src/test/groovy/org/onap/cps/api/impl/CpsAdminPersistenceServiceImplSpec.groovy deleted file mode 100644 index 465badd0c..000000000 --- a/cps-service/src/test/groovy/org/onap/cps/api/impl/CpsAdminPersistenceServiceImplSpec.groovy +++ /dev/null @@ -1,86 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * Copyright (C) 2020 Nordix Foundation - * 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. - * 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.api.impl - -import org.onap.cps.exceptions.CpsValidationException -import org.onap.cps.spi.CpsAdminPersistenceService -import org.onap.cps.spi.model.Anchor -import spock.lang.Specification - -class CpsAdminPersistenceServiceImplSpec extends Specification { - def mockCpsAdminService = Mock(CpsAdminPersistenceService) - def objectUnderTest = new CpsAdminServiceImpl() - - def setup() { - objectUnderTest.cpsAdminPersistenceService = mockCpsAdminService - } - - def 'Create an anchor with a non-existant dataspace'() { - given: 'that the dataspace does not exist service throws an exception' - Anchor anchor = new Anchor() - anchor.setDataspaceName('dummyDataspace') - mockCpsAdminService.createAnchor(anchor) >> { throw new CpsValidationException(_ as String, _ as String) } - when: 'we try to create a anchor with a non-existant dataspace' - objectUnderTest.createAnchor(anchor) - then: 'the same exception is thrown by CPS' - thrown(CpsValidationException) - } - - def 'Create an anchor with invalid dataspace, namespace and revision'() { - given: 'that the dataspace, namespace and revison combination does not exist service throws an exception' - Anchor anchor = new Anchor() - anchor.setDataspaceName('dummyDataspace') - anchor.setNamespace('dummyNamespace') - anchor.setRevision('dummyRevision') - mockCpsAdminService.createAnchor(anchor) >> { throw new CpsValidationException(_ as String, _ as String) } - when: 'we try to create a anchor with a non-existant dataspace, namespace and revison combination' - objectUnderTest.createAnchor(anchor) - then: 'the same exception is thrown by CPS' - thrown(CpsValidationException) - } - - def 'Create a duplicate anchor'() { - given: 'that the anchor already exist service throws an exception' - Anchor anchor = new Anchor() - anchor.setDataspaceName('dummyDataspace') - anchor.setNamespace('dummyNamespace') - anchor.setRevision('dummyRevision') - anchor.setRevision('dummyAnchorName') - mockCpsAdminService.createAnchor(anchor) >> { throw new CpsValidationException(_ as String, _ as String) } - when: 'we try to create a duplicate anchor' - objectUnderTest.createAnchor(anchor) - then: 'the same exception is thrown by CPS' - thrown(CpsValidationException) - } - - def 'Create an anchor with supplied anchor name, dataspace, namespace and revision'() { - given: 'that the anchor does not pre-exist service creates an anchor' - Anchor anchor = new Anchor() - anchor.setDataspaceName('dummyDataspace') - anchor.setNamespace('dummyNamespace') - anchor.setRevision('dummyRevision') - anchor.setRevision('dummyAnchorName') - mockCpsAdminService.createAnchor(anchor) >> 'dummyAnchorName' - expect: 'anchor name is returned by service' - objectUnderTest.createAnchor(anchor) == 'dummyAnchorName' - } - -} 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 new file mode 100644 index 000000000..65a8e71bc --- /dev/null +++ b/cps-service/src/test/groovy/org/onap/cps/api/impl/CpsAdminServiceImplSpec.groovy @@ -0,0 +1,55 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2020 Nordix Foundation + * 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. + * 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.api.impl + + +import org.onap.cps.spi.CpsAdminPersistenceService +import org.onap.cps.spi.model.Anchor +import spock.lang.Specification + +class CpsAdminServiceImplSpec extends Specification { + def mockCpsAdminPersistenceService = Mock(CpsAdminPersistenceService) + def objectUnderTest = new CpsAdminServiceImpl() + def anchor = new Anchor() + + def setup() { + objectUnderTest.cpsAdminPersistenceService = mockCpsAdminPersistenceService + } + + def 'Create an anchor'() { + given: 'that the persistence service returns the name of the anchor' + def anchorName = 'some anchor name' + mockCpsAdminPersistenceService.createAnchor(_) >> anchorName + expect: 'the same anchor name is returned by CPS Admin service' + objectUnderTest.createAnchor(anchor) == anchorName + } + + def 'Create an anchor with some exception in the persistence layer'() { + given: 'that the persistence service throws some exception' + def exceptionThrownInPersistenceLayer = new RuntimeException() + mockCpsAdminPersistenceService.createAnchor(_) >> { throw exceptionThrownInPersistenceLayer } + when: 'we try to create an anchor' + objectUnderTest.createAnchor(anchor) + then: 'the same exception is thrown by the CPS Admin Service' + def exceptionThrownInServiceLayer = thrown(Exception) + exceptionThrownInServiceLayer == exceptionThrownInPersistenceLayer + } +} diff --git a/cps-service/src/test/groovy/org/onap/cps/api/impl/CpsModulePersistenceServiceImplSpec.groovy b/cps-service/src/test/groovy/org/onap/cps/api/impl/CpsModulePersistenceServiceImplSpec.groovy index 79b981b2a..da2df404e 100644 --- a/cps-service/src/test/groovy/org/onap/cps/api/impl/CpsModulePersistenceServiceImplSpec.groovy +++ b/cps-service/src/test/groovy/org/onap/cps/api/impl/CpsModulePersistenceServiceImplSpec.groovy @@ -21,8 +21,8 @@ package org.onap.cps.api.impl import org.onap.cps.TestUtils -import org.onap.cps.exceptions.CpsValidationException import org.onap.cps.spi.CpsModulePersistenceService +import org.onap.cps.spi.exceptions.CpsException import org.opendaylight.yangtools.yang.common.Revision import org.opendaylight.yangtools.yang.model.api.SchemaContext import spock.lang.Specification @@ -65,7 +65,7 @@ class CpsModulePersistenceServiceImplSpec extends Specification { when: 'the model is parsed and validated' objectUnderTest.parseAndValidateModel(file) then: 'a CpsValidationException is thrown' - thrown(CpsValidationException) + thrown(CpsException) } def 'Store a SchemaContext'() { -- 2.16.6