Merge "Added get APIs for dataspace."
[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  *  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.model.Anchor
29 import org.onap.cps.spi.model.Dataspace
30 import org.onap.cps.spi.utils.CpsValidator
31 import spock.lang.Specification
32 import java.time.OffsetDateTime
33
34 class CpsAdminServiceImplSpec extends Specification {
35     def mockCpsAdminPersistenceService = Mock(CpsAdminPersistenceService)
36     def mockCpsDataService = Mock(CpsDataService)
37     def mockCpsValidator = Mock(CpsValidator)
38     def objectUnderTest = new CpsAdminServiceImpl(mockCpsAdminPersistenceService, mockCpsDataService,mockCpsValidator)
39
40     def 'Create dataspace method invokes persistence service.'() {
41         when: 'create dataspace method is invoked'
42             objectUnderTest.createDataspace('someDataspace')
43         then: 'the persistence service method is invoked with same parameters'
44             1 * mockCpsAdminPersistenceService.createDataspace('someDataspace')
45         and: 'the CpsValidator is called on the dataspaceName'
46             1 * mockCpsValidator.validateNameCharacters('someDataspace')
47     }
48
49     def 'Create anchor method invokes persistence service.'() {
50         when: 'create anchor method is invoked'
51             objectUnderTest.createAnchor('someDataspace', 'someSchemaSet', 'someAnchorName')
52         then: 'the persistence service method is invoked with same parameters'
53             1 * mockCpsAdminPersistenceService.createAnchor('someDataspace', 'someSchemaSet', 'someAnchorName')
54         and: 'the CpsValidator is called on the dataspaceName, schemaSetName and anchorName'
55             1 * mockCpsValidator.validateNameCharacters('someDataspace', 'someSchemaSet', 'someAnchorName')
56     }
57
58     def 'Retrieve all anchors for dataspace.'() {
59         given: 'that an anchor is associated with the dataspace'
60             def anchors = [new Anchor()]
61             mockCpsAdminPersistenceService.getAnchors('someDataspace') >> anchors
62         when: 'get Anchors is called for a dataspace name'
63             def result = objectUnderTest.getAnchors('someDataspace')
64         then: 'the collection provided by persistence service is returned as result'
65             result == anchors
66         and: 'the CpsValidator is called on the dataspaceName'
67             1 * mockCpsValidator.validateNameCharacters('someDataspace')
68     }
69
70     def 'Retrieve all anchors for schema-set.'() {
71         given: 'that anchor is associated with the dataspace and schemaset'
72             def anchors = [new Anchor()]
73             mockCpsAdminPersistenceService.getAnchors('someDataspace', 'someSchemaSet') >> anchors
74         when: 'get anchors is called for a dataspace name and schema set name'
75             def result = objectUnderTest.getAnchors('someDataspace', 'someSchemaSet')
76         then: 'the collection provided by persistence service is returned as result'
77             result == anchors
78         and: 'the CpsValidator is called on the dataspaceName, schemaSetName'
79             1 * mockCpsValidator.validateNameCharacters('someDataspace', 'someSchemaSet')
80     }
81
82     def 'Retrieve anchor for dataspace and provided anchor name.'() {
83         given: 'that anchor name is associated with the dataspace'
84             Anchor anchor = new Anchor()
85             mockCpsAdminPersistenceService.getAnchor('someDataspace','someAnchor') >>  anchor
86         when: 'get anchor is called for a dataspace name and anchor name'
87             def result = objectUnderTest.getAnchor('someDataspace','someAnchor')
88         then: 'the anchor provided by persistence service is returned as result'
89             result == anchor
90         and: 'the CpsValidator is called on the dataspaceName, anchorName'
91             1 * mockCpsValidator.validateNameCharacters('someDataspace', 'someAnchor')
92     }
93
94     def 'Retrieve dataspace.'() {
95         given: 'a dataspace is already created'
96             def dataspace = new Dataspace(name: "someDataspace")
97             mockCpsAdminPersistenceService.getDataspace('someDataspace') >> dataspace
98         expect: 'the dataspace provided by persistence service is returned as result'
99           assert objectUnderTest.getDataspace('someDataspace') == dataspace
100     }
101
102     def 'Retrieve all dataspaces.'() {
103         given: 'that all given dataspaces are already created'
104         def dataspaces = [new Dataspace(name: "test-dataspace1"), new Dataspace(name: "test-dataspace2")]
105             mockCpsAdminPersistenceService.getAllDataspaces() >> dataspaces
106         expect: 'the dataspace provided by persistence service is returned as result'
107            assert objectUnderTest.getAllDataspaces() == dataspaces
108     }
109
110     def 'Delete anchor.'() {
111         when: 'delete anchor is invoked'
112             objectUnderTest.deleteAnchor('someDataspace','someAnchor')
113         then: 'delete data nodes is invoked on the data service with expected parameters'
114             1 * mockCpsDataService.deleteDataNodes('someDataspace','someAnchor', _ as OffsetDateTime )
115         and: 'the persistence service method is invoked with same parameters to delete anchor'
116              1 * mockCpsAdminPersistenceService.deleteAnchor('someDataspace','someAnchor')
117         and: 'the CpsValidator is called on the dataspaceName, anchorName'
118             1 * mockCpsValidator.validateNameCharacters('someDataspace', 'someAnchor')
119     }
120
121     def 'Query all anchor identifiers for a dataspace and module names.'() {
122         given: 'the persistence service is invoked with the expected parameters and returns a list of anchors'
123             mockCpsAdminPersistenceService.queryAnchors('some-dataspace-name', ['some-module-name']) >> [new Anchor(name:'some-anchor-identifier')]
124         when: 'query anchor names is called using a dataspace name and module name'
125             def result = objectUnderTest.queryAnchorNames('some-dataspace-name', ['some-module-name'])
126         then: 'get anchor identifiers returns the same anchor identifier returned by the persistence layer'
127             result == ['some-anchor-identifier']
128         and: 'the CpsValidator is called on the dataspaceName'
129             1 * mockCpsValidator.validateNameCharacters('some-dataspace-name')
130     }
131
132     def 'Delete dataspace.'() {
133         when: 'delete dataspace is invoked'
134             objectUnderTest.deleteDataspace('someDataspace')
135         then: 'associated persistence service method is invoked with correct parameter'
136             1 * mockCpsAdminPersistenceService.deleteDataspace('someDataspace')
137         and: 'the CpsValidator is called on the dataspaceName'
138             1 * mockCpsValidator.validateNameCharacters('someDataspace')
139     }
140 }