2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2023-2024 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.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.ApplicationStartedEvent
36 import org.springframework.context.annotation.AnnotationConfigApplicationContext
37 import spock.lang.Specification
39 class AbstractModelLoaderSpec extends Specification {
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, mockCpsAnchorService, mockCpsDataService))
47 def applicationContext = new AnnotationConfigApplicationContext()
49 def yangResourceToContentMap
50 def logger = (Logger) LoggerFactory.getLogger(AbstractModelLoader)
51 def loggingListAppender
54 yangResourceToContentMap = objectUnderTest.createYangResourcesToContentMap('cm-data-subscriptions@2024-02-12.yang')
55 logger.setLevel(Level.DEBUG)
56 loggingListAppender = new ListAppender()
57 logger.addAppender(loggingListAppender)
58 loggingListAppender.start()
59 applicationContext.refresh()
63 ((Logger) LoggerFactory.getLogger(CmDataSubscriptionModelLoader.class)).detachAndStopAllAppenders()
64 applicationContext.close()
67 def 'Application started event'() {
68 when: 'Application (started) event is triggered'
69 objectUnderTest.onApplicationEvent(Mock(ApplicationStartedEvent))
70 then: 'the onboard/upgrade method is executed'
71 1 * objectUnderTest.onboardOrUpgradeModel()
74 def 'Application started 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 (started) event is triggered'
78 objectUnderTest.onApplicationEvent(new ApplicationStartedEvent(new SpringApplication(), null, applicationContext, null))
79 then: 'the exception message is logged'
80 def logs = loggingListAppender.list.toString()
81 assert logs.contains('test message')
84 def 'Create schema set.'() {
85 when: 'creating a schema set'
86 objectUnderTest.createSchemaSet('some dataspace','new name','cm-data-subscriptions@2024-02-12.yang')
87 then: 'the operation is delegated to the admin service'
88 1 * mockCpsModuleService.createSchemaSet('some dataspace','new name',_)
91 def 'Create schema set with already defined exception.'() {
92 given: 'the module service throws an already defined exception'
93 mockCpsModuleService.createSchemaSet(*_) >> { throw AlreadyDefinedException.forSchemaSet('name','context',null) }
94 when: 'attempt to create a schema set'
95 objectUnderTest.createSchemaSet('some dataspace','new name','cm-data-subscriptions@2024-02-12.yang')
96 then: 'the exception is ignored i.e. no exception thrown up'
98 and: 'the exception message is logged'
99 def logs = loggingListAppender.list.toString()
100 assert logs.contains('Creating new schema set failed as schema set already exists')
103 def 'Create schema set with non existing yang file.'() {
104 when: 'attempt to create a schema set from a non existing file'
105 objectUnderTest.createSchemaSet('some dataspace','some name','no such yang file')
106 then: 'a startup exception with correct message and details is thrown'
107 def thrown = thrown(NcmpStartUpException)
108 assert thrown.message.contains('Creating schema set failed')
109 assert thrown.details.contains('unable to read file')
112 def 'Delete unused schema sets.'() {
113 when: 'several unused schemas are deleted '
114 objectUnderTest.deleteUnusedSchemaSets('some dataspace','schema set 1', 'schema set 2')
115 then: 'a request to delete each (without cascade) is delegated to the module service'
116 1 * mockCpsModuleService.deleteSchemaSet('some dataspace', 'schema set 1', CascadeDeleteAllowed.CASCADE_DELETE_PROHIBITED)
117 1 * mockCpsModuleService.deleteSchemaSet('some dataspace', 'schema set 2', CascadeDeleteAllowed.CASCADE_DELETE_PROHIBITED)
121 def 'Delete unused schema sets with exception.'() {
122 given: 'deleting the first schemaset causes an exception'
123 mockCpsModuleService.deleteSchemaSet(_, 'schema set 1', _) >> { throw new RuntimeException('test message')}
124 when: 'several unused schemas are deleted '
125 objectUnderTest.deleteUnusedSchemaSets('some dataspace','schema set 1', 'schema set 2')
126 then: 'the exception message is logged'
127 def logs = loggingListAppender.list.toString()
128 assert logs.contains('Deleting schema set failed')
129 assert logs.contains('test message')
130 and: 'the second schema set is still deleted'
131 1 * mockCpsModuleService.deleteSchemaSet('some dataspace', 'schema set 2', CascadeDeleteAllowed.CASCADE_DELETE_PROHIBITED)
134 def 'Create anchor.'() {
135 when: 'creating an anchor'
136 objectUnderTest.createAnchor('some dataspace','some schema set','new name')
137 then: 'the operation is delegated to the admin service'
138 1 * mockCpsAnchorService.createAnchor('some dataspace','some schema set', 'new name')
141 def 'Create anchor with already defined exception.'() {
142 given: 'the admin service throws an already defined exception'
143 mockCpsAnchorService.createAnchor(*_)>> { throw AlreadyDefinedException.forAnchor('name','context',null) }
144 when: 'attempt to create anchor'
145 objectUnderTest.createAnchor('some dataspace','some schema set','new name')
146 then: 'the exception is ignored i.e. no exception thrown up'
148 and: 'the exception message is logged'
149 def logs = loggingListAppender.list.toString()
150 assert logs.contains('Creating new anchor failed as anchor already exists')
153 def 'Create anchor with any other exception.'() {
154 given: 'the admin service throws a exception'
155 mockCpsAnchorService.createAnchor(*_)>> { throw new RuntimeException('test message') }
156 when: 'attempt to create anchor'
157 objectUnderTest.createAnchor('some dataspace','some schema set','new name')
158 then: 'a startup exception with correct message and details is thrown'
159 def thrown = thrown(NcmpStartUpException)
160 assert thrown.message.contains('Creating anchor failed')
161 assert thrown.details.contains('test message')
164 def 'Create top level node.'() {
165 when: 'top level node is created'
166 objectUnderTest.createTopLevelDataNode('dataspace','anchor','new node')
167 then: 'the correct json is saved using the data service'
168 1 * mockCpsDataService.saveData('dataspace','anchor', '{"new node":{}}',_)
171 def 'Create top level node with already defined exception.'() {
172 given: 'the data service throws an Already Defined exception'
173 mockCpsDataService.saveData(*_) >> { throw AlreadyDefinedException.forDataNodes([], 'some context') }
174 when: 'attempt to create top level node'
175 objectUnderTest.createTopLevelDataNode('dataspace','anchor','new node')
176 then: 'the exception is ignored i.e. no exception thrown up'
178 and: 'the exception message is logged'
179 def logs = loggingListAppender.list.toString()
180 assert logs.contains('failed as data node already exists')
183 def 'Create top level node with any other exception.'() {
184 given: 'the data service throws an exception'
185 mockCpsDataService.saveData(*_) >> { throw new RuntimeException('test message') }
186 when: 'attempt to create top level node'
187 objectUnderTest.createTopLevelDataNode('dataspace','anchor','new node')
188 then: 'a startup exception with correct message and details is thrown'
189 def thrown = thrown(NcmpStartUpException)
190 assert thrown.message.contains('Creating data node failed')
191 assert thrown.details.contains('test message')
194 def 'Update anchor schema set.'() {
195 when: 'a schema set for an anchor is updated'
196 objectUnderTest.updateAnchorSchemaSet('some dataspace', 'anchor', 'new schema set')
197 then: 'the request is delegated to the admin service'
198 1 * mockCpsAnchorService.updateAnchorSchemaSet('some dataspace', 'anchor', 'new schema set')
201 def 'Update anchor schema set with exception.'() {
202 given: 'the admin service throws an exception'
203 mockCpsAnchorService.updateAnchorSchemaSet(*_) >> { throw new RuntimeException('test message') }
204 when: 'a schema set for an anchor is updated'
205 objectUnderTest.updateAnchorSchemaSet('some dataspace', 'anchor', 'new schema set')
206 then: 'a startup exception with correct message and details is thrown'
207 def thrown = thrown(NcmpStartUpException)
208 assert thrown.message.contains('Updating schema set failed')
209 assert thrown.details.contains('test message')
212 class TestModelLoader extends AbstractModelLoader {
214 TestModelLoader(final CpsDataspaceService cpsDataspaceService,
215 final CpsModuleService cpsModuleService,
216 final CpsAnchorService cpsAnchorService,
217 final CpsDataService cpsDataService) {
218 super(cpsDataspaceService, cpsModuleService, cpsAnchorService, cpsDataService)
219 super.maximumAttemptCount = 2
220 super.retryTimeMs = 1
224 void onboardOrUpgradeModel() { }