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
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.api.impl
23 import org.onap.cps.TestUtils
24 import org.onap.cps.api.model.AnchorDetails
25 import org.onap.cps.exceptions.CpsValidationException
26 import org.onap.cps.spi.DataPersistenceService
27 import org.onap.cps.spi.FragmentPersistenceService
28 import org.opendaylight.yangtools.yang.common.Revision
29 import org.opendaylight.yangtools.yang.model.api.SchemaContext
30 import spock.lang.Specification
32 class CpServiceImplSpec extends Specification {
34 def mockDataPersistenceService = Mock(DataPersistenceService)
35 def mockFragmentPersistenceService = Mock(FragmentPersistenceService)
36 def objectUnderTest = new CpServiceImpl()
39 objectUnderTest.dataPersistenceService = mockDataPersistenceService
40 objectUnderTest.fragmentPersistenceService = mockFragmentPersistenceService
43 def 'Cps Service provides to its client the id assigned by the system when storing a data structure'() {
44 given: 'that data persistence service is giving id 123 to a data structure it is asked to store'
45 mockDataPersistenceService.storeJsonStructure(_) >> 123
46 expect: 'Cps service returns the same id when storing data structure'
47 objectUnderTest.storeJsonStructure('') == 123
50 def 'Parse and Validate a Yang Model with a Valid Yang Model'() {
51 given: 'a yang model (file)'
52 def yangModel = TestUtils.getResourceFileContent('bookstore.yang')
53 when: 'a valid model is parsed and validated'
54 def result = objectUnderTest.parseAndValidateModel(yangModel)
55 then: 'Verify a schema context for that model is created with the correct identity'
59 def 'Parse and Validate a Yang Model Using a File'() {
60 given: 'a yang file that contains a yang model'
61 File file = new File(ClassLoader.getSystemClassLoader().getResource('bookstore.yang').getFile())
62 when: 'a model is parsed and validated'
63 def result = objectUnderTest.parseAndValidateModel(file)
64 then: 'Verify a schema context for that model is created with the correct identity'
69 def assertModule(SchemaContext schemaContext){
70 def optionalModule = schemaContext.findModule('bookstore', Revision.of('2020-09-15'))
71 return schemaContext.modules.size() == 1 && optionalModule.isPresent()
74 def 'Parse and Validate an Invalid Model'() {
75 given: 'a yang file that contains a invalid yang model'
76 File file = new File(ClassLoader.getSystemClassLoader().getResource('invalid.yang').getFile())
77 when: 'the model is parsed and validated'
78 objectUnderTest.parseAndValidateModel(file)
79 then: 'a CpsValidationException is thrown'
80 thrown(CpsValidationException)
83 def 'Store a SchemaContext'() {
84 expect: 'No exception to be thrown when a valid model (schema) is stored'
85 objectUnderTest.storeSchemaContext(Stub(SchemaContext.class), "sampleDataspace")
88 def 'Read a JSON object with a valid identifier'(){
89 given: 'that the data persistence service returns a JSON structure for identifier 1'
90 mockDataPersistenceService.getJsonById(1) >> '{name : hello}'
91 expect: 'that the same JSON structure is returned by CPS'
92 objectUnderTest.getJsonById(1) == '{name : hello}'
95 def 'Read a JSON object with an identifier that does not exist'(){
96 given: 'that the data persistence service throws an exception'
97 def exceptionThrownByPersistenceService = new IllegalStateException()
98 mockDataPersistenceService.getJsonById(_) >> {throw exceptionThrownByPersistenceService}
99 when: 'we try to get the JSON structure'
100 objectUnderTest.getJsonById(1);
101 then: 'the same exception is thrown by CPS'
102 thrown(IllegalStateException)
105 def 'Delete a JSON object with a valid identifier'(){
106 given: 'that the data persistence service can delete a JSON structure for identifier 1'
107 mockDataPersistenceService.deleteJsonById(1)
108 expect: 'No exception is thrown when we delete a JSON structure with identifier 1'
109 objectUnderTest.deleteJsonById(1)
112 def 'Delete a JSON object with an identifier that does not exist'(){
113 given: 'that the data persistence service throws an exception'
114 mockDataPersistenceService.deleteJsonById(_) >> {throw new IllegalStateException()}
115 when: 'we try to delete a JSON structure'
116 objectUnderTest.deleteJsonById(100);
117 then: 'the same exception is thrown by CPS'
118 thrown(IllegalStateException)
121 def 'Create an anchor with a non-existant dataspace'(){
122 given: 'that the dataspace does not exist service throws an exception'
123 AnchorDetails anchorDetails = new AnchorDetails()
124 anchorDetails.setDataspace('dummyDataspace')
125 mockFragmentPersistenceService.createAnchor(anchorDetails) >> {throw new CpsValidationException(_ as String, _ as String)}
126 when: 'we try to create a anchor with a non-existant dataspace'
127 objectUnderTest.createAnchor(anchorDetails)
128 then: 'the same exception is thrown by CPS'
129 thrown(CpsValidationException)
132 def 'Create an anchor with invalid dataspace, namespace and revision'(){
133 given: 'that the dataspace, namespace and revison combination does not exist service throws an exception'
134 AnchorDetails anchorDetails = new AnchorDetails()
135 anchorDetails.setDataspace('dummyDataspace')
136 anchorDetails.setNamespace('dummyNamespace')
137 anchorDetails.setRevision('dummyRevision')
138 mockFragmentPersistenceService.createAnchor(anchorDetails) >> {throw new CpsValidationException(_ as String, _ as String)}
139 when: 'we try to create a anchor with a non-existant dataspace, namespace and revison combination'
140 objectUnderTest.createAnchor(anchorDetails)
141 then: 'the same exception is thrown by CPS'
142 thrown(CpsValidationException)
145 def 'Create a duplicate anchor'(){
146 given: 'that the anchor already exist service throws an exception'
147 AnchorDetails anchorDetails = new AnchorDetails()
148 anchorDetails.setDataspace('dummyDataspace')
149 anchorDetails.setNamespace('dummyNamespace')
150 anchorDetails.setRevision('dummyRevision')
151 anchorDetails.setRevision('dummyAnchorName')
152 mockFragmentPersistenceService.createAnchor(anchorDetails) >> {throw new CpsValidationException(_ as String, _ as String)}
153 when: 'we try to create a duplicate anchor'
154 objectUnderTest.createAnchor(anchorDetails)
155 then: 'the same exception is thrown by CPS'
156 thrown(CpsValidationException)
159 def 'Create an anchor with supplied anchor name, dataspace, namespace and revision'(){
160 given: 'that the anchor does not pre-exist service creates an anchor'
161 AnchorDetails anchorDetails = new AnchorDetails()
162 anchorDetails.setDataspace('dummyDataspace')
163 anchorDetails.setNamespace('dummyNamespace')
164 anchorDetails.setRevision('dummyRevision')
165 anchorDetails.setRevision('dummyAnchorName')
166 mockFragmentPersistenceService.createAnchor(anchorDetails) >> 'dummyAnchorName'
167 expect: 'anchor name is returned by service'
168 objectUnderTest.createAnchor(anchorDetails) == 'dummyAnchorName'