Combine alreadyDefinedException classes
[cps.git] / cps-service / src / test / groovy / org / onap / cps / spi / exceptions / CpsExceptionsSpec.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-2023 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
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 package org.onap.cps.spi.exceptions
23
24 import spock.lang.Specification
25
26 class CpsExceptionsSpec extends Specification {
27     def dataspaceName = 'some data space'
28     def anchorName = 'some anchor'
29     def schemaSetName = 'some schema set'
30     def rootCause = new Throwable()
31     def providedMessage = 'some message'
32     def providedDetails = 'some details'
33     def xpath = 'some xpath'
34     def additionalInformation = 'some information'
35
36     def 'Creating an exception that the Anchor already exist.'() {
37         given: 'an exception dat the Anchor already exist is created'
38             def exception = new AlreadyDefinedException('Anchor', anchorName, dataspaceName, rootCause)
39         expect: 'the exception details contains the correct message with Anchor name and Dataspace name'
40             exception.details == "Anchor with name ${anchorName} already exists for ${dataspaceName}."
41         and: 'the correct root cause is maintained'
42             exception.cause == rootCause
43     }
44
45     def 'Creating an exception that the dataspace already exists.'() {
46         given: 'an exception that the dataspace already exists is created'
47             def exception = new AlreadyDefinedException(dataspaceName, rootCause)
48         expect: 'the exception details contains the correct message with dataspace name'
49             exception.details == "${dataspaceName} already exists."
50         and: 'the correct root cause is maintained'
51             exception.cause == rootCause
52     }
53
54     def 'Creating a exception that a dataspace is not found.'() {
55         expect: 'the exception details contains the correct message with dataspace name'
56             (new DataspaceNotFoundException(dataspaceName)).details
57                     == "Dataspace with name ${dataspaceName} does not exist."
58     }
59
60     def 'Creating a data validation exception with root cause.'() {
61         given: 'a data validation exception is created'
62             def exception = new DataValidationException(providedMessage, providedDetails, rootCause)
63         expect: 'the exception has the provided message'
64             exception.message == providedMessage
65         and: 'the exception has the provided details'
66             exception.details == providedDetails
67         and: 'the correct root cause is maintained'
68             exception.cause == rootCause
69     }
70
71     def 'Creating a data validation exception.'() {
72         given: 'a data validation exception is created'
73             def exception = new DataValidationException(providedMessage, providedDetails)
74         expect: 'the exception has the provided message'
75             exception.message == providedMessage
76         and: 'the exception has the provided details'
77             exception.details == providedDetails
78     }
79
80     def 'Creating a model validation exception.'() {
81         given: 'a data validation exception is created'
82             def exception = new ModelValidationException(providedMessage, providedDetails)
83         expect: 'the exception has the provided message'
84             exception.message == providedMessage
85         and: 'the exception has the provided details'
86             exception.details == providedDetails
87     }
88
89     def 'Creating a model validation exception with a root cause.'() {
90         given: 'a model validation exception is created'
91             def exception = new ModelValidationException(providedMessage, providedDetails, rootCause)
92         expect: 'the exception has the provided message'
93             exception.message == providedMessage
94         and: 'the exception has the provided details'
95             exception.details == providedDetails
96         and: 'the correct root cause is maintained'
97             exception.cause == rootCause
98     }
99
100     def 'Creating a exception for an object not found in a dataspace.'() {
101         def descriptionOfObject = 'some object'
102         expect: 'the exception details contains the correct message with dataspace name and description of the object'
103             (new NotFoundInDataspaceException(dataspaceName, descriptionOfObject)).details
104                     == "${descriptionOfObject} does not exist in dataspace ${dataspaceName}."
105     }
106
107     def 'Creating a exception that a schema set cannot be found.'() {
108         expect: 'the exception details contains the correct message with dataspace and schema set names'
109             (new SchemaSetNotFoundException(dataspaceName, schemaSetName)).details
110                     == "Schema Set with name ${schemaSetName} was not found for dataspace ${dataspaceName}."
111     }
112
113     def 'Creating a exception that an anchor cannot be found.'() {
114         expect: 'the exception details contains the correct message with dataspace and anchor name'
115             (new AnchorNotFoundException(anchorName, dataspaceName)).details
116                     == "Anchor with name ${anchorName} does not exist in dataspace ${dataspaceName}."
117     }
118
119     def 'Creating an exception that the schema set being used and cannot be deleted.'() {
120         expect: 'the exception details contains the correct message with dataspace and schema set names'
121             (new SchemaSetInUseException(dataspaceName, schemaSetName)).details
122                     == ("Schema Set with name ${schemaSetName} in dataspace ${dataspaceName} is having" +
123                 " Anchor records associated.")
124     }
125
126     def 'Creating a exception that a datanode with a specified xpath does not exist.'() {
127         expect: 'the exception details contains the correct message with dataspace name and xpath.'
128             (new DataNodeNotFoundException(dataspaceName, anchorName, xpath)).details
129                     == "DataNode with xpath ${xpath} was not found for anchor ${anchorName} and dataspace ${dataspaceName}."
130     }
131
132     def 'Creating a exception that a datanode does not exist.'() {
133         expect: 'the exception details contains the correct message with dataspace name and anchor.'
134             (new DataNodeNotFoundException(dataspaceName, anchorName)).details
135                     == "DataNode not found for anchor ${anchorName} and dataspace ${dataspaceName}."
136     }
137
138     def 'Creating a exception that a datanode with a specified xpath with additional information does not exist.'() {
139         expect: 'the exception details contains the correct message with dataspace name and anchor.'
140         (new DataNodeNotFoundException(dataspaceName, anchorName, xpath, additionalInformation)).details
141                 == "DataNode with xpath ${xpath} was not found for anchor ${anchorName} and dataspace ${dataspaceName}, ${additionalInformation}."
142     }
143
144     def 'Creating a exception that a dataspace already exists.'() {
145         expect: 'the exception details contains the correct message with dataspace name.'
146             (AlreadyDefinedException.forDataspace(dataspaceName, rootCause)).details
147                     == "${dataspaceName} already exists."
148     }
149
150     def 'Creating a exception that a anchor already exists.'() {
151         expect: 'the exception details contains the correct message with anchor name and dataspace name.'
152             (AlreadyDefinedException.forAnchor(anchorName, dataspaceName, rootCause)).details
153                     == "Anchor with name ${anchorName} already exists for ${dataspaceName}."
154     }
155
156     def 'Creating a exception that data nodes already exist.'() {
157         expect: 'the exception details contains the correct message with xpath and dataspace name.'
158             (AlreadyDefinedException.forDataNodes([xpath], anchorName)).details
159                     == "1 data node(s) already exist for ${anchorName}."
160     }
161
162     def 'Creating a exception that a schema set already exists.'() {
163         expect: 'the exception details contains the correct message with schema set and dataspace name.'
164             (AlreadyDefinedException.forSchemaSet(schemaSetName, dataspaceName, rootCause)).details
165                     == "Schema Set with name ${schemaSetName} already exists for ${dataspaceName}."
166     }
167
168     def 'Creating a cps path exception.'() {
169         given: 'a cps path exception is created'
170             def exception = new CpsPathException(providedDetails)
171         expect: 'the exception has the provided details'
172             exception.details == providedDetails
173     }
174
175     def 'Creating an exception that the dataspace is being used and cannot be deleted.'() {
176         given: 'a dataspace in use exception is created'
177             def exception = new DataspaceInUseException(dataspaceName,providedDetails)
178         expect: 'the exception has the correct message with dataspace name and provided details'
179             exception.message == "Dataspace with name ${dataspaceName} is being used."
180             exception.details == providedDetails
181     }
182 }