da2df404eb95eeea1b2fd3c878e2a37a5342e63c
[cps.git] / cps-service / src / test / groovy / org / onap / cps / api / impl / CpsModulePersistenceServiceImplSpec.groovy
1 /*
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
9  *
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.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.api.impl
22
23 import org.onap.cps.TestUtils
24 import org.onap.cps.spi.CpsModulePersistenceService
25 import org.onap.cps.spi.exceptions.CpsException
26 import org.opendaylight.yangtools.yang.common.Revision
27 import org.opendaylight.yangtools.yang.model.api.SchemaContext
28 import spock.lang.Specification
29
30 class CpsModulePersistenceServiceImplSpec extends Specification {
31     def mockModuleStoreService = Mock(CpsModulePersistenceService)
32     def objectUnderTest = new CpsModuleServiceImpl()
33
34     def setup() {
35         objectUnderTest.cpsModulePersistenceService = mockModuleStoreService
36     }
37
38     def 'Parse and Validate a Yang Model with a Valid Yang Model'() {
39         given: 'a yang model (file)'
40             def yangModel = TestUtils.getResourceFileContent('bookstore.yang')
41         when: 'a valid model is parsed and validated'
42             def result = objectUnderTest.parseAndValidateModel(yangModel)
43         then: 'Verify a schema context for that model is created with the correct identity'
44             assertModule(result)
45     }
46
47     def 'Parse and Validate a Yang Model Using a File'() {
48         given: 'a yang file that contains a yang model'
49             File file = new File(ClassLoader.getSystemClassLoader().getResource('bookstore.yang').getFile())
50         when: 'a model is parsed and validated'
51             def result = objectUnderTest.parseAndValidateModel(file)
52         then: 'Verify a schema context for that model is created with the correct identity'
53             assertModule(result)
54
55     }
56
57     def assertModule(SchemaContext schemaContext) {
58         def optionalModule = schemaContext.findModule('stores', Revision.of('2020-09-15'))
59         return schemaContext.modules.size() == 1 && optionalModule.isPresent()
60     }
61
62     def 'Parse and Validate an Invalid Model'() {
63         given: 'a yang file that contains a invalid yang model'
64             File file = new File(ClassLoader.getSystemClassLoader().getResource('invalid.yang').getFile())
65         when: 'the model is parsed and validated'
66             objectUnderTest.parseAndValidateModel(file)
67         then: 'a CpsValidationException is thrown'
68             thrown(CpsException)
69     }
70
71     def 'Store a SchemaContext'() {
72         expect: 'No exception to be thrown when a valid model (schema) is stored'
73             objectUnderTest.storeSchemaContext(Stub(SchemaContext.class), "sampleDataspace")
74     }
75
76 }