diff options
author | ToineSiebelink <toine.siebelink@est.tech> | 2020-12-08 12:08:31 +0000 |
---|---|---|
committer | ToineSiebelink <toine.siebelink@est.tech> | 2020-12-10 10:11:53 +0000 |
commit | 098a93c84aae8d8d74e7589518a576b84c38ca27 (patch) | |
tree | 97ef75d4be28415dbc71d093db1d8d27d6ac3f4c /cps-ri/src/main/java | |
parent | 9647834359b168ed8091cecaecd363cb87abe874 (diff) |
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 <toine.siebelink@est.tech>
Diffstat (limited to 'cps-ri/src/main/java')
4 files changed, 33 insertions, 40 deletions
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 684750c459..f119507214 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 52f8034575..e63ae5962f 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<String> yangResourcesAsStrings) { - final Dataspace dataspace = dataspaceRepository.findByName(dataspaceName) - .orElseThrow(() -> invalidDataspaceException(dataspaceName)); - + final Dataspace dataspace = dataspaceRepository.getByName(dataspaceName); final Set<YangResource> 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 4b649167f3..ce231c9c51 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<Dataspace, Integer> { Optional<Dataspace> 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 d3a8bc1f09..cab7e19300 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<Module, Integer> { Optional<Module> 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 +} |