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.exceptions.CpsValidationException
25 import org.onap.cps.spi.DataPersistencyService
26 import org.opendaylight.yangtools.yang.common.Revision
27 import org.opendaylight.yangtools.yang.model.api.SchemaContext
28 import spock.lang.Specification
30 class CpServiceImplSpec extends Specification {
32 def mockDataPersistencyService = Mock(DataPersistencyService)
33 def objectUnderTest = new CpServiceImpl()
36 objectUnderTest.dataPersistencyService = mockDataPersistencyService
39 def 'Cps Service provides to its client the id assigned by the system when storing a data structure'() {
40 given: 'that data persistency service is giving id 123 to a data structure it is asked to store'
41 mockDataPersistencyService.storeJsonStructure(_) >> 123
42 expect: 'Cps service returns the same id when storing data structure'
43 objectUnderTest.storeJsonStructure('') == 123
46 def 'Parse and Validate a Yang Model with a Valid Yang Model'() {
47 given: 'a yang model (file)'
48 def yangModel = TestUtils.getResourceFileContent('bookstore.yang')
49 when: 'a valid model is parsed and validated'
50 def result = objectUnderTest.parseAndValidateModel(yangModel)
51 then: 'Verify a schema context for that model is created with the correct identity'
55 def 'Parse and Validate a Yang Model Using a File'() {
56 given: 'a yang file that contains a yang model'
57 File file = new File(ClassLoader.getSystemClassLoader().getResource('bookstore.yang').getFile())
58 when: 'a model is parsed and validated'
59 def result = objectUnderTest.parseAndValidateModel(file)
60 then: 'Verify a schema context for that model is created with the correct identity'
65 def assertModule(SchemaContext schemaContext){
66 def optionalModule = schemaContext.findModule('bookstore', Revision.of('2020-09-15'))
67 return schemaContext.modules.size() == 1 && optionalModule.isPresent()
70 def 'Parse and Validate an Invalid Model'() {
71 given: 'a yang file that contains a invalid yang model'
72 File file = new File(ClassLoader.getSystemClassLoader().getResource('invalid.yang').getFile())
73 when: 'the model is parsed and validated'
74 objectUnderTest.parseAndValidateModel(file)
75 then: 'a CpsValidationException is thrown'
76 thrown(CpsValidationException)
79 def 'Store a SchemaContext'() {
80 expect: 'No exception to be thrown when a valid model (schema) is stored'
81 objectUnderTest.storeSchemaContext(Stub(SchemaContext.class), "sampleDataspace")
84 def 'Read a JSON object with a valid identifier'(){
85 given: 'that the data persistence service returns a JSON structure for identifier 1'
86 mockDataPersistencyService.getJsonById(1) >> '{name : hello}'
87 expect: 'that the same JSON structure is returned by CPS'
88 objectUnderTest.getJsonById(1) == '{name : hello}'
91 def 'Read a JSON object with an identifier that does not exist'(){
92 given: 'that the data persistence service throws an exception'
93 def exceptionThrownByPersistenceService = new IllegalStateException()
94 mockDataPersistencyService.getJsonById(_) >> {throw exceptionThrownByPersistenceService}
95 when: 'we try to get the JSON structure'
96 objectUnderTest.getJsonById(1);
97 then: 'the same exception is thrown by CPS'
98 thrown(IllegalStateException)
101 def 'Delete a JSON object with a valid identifier'(){
102 given: 'that the data persistence service can delete a JSON structure for identifier 1'
103 mockDataPersistencyService.deleteJsonById(1)
104 expect: 'No exception is thrown when we delete a JSON structure with identifier 1'
105 objectUnderTest.deleteJsonById(1)
108 def 'Delete a JSON object with an identifier that does not exist'(){
109 given: 'that the data persistence service throws an exception'
110 mockDataPersistencyService.deleteJsonById(_) >> {throw new IllegalStateException()}
111 when: 'we try to delete a JSON structure'
112 objectUnderTest.deleteJsonById(100);
113 then: 'the same exception is thrown by CPS'
114 thrown(IllegalStateException)