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