2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020 Nordix Foundation
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 * SPDX-License-Identifier: Apache-2.0
17 * ============LICENSE_END=========================================================
20 package org.onap.cps.api.impl
22 import org.onap.cps.TestUtils
23 import org.onap.cps.spi.DataPersistencyService
24 import org.opendaylight.yangtools.yang.common.Revision
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext
26 import org.opendaylight.yangtools.yang.model.parser.api.YangParserException
27 import spock.lang.Specification
29 class CpServiceImplSpec extends Specification {
31 def mockDataPersistencyService = Mock(DataPersistencyService)
32 def objectUnderTest = new CpServiceImpl()
35 objectUnderTest.dataPersistencyService = mockDataPersistencyService;
38 def 'Cps Service provides to its client the id assigned by the system when storing a data structure'() {
39 given: 'that data persistency service is giving id 123 to a data structure it is asked to store'
40 mockDataPersistencyService.storeJsonStructure(_) >> 123
41 expect: 'Cps service returns the same id when storing data structure'
42 objectUnderTest.storeJsonStructure('') == 123
45 def 'Parse and Validate a Yang Model with a Valid Yang Model'() {
46 given: 'a yang model (file)'
47 def yangModel = TestUtils.getResourceFileContent('bookstore.yang')
48 when: 'a valid model is parsed and validated'
49 def result = objectUnderTest.parseAndValidateModel(yangModel)
50 then: 'Verify a schema context for that model is created with the correct identity'
54 def 'Parse and Validate a Yang Model Using a File'() {
55 given: 'a yang file that contains a yang model'
56 File file = new File(ClassLoader.getSystemClassLoader().getResource('bookstore.yang').getFile())
57 when: 'a model is parsed and validated'
58 def result = objectUnderTest.parseAndValidateModel(file)
59 then: 'Verify a schema context for that model is created with the correct identity'
64 def assertModule(SchemaContext schemaContext){
65 def optionalModule = schemaContext.findModule('bookstore', Revision.of('2020-09-15'))
66 return schemaContext.modules.size() == 1 && optionalModule.isPresent()
69 def 'Parse and Validate an Invalid Model'() {
70 given: 'a yang file that contains a invalid yang model'
71 File file = new File(ClassLoader.getSystemClassLoader().getResource('invalid.yang').getFile())
72 when: 'the model is parsed and validated'
73 objectUnderTest.parseAndValidateModel(file)
74 then: 'a YangParserException is thrown'
75 thrown(YangParserException)
78 def 'Store a SchemaContext'() {
79 expect: 'No exception to be thrown when a valid model (schema) is stored'
80 objectUnderTest.storeSchemaContext(Stub(SchemaContext.class))
83 def 'Read a JSON object with a valid identifier'(){
84 given: 'that the data persistence service returns a JSON structure for identifier 1'
85 mockDataPersistencyService.getJsonById(1) >> '{name : hello}'
86 expect: 'that the same JSON structure is returned by CPS'
87 objectUnderTest.getJsonById(1) == '{name : hello}'
90 def 'Read a JSON object with an identifier that does not exist'(){
91 given: 'that the data persistence service throws an exception'
92 def exceptionThrownByPersistenceService = new IllegalStateException()
93 mockDataPersistencyService.getJsonById(_) >> {throw exceptionThrownByPersistenceService}
94 when: 'we try to get the JSON structure'
95 objectUnderTest.getJsonById(1);
96 then: 'the same exception is thrown by CPS'
97 thrown(IllegalStateException)