Retrieve All anchors for a given Dataspace
[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 Bell Canada. All rights reserved.
5  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
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.exceptions.DataspaceNotFoundException
25 import org.onap.cps.spi.model.Anchor
26 import spock.lang.Specification
27
28 class CpsAdminServiceImplSpec extends Specification {
29     def mockCpsAdminPersistenceService = Mock(CpsAdminPersistenceService)
30     def objectUnderTest = new CpsAdminServiceImpl()
31     def anchor = new Anchor()
32
33     def setup() {
34         objectUnderTest.cpsAdminPersistenceService = mockCpsAdminPersistenceService
35     }
36
37     def 'Create an anchor'() {
38         given: 'that the persistence service returns the name of the anchor'
39             def anchorName = 'some anchor name'
40             mockCpsAdminPersistenceService.createAnchor(_) >> anchorName
41         expect: 'the same anchor name is returned by CPS Admin service'
42             objectUnderTest.createAnchor(anchor) == anchorName
43     }
44
45     def 'Create an anchor with some exception in the persistence layer'() {
46         given: 'that the persistence service throws some exception'
47             def exceptionThrownInPersistenceLayer = new RuntimeException()
48             mockCpsAdminPersistenceService.createAnchor(_) >> { throw exceptionThrownInPersistenceLayer }
49         when: 'we try to create an anchor'
50             objectUnderTest.createAnchor(anchor)
51         then: 'the same exception is thrown by the CPS Admin Service'
52             def exceptionThrownInServiceLayer = thrown(Exception)
53             exceptionThrownInServiceLayer == exceptionThrownInPersistenceLayer
54     }
55
56     def 'Retrieve all anchors for an existing dataspace'() {
57         given: 'that the dataspace exist and an anchor is associated with the dataspace'
58             Collection<Anchor> anchorCollection = Arrays.asList(anchor)
59             mockCpsAdminPersistenceService.getAnchors('dummyDataspace') >> { anchorCollection }
60         expect: 'we try to retrieve an anchor, a collection of anchor is returned by the service'
61             objectUnderTest.getAnchors('dummyDataspace') == anchorCollection
62     }
63
64     def 'Retrieve all anchors for a non existing dataspace'() {
65         given: 'that the dataspace does not exist, service throws an exception'
66             def exceptionThrownInPersistenceLayer = new DataspaceNotFoundException(_ as String)
67             mockCpsAdminPersistenceService.getAnchors('dummyDataspace') >>
68                     { throw exceptionThrownInPersistenceLayer }
69         when: 'we try to retrieve a anchor with a non-existant dataspace'
70             objectUnderTest.getAnchors('dummyDataspace')
71         then: 'the same exception is thrown by CPS'
72             def exceptionThrownInServiceLayer = thrown(Exception)
73             exceptionThrownInServiceLayer == exceptionThrownInPersistenceLayer
74     }
75 }