Use constants for magic numbers in perf tests
[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.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 all anchors for multiple schema-sets.'() {
83         given: 'that anchor is associated with the dataspace and schemasets'
84             def anchors = [new Anchor(), new Anchor()]
85             mockCpsAdminPersistenceService.getAnchors('someDataspace', _ as Collection<String>) >> anchors
86         when: 'get anchors is called for a dataspace name and schema set names'
87             def result = objectUnderTest.getAnchors('someDataspace', ['schemaSet1', 'schemaSet2'])
88         then: 'the collection provided by persistence service is returned as result'
89             result == anchors
90         and: 'the CpsValidator is called on the dataspace name and schema-set names'
91             1 * mockCpsValidator.validateNameCharacters('someDataspace')
92             1 * mockCpsValidator.validateNameCharacters(_)
93     }
94
95     def 'Retrieve anchor for dataspace and provided anchor name.'() {
96         given: 'that anchor name is associated with the dataspace'
97             Anchor anchor = new Anchor()
98             mockCpsAdminPersistenceService.getAnchor('someDataspace','someAnchor') >>  anchor
99         when: 'get anchor is called for a dataspace name and anchor name'
100             def result = objectUnderTest.getAnchor('someDataspace','someAnchor')
101         then: 'the anchor provided by persistence service is returned as result'
102             result == anchor
103         and: 'the CpsValidator is called on the dataspaceName, anchorName'
104             1 * mockCpsValidator.validateNameCharacters('someDataspace', 'someAnchor')
105     }
106
107     def 'Retrieve dataspace.'() {
108         given: 'a dataspace is already created'
109             def dataspace = new Dataspace(name: "someDataspace")
110             mockCpsAdminPersistenceService.getDataspace('someDataspace') >> dataspace
111         expect: 'the dataspace provided by persistence service is returned as result'
112           assert objectUnderTest.getDataspace('someDataspace') == dataspace
113     }
114
115     def 'Retrieve all dataspaces.'() {
116         given: 'that all given dataspaces are already created'
117         def dataspaces = [new Dataspace(name: "test-dataspace1"), new Dataspace(name: "test-dataspace2")]
118             mockCpsAdminPersistenceService.getAllDataspaces() >> dataspaces
119         expect: 'the dataspace provided by persistence service is returned as result'
120            assert objectUnderTest.getAllDataspaces() == dataspaces
121     }
122
123     def 'Delete anchor.'() {
124         when: 'delete anchor is invoked'
125             objectUnderTest.deleteAnchor('someDataspace','someAnchor')
126         then: 'delete data nodes is invoked on the data service with expected parameters'
127             1 * mockCpsDataService.deleteDataNodes('someDataspace','someAnchor', _ as OffsetDateTime )
128         and: 'the persistence service method is invoked with same parameters to delete anchor'
129              1 * mockCpsAdminPersistenceService.deleteAnchor('someDataspace','someAnchor')
130         and: 'the CpsValidator is called on the dataspaceName, anchorName'
131             1 * mockCpsValidator.validateNameCharacters('someDataspace', 'someAnchor')
132     }
133
134     def 'Delete multiple anchors.'() {
135         when: 'delete anchors is invoked'
136             objectUnderTest.deleteAnchors('someDataspace', ['anchor1', 'anchor2'])
137         then: 'delete data nodes is invoked on the data service with expected parameters'
138             1 * mockCpsDataService.deleteDataNodes('someDataspace', _ as Collection<String>, _ as OffsetDateTime)
139         and: 'the persistence service method is invoked with same parameters to delete anchor'
140             1 * mockCpsAdminPersistenceService.deleteAnchors('someDataspace',_ as Collection<String>)
141         and: 'the CpsValidator is called on the dataspace name and anchor names'
142             1 * mockCpsValidator.validateNameCharacters('someDataspace')
143             1 * mockCpsValidator.validateNameCharacters(_)
144     }
145
146     def 'Query all anchor identifiers for a dataspace and module names.'() {
147         given: 'the persistence service is invoked with the expected parameters and returns a list of anchors'
148             mockCpsAdminPersistenceService.queryAnchors('some-dataspace-name', ['some-module-name']) >> [new Anchor(name:'some-anchor-identifier')]
149         when: 'query anchor names is called using a dataspace name and module name'
150             def result = objectUnderTest.queryAnchorNames('some-dataspace-name', ['some-module-name'])
151         then: 'get anchor identifiers returns the same anchor identifier returned by the persistence layer'
152             result == ['some-anchor-identifier']
153         and: 'the CpsValidator is called on the dataspaceName'
154             1 * mockCpsValidator.validateNameCharacters('some-dataspace-name')
155     }
156
157     def 'Delete dataspace.'() {
158         when: 'delete dataspace is invoked'
159             objectUnderTest.deleteDataspace('someDataspace')
160         then: 'associated persistence service method is invoked with correct parameter'
161             1 * mockCpsAdminPersistenceService.deleteDataspace('someDataspace')
162         and: 'the CpsValidator is called on the dataspaceName'
163             1 * mockCpsValidator.validateNameCharacters('someDataspace')
164     }
165 }