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