diff options
author | niamhcore <niamh.core@est.tech> | 2021-03-15 15:54:12 +0000 |
---|---|---|
committer | Niamh Core <niamh.core@est.tech> | 2021-03-24 09:19:24 +0000 |
commit | 63be201ed60ca0d0b16ebe5a1a9d3a8e3f7b8482 (patch) | |
tree | 91b34d99cf07d0d599801ea4d60ecfdbe46760ff /cps-service | |
parent | 840eecbf7210d0433d94da895a3a11b97d3a4e6f (diff) |
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 <niamh.core@est.tech>
Change-Id: Ie645237b5dd5b8e2b1d074c5613e7da560f57484
Diffstat (limited to 'cps-service')
9 files changed, 109 insertions, 147 deletions
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 ed89c43091..0379ac21d6 100755 --- a/cps-service/src/main/java/org/onap/cps/api/CpsAdminService.java +++ b/cps-service/src/main/java/org/onap/cps/api/CpsAdminService.java @@ -22,8 +22,8 @@ package org.onap.cps.api; import java.util.Collection; import org.checkerframework.checker.nullness.qual.NonNull; +import org.onap.cps.spi.exceptions.AlreadyDefinedException; import org.onap.cps.spi.exceptions.CpsException; -import org.onap.cps.spi.exceptions.DataspaceAlreadyDefinedException; import org.onap.cps.spi.model.Anchor; /** @@ -35,7 +35,7 @@ public interface CpsAdminService { * Create dataspace. * * @param dataspaceName dataspace name - * @throws DataspaceAlreadyDefinedException if dataspace with same name already exists + * @throws AlreadyDefinedException if dataspace with same name already exists */ void createDataspace(@NonNull String dataspaceName); diff --git a/cps-service/src/main/java/org/onap/cps/spi/CpsAdminPersistenceService.java b/cps-service/src/main/java/org/onap/cps/spi/CpsAdminPersistenceService.java index 06c04ceb62..f47af5f976 100755 --- a/cps-service/src/main/java/org/onap/cps/spi/CpsAdminPersistenceService.java +++ b/cps-service/src/main/java/org/onap/cps/spi/CpsAdminPersistenceService.java @@ -23,7 +23,7 @@ package org.onap.cps.spi; import java.util.Collection; import org.checkerframework.checker.nullness.qual.NonNull; -import org.onap.cps.spi.exceptions.DataspaceAlreadyDefinedException; +import org.onap.cps.spi.exceptions.AlreadyDefinedException; import org.onap.cps.spi.model.Anchor; /* @@ -35,7 +35,7 @@ public interface CpsAdminPersistenceService { * Create dataspace. * * @param dataspaceName dataspace name - * @throws DataspaceAlreadyDefinedException if dataspace with same name already exists + * @throws AlreadyDefinedException if dataspace with same name already exists */ void createDataspace(@NonNull String dataspaceName); diff --git a/cps-service/src/main/java/org/onap/cps/spi/exceptions/AlreadyDefinedException.java b/cps-service/src/main/java/org/onap/cps/spi/exceptions/AlreadyDefinedException.java new file mode 100644 index 0000000000..9e54f34937 --- /dev/null +++ b/cps-service/src/main/java/org/onap/cps/spi/exceptions/AlreadyDefinedException.java @@ -0,0 +1,73 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 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; + +/** + * Already defined exception. Indicates the cps object with same name already exists. + */ + +@SuppressWarnings("squid:S110") // Team agreed to accept 6 levels of inheritance for CPS Exceptions +public class AlreadyDefinedException extends CpsAdminException { + + private static final long serialVersionUID = 501929839139881112L; + + /** + * Constructor. + * + * @param objectType the object type + * @param objectName the name of the object + * @param contextName the context name e.g. Anchor or Dataspace + * @param cause the cause of the exception + */ + private AlreadyDefinedException(final String objectType, final String objectName, final String contextName, + final Throwable cause) { + super("Already defined exception", + String.format("%s with name %s already exists for %s.", objectType, objectName, contextName), cause); + } + + /** + * Constructor. + * + * @param objectName the name of the object + * @param cause the cause of the exception + */ + private AlreadyDefinedException(final String objectName, final Throwable cause) { + super("Already defined exception", String.format("%s already exists.", objectName), cause); + } + + public static AlreadyDefinedException forDataspace(final String dataspaceName, final Throwable cause) { + return new AlreadyDefinedException(dataspaceName, cause); + } + + public static AlreadyDefinedException forAnchor(final String anchorName, final String contextName, + final Throwable cause) { + return new AlreadyDefinedException("Anchor", anchorName, contextName, cause); + } + + public static AlreadyDefinedException forSchemaSet(final String schemaSetName, final String contextName, + final Throwable cause) { + return new AlreadyDefinedException("Schema Set", schemaSetName, contextName, cause); + } + + public static AlreadyDefinedException forDataNode(final String xpath, final String contextName, + final Throwable cause) { + return new AlreadyDefinedException("Data node", xpath, contextName, cause); + } +} 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 deleted file mode 100644 index 2a4abad319..0000000000 --- a/cps-service/src/main/java/org/onap/cps/spi/exceptions/AnchorAlreadyDefinedException.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * ============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; - -/** - * Anchor already defined exception. Indicates the an anchor with same name already exists in the same dataspace - */ - -@SuppressWarnings("squid:S110") // Team agreed to accept 6 levels of inheritance for CPS 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/spi/exceptions/DataspaceAlreadyDefinedException.java b/cps-service/src/main/java/org/onap/cps/spi/exceptions/DataspaceAlreadyDefinedException.java deleted file mode 100644 index fcc085aa08..0000000000 --- a/cps-service/src/main/java/org/onap/cps/spi/exceptions/DataspaceAlreadyDefinedException.java +++ /dev/null @@ -1,42 +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.spi.exceptions; - -/** - * Dataspace already defined exception. Indicates the dataspace with same name already exists. - */ - -@SuppressWarnings("squid:S110") // Team agreed to accept 6 levels of inheritance for CPS Exceptions -public class DataspaceAlreadyDefinedException extends CpsAdminException { - - private static final long serialVersionUID = -5813793951842079228L; - - /** - * Constructor. - * - * @param dataspaceName dataspace name - * @param cause the cause of this exception - */ - public DataspaceAlreadyDefinedException(final String dataspaceName, final Throwable cause) { - super("Duplicate Dataspace.", - String.format("Dataspace with name %s already exists.", dataspaceName), - cause); - } -} 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 deleted file mode 100644 index 1e0b8dbb65..0000000000 --- a/cps-service/src/main/java/org/onap/cps/spi/exceptions/SchemaSetAlreadyDefinedException.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * ============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; - -/** - * Schema set already defined exception. Indicates the a schema set with same name already exists in the same dataspace - */ - -@SuppressWarnings("squid:S110") // Team agreed to accept 6 levels of inheritance for CPS 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/main/java/org/onap/cps/utils/YangUtils.java b/cps-service/src/main/java/org/onap/cps/utils/YangUtils.java index 733378ecba..873e14cd3a 100644 --- a/cps-service/src/main/java/org/onap/cps/utils/YangUtils.java +++ b/cps-service/src/main/java/org/onap/cps/utils/YangUtils.java @@ -21,6 +21,7 @@ package org.onap.cps.utils; +import com.google.gson.JsonSyntaxException; import com.google.gson.stream.JsonReader; import java.io.IOException; import java.io.StringReader; @@ -93,9 +94,9 @@ public class YangUtils { final JsonReader jsonReader = new JsonReader(new StringReader(jsonData)); jsonParserStream.parse(jsonReader); - } catch (final IOException | IllegalStateException e) { + } catch (final IOException | IllegalStateException | JsonSyntaxException exception) { throw new DataValidationException("Failed to parse json data.", String - .format("Exception occurred on parsing string %s.", jsonData), e); + .format("Exception occurred on parsing string %s.", jsonData), exception); } return normalizedNodeResult.getResult(); } diff --git a/cps-service/src/test/groovy/org/onap/cps/spi/exceptions/CpsExceptionsSpec.groovy b/cps-service/src/test/groovy/org/onap/cps/spi/exceptions/CpsExceptionsSpec.groovy index a4a13ff4c9..d2f43c9362 100755 --- a/cps-service/src/test/groovy/org/onap/cps/spi/exceptions/CpsExceptionsSpec.groovy +++ b/cps-service/src/test/groovy/org/onap/cps/spi/exceptions/CpsExceptionsSpec.groovy @@ -31,18 +31,18 @@ class CpsExceptionsSpec extends Specification { def 'Creating an exception that the Anchor already exist.'() { given: 'an exception dat the Anchor already exist is created' - def exception = new AnchorAlreadyDefinedException(dataspaceName, anchorName, rootCause) + def exception = new AlreadyDefinedException('Anchor', anchorName, dataspaceName, rootCause) expect: 'the exception details contains the correct message with Anchor name and Dataspace name' - exception.details == "Anchor with name ${anchorName} already exists for dataspace ${dataspaceName}." + exception.details == "Anchor with name ${anchorName} already exists for ${dataspaceName}." and: 'the correct root cause is maintained' exception.cause == rootCause } def 'Creating an exception that the dataspace already exists.'() { given: 'an exception that the dataspace already exists is created' - def exception = new DataspaceAlreadyDefinedException(dataspaceName, rootCause) + def exception = new AlreadyDefinedException(dataspaceName, rootCause) expect: 'the exception details contains the correct message with dataspace name' - exception.details == "Dataspace with name ${dataspaceName} already exists." + exception.details == "${dataspaceName} already exists." and: 'the correct root cause is maintained' exception.cause == rootCause } @@ -100,15 +100,6 @@ class CpsExceptionsSpec extends Specification { == "${descriptionOfObject} does not exist in dataspace ${dataspaceName}." } - def 'Creating an exception that the schema set already exists.'() { - given: 'an exception that the schema set already exists is created' - def exception = new SchemaSetAlreadyDefinedException(dataspaceName, schemaSetName, rootCause) - expect: 'the exception details contains the correct message with dataspace and schema set names' - exception.details == "Schema Set with name ${schemaSetName} already exists for dataspace ${dataspaceName}." - and: 'the correct root cause is maintained' - exception.cause == rootCause - } - def 'Creating a exception that a schema set cannot be found.'() { expect: 'the exception details contains the correct message with dataspace and schema set names' (new SchemaSetNotFoundException(dataspaceName, schemaSetName)).details @@ -134,6 +125,30 @@ class CpsExceptionsSpec extends Specification { == "DataNode with xpath ${xpath} was not found for anchor ${anchorName} and dataspace ${dataspaceName}." } + def 'Creating a exception that a dataspace already exists.'() { + expect: 'the exception details contains the correct message with dataspace name.' + (AlreadyDefinedException.forDataspace(dataspaceName, rootCause)).details + == "${dataspaceName} already exists." + } + + def 'Creating a exception that a anchor already exists.'() { + expect: 'the exception details contains the correct message with anchor name and dataspace name.' + (AlreadyDefinedException.forAnchor(anchorName, dataspaceName, rootCause)).details + == "Anchor with name ${anchorName} already exists for ${dataspaceName}." + } + + def 'Creating a exception that a data node already exists.'() { + expect: 'the exception details contains the correct message with xpath and dataspace name.' + (AlreadyDefinedException.forDataNode(xpath, dataspaceName, rootCause)).details + == "Data node with name ${xpath} already exists for ${dataspaceName}." + } + + def 'Creating a exception that a schema set already exists.'() { + expect: 'the exception details contains the correct message with schema set and dataspace name.' + (AlreadyDefinedException.forSchemaSet(schemaSetName, dataspaceName, rootCause)).details + == "Schema Set with name ${schemaSetName} already exists for ${dataspaceName}." + } + def 'Creating a cps path exception.'() { given: 'a cps path exception is created' def exception = new CpsPathException(providedMessage, providedDetails) diff --git a/cps-service/src/test/groovy/org/onap/cps/utils/YangUtilsSpec.groovy b/cps-service/src/test/groovy/org/onap/cps/utils/YangUtilsSpec.groovy index 826cdd8c71..0b00cbb835 100644 --- a/cps-service/src/test/groovy/org/onap/cps/utils/YangUtilsSpec.groovy +++ b/cps-service/src/test/groovy/org/onap/cps/utils/YangUtilsSpec.groovy @@ -54,6 +54,7 @@ class YangUtilsSpec extends Specification { invalidJson | description '{incomplete json' | 'incomplete json' '{"test:bookstore": {"address": "Parnell st." }}' | 'json with un-modelled data' + '{" }' | 'json with syntax exception' } @Unroll |