2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Nordix Foundation
4 * Modifications Copyright (C) 2021 Pantheon.tech
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
21 package org.onap.cps.spi.exceptions
23 import spock.lang.Specification
25 class CpsExceptionsSpec extends Specification {
26 def dataspaceName = 'some data space'
27 def anchorName = 'some anchor'
28 def schemaSetName = 'some schema set'
29 def rootCause = new Throwable()
30 def providedMessage = 'some message'
31 def providedDetails = 'some details'
32 def xpath = 'some xpath'
34 def 'Creating an exception that the Anchor already exist.'() {
35 given: 'an exception dat the Anchor already exist is created'
36 def exception = new AlreadyDefinedException('Anchor', anchorName, dataspaceName, rootCause)
37 expect: 'the exception details contains the correct message with Anchor name and Dataspace name'
38 exception.details == "Anchor with name ${anchorName} already exists for ${dataspaceName}."
39 and: 'the correct root cause is maintained'
40 exception.cause == rootCause
43 def 'Creating an exception that the dataspace already exists.'() {
44 given: 'an exception that the dataspace already exists is created'
45 def exception = new AlreadyDefinedException(dataspaceName, rootCause)
46 expect: 'the exception details contains the correct message with dataspace name'
47 exception.details == "${dataspaceName} already exists."
48 and: 'the correct root cause is maintained'
49 exception.cause == rootCause
52 def 'Creating a exception that a dataspace is not found.'() {
53 expect: 'the exception details contains the correct message with dataspace name'
54 (new DataspaceNotFoundException(dataspaceName)).details
55 == "Dataspace with name ${dataspaceName} does not exist."
58 def 'Creating a data validation exception with root cause.'() {
59 given: 'a data validation exception is created'
60 def exception = new DataValidationException(providedMessage, providedDetails, rootCause)
61 expect: 'the exception has the provided message'
62 exception.message == providedMessage
63 and: 'the exception has the provided details'
64 exception.details == providedDetails
65 and: 'the correct root cause is maintained'
66 exception.cause == rootCause
69 def 'Creating a data validation exception.'() {
70 given: 'a data validation exception is created'
71 def exception = new DataValidationException(providedMessage, providedDetails)
72 expect: 'the exception has the provided message'
73 exception.message == providedMessage
74 and: 'the exception has the provided details'
75 exception.details == providedDetails
78 def 'Creating a model validation exception.'() {
79 given: 'a data validation exception is created'
80 def exception = new ModelValidationException(providedMessage, providedDetails)
81 expect: 'the exception has the provided message'
82 exception.message == providedMessage
83 and: 'the exception has the provided details'
84 exception.details == providedDetails
87 def 'Creating a model validation exception with a root cause.'() {
88 given: 'a model validation exception is created'
89 def exception = new ModelValidationException(providedMessage, providedDetails, rootCause)
90 expect: 'the exception has the provided message'
91 exception.message == providedMessage
92 and: 'the exception has the provided details'
93 exception.details == providedDetails
94 and: 'the correct root cause is maintained'
95 exception.cause == rootCause
98 def 'Creating a exception for an object not found in a dataspace.'() {
99 def descriptionOfObject = 'some object'
100 expect: 'the exception details contains the correct message with dataspace name and description of the object'
101 (new NotFoundInDataspaceException(dataspaceName, descriptionOfObject)).details
102 == "${descriptionOfObject} does not exist in dataspace ${dataspaceName}."
105 def 'Creating a exception that a schema set cannot be found.'() {
106 expect: 'the exception details contains the correct message with dataspace and schema set names'
107 (new SchemaSetNotFoundException(dataspaceName, schemaSetName)).details
108 == "Schema Set with name ${schemaSetName} was not found for dataspace ${dataspaceName}."
111 def 'Creating a exception that an anchor cannot be found.'() {
112 expect: 'the exception details contains the correct message with dataspace and anchor name'
113 (new AnchorNotFoundException(anchorName, dataspaceName)).details
114 == "Anchor with name ${anchorName} does not exist in dataspace ${dataspaceName}."
117 def 'Creating an exception that the schema set being used and cannot be deleted.'() {
118 expect: 'the exception details contains the correct message with dataspace and schema set names'
119 (new SchemaSetInUseException(dataspaceName, schemaSetName)).details
120 == ("Schema Set with name ${schemaSetName} in dataspace ${dataspaceName} is having" +
121 " Anchor records associated.")
124 def 'Creating a exception that a datanode with a specified xpath does not exist.'() {
125 expect: 'the exception details contains the correct message with dataspace name and xpath.'
126 (new DataNodeNotFoundException(dataspaceName, anchorName, xpath)).details
127 == "DataNode with xpath ${xpath} was not found for anchor ${anchorName} and dataspace ${dataspaceName}."
130 def 'Creating a exception that a datanode does not exist.'() {
131 expect: 'the exception details contains the correct message with dataspace name and anchor.'
132 (new DataNodeNotFoundException(dataspaceName, anchorName)).details
133 == "DataNode not found for anchor ${anchorName} and dataspace ${dataspaceName}."
136 def 'Creating a exception that a dataspace already exists.'() {
137 expect: 'the exception details contains the correct message with dataspace name.'
138 (AlreadyDefinedException.forDataspace(dataspaceName, rootCause)).details
139 == "${dataspaceName} already exists."
142 def 'Creating a exception that a anchor already exists.'() {
143 expect: 'the exception details contains the correct message with anchor name and dataspace name.'
144 (AlreadyDefinedException.forAnchor(anchorName, dataspaceName, rootCause)).details
145 == "Anchor with name ${anchorName} already exists for ${dataspaceName}."
148 def 'Creating a exception that a data node already exists.'() {
149 expect: 'the exception details contains the correct message with xpath and dataspace name.'
150 (AlreadyDefinedException.forDataNode(xpath, dataspaceName, rootCause)).details
151 == "Data node with name ${xpath} already exists for ${dataspaceName}."
154 def 'Creating a exception that a schema set already exists.'() {
155 expect: 'the exception details contains the correct message with schema set and dataspace name.'
156 (AlreadyDefinedException.forSchemaSet(schemaSetName, dataspaceName, rootCause)).details
157 == "Schema Set with name ${schemaSetName} already exists for ${dataspaceName}."
160 def 'Creating a cps path exception.'() {
161 given: 'a cps path exception is created'
162 def exception = new CpsPathException(providedDetails)
163 expect: 'the exception has the provided details'
164 exception.details == providedDetails