Adding tests for CpServiceImpl
[cps.git] / cps / cps-service / src / test / groovy / org / onap / cps / api / impl / CpServiceImplSpec.groovy
1 /*
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
8  *
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.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19
20 package org.onap.cps.api.impl
21
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
28
29 class CpServiceImplSpec extends Specification {
30
31     def mockDataPersistencyService = Mock(DataPersistencyService)
32     def objectUnderTest = new CpServiceImpl()
33
34     def setup() {
35         objectUnderTest.dataPersistencyService = mockDataPersistencyService;
36     }
37
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
43     }
44
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'
51             assertModule(result)
52     }
53
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'
60             assertModule(result)
61
62     }
63
64     def assertModule(SchemaContext schemaContext){
65         def optionalModule = schemaContext.findModule('bookstore', Revision.of('2020-09-15'))
66         return schemaContext.modules.size() == 1 && optionalModule.isPresent()
67     }
68
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)
76     }
77
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))
81     }
82 }