Merge "Schedule response to client"
[cps.git] / cps-service / src / test / groovy / org / onap / cps / api / impl / CpsAnchorServiceImplSpec.groovy
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2023 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.spi.CpsAdminPersistenceService
24 import org.onap.cps.spi.CpsDataPersistenceService
25 import org.onap.cps.spi.exceptions.ModuleNamesNotFoundException
26 import org.onap.cps.spi.model.Anchor
27 import org.onap.cps.spi.utils.CpsValidator
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.getAnchors('someDataspace', 'someSchemaSet') >> anchors
63         when: 'get anchors is called for a dataspace name and schema set name'
64             def result = objectUnderTest.getAnchors('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.getAnchors('someDataspace', _ as Collection<String>) >> anchors
75         when: 'get anchors is called for a dataspace name and schema set names'
76             def result = objectUnderTest.getAnchors('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 'Delete anchor.'() {
97         when: 'delete anchor is invoked'
98             objectUnderTest.deleteAnchor('someDataspace','someAnchor')
99         then: 'delete data nodes is invoked on the data service with expected parameters'
100             1 * mockCpsDataPersistenceService.deleteDataNodes('someDataspace','someAnchor')
101         and: 'the persistence service method is invoked with same parameters to delete anchor'
102             1 * mockCpsAdminPersistenceService.deleteAnchor('someDataspace','someAnchor')
103         and: 'the CpsValidator is called on the dataspaceName, anchorName'
104             1 * mockCpsValidator.validateNameCharacters('someDataspace', 'someAnchor')
105     }
106
107     def 'Delete multiple anchors.'() {
108         when: 'delete anchors is invoked'
109             objectUnderTest.deleteAnchors('someDataspace', ['anchor1', 'anchor2'])
110         then: 'delete data nodes is invoked on the data service with expected parameters'
111             1 * mockCpsDataPersistenceService.deleteDataNodes('someDataspace', _ as Collection<String>)
112         and: 'the persistence service method is invoked with same parameters to delete anchor'
113             1 * mockCpsAdminPersistenceService.deleteAnchors('someDataspace',_ as Collection<String>)
114         and: 'the CpsValidator is called on the dataspace name and anchor names'
115             1 * mockCpsValidator.validateNameCharacters('someDataspace')
116             1 * mockCpsValidator.validateNameCharacters(_)
117     }
118
119     def 'Query all anchor identifiers for a dataspace and module names.'() {
120         given: 'the persistence service is invoked with the expected parameters and returns a list of anchors'
121             mockCpsAdminPersistenceService.queryAnchorNames('some-dataspace-name', ['some-module-name']) >> ['some-anchor-identifier']
122         when: 'query anchor names is called using a dataspace name and module name'
123             def result = objectUnderTest.queryAnchorNames('some-dataspace-name', ['some-module-name'])
124         then: 'get anchor identifiers returns the same anchor identifier returned by the persistence layer'
125             result == ['some-anchor-identifier']
126         and: 'the CpsValidator is called on the dataspaceName'
127             1 * mockCpsValidator.validateNameCharacters('some-dataspace-name')
128     }
129
130     def 'Query all anchors with Module Names Not Found Exception in persistence layer.'() {
131         given: 'the persistence layer throws a Module Names Not Found Exception'
132             def originalException = new ModuleNamesNotFoundException('exception-ds', ['m1', 'm2'])
133             mockCpsAdminPersistenceService.queryAnchorNames(*_) >> { throw originalException}
134         when: 'attempt query anchors'
135             objectUnderTest.queryAnchorNames('some-dataspace-name', [])
136         then: 'the same exception is thrown (up)'
137             def thrownUp = thrown(ModuleNamesNotFoundException)
138             assert thrownUp == originalException
139         and: 'the exception details contains the relevant data'
140             assert thrownUp.details.contains('exception-ds')
141             assert thrownUp.details.contains('m1')
142             assert thrownUp.details.contains('m2')
143     }
144
145     def 'Update anchor schema set.'() {
146         when: 'update anchor is invoked'
147             objectUnderTest.updateAnchorSchemaSet('someDataspace', 'someAnchor', 'someSchemaSetName')
148         then: 'associated persistence service method is invoked with correct parameter'
149             1 * mockCpsAdminPersistenceService.updateAnchorSchemaSet('someDataspace', 'someAnchor', 'someSchemaSetName')
150     }
151
152 }