500b80152d9075ed9124fb63744725d022fc22c7
[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 AnchorAlreadyDefinedException(dataspaceName, anchorName, 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 dataspace ${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 DataspaceAlreadyDefinedException(dataspaceName, rootCause)
44         expect: 'the exception details contains the correct message with dataspace name'
45             exception.details == "Dataspace with name ${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 an exception that the schema set already exists.'() {
104         given: 'an exception that the schema set already exists is created'
105             def exception = new SchemaSetAlreadyDefinedException(dataspaceName, schemaSetName, rootCause)
106         expect: 'the exception details contains the correct message with dataspace and schema set names'
107             exception.details == "Schema Set with name ${schemaSetName} already exists for dataspace ${dataspaceName}."
108         and: 'the correct root cause is maintained'
109             exception.cause == rootCause
110     }
111
112     def 'Creating a exception that a schema set cannot be found.'() {
113         expect: 'the exception details contains the correct message with dataspace and schema set names'
114             (new SchemaSetNotFoundException(dataspaceName, schemaSetName)).details
115                     == "Schema Set with name ${schemaSetName} was not found for dataspace ${dataspaceName}."
116     }
117
118     def 'Creating a exception that an anchor cannot be found.'() {
119         expect: 'the exception details contains the correct message with dataspace and anchor name'
120             (new AnchorNotFoundException(anchorName, dataspaceName)).details
121                     == "Anchor with name ${anchorName} does not exist in dataspace ${dataspaceName}."
122     }
123
124     def 'Creating an exception that the schema set being used and cannot be deleted.'() {
125         expect: 'the exception details contains the correct message with dataspace and schema set names'
126             (new SchemaSetInUseException(dataspaceName, schemaSetName)).details
127                     == ("Schema Set with name ${schemaSetName} in dataspace ${dataspaceName} is having "
128                     + "Anchor records associated.")
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 xpath.'
133             (new DataNodeNotFoundException(dataspaceName, anchorName, xpath)).details
134                     == "DataNode with xpath ${xpath} was not found for anchor ${anchorName} and dataspace ${dataspaceName}."
135     }
136 }