Refactoring/ Adding Tests for Validation
[cps.git] / cps-service / src / test / groovy / org / onap / cps / api / impl / CpsAdminServiceImplSpec.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2020-2022 Nordix Foundation
4  *  Modifications Copyright (C) 2020-2022 Bell Canada.
5  *  Modifications Copyright (C) 2021 Pantheon.tech
6  *  ================================================================================
7  *  Licensed under the Apache License, Version 2.0 (the "License");
8  *  you may not use this file except in compliance with the License.
9  *  You may obtain a copy of the License at
10  *
11  *        http://www.apache.org/licenses/LICENSE-2.0
12  *
13  *  Unless required by applicable law or agreed to in writing, software
14  *  distributed under the License is distributed on an "AS IS" BASIS,
15  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *  See the License for the specific language governing permissions and
17  *  limitations under the License.
18  *
19  *  SPDX-License-Identifier: Apache-2.0
20  *  ============LICENSE_END=========================================================
21  */
22
23 package org.onap.cps.api.impl
24
25 import org.onap.cps.api.CpsDataService
26 import org.onap.cps.spi.CpsAdminPersistenceService
27 import org.onap.cps.spi.exceptions.DataValidationException
28 import org.onap.cps.spi.model.Anchor
29 import org.onap.cps.spi.model.CmHandleQueryParameters
30 import spock.lang.Specification
31 import java.time.OffsetDateTime
32
33 class CpsAdminServiceImplSpec extends Specification {
34     def mockCpsAdminPersistenceService = Mock(CpsAdminPersistenceService)
35     def mockCpsDataService = Mock(CpsDataService)
36     def objectUnderTest = new CpsAdminServiceImpl(mockCpsAdminPersistenceService, mockCpsDataService)
37
38     def 'Create dataspace method invokes persistence service.'() {
39         when: 'create dataspace method is invoked'
40             objectUnderTest.createDataspace('someDataspace')
41         then: 'the persistence service method is invoked with same parameters'
42             1 * mockCpsAdminPersistenceService.createDataspace('someDataspace')
43     }
44
45     def 'Create a dataspace with an invalid dataspace name.'() {
46         when: 'create dataspace method is invoked with incorrectly named dataspace'
47             objectUnderTest.createDataspace('Dataspace Name with spaces')
48         then: 'a data validation exception is thrown'
49             thrown(DataValidationException)
50         and: 'the persistence service method is not invoked'
51             0 * mockCpsAdminPersistenceService.createDataspace(_)
52     }
53
54     def 'Create anchor method invokes persistence service.'() {
55         when: 'create anchor method is invoked'
56             objectUnderTest.createAnchor('someDataspace', 'someSchemaSet', 'someAnchorName')
57         then: 'the persistence service method is invoked with same parameters'
58             1 * mockCpsAdminPersistenceService.createAnchor('someDataspace', 'someSchemaSet', 'someAnchorName')
59     }
60
61     def 'Create an anchor with an invalid anchor name.'() {
62         when: 'create anchor method is invoked with incorrectly named dataspace'
63             objectUnderTest.createAnchor('someDataspace', 'someSchemaSet', 'Anchor Name With Spaces')
64         then: 'a data validation exception is thrown'
65             thrown(DataValidationException)
66         and: 'the persistence service method is not invoked'
67             0 * mockCpsAdminPersistenceService.createAnchor(_, _, _)
68     }
69
70     def 'Retrieve all anchors for dataspace.'() {
71         given: 'that anchor is associated with the dataspace'
72             def anchors = [new Anchor()]
73             mockCpsAdminPersistenceService.getAnchors('someDataspace') >> anchors
74         expect: 'the collection provided by persistence service is returned as result'
75             objectUnderTest.getAnchors('someDataspace') == anchors
76     }
77
78     def 'Retrieve all anchors with an invalid dataspace name.'() {
79         when: 'get anchors is invoked with an invalid dataspace name'
80             objectUnderTest.getAnchors('Dataspace name with spaces')
81         then: 'a data validation exception is thrown'
82             thrown(DataValidationException)
83         and: 'cps admin persistence get anchors is not invoked'
84             0 * mockCpsAdminPersistenceService.getAnchors(_)
85     }
86
87     def 'Retrieve all anchors for schema-set.'() {
88         given: 'that anchor is associated with the dataspace and schemaset'
89             def anchors = [new Anchor()]
90             mockCpsAdminPersistenceService.getAnchors('someDataspace', 'someSchemaSet') >> anchors
91         expect: 'the collection provided by persistence service is returned as result'
92             objectUnderTest.getAnchors('someDataspace', 'someSchemaSet') == anchors
93     }
94     def 'Retrieve all anchors for schema-set with invalid #scenario.'() {
95         when: 'the collection provided by persistence service is returned as result'
96             objectUnderTest.getAnchors(dataspaceName, schemaSetName)
97         then: 'a data validation exception is thrown'
98             thrown(DataValidationException)
99         and: 'cps admin persistence get anchors is not invoked'
100             0 * mockCpsAdminPersistenceService.getAnchors(_, _)
101         where: 'the following parameters are used'
102             scenario                         | dataspaceName                 | schemaSetName
103             'dataspace name'                 | 'dataspace names with spaces' | 'schemaSetName'
104             'schema set name'                | 'dataspaceName'               | 'schema set name with spaces'
105             'dataspace and schema set name'  | 'dataspace name with spaces'  | 'schema set name with spaces'
106     }
107
108
109     def 'Retrieve anchor for dataspace and provided anchor name.'() {
110         given: 'that anchor name is associated with the dataspace'
111             Anchor anchor = new Anchor()
112             mockCpsAdminPersistenceService.getAnchor('someDataspace','someAnchor') >>  anchor
113         expect: 'the anchor provided by persistence service is returned as result'
114             assert objectUnderTest.getAnchor('someDataspace','someAnchor') == anchor
115     }
116
117     def 'Retrieve anchor with invalid #scenario.'() {
118         when: 'get anchors is invoked with an invalid dataspace name'
119             objectUnderTest.getAnchor(dataspaceName, anchorName)
120         then: 'a data validation exception is thrown'
121             thrown(DataValidationException)
122         and: 'cps admin persistence get anchor is not invoked'
123             0 * mockCpsAdminPersistenceService.getAnchor(_, _)
124         where: 'the following parameters are used'
125             scenario                     | dataspaceName                 | anchorName
126             'dataspace name'             | 'dataspace names with spaces' | 'anchorName'
127             'anchor name'                | 'dataspaceName'               | 'anchor name with spaces'
128             'dataspace and anchor name'  | 'dataspace name with spaces'  | 'anchor name with spaces'
129     }
130
131     def 'Delete anchor.'() {
132         when: 'delete anchor is invoked'
133             objectUnderTest.deleteAnchor('someDataspace','someAnchor')
134         then: 'delete data nodes is invoked on the data service with expected parameters'
135             1 * mockCpsDataService.deleteDataNodes('someDataspace','someAnchor', _ as OffsetDateTime )
136         and: 'the persistence service method is invoked with same parameters to delete anchor'
137              1 * mockCpsAdminPersistenceService.deleteAnchor('someDataspace','someAnchor')
138     }
139
140     def 'Delete anchor with invalid #scenario.'() {
141         when: 'delete anchor is invoked'
142             objectUnderTest.deleteAnchor(dataspaceName, anchorName)
143         then: 'a data validation exception is thrown'
144             thrown(DataValidationException)
145         and: 'delete data nodes is invoked on the data service with expected parameters'
146             0 * mockCpsDataService.deleteDataNodes(_,_, _ as OffsetDateTime )
147         and: 'the persistence service method is invoked with same parameters to delete anchor'
148             0 * mockCpsAdminPersistenceService.deleteAnchor(_,_)
149         where: 'the following parameters are used'
150             scenario                     | dataspaceName                 | anchorName
151             'dataspace name'             | 'dataspace names with spaces' | 'anchorName'
152             'anchor name'                | 'dataspaceName'               | 'anchor name with spaces'
153             'dataspace and anchor name'  | 'dataspace name with spaces'  | 'anchor name with spaces'
154     }
155
156     def 'Query all anchor identifiers for a dataspace and module names.'() {
157         given: 'the persistence service is invoked with the expected parameters and returns a list of anchors'
158             mockCpsAdminPersistenceService.queryAnchors('some-dataspace-name', ['some-module-name']) >> [new Anchor(name:'some-anchor-identifier')]
159         expect: 'get anchor identifiers returns the same anchor identifier returned by the persistence layer'
160             objectUnderTest.queryAnchorNames('some-dataspace-name', ['some-module-name']) == ['some-anchor-identifier']
161
162     }
163
164     def 'Query all anchor identifiers for a dataspace and module names with an invalid dataspace name.'() {
165         when: 'delete anchor is invoked'
166             objectUnderTest.queryAnchorNames('some dataspace name', _ as Collection<String>)
167         then: 'a data validation exception is thrown'
168             thrown(DataValidationException)
169         and: 'delete data nodes is not invoked'
170             0 * mockCpsAdminPersistenceService.queryAnchors(_, _)
171     }
172
173     def 'Delete dataspace.'() {
174         when: 'delete dataspace is invoked'
175             objectUnderTest.deleteDataspace('someDataspace')
176         then: 'associated persistence service method is invoked with correct parameter'
177             1 * mockCpsAdminPersistenceService.deleteDataspace('someDataspace')
178     }
179
180     def 'Query CM Handles.'() {
181         given: 'a cm handle query'
182             def cmHandleQueryParameters = new CmHandleQueryParameters()
183         when: 'query cm handles is invoked'
184             objectUnderTest.queryCmHandles(cmHandleQueryParameters)
185         then: 'associated persistence service method is invoked with correct parameter'
186             1 * mockCpsAdminPersistenceService.queryCmHandles(cmHandleQueryParameters)
187     }
188
189     def 'Delete dataspace with invalid dataspace id.'() {
190         when: 'delete dataspace is invoked'
191             objectUnderTest.deleteDataspace('some dataspace name')
192         then: 'a data validation exception is thrown'
193             thrown(DataValidationException)
194         and: 'associated persistence service method is not invoked'
195             0 * mockCpsAdminPersistenceService.deleteDataspace(_)
196     }
197
198 }