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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.cps.api.impl
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 spock.lang.Specification
30 import java.time.OffsetDateTime
32 class CpsAdminServiceImplSpec extends Specification {
33 def mockCpsAdminPersistenceService = Mock(CpsAdminPersistenceService)
34 def mockCpsDataService = Mock(CpsDataService)
35 def objectUnderTest = new CpsAdminServiceImpl(mockCpsAdminPersistenceService, mockCpsDataService)
37 def 'Create dataspace method invokes persistence service.'() {
38 when: 'create dataspace method is invoked'
39 objectUnderTest.createDataspace('someDataspace')
40 then: 'the persistence service method is invoked with same parameters'
41 1 * mockCpsAdminPersistenceService.createDataspace('someDataspace')
44 def 'Create a dataspace with an invalid dataspace name.'() {
45 when: 'create dataspace method is invoked with incorrectly named dataspace'
46 objectUnderTest.createDataspace('Dataspace Name with spaces')
47 then: 'a data validation exception is thrown'
48 thrown(DataValidationException)
49 and: 'the persistence service method is not invoked'
50 0 * mockCpsAdminPersistenceService.createDataspace(_)
53 def 'Create anchor method invokes persistence service.'() {
54 when: 'create anchor method is invoked'
55 objectUnderTest.createAnchor('someDataspace', 'someSchemaSet', 'someAnchorName')
56 then: 'the persistence service method is invoked with same parameters'
57 1 * mockCpsAdminPersistenceService.createAnchor('someDataspace', 'someSchemaSet', 'someAnchorName')
60 def 'Create an anchor with an invalid anchor name.'() {
61 when: 'create anchor method is invoked with incorrectly named dataspace'
62 objectUnderTest.createAnchor('someDataspace', 'someSchemaSet', 'Anchor Name With Spaces')
63 then: 'a data validation exception is thrown'
64 thrown(DataValidationException)
65 and: 'the persistence service method is not invoked'
66 0 * mockCpsAdminPersistenceService.createAnchor(_, _, _)
69 def 'Retrieve all anchors for dataspace.'() {
70 given: 'that anchor is associated with the dataspace'
71 def anchors = [new Anchor()]
72 mockCpsAdminPersistenceService.getAnchors('someDataspace') >> anchors
73 expect: 'the collection provided by persistence service is returned as result'
74 objectUnderTest.getAnchors('someDataspace') == anchors
77 def 'Retrieve all anchors with an invalid dataspace name.'() {
78 when: 'get anchors is invoked with an invalid dataspace name'
79 objectUnderTest.getAnchors('Dataspace name with spaces')
80 then: 'a data validation exception is thrown'
81 thrown(DataValidationException)
82 and: 'cps admin persistence get anchors is not invoked'
83 0 * mockCpsAdminPersistenceService.getAnchors(_)
86 def 'Retrieve all anchors for schema-set.'() {
87 given: 'that anchor is associated with the dataspace and schemaset'
88 def anchors = [new Anchor()]
89 mockCpsAdminPersistenceService.getAnchors('someDataspace', 'someSchemaSet') >> anchors
90 expect: 'the collection provided by persistence service is returned as result'
91 objectUnderTest.getAnchors('someDataspace', 'someSchemaSet') == anchors
93 def 'Retrieve all anchors for schema-set with invalid #scenario.'() {
94 when: 'the collection provided by persistence service is returned as result'
95 objectUnderTest.getAnchors(dataspaceName, schemaSetName)
96 then: 'a data validation exception is thrown'
97 thrown(DataValidationException)
98 and: 'cps admin persistence get anchors is not invoked'
99 0 * mockCpsAdminPersistenceService.getAnchors(_, _)
100 where: 'the following parameters are used'
101 scenario | dataspaceName | schemaSetName
102 'dataspace name' | 'dataspace names with spaces' | 'schemaSetName'
103 'schema set name' | 'dataspaceName' | 'schema set name with spaces'
104 'dataspace and schema set name' | 'dataspace name with spaces' | 'schema set name with spaces'
108 def 'Retrieve anchor for dataspace and provided anchor name.'() {
109 given: 'that anchor name is associated with the dataspace'
110 Anchor anchor = new Anchor()
111 mockCpsAdminPersistenceService.getAnchor('someDataspace','someAnchor') >> anchor
112 expect: 'the anchor provided by persistence service is returned as result'
113 assert objectUnderTest.getAnchor('someDataspace','someAnchor') == anchor
116 def 'Retrieve anchor with invalid #scenario.'() {
117 when: 'get anchors is invoked with an invalid dataspace name'
118 objectUnderTest.getAnchor(dataspaceName, anchorName)
119 then: 'a data validation exception is thrown'
120 thrown(DataValidationException)
121 and: 'cps admin persistence get anchor is not invoked'
122 0 * mockCpsAdminPersistenceService.getAnchor(_, _)
123 where: 'the following parameters are used'
124 scenario | dataspaceName | anchorName
125 'dataspace name' | 'dataspace names with spaces' | 'anchorName'
126 'anchor name' | 'dataspaceName' | 'anchor name with spaces'
127 'dataspace and anchor name' | 'dataspace name with spaces' | 'anchor name with spaces'
130 def 'Delete anchor.'() {
131 when: 'delete anchor is invoked'
132 objectUnderTest.deleteAnchor('someDataspace','someAnchor')
133 then: 'delete data nodes is invoked on the data service with expected parameters'
134 1 * mockCpsDataService.deleteDataNodes('someDataspace','someAnchor', _ as OffsetDateTime )
135 and: 'the persistence service method is invoked with same parameters to delete anchor'
136 1 * mockCpsAdminPersistenceService.deleteAnchor('someDataspace','someAnchor')
139 def 'Delete anchor with invalid #scenario.'() {
140 when: 'delete anchor is invoked'
141 objectUnderTest.deleteAnchor(dataspaceName, anchorName)
142 then: 'a data validation exception is thrown'
143 thrown(DataValidationException)
144 and: 'delete data nodes is invoked on the data service with expected parameters'
145 0 * mockCpsDataService.deleteDataNodes(_,_, _ as OffsetDateTime )
146 and: 'the persistence service method is invoked with same parameters to delete anchor'
147 0 * mockCpsAdminPersistenceService.deleteAnchor(_,_)
148 where: 'the following parameters are used'
149 scenario | dataspaceName | anchorName
150 'dataspace name' | 'dataspace names with spaces' | 'anchorName'
151 'anchor name' | 'dataspaceName' | 'anchor name with spaces'
152 'dataspace and anchor name' | 'dataspace name with spaces' | 'anchor name with spaces'
155 def 'Query all anchor identifiers for a dataspace and module names.'() {
156 given: 'the persistence service is invoked with the expected parameters and returns a list of anchors'
157 mockCpsAdminPersistenceService.queryAnchors('some-dataspace-name', ['some-module-name']) >> [new Anchor(name:'some-anchor-identifier')]
158 expect: 'get anchor identifiers returns the same anchor identifier returned by the persistence layer'
159 objectUnderTest.queryAnchorNames('some-dataspace-name', ['some-module-name']) == ['some-anchor-identifier']
163 def 'Query all anchor identifiers for a dataspace and module names with an invalid dataspace name.'() {
164 when: 'delete anchor is invoked'
165 objectUnderTest.queryAnchorNames('some dataspace name', _ as Collection<String>)
166 then: 'a data validation exception is thrown'
167 thrown(DataValidationException)
168 and: 'delete data nodes is not invoked'
169 0 * mockCpsAdminPersistenceService.queryAnchors(_, _)
172 def 'Delete dataspace.'() {
173 when: 'delete dataspace is invoked'
174 objectUnderTest.deleteDataspace('someDataspace')
175 then: 'associated persistence service method is invoked with correct parameter'
176 1 * mockCpsAdminPersistenceService.deleteDataspace('someDataspace')
179 def 'Delete dataspace with invalid dataspace id.'() {
180 when: 'delete dataspace is invoked'
181 objectUnderTest.deleteDataspace('some dataspace name')
182 then: 'a data validation exception is thrown'
183 thrown(DataValidationException)
184 and: 'associated persistence service method is not invoked'
185 0 * mockCpsAdminPersistenceService.deleteDataspace(_)