eb41e2085ff7aed0e3dd193971dba5c52da305ae
[cps.git] / cps-service / src / test / groovy / org / onap / cps / api / impl / CpsAdminServiceImplSpec.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2020-2023 Nordix Foundation
4  *  Modifications Copyright (C) 2020-2022 Bell Canada.
5  *  Modifications Copyright (C) 2021 Pantheon.tech
6  *  Modifications Copyright (C) 2022 TechMahindra Ltd.
7  *  ================================================================================
8  *  Licensed under the Apache License, Version 2.0 (the "License");
9  *  you may not use this file except in compliance with the License.
10  *  You may obtain a copy of the License at
11  *
12  *        http://www.apache.org/licenses/LICENSE-2.0
13  *
14  *  Unless required by applicable law or agreed to in writing, software
15  *  distributed under the License is distributed on an "AS IS" BASIS,
16  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  *  See the License for the specific language governing permissions and
18  *  limitations under the License.
19  *
20  *  SPDX-License-Identifier: Apache-2.0
21  *  ============LICENSE_END=========================================================
22  */
23
24 package org.onap.cps.api.impl
25
26 import org.onap.cps.api.CpsDataService
27 import org.onap.cps.spi.CpsAdminPersistenceService
28 import org.onap.cps.spi.exceptions.ModuleNamesNotFoundException
29 import org.onap.cps.spi.model.Anchor
30 import org.onap.cps.spi.model.Dataspace
31 import org.onap.cps.spi.utils.CpsValidator
32 import spock.lang.Specification
33 import java.time.OffsetDateTime
34
35 class CpsAdminServiceImplSpec extends Specification {
36     def mockCpsAdminPersistenceService = Mock(CpsAdminPersistenceService)
37     def mockCpsDataService = Mock(CpsDataService)
38     def mockCpsValidator = Mock(CpsValidator)
39     def objectUnderTest = new CpsAdminServiceImpl(mockCpsAdminPersistenceService, mockCpsDataService,mockCpsValidator)
40
41     def 'Create dataspace method invokes persistence service.'() {
42         when: 'create dataspace method is invoked'
43             objectUnderTest.createDataspace('someDataspace')
44         then: 'the persistence service method is invoked with same parameters'
45             1 * mockCpsAdminPersistenceService.createDataspace('someDataspace')
46         and: 'the CpsValidator is called on the dataspaceName'
47             1 * mockCpsValidator.validateNameCharacters('someDataspace')
48     }
49
50     def 'Create anchor method invokes persistence service.'() {
51         when: 'create anchor method is invoked'
52             objectUnderTest.createAnchor('someDataspace', 'someSchemaSet', 'someAnchorName')
53         then: 'the persistence service method is invoked with same parameters'
54             1 * mockCpsAdminPersistenceService.createAnchor('someDataspace', 'someSchemaSet', 'someAnchorName')
55         and: 'the CpsValidator is called on the dataspaceName, schemaSetName and anchorName'
56             1 * mockCpsValidator.validateNameCharacters('someDataspace', 'someSchemaSet', 'someAnchorName')
57     }
58
59     def 'Retrieve all anchors for dataspace.'() {
60         given: 'that an anchor is associated with the dataspace'
61             def anchors = [new Anchor()]
62             mockCpsAdminPersistenceService.getAnchors('someDataspace') >> anchors
63         when: 'get Anchors is called for a dataspace name'
64             def result = objectUnderTest.getAnchors('someDataspace')
65         then: 'the collection provided by persistence service is returned as result'
66             result == anchors
67         and: 'the CpsValidator is called on the dataspaceName'
68             1 * mockCpsValidator.validateNameCharacters('someDataspace')
69     }
70
71     def 'Retrieve all anchors for schema-set.'() {
72         given: 'that anchor is associated with the dataspace and schemaset'
73             def anchors = [new Anchor()]
74             mockCpsAdminPersistenceService.getAnchors('someDataspace', 'someSchemaSet') >> anchors
75         when: 'get anchors is called for a dataspace name and schema set name'
76             def result = objectUnderTest.getAnchors('someDataspace', 'someSchemaSet')
77         then: 'the collection provided by persistence service is returned as result'
78             result == anchors
79         and: 'the CpsValidator is called on the dataspaceName, schemaSetName'
80             1 * mockCpsValidator.validateNameCharacters('someDataspace', 'someSchemaSet')
81     }
82
83     def 'Retrieve all anchors for multiple schema-sets.'() {
84         given: 'that anchor is associated with the dataspace and schemasets'
85             def anchors = [new Anchor(), new Anchor()]
86             mockCpsAdminPersistenceService.getAnchors('someDataspace', _ as Collection<String>) >> anchors
87         when: 'get anchors is called for a dataspace name and schema set names'
88             def result = objectUnderTest.getAnchors('someDataspace', ['schemaSet1', 'schemaSet2'])
89         then: 'the collection provided by persistence service is returned as result'
90             result == anchors
91         and: 'the CpsValidator is called on the dataspace name and schema-set names'
92             1 * mockCpsValidator.validateNameCharacters('someDataspace')
93             1 * mockCpsValidator.validateNameCharacters(_)
94     }
95
96     def 'Retrieve anchor for dataspace and provided anchor name.'() {
97         given: 'that anchor name is associated with the dataspace'
98             Anchor anchor = new Anchor()
99             mockCpsAdminPersistenceService.getAnchor('someDataspace','someAnchor') >>  anchor
100         when: 'get anchor is called for a dataspace name and anchor name'
101             def result = objectUnderTest.getAnchor('someDataspace','someAnchor')
102         then: 'the anchor provided by persistence service is returned as result'
103             result == anchor
104         and: 'the CpsValidator is called on the dataspaceName, anchorName'
105             1 * mockCpsValidator.validateNameCharacters('someDataspace', 'someAnchor')
106     }
107
108     def 'Retrieve dataspace.'() {
109         given: 'a dataspace is already created'
110             def dataspace = new Dataspace(name: "someDataspace")
111             mockCpsAdminPersistenceService.getDataspace('someDataspace') >> dataspace
112         expect: 'the dataspace provided by persistence service is returned as result'
113           assert objectUnderTest.getDataspace('someDataspace') == dataspace
114     }
115
116     def 'Retrieve all dataspaces.'() {
117         given: 'that all given dataspaces are already created'
118         def dataspaces = [new Dataspace(name: "test-dataspace1"), new Dataspace(name: "test-dataspace2")]
119             mockCpsAdminPersistenceService.getAllDataspaces() >> dataspaces
120         expect: 'the dataspace provided by persistence service is returned as result'
121            assert objectUnderTest.getAllDataspaces() == dataspaces
122     }
123
124     def 'Delete anchor.'() {
125         when: 'delete anchor is invoked'
126             objectUnderTest.deleteAnchor('someDataspace','someAnchor')
127         then: 'delete data nodes is invoked on the data service with expected parameters'
128             1 * mockCpsDataService.deleteDataNodes('someDataspace','someAnchor', _ as OffsetDateTime )
129         and: 'the persistence service method is invoked with same parameters to delete anchor'
130              1 * mockCpsAdminPersistenceService.deleteAnchor('someDataspace','someAnchor')
131         and: 'the CpsValidator is called on the dataspaceName, anchorName'
132             1 * mockCpsValidator.validateNameCharacters('someDataspace', 'someAnchor')
133     }
134
135     def 'Delete multiple anchors.'() {
136         when: 'delete anchors is invoked'
137             objectUnderTest.deleteAnchors('someDataspace', ['anchor1', 'anchor2'])
138         then: 'delete data nodes is invoked on the data service with expected parameters'
139             1 * mockCpsDataService.deleteDataNodes('someDataspace', _ as Collection<String>, _ as OffsetDateTime)
140         and: 'the persistence service method is invoked with same parameters to delete anchor'
141             1 * mockCpsAdminPersistenceService.deleteAnchors('someDataspace',_ as Collection<String>)
142         and: 'the CpsValidator is called on the dataspace name and anchor names'
143             1 * mockCpsValidator.validateNameCharacters('someDataspace')
144             1 * mockCpsValidator.validateNameCharacters(_)
145     }
146
147     def 'Query all anchor identifiers for a dataspace and module names.'() {
148         given: 'the persistence service is invoked with the expected parameters and returns a list of anchors'
149             mockCpsAdminPersistenceService.queryAnchors('some-dataspace-name', ['some-module-name']) >> [new Anchor(name:'some-anchor-identifier')]
150         when: 'query anchor names is called using a dataspace name and module name'
151             def result = objectUnderTest.queryAnchorNames('some-dataspace-name', ['some-module-name'])
152         then: 'get anchor identifiers returns the same anchor identifier returned by the persistence layer'
153             result == ['some-anchor-identifier']
154         and: 'the CpsValidator is called on the dataspaceName'
155             1 * mockCpsValidator.validateNameCharacters('some-dataspace-name')
156     }
157
158     def 'Query all anchors with Module Names Not Found Exception in persistence layer.'() {
159         given: 'the persistence layer throws a Module Names Not Found Exception'
160             def originalException = new ModuleNamesNotFoundException('exception-ds', [ 'm1', 'm2'])
161             mockCpsAdminPersistenceService.queryAnchors(*_) >> { throw originalException}
162         when: 'attempt query anchors'
163             objectUnderTest.queryAnchorNames('some-dataspace-name', [])
164         then: 'the same exception is thrown (up)'
165             def thrownUp = thrown(ModuleNamesNotFoundException)
166             assert thrownUp == originalException
167         and: 'the exception details contains the relevant data'
168             assert thrownUp.details.contains('exception-ds')
169             assert thrownUp.details.contains('m1')
170             assert thrownUp.details.contains('m2')
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         and: 'the CpsValidator is called on the dataspaceName'
179             1 * mockCpsValidator.validateNameCharacters('someDataspace')
180     }
181 }