bb122d1ae20874f3eeeb8b1baa6df4ecb550013c
[cps.git] / cps-service / src / test / groovy / org / onap / cps / api / impl / CpsAdminServiceImplSpec.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 Nordix Foundation
4  *  Modifications Copyright (C) 2020-2022 Bell Canada.
5  *  Modifications Copyright (C) 2021 Pantheon.tech
6  *  ================================================================================
7  *  Licensed under the Apache License, Version 2.0 (the "License");
8  *  you may not use this file except in compliance with the License.
9  *  You may obtain a copy of the License at
10  *
11  *        http://www.apache.org/licenses/LICENSE-2.0
12  *
13  *  Unless required by applicable law or agreed to in writing, software
14  *  distributed under the License is distributed on an "AS IS" BASIS,
15  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *  See the License for the specific language governing permissions and
17  *  limitations under the License.
18  *
19  *  SPDX-License-Identifier: Apache-2.0
20  *  ============LICENSE_END=========================================================
21  */
22
23 package org.onap.cps.api.impl
24
25 import org.onap.cps.api.CpsDataService
26 import org.onap.cps.spi.CpsAdminPersistenceService
27 import org.onap.cps.spi.model.Anchor
28 import spock.lang.Specification
29 import java.time.OffsetDateTime
30
31 class CpsAdminServiceImplSpec extends Specification {
32     def mockCpsAdminPersistenceService = Mock(CpsAdminPersistenceService)
33     def mockCpsDataService = Mock(CpsDataService)
34     def objectUnderTest = new CpsAdminServiceImpl(mockCpsAdminPersistenceService, mockCpsDataService)
35
36     def 'Create dataspace method invokes persistence service.'() {
37         when: 'create dataspace method is invoked'
38             objectUnderTest.createDataspace('someDataspace')
39         then: 'the persistence service method is invoked with same parameters'
40             1 * mockCpsAdminPersistenceService.createDataspace('someDataspace')
41     }
42
43     def 'Create anchor method invokes persistence service.'() {
44         when: 'create anchor method is invoked'
45             objectUnderTest.createAnchor('someDataspace', 'someSchemaSet', 'someAnchorName')
46         then: 'the persistence service method is invoked with same parameters'
47             1 * mockCpsAdminPersistenceService.createAnchor('someDataspace', 'someSchemaSet', 'someAnchorName')
48     }
49
50     def 'Retrieve all anchors for dataspace.'() {
51         given: 'that anchor is associated with the dataspace'
52             def anchors = [new Anchor()]
53             mockCpsAdminPersistenceService.getAnchors('someDataspace') >> anchors
54         expect: 'the collection provided by persistence service is returned as result'
55             objectUnderTest.getAnchors('someDataspace') == anchors
56     }
57
58     def 'Retrieve all anchors for schema-set.'() {
59         given: 'that anchor is associated with the dataspace and schemaset'
60             def anchors = [new Anchor()]
61             mockCpsAdminPersistenceService.getAnchors('someDataspace', 'someSchemaSet') >> anchors
62         expect: 'the collection provided by persistence service is returned as result'
63             objectUnderTest.getAnchors('someDataspace', 'someSchemaSet') == anchors
64     }
65
66     def 'Retrieve anchor for dataspace and provided anchor name.'() {
67         given: 'that anchor name is associated with the dataspace'
68             Anchor anchor = new Anchor()
69             mockCpsAdminPersistenceService.getAnchor('someDataspace','someAnchor') >>  anchor
70         expect: 'the anchor provided by persistence service is returned as result'
71             assert objectUnderTest.getAnchor('someDataspace','someAnchor') == anchor
72     }
73
74     def 'Delete anchor.'() {
75         when: 'delete anchor is invoked'
76             objectUnderTest.deleteAnchor('someDataspace','someAnchor')
77         then: 'delete data nodes is invoked on the data service with expected parameters'
78             1 * mockCpsDataService.deleteDataNodes('someDataspace','someAnchor', _ as OffsetDateTime )
79         and: 'the persistence service method is invoked with same parameters to delete anchor'
80              1 * mockCpsAdminPersistenceService.deleteAnchor('someDataspace','someAnchor')
81     }
82
83     def 'Query all anchor identifiers for a dataspace and module names.'() {
84         given: 'the persistence service is invoked with the expected parameters and returns a list of anchors'
85             mockCpsAdminPersistenceService.queryAnchors('some-dataspace-name', ['some-module-name']) >> [new Anchor(name:'some-anchor-identifier')]
86         expect: 'get anchor identifiers returns the same anchor identifier returned by the persistence layer'
87             objectUnderTest.queryAnchorNames('some-dataspace-name', ['some-module-name']) == ['some-anchor-identifier']
88
89     }
90
91     def 'Delete dataspace.'() {
92         when: 'delete dataspace is invoked'
93             objectUnderTest.deleteDataspace('someDataspace')
94         then: 'associated persistence service method is invoked with correct parameter'
95             1 * mockCpsAdminPersistenceService.deleteDataspace('someDataspace')
96     }
97
98 }