Create Common Model objects in SPI
[cps.git] / cps-service / src / test / groovy / org / onap / cps / api / impl / CpsAdminPersistenceServiceImplSpec.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.exceptions.CpsValidationException
24 import org.onap.cps.spi.CpsAdminPersistenceService
25 import org.onap.cps.spi.model.Anchor
26 import spock.lang.Specification
27
28 class CpsAdminPersistenceServiceImplSpec extends Specification {
29     def mockCpsAdminService = Mock(CpsAdminPersistenceService)
30     def objectUnderTest = new CpsAdminServiceImpl()
31
32     def setup() {
33         objectUnderTest.cpsAdminPersistenceService = mockCpsAdminService
34     }
35
36     def 'Create an anchor with a non-existant dataspace'() {
37         given: 'that the dataspace does not exist service throws an exception'
38             Anchor anchor = new Anchor()
39             anchor.setDataspaceName('dummyDataspace')
40             mockCpsAdminService.createAnchor(anchor) >> { throw new CpsValidationException(_ as String, _ as String) }
41         when: 'we try to create a anchor with a non-existant dataspace'
42             objectUnderTest.createAnchor(anchor)
43         then: 'the same exception is thrown by CPS'
44             thrown(CpsValidationException)
45     }
46
47     def 'Create an anchor with invalid dataspace, namespace and revision'() {
48         given: 'that the dataspace, namespace and revison combination does not exist service throws an exception'
49             Anchor anchor = new Anchor()
50             anchor.setDataspaceName('dummyDataspace')
51             anchor.setNamespace('dummyNamespace')
52             anchor.setRevision('dummyRevision')
53             mockCpsAdminService.createAnchor(anchor) >> { throw new CpsValidationException(_ as String, _ as String) }
54         when: 'we try to create a anchor with a non-existant dataspace, namespace and revison combination'
55             objectUnderTest.createAnchor(anchor)
56         then: 'the same exception is thrown by CPS'
57             thrown(CpsValidationException)
58     }
59
60     def 'Create a duplicate anchor'() {
61         given: 'that the anchor already exist service throws an exception'
62             Anchor anchor = new Anchor()
63             anchor.setDataspaceName('dummyDataspace')
64             anchor.setNamespace('dummyNamespace')
65             anchor.setRevision('dummyRevision')
66             anchor.setRevision('dummyAnchorName')
67             mockCpsAdminService.createAnchor(anchor) >> { throw new CpsValidationException(_ as String, _ as String) }
68         when: 'we try to create a duplicate anchor'
69             objectUnderTest.createAnchor(anchor)
70         then: 'the same exception is thrown by CPS'
71             thrown(CpsValidationException)
72     }
73
74     def 'Create an anchor with supplied anchor name, dataspace, namespace and revision'() {
75         given: 'that the anchor does not pre-exist service creates an anchor'
76             Anchor anchor = new Anchor()
77             anchor.setDataspaceName('dummyDataspace')
78             anchor.setNamespace('dummyNamespace')
79             anchor.setRevision('dummyRevision')
80             anchor.setRevision('dummyAnchorName')
81             mockCpsAdminService.createAnchor(anchor) >> 'dummyAnchorName'
82         expect: 'anchor name is returned by service'
83             objectUnderTest.createAnchor(anchor) == 'dummyAnchorName'
84     }
85
86 }