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
9 * 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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.ncmp.init
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.CpsAdminService
27 import org.onap.cps.api.CpsDataService
28 import org.onap.cps.api.CpsModuleService
29 import org.onap.cps.ncmp.api.impl.exception.NcmpStartUpException
30 import org.onap.cps.spi.CascadeDeleteAllowed
31 import org.onap.cps.spi.exceptions.AlreadyDefinedException
32 import org.springframework.boot.SpringApplication
33 import org.slf4j.LoggerFactory
34 import org.springframework.boot.context.event.ApplicationReadyEvent
35 import org.springframework.context.annotation.AnnotationConfigApplicationContext
36 import spock.lang.Specification
38 class AbstractModelLoaderSpec extends Specification {
40 def mockCpsAdminService = Mock(CpsAdminService)
41 def mockCpsModuleService = Mock(CpsModuleService)
42 def mockCpsDataService = Mock(CpsDataService)
43 def objectUnderTest = Spy(new TestModelLoader(mockCpsAdminService, mockCpsModuleService, mockCpsDataService))
45 def applicationContext = new AnnotationConfigApplicationContext()
47 def yangResourceToContentMap
48 def logger = (Logger) LoggerFactory.getLogger(AbstractModelLoader)
49 def loggingListAppender
52 yangResourceToContentMap = objectUnderTest.createYangResourceToContentMap('subscription.yang')
53 logger.setLevel(Level.DEBUG)
54 loggingListAppender = new ListAppender()
55 logger.addAppender(loggingListAppender)
56 loggingListAppender.start()
57 applicationContext.refresh()
61 ((Logger) LoggerFactory.getLogger(SubscriptionModelLoader.class)).detachAndStopAllAppenders()
62 applicationContext.close()
65 def 'Application ready event'() {
66 when: 'Application (ready) event is triggered'
67 objectUnderTest.onApplicationEvent(Mock(ApplicationReadyEvent))
68 then: 'the onboard/upgrade method is executed'
69 1 * objectUnderTest.onboardOrUpgradeModel()
72 def 'Application ready event with start up exception'() {
73 given: 'a start up exception is thrown doing model onboarding'
74 objectUnderTest.onboardOrUpgradeModel() >> { throw new NcmpStartUpException('test message','details are not logged') }
75 when: 'Application (ready) event is triggered'
76 objectUnderTest.onApplicationEvent(new ApplicationReadyEvent(new SpringApplication(), null, applicationContext, null))
77 then: 'the exception message is logged'
78 def logs = loggingListAppender.list.toString()
79 assert logs.contains('test message')
82 def 'Wait for non-existing dataspace'() {
83 when: 'wait for the dataspace'
84 objectUnderTest.waitUntilDataspaceIsAvailable('some dataspace')
85 then: 'a startup exception is thrown'
86 def thrown = thrown(NcmpStartUpException)
87 assert thrown.message.contains('Retrieval of NCMP dataspace failed')
90 def 'Create schema set.'() {
91 when: 'creating a schema set'
92 objectUnderTest.createSchemaSet('some dataspace','new name','subscription.yang')
93 then: 'the operation is delegated to the admin service'
94 1 * mockCpsModuleService.createSchemaSet('some dataspace','new name',_)
97 def 'Create schema set with already defined exception.'() {
98 given: 'the module service throws an already defined exception'
99 mockCpsModuleService.createSchemaSet(*_) >> { throw AlreadyDefinedException.forSchemaSet('name','context',null) }
100 when: 'attempt to create a schema set'
101 objectUnderTest.createSchemaSet('some dataspace','new name','subscription.yang')
102 then: 'the exception is ignored i.e. no exception thrown up'
104 and: 'the exception message is logged'
105 def logs = loggingListAppender.list.toString()
106 assert logs.contains('Creating new schema set failed as schema set already exists')
109 def 'Create schema set with non existing yang file.'() {
110 when: 'attempt to create a schema set from a non existing file'
111 objectUnderTest.createSchemaSet('some dataspace','some name','no such yang file')
112 then: 'a startup exception with correct message and details is thrown'
113 def thrown = thrown(NcmpStartUpException)
114 assert thrown.message.contains('Creating schema set failed')
115 assert thrown.details.contains('unable to read file')
118 def 'Delete unused schema sets.'() {
119 when: 'several unused schemas are deleted '
120 objectUnderTest.deleteUnusedSchemaSets('some dataspace','schema set 1', 'schema set 2')
121 then: 'a request to delete each (without cascade) is delegated to the module service'
122 1 * mockCpsModuleService.deleteSchemaSet('some dataspace', 'schema set 1', CascadeDeleteAllowed.CASCADE_DELETE_PROHIBITED)
123 1 * mockCpsModuleService.deleteSchemaSet('some dataspace', 'schema set 2', CascadeDeleteAllowed.CASCADE_DELETE_PROHIBITED)
127 def 'Delete unused schema sets with exception.'() {
128 given: 'deleting the first schemaset causes an exception'
129 mockCpsModuleService.deleteSchemaSet(_, 'schema set 1', _) >> { throw new RuntimeException('test message')}
130 when: 'several unused schemas are deleted '
131 objectUnderTest.deleteUnusedSchemaSets('some dataspace','schema set 1', 'schema set 2')
132 then: 'the exception message is logged'
133 def logs = loggingListAppender.list.toString()
134 assert logs.contains('Deleting schema set failed')
135 assert logs.contains('test message')
136 and: 'the second schema set is still deleted'
137 1 * mockCpsModuleService.deleteSchemaSet('some dataspace', 'schema set 2', CascadeDeleteAllowed.CASCADE_DELETE_PROHIBITED)
140 def 'Create anchor.'() {
141 when: 'creating an anchor'
142 objectUnderTest.createAnchor('some dataspace','some schema set','new name')
143 then: 'the operation is delegated to the admin service'
144 1 * mockCpsAdminService.createAnchor('some dataspace','some schema set', 'new name')
147 def 'Create anchor with already defined exception.'() {
148 given: 'the admin service throws an already defined exception'
149 mockCpsAdminService.createAnchor(*_)>> { throw AlreadyDefinedException.forAnchor('name','context',null) }
150 when: 'attempt to create anchor'
151 objectUnderTest.createAnchor('some dataspace','some schema set','new name')
152 then: 'the exception is ignored i.e. no exception thrown up'
154 and: 'the exception message is logged'
155 def logs = loggingListAppender.list.toString()
156 assert logs.contains('Creating new anchor failed as anchor already exists')
159 def 'Create anchor with any other exception.'() {
160 given: 'the admin service throws a exception'
161 mockCpsAdminService.createAnchor(*_)>> { throw new RuntimeException('test message') }
162 when: 'attempt to create anchor'
163 objectUnderTest.createAnchor('some dataspace','some schema set','new name')
164 then: 'a startup exception with correct message and details is thrown'
165 def thrown = thrown(NcmpStartUpException)
166 assert thrown.message.contains('Creating anchor failed')
167 assert thrown.details.contains('test message')
170 def 'Create top level node.'() {
171 when: 'top level node is created'
172 objectUnderTest.createTopLevelDataNode('dataspace','anchor','new node')
173 then: 'the correct json is saved using the data service'
174 1 * mockCpsDataService.saveData('dataspace','anchor', '{"new node":{}}',_)
177 def 'Create top level node with already defined exception.'() {
178 given: 'the data service throws an Already Defined exception'
179 mockCpsDataService.saveData(*_) >> { throw AlreadyDefinedException.forDataNodes([], 'some context') }
180 when: 'attempt to create top level node'
181 objectUnderTest.createTopLevelDataNode('dataspace','anchor','new node')
182 then: 'the exception is ignored i.e. no exception thrown up'
184 and: 'the exception message is logged'
185 def logs = loggingListAppender.list.toString()
186 assert logs.contains('failed as data node already exists')
189 def 'Create top level node with any other exception.'() {
190 given: 'the data service throws an exception'
191 mockCpsDataService.saveData(*_) >> { throw new RuntimeException('test message') }
192 when: 'attempt to create top level node'
193 objectUnderTest.createTopLevelDataNode('dataspace','anchor','new node')
194 then: 'a startup exception with correct message and details is thrown'
195 def thrown = thrown(NcmpStartUpException)
196 assert thrown.message.contains('Creating data node failed')
197 assert thrown.details.contains('test message')
200 def 'Update anchor schema set.'() {
201 when: 'a schema set for an anchor is updated'
202 objectUnderTest.updateAnchorSchemaSet('some dataspace', 'anchor', 'new schema set')
203 then: 'the request is delegated to the admin service'
204 1 * mockCpsAdminService.updateAnchorSchemaSet('some dataspace', 'anchor', 'new schema set')
207 def 'Update anchor schema set with exception.'() {
208 given: 'the admin service throws an exception'
209 mockCpsAdminService.updateAnchorSchemaSet(*_) >> { throw new RuntimeException('test message') }
210 when: 'a schema set for an anchor is updated'
211 objectUnderTest.updateAnchorSchemaSet('some dataspace', 'anchor', 'new schema set')
212 then: 'a startup exception with correct message and details is thrown'
213 def thrown = thrown(NcmpStartUpException)
214 assert thrown.message.contains('Updating schema set failed')
215 assert thrown.details.contains('test message')
218 class TestModelLoader extends AbstractModelLoader {
220 TestModelLoader(final CpsAdminService cpsAdminService,
221 final CpsModuleService cpsModuleService,
222 final CpsDataService cpsDataService) {
223 super(cpsAdminService, cpsModuleService, cpsDataService)
224 super.maximumAttemptCount = 2
225 super.retryTimeMs = 1
229 void onboardOrUpgradeModel() { }