From 63be201ed60ca0d0b16ebe5a1a9d3a8e3f7b8482 Mon Sep 17 00:00:00 2001 From: niamhcore Date: Mon, 15 Mar 2021 15:54:12 +0000 Subject: Internal Server Error when creating the same data node twice This change adds a generic exception handler class for a already defined object and handles a JsonSyntaxException. Issue-ID: CPS-290 Signed-off-by: niamhcore Change-Id: Ie645237b5dd5b8e2b1d074c5613e7da560f57484 --- .../org/onap/cps/spi/impl/CpsAdminPersistenceServiceImpl.java | 7 +++---- .../org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java | 10 ++++++++-- .../org/onap/cps/spi/impl/CpsModulePersistenceServiceImpl.java | 4 ++-- .../onap/cps/spi/impl/CpsAdminPersistenceServiceSpec.groovy | 7 +++---- .../org/onap/cps/spi/impl/CpsDataPersistenceServiceSpec.groovy | 6 +++++- .../onap/cps/spi/impl/CpsModulePersistenceServiceSpec.groovy | 4 ++-- 6 files changed, 23 insertions(+), 15 deletions(-) (limited to 'cps-ri/src') 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 ddbfeb228..1b8f1968b 100755 --- 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 @@ -27,8 +27,7 @@ import org.onap.cps.spi.CpsAdminPersistenceService; import org.onap.cps.spi.entities.AnchorEntity; import org.onap.cps.spi.entities.DataspaceEntity; import org.onap.cps.spi.entities.SchemaSetEntity; -import org.onap.cps.spi.exceptions.AnchorAlreadyDefinedException; -import org.onap.cps.spi.exceptions.DataspaceAlreadyDefinedException; +import org.onap.cps.spi.exceptions.AlreadyDefinedException; import org.onap.cps.spi.model.Anchor; import org.onap.cps.spi.repository.AnchorRepository; import org.onap.cps.spi.repository.DataspaceRepository; @@ -54,7 +53,7 @@ public class CpsAdminPersistenceServiceImpl implements CpsAdminPersistenceServic try { dataspaceRepository.save(new DataspaceEntity(dataspaceName)); } catch (final DataIntegrityViolationException e) { - throw new DataspaceAlreadyDefinedException(dataspaceName, e); + throw AlreadyDefinedException.forDataspace(dataspaceName, e); } } @@ -71,7 +70,7 @@ public class CpsAdminPersistenceServiceImpl implements CpsAdminPersistenceServic try { anchorRepository.save(anchorEntity); } catch (final DataIntegrityViolationException e) { - throw new AnchorAlreadyDefinedException(dataspaceName, anchorName, e); + throw AlreadyDefinedException.forAnchor(anchorName, dataspaceName, e); } } diff --git a/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java b/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java index 4cfa78b3e..26aa4ac0b 100644 --- a/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java @@ -36,6 +36,7 @@ import org.onap.cps.spi.FetchDescendantsOption; import org.onap.cps.spi.entities.AnchorEntity; import org.onap.cps.spi.entities.DataspaceEntity; import org.onap.cps.spi.entities.FragmentEntity; +import org.onap.cps.spi.exceptions.AlreadyDefinedException; import org.onap.cps.spi.model.DataNode; import org.onap.cps.spi.model.DataNodeBuilder; import org.onap.cps.spi.query.CpsPathQuery; @@ -44,6 +45,7 @@ import org.onap.cps.spi.repository.AnchorRepository; import org.onap.cps.spi.repository.DataspaceRepository; import org.onap.cps.spi.repository.FragmentRepository; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.dao.DataIntegrityViolationException; import org.springframework.stereotype.Service; @Service @@ -76,7 +78,11 @@ public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService final AnchorEntity anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName); final FragmentEntity fragmentEntity = convertToFragmentWithAllDescendants(dataspaceEntity, anchorEntity, dataNode); - fragmentRepository.save(fragmentEntity); + try { + fragmentRepository.save(fragmentEntity); + } catch (final DataIntegrityViolationException exception) { + throw AlreadyDefinedException.forDataNode(dataNode.getXpath(), anchorName, exception); + } } /** @@ -139,7 +145,7 @@ public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService .getLeafName(), cpsPathQuery.getLeafValue()); } else { fragmentEntities = fragmentRepository - .getByAnchorAndEndsWithXpath(anchorEntity.getId(), cpsPathQuery.getEndsWith()); + .getByAnchorAndEndsWithXpath(anchorEntity.getId(), cpsPathQuery.getEndsWith()); } return fragmentEntities.stream() .map(fragmentEntity -> toDataNode(fragmentEntity, fetchDescendantsOption)) 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 9a8ea6af4..9d3298e35 100755 --- 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 @@ -36,7 +36,7 @@ import org.onap.cps.spi.entities.AnchorEntity; import org.onap.cps.spi.entities.DataspaceEntity; import org.onap.cps.spi.entities.SchemaSetEntity; import org.onap.cps.spi.entities.YangResourceEntity; -import org.onap.cps.spi.exceptions.SchemaSetAlreadyDefinedException; +import org.onap.cps.spi.exceptions.AlreadyDefinedException; import org.onap.cps.spi.exceptions.SchemaSetInUseException; import org.onap.cps.spi.model.Anchor; import org.onap.cps.spi.repository.AnchorRepository; @@ -84,7 +84,7 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ try { schemaSetRepository.save(schemaSetEntity); } catch (final DataIntegrityViolationException e) { - throw new SchemaSetAlreadyDefinedException(dataspaceName, schemaSetName, e); + throw AlreadyDefinedException.forSchemaSet(schemaSetName, dataspaceName, e); } } diff --git a/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsAdminPersistenceServiceSpec.groovy b/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsAdminPersistenceServiceSpec.groovy index fd3e96481..7ab099dd4 100644 --- a/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsAdminPersistenceServiceSpec.groovy +++ b/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsAdminPersistenceServiceSpec.groovy @@ -20,9 +20,8 @@ package org.onap.cps.spi.impl import org.onap.cps.spi.CpsAdminPersistenceService -import org.onap.cps.spi.exceptions.AnchorAlreadyDefinedException +import org.onap.cps.spi.exceptions.AlreadyDefinedException import org.onap.cps.spi.exceptions.AnchorNotFoundException -import org.onap.cps.spi.exceptions.DataspaceAlreadyDefinedException import org.onap.cps.spi.exceptions.DataspaceNotFoundException import org.onap.cps.spi.exceptions.SchemaSetNotFoundException import org.onap.cps.spi.model.Anchor @@ -54,7 +53,7 @@ class CpsAdminPersistenceServiceSpec extends CpsPersistenceSpecBase { when: 'an attempt is made to create an already existing dataspace' objectUnderTest.createDataspace(DATASPACE_NAME) then: 'an exception that is is already defined is thrown with the correct details' - def thrown = thrown(DataspaceAlreadyDefinedException) + def thrown = thrown(AlreadyDefinedException) thrown.details.contains(DATASPACE_NAME) } @@ -81,7 +80,7 @@ class CpsAdminPersistenceServiceSpec extends CpsPersistenceSpecBase { scenario | dataspaceName | schemaSetName | anchorName || expectedException 'dataspace does not exist' | 'unknown' | 'not-relevant' | 'not-relevant' || DataspaceNotFoundException 'schema set does not exist' | DATASPACE_NAME | 'unknown' | 'not-relevant' || SchemaSetNotFoundException - 'anchor already exists' | DATASPACE_NAME | SCHEMA_SET_NAME1 | ANCHOR_NAME1 || AnchorAlreadyDefinedException + 'anchor already exists' | DATASPACE_NAME | SCHEMA_SET_NAME1 | ANCHOR_NAME1 || AlreadyDefinedException } @Unroll diff --git a/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsDataPersistenceServiceSpec.groovy b/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsDataPersistenceServiceSpec.groovy index bb0b47125..51f780940 100644 --- a/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsDataPersistenceServiceSpec.groovy +++ b/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsDataPersistenceServiceSpec.groovy @@ -25,6 +25,7 @@ import com.google.gson.GsonBuilder import org.onap.cps.spi.CpsDataPersistenceService import org.onap.cps.spi.FetchDescendantsOption import org.onap.cps.spi.entities.FragmentEntity +import org.onap.cps.spi.exceptions.AlreadyDefinedException import org.onap.cps.spi.exceptions.AnchorNotFoundException import org.onap.cps.spi.exceptions.DataNodeNotFoundException import org.onap.cps.spi.exceptions.DataspaceNotFoundException @@ -35,6 +36,8 @@ import org.springframework.dao.DataIntegrityViolationException import org.springframework.test.context.jdbc.Sql import spock.lang.Unroll +import javax.validation.ConstraintViolationException + import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS @@ -99,7 +102,8 @@ class CpsDataPersistenceServiceSpec extends CpsPersistenceSpecBase { scenario | dataspaceName | anchorName | dataNode || expectedException 'dataspace does not exist' | 'unknown' | 'not-relevant' | newDataNode || DataspaceNotFoundException 'schema set does not exist' | DATASPACE_NAME | 'unknown' | newDataNode || AnchorNotFoundException - 'anchor already exists' | DATASPACE_NAME | ANCHOR_NAME1 | existingDataNode || DataIntegrityViolationException + 'anchor already exists' | DATASPACE_NAME | ANCHOR_NAME1 | newDataNode || ConstraintViolationException + 'datanode already exists' | DATASPACE_NAME | ANCHOR_NAME1 | existingDataNode || AlreadyDefinedException } @Sql([CLEAR_DATA, SET_DATA]) diff --git a/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsModulePersistenceServiceSpec.groovy b/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsModulePersistenceServiceSpec.groovy index d3d3768c9..441ddb65f 100644 --- a/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsModulePersistenceServiceSpec.groovy +++ b/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsModulePersistenceServiceSpec.groovy @@ -25,7 +25,7 @@ import org.onap.cps.spi.CpsAdminPersistenceService import org.onap.cps.spi.CpsModulePersistenceService import org.onap.cps.spi.entities.YangResourceEntity import org.onap.cps.spi.exceptions.DataspaceNotFoundException -import org.onap.cps.spi.exceptions.SchemaSetAlreadyDefinedException +import org.onap.cps.spi.exceptions.AlreadyDefinedException import org.onap.cps.spi.exceptions.SchemaSetInUseException import org.onap.cps.spi.exceptions.SchemaSetNotFoundException import org.onap.cps.spi.repository.AnchorRepository @@ -76,7 +76,7 @@ class CpsModulePersistenceServiceSpec extends CpsPersistenceSpecBase { where: 'the following data is used' scenario | dataspaceName | schemaSetName || expectedException 'dataspace does not exist' | 'unknown' | 'not-relevant' || DataspaceNotFoundException - 'schema set already exists' | DATASPACE_NAME | EXISTING_SCHEMA_SET_NAME || SchemaSetAlreadyDefinedException + 'schema set already exists' | DATASPACE_NAME | EXISTING_SCHEMA_SET_NAME || AlreadyDefinedException } @Sql([CLEAR_DATA, SET_DATA]) -- cgit 1.2.3-korg