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