Exception handling on REST interface
[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  *  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.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
29
30 class CpServiceImplSpec extends Specification {
31
32     def mockDataPersistencyService = Mock(DataPersistencyService)
33     def objectUnderTest = new CpServiceImpl()
34
35     def setup() {
36         objectUnderTest.dataPersistencyService = mockDataPersistencyService
37     }
38
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
44     }
45
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'
52             assertModule(result)
53     }
54
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'
61             assertModule(result)
62
63     }
64
65     def assertModule(SchemaContext schemaContext){
66         def optionalModule = schemaContext.findModule('bookstore', Revision.of('2020-09-15'))
67         return schemaContext.modules.size() == 1 && optionalModule.isPresent()
68     }
69
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)
77     }
78
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")
82     }
83
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}'
89     }
90
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)
99     }
100
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)
106     }
107
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)
115     }
116 }