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