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 * ================================================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.cps.api.impl
25 import org.onap.cps.api.CpsDataService
26 import org.onap.cps.spi.CpsAdminPersistenceService
27 import org.onap.cps.spi.model.Anchor
28 import org.onap.cps.spi.utils.CpsValidator
29 import spock.lang.Specification
30 import java.time.OffsetDateTime
32 class CpsAdminServiceImplSpec extends Specification {
33 def mockCpsAdminPersistenceService = Mock(CpsAdminPersistenceService)
34 def mockCpsDataService = Mock(CpsDataService)
35 def mockCpsValidator = Mock(CpsValidator)
36 def objectUnderTest = new CpsAdminServiceImpl(mockCpsAdminPersistenceService, mockCpsDataService,mockCpsValidator)
38 def 'Create dataspace method invokes persistence service.'() {
39 when: 'create dataspace method is invoked'
40 objectUnderTest.createDataspace('someDataspace')
41 then: 'the persistence service method is invoked with same parameters'
42 1 * mockCpsAdminPersistenceService.createDataspace('someDataspace')
43 and: 'the CpsValidator is called on the dataspaceName'
44 1 * mockCpsValidator.validateNameCharacters('someDataspace')
47 def 'Create anchor method invokes persistence service.'() {
48 when: 'create anchor method is invoked'
49 objectUnderTest.createAnchor('someDataspace', 'someSchemaSet', 'someAnchorName')
50 then: 'the persistence service method is invoked with same parameters'
51 1 * mockCpsAdminPersistenceService.createAnchor('someDataspace', 'someSchemaSet', 'someAnchorName')
52 and: 'the CpsValidator is called on the dataspaceName, schemaSetName and anchorName'
53 1 * mockCpsValidator.validateNameCharacters('someDataspace', 'someSchemaSet', 'someAnchorName')
56 def 'Retrieve all anchors for dataspace.'() {
57 given: 'that an anchor is associated with the dataspace'
58 def anchors = [new Anchor()]
59 mockCpsAdminPersistenceService.getAnchors('someDataspace') >> anchors
60 when: 'get Anchors is called for a dataspace name'
61 def result = objectUnderTest.getAnchors('someDataspace')
62 then: 'the collection provided by persistence service is returned as result'
64 and: 'the CpsValidator is called on the dataspaceName'
65 1 * mockCpsValidator.validateNameCharacters('someDataspace')
68 def 'Retrieve all anchors for schema-set.'() {
69 given: 'that anchor is associated with the dataspace and schemaset'
70 def anchors = [new Anchor()]
71 mockCpsAdminPersistenceService.getAnchors('someDataspace', 'someSchemaSet') >> anchors
72 when: 'get anchors is called for a dataspace name and schema set name'
73 def result = objectUnderTest.getAnchors('someDataspace', 'someSchemaSet')
74 then: 'the collection provided by persistence service is returned as result'
76 and: 'the CpsValidator is called on the dataspaceName, schemaSetName'
77 1 * mockCpsValidator.validateNameCharacters('someDataspace', 'someSchemaSet')
80 def 'Retrieve anchor for dataspace and provided anchor name.'() {
81 given: 'that anchor name is associated with the dataspace'
82 Anchor anchor = new Anchor()
83 mockCpsAdminPersistenceService.getAnchor('someDataspace','someAnchor') >> anchor
84 when: 'get anchor is called for a dataspace name and anchor name'
85 def result = objectUnderTest.getAnchor('someDataspace','someAnchor')
86 then: 'the anchor provided by persistence service is returned as result'
88 and: 'the CpsValidator is called on the dataspaceName, anchorName'
89 1 * mockCpsValidator.validateNameCharacters('someDataspace', 'someAnchor')
92 def 'Delete anchor.'() {
93 when: 'delete anchor is invoked'
94 objectUnderTest.deleteAnchor('someDataspace','someAnchor')
95 then: 'delete data nodes is invoked on the data service with expected parameters'
96 1 * mockCpsDataService.deleteDataNodes('someDataspace','someAnchor', _ as OffsetDateTime )
97 and: 'the persistence service method is invoked with same parameters to delete anchor'
98 1 * mockCpsAdminPersistenceService.deleteAnchor('someDataspace','someAnchor')
99 and: 'the CpsValidator is called on the dataspaceName, anchorName'
100 1 * mockCpsValidator.validateNameCharacters('someDataspace', 'someAnchor')
103 def 'Query all anchor identifiers for a dataspace and module names.'() {
104 given: 'the persistence service is invoked with the expected parameters and returns a list of anchors'
105 mockCpsAdminPersistenceService.queryAnchors('some-dataspace-name', ['some-module-name']) >> [new Anchor(name:'some-anchor-identifier')]
106 when: 'query anchor names is called using a dataspace name and module name'
107 def result = objectUnderTest.queryAnchorNames('some-dataspace-name', ['some-module-name'])
108 then: 'get anchor identifiers returns the same anchor identifier returned by the persistence layer'
109 result == ['some-anchor-identifier']
110 and: 'the CpsValidator is called on the dataspaceName'
111 1 * mockCpsValidator.validateNameCharacters('some-dataspace-name')
114 def 'Delete dataspace.'() {
115 when: 'delete dataspace is invoked'
116 objectUnderTest.deleteDataspace('someDataspace')
117 then: 'associated persistence service method is invoked with correct parameter'
118 1 * mockCpsAdminPersistenceService.deleteDataspace('someDataspace')
119 and: 'the CpsValidator is called on the dataspaceName'
120 1 * mockCpsValidator.validateNameCharacters('someDataspace')