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