3b1c25ba6a292fff5f1513bc89e97767431f554c
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / init / AbstractModelLoaderSpec.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2023 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  *
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.ncmp.init
22
23 import ch.qos.logback.classic.Level
24 import ch.qos.logback.classic.Logger
25 import ch.qos.logback.core.read.ListAppender
26 import org.onap.cps.api.CpsDataspaceService
27 import org.onap.cps.api.CpsAnchorService
28 import org.onap.cps.api.CpsDataService
29 import org.onap.cps.api.CpsModuleService
30 import org.onap.cps.ncmp.api.impl.exception.NcmpStartUpException
31 import org.onap.cps.spi.CascadeDeleteAllowed
32 import org.onap.cps.spi.exceptions.AlreadyDefinedException
33 import org.springframework.boot.SpringApplication
34 import org.slf4j.LoggerFactory
35 import org.springframework.boot.context.event.ApplicationReadyEvent
36 import org.springframework.context.annotation.AnnotationConfigApplicationContext
37 import spock.lang.Specification
38
39 class AbstractModelLoaderSpec extends Specification {
40
41     def mockCpsDataspaceService = Mock(CpsDataspaceService)
42     def mockCpsModuleService = Mock(CpsModuleService)
43     def mockCpsDataService = Mock(CpsDataService)
44     def mockCpsAnchorService = Mock(CpsAnchorService)
45     def objectUnderTest = Spy(new TestModelLoader(mockCpsDataspaceService, mockCpsModuleService, mockCpsDataService, mockCpsAnchorService))
46
47     def applicationContext = new AnnotationConfigApplicationContext()
48
49     def yangResourceToContentMap
50     def logger = (Logger) LoggerFactory.getLogger(AbstractModelLoader)
51     def loggingListAppender
52
53     void setup() {
54         yangResourceToContentMap = objectUnderTest.createYangResourcesToContentMap('subscription.yang')
55         logger.setLevel(Level.DEBUG)
56         loggingListAppender = new ListAppender()
57         logger.addAppender(loggingListAppender)
58         loggingListAppender.start()
59         applicationContext.refresh()
60     }
61
62     void cleanup() {
63         ((Logger) LoggerFactory.getLogger(CmDataSubscriptionModelLoader.class)).detachAndStopAllAppenders()
64         applicationContext.close()
65     }
66
67     def 'Application ready event'() {
68         when: 'Application (ready) event is triggered'
69             objectUnderTest.onApplicationEvent(Mock(ApplicationReadyEvent))
70         then: 'the onboard/upgrade method is executed'
71             1 * objectUnderTest.onboardOrUpgradeModel()
72     }
73
74     def 'Application ready event with start up exception'() {
75         given: 'a start up exception is thrown doing model onboarding'
76             objectUnderTest.onboardOrUpgradeModel() >> { throw new NcmpStartUpException('test message','details are not logged') }
77         when: 'Application (ready) event is triggered'
78             objectUnderTest.onApplicationEvent(new ApplicationReadyEvent(new SpringApplication(), null, applicationContext, null))
79         then: 'the exception message is logged'
80             def logs = loggingListAppender.list.toString()
81             assert logs.contains('test message')
82     }
83
84     def 'Wait for non-existing dataspace'() {
85         when: 'wait for the dataspace'
86             objectUnderTest.waitUntilDataspaceIsAvailable('some dataspace')
87         then: 'a startup exception is thrown'
88             def thrown = thrown(NcmpStartUpException)
89             assert thrown.message.contains('Retrieval of NCMP dataspace failed')
90     }
91
92     def 'Create schema set.'() {
93         when: 'creating a schema set'
94             objectUnderTest.createSchemaSet('some dataspace','new name','subscription.yang')
95         then: 'the operation is delegated to the admin service'
96             1 * mockCpsModuleService.createSchemaSet('some dataspace','new name',_)
97     }
98
99     def 'Create schema set with already defined exception.'() {
100         given: 'the module service throws an already defined exception'
101             mockCpsModuleService.createSchemaSet(*_) >>  { throw AlreadyDefinedException.forSchemaSet('name','context',null) }
102         when: 'attempt to create a schema set'
103             objectUnderTest.createSchemaSet('some dataspace','new name','subscription.yang')
104         then: 'the exception is ignored i.e. no exception thrown up'
105             noExceptionThrown()
106         and: 'the exception message is logged'
107             def logs = loggingListAppender.list.toString()
108             assert logs.contains('Creating new schema set failed as schema set already exists')
109     }
110
111     def 'Create schema set with non existing yang file.'() {
112         when: 'attempt to create a schema set from a non existing file'
113             objectUnderTest.createSchemaSet('some dataspace','some name','no such yang file')
114         then: 'a startup exception with correct message and details is thrown'
115             def thrown = thrown(NcmpStartUpException)
116             assert thrown.message.contains('Creating schema set failed')
117             assert thrown.details.contains('unable to read file')
118     }
119
120     def 'Delete unused schema sets.'() {
121         when: 'several unused schemas are deleted '
122             objectUnderTest.deleteUnusedSchemaSets('some dataspace','schema set 1', 'schema set 2')
123         then: 'a request to delete each (without cascade) is delegated to the module service'
124             1 * mockCpsModuleService.deleteSchemaSet('some dataspace', 'schema set 1', CascadeDeleteAllowed.CASCADE_DELETE_PROHIBITED)
125             1 * mockCpsModuleService.deleteSchemaSet('some dataspace', 'schema set 2', CascadeDeleteAllowed.CASCADE_DELETE_PROHIBITED)
126
127     }
128
129     def 'Delete unused schema sets with exception.'() {
130         given: 'deleting the first schemaset causes an exception'
131             mockCpsModuleService.deleteSchemaSet(_, 'schema set 1', _) >> { throw new RuntimeException('test message')}
132         when: 'several unused schemas are deleted '
133             objectUnderTest.deleteUnusedSchemaSets('some dataspace','schema set 1', 'schema set 2')
134         then: 'the exception message is logged'
135             def logs = loggingListAppender.list.toString()
136             assert logs.contains('Deleting schema set failed')
137             assert logs.contains('test message')
138         and: 'the second schema set is still deleted'
139             1 * mockCpsModuleService.deleteSchemaSet('some dataspace', 'schema set 2', CascadeDeleteAllowed.CASCADE_DELETE_PROHIBITED)
140     }
141
142     def 'Create anchor.'() {
143         when: 'creating an anchor'
144             objectUnderTest.createAnchor('some dataspace','some schema set','new name')
145         then: 'the operation is delegated to the admin service'
146             1 * mockCpsAnchorService.createAnchor('some dataspace','some schema set', 'new name')
147     }
148
149     def 'Create anchor with already defined exception.'() {
150         given: 'the admin service throws an already defined exception'
151             mockCpsAnchorService.createAnchor(*_)>>  { throw AlreadyDefinedException.forAnchor('name','context',null) }
152         when: 'attempt to create anchor'
153             objectUnderTest.createAnchor('some dataspace','some schema set','new name')
154         then: 'the exception is ignored i.e. no exception thrown up'
155             noExceptionThrown()
156         and: 'the exception message is logged'
157             def logs = loggingListAppender.list.toString()
158             assert logs.contains('Creating new anchor failed as anchor already exists')
159     }
160
161     def 'Create anchor with any other exception.'() {
162         given: 'the admin service throws a exception'
163             mockCpsAnchorService.createAnchor(*_)>>  { throw new RuntimeException('test message') }
164         when: 'attempt to create anchor'
165             objectUnderTest.createAnchor('some dataspace','some schema set','new name')
166         then: 'a startup exception with correct message and details is thrown'
167             def thrown = thrown(NcmpStartUpException)
168             assert thrown.message.contains('Creating anchor failed')
169             assert thrown.details.contains('test message')
170     }
171
172     def 'Create top level node.'() {
173         when: 'top level node is created'
174             objectUnderTest.createTopLevelDataNode('dataspace','anchor','new node')
175         then: 'the correct json is saved using the data service'
176             1 * mockCpsDataService.saveData('dataspace','anchor', '{"new node":{}}',_)
177     }
178
179     def 'Create top level node with already defined exception.'() {
180         given: 'the data service throws an Already Defined exception'
181             mockCpsDataService.saveData(*_) >> { throw AlreadyDefinedException.forDataNodes([], 'some context') }
182         when: 'attempt to create top level node'
183             objectUnderTest.createTopLevelDataNode('dataspace','anchor','new node')
184         then: 'the exception is ignored i.e. no exception thrown up'
185             noExceptionThrown()
186         and: 'the exception message is logged'
187             def logs = loggingListAppender.list.toString()
188             assert logs.contains('failed as data node already exists')
189     }
190
191     def 'Create top level node with any other exception.'() {
192         given: 'the data service throws an exception'
193             mockCpsDataService.saveData(*_) >> { throw new RuntimeException('test message') }
194         when: 'attempt to create top level node'
195             objectUnderTest.createTopLevelDataNode('dataspace','anchor','new node')
196         then: 'a startup exception with correct message and details is thrown'
197             def thrown = thrown(NcmpStartUpException)
198             assert thrown.message.contains('Creating data node failed')
199             assert thrown.details.contains('test message')
200     }
201
202     def 'Update anchor schema set.'() {
203         when: 'a schema set for an anchor is updated'
204             objectUnderTest.updateAnchorSchemaSet('some dataspace', 'anchor', 'new schema set')
205         then: 'the request is delegated to the admin service'
206             1 * mockCpsAnchorService.updateAnchorSchemaSet('some dataspace', 'anchor', 'new schema set')
207     }
208
209     def 'Update anchor schema set with exception.'() {
210         given: 'the admin service throws an exception'
211             mockCpsAnchorService.updateAnchorSchemaSet(*_) >> { throw new RuntimeException('test message') }
212         when: 'a schema set for an anchor is updated'
213             objectUnderTest.updateAnchorSchemaSet('some dataspace', 'anchor', 'new schema set')
214         then: 'a startup exception with correct message and details is thrown'
215             def thrown = thrown(NcmpStartUpException)
216             assert thrown.message.contains('Updating schema set failed')
217             assert thrown.details.contains('test message')
218     }
219
220     class TestModelLoader extends AbstractModelLoader {
221
222         TestModelLoader(final CpsDataspaceService cpsDataspaceService,
223                         final CpsModuleService cpsModuleService,
224                         final CpsDataService cpsDataService,
225                         final CpsAnchorService cpsAnchorService) {
226             super(cpsDataspaceService, cpsModuleService, cpsDataService, cpsAnchorService)
227             super.maximumAttemptCount = 2
228             super.retryTimeMs = 1
229         }
230
231         @Override
232         void onboardOrUpgradeModel() { }
233     }
234
235 }