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.junit.jupiter.api.AfterEach
27 import org.junit.jupiter.api.BeforeEach
28 import org.onap.cps.api.CpsAdminService
29 import org.onap.cps.api.CpsDataService
30 import org.onap.cps.api.CpsModuleService
31 import org.onap.cps.ncmp.api.impl.exception.NcmpStartUpException
32 import org.onap.cps.spi.exceptions.AlreadyDefinedException
33 import org.onap.cps.spi.exceptions.DataValidationException
34 import org.onap.cps.spi.exceptions.SchemaSetNotFoundException
35 import org.springframework.boot.SpringApplication
36 import org.slf4j.LoggerFactory
37 import org.springframework.boot.context.event.ApplicationReadyEvent
38 import org.springframework.context.annotation.AnnotationConfigApplicationContext
39 import spock.lang.Specification
41 class SubscriptionModelLoaderSpec extends Specification {
43 def mockCpsAdminService = Mock(CpsAdminService)
44 def mockCpsModuleService = Mock(CpsModuleService)
45 def mockCpsDataService = Mock(CpsDataService)
46 def objectUnderTest = new SubscriptionModelLoader(mockCpsAdminService, mockCpsModuleService, mockCpsDataService)
48 def sampleYangContentMap = ['subscription.yang':'module subscription { *sample content* }']
50 def applicationContext = new AnnotationConfigApplicationContext()
52 def applicationReadyEvent = new ApplicationReadyEvent(new SpringApplication(), null, applicationContext, null)
54 def yangResourceToContentMap
60 yangResourceToContentMap = objectUnderTest.createYangResourceToContentMap()
61 logger = (Logger) LoggerFactory.getLogger(objectUnderTest.getClass())
62 appender = new ListAppender()
63 logger.setLevel(Level.DEBUG)
65 logger.addAppender(appender)
66 applicationContext.refresh()
71 ((Logger) LoggerFactory.getLogger(SubscriptionModelLoader.class)).detachAndStopAllAppenders()
72 applicationContext.close()
75 def 'Onboard subscription model successfully via application ready event'() {
76 when:'model loader is enabled'
77 objectUnderTest.subscriptionModelLoaderEnabled = true
78 and: 'the application is ready'
79 objectUnderTest.onApplicationEvent(applicationReadyEvent)
80 then: 'the module service to create schema set is called once'
81 1 * mockCpsModuleService.createSchemaSet('NCMP-Admin', 'subscriptions',sampleYangContentMap)
82 and: 'the admin service to create an anchor set is called once'
83 1 * mockCpsAdminService.createAnchor('NCMP-Admin', 'subscriptions', 'AVC-Subscriptions')
84 and: 'the data service to create a top level datanode is called once'
85 1 * mockCpsDataService.saveData('NCMP-Admin', 'AVC-Subscriptions', '{"subscription-registry":{}}', _)
88 def 'No subscription model onboarding when subscription model loader is disabled' () {
89 when: 'model loader is disabled'
90 objectUnderTest.subscriptionModelLoaderEnabled = false
91 and: 'application is ready'
92 objectUnderTest.onApplicationEvent(applicationReadyEvent)
93 then: 'the module service to create schema set was not called'
94 0 * mockCpsModuleService.createSchemaSet(*_)
95 and: 'the admin service to create an anchor set was not called'
96 0 * mockCpsAdminService.createAnchor(*_)
97 and: 'the data service to create a top level datanode was not called'
98 0 * mockCpsDataService.saveData(*_)
101 def 'Exception occurred while schema set creation' () {
102 given: 'creating a schema set throws an exception'
103 mockCpsModuleService.createSchemaSet(*_) >> { throw new DataValidationException(*_) }
104 and: 'model loader is enabled'
105 objectUnderTest.subscriptionModelLoaderEnabled = true
106 when: 'application is ready'
107 objectUnderTest.onApplicationEvent(applicationReadyEvent)
108 then: 'the admin service to create an anchor set was not called'
109 0 * mockCpsAdminService.createAnchor(*_)
110 and: 'the data service to create a top level datanode was not called'
111 0 * mockCpsDataService.saveData(*_)
114 def 'Create schema set from model file'() {
115 when: 'the method to create schema set is called with the following parameters'
116 objectUnderTest.createSchemaSet("myDataspace", "mySchemaSet", yangResourceToContentMap)
117 then: 'yang resource to content map is as expected'
118 assert sampleYangContentMap == yangResourceToContentMap
119 and: 'the module service to create schema set is called once with the correct map'
120 1 * mockCpsModuleService.createSchemaSet(_, _, yangResourceToContentMap)
123 def 'Create schema set fails due to AlreadyDefined exception'() {
124 given: 'creating a schema set throws an exception as it already exists'
125 mockCpsModuleService.createSchemaSet('NCMP-Admin', 'subscriptions', yangResourceToContentMap) >>
126 { throw AlreadyDefinedException.forSchemaSet('subscriptions', "sampleContextName", null) }
127 when: 'the method to onboard model is called'
128 objectUnderTest.onboardSubscriptionModel(yangResourceToContentMap)
129 then: 'the admin service to create an anchor set is then called once'
130 1 * mockCpsAdminService.createAnchor('NCMP-Admin', 'subscriptions', 'AVC-Subscriptions')
133 def 'Create schema set fails due to any other exception'() {
134 given: 'creating a schema set throws an exception'
135 mockCpsModuleService.createSchemaSet(*_) >> { throw new NcmpStartUpException("Creating schema set failed", "") }
136 when: 'the method to onboard model is called'
137 objectUnderTest.onboardSubscriptionModel(yangResourceToContentMap)
138 then: 'the log message contains the correct exception message'
139 def debugMessage = appender.list[0].toString()
140 assert debugMessage.contains("Creating schema set failed")
141 and: 'exception is thrown'
142 thrown(NcmpStartUpException)
145 def 'Create anchor fails due to AlreadyDefined exception'() {
146 given: 'creating anchor throws an exception as it already exists'
147 mockCpsAdminService.createAnchor(*_) >>
148 { throw AlreadyDefinedException.forSchemaSet('subscriptions', "sampleContextName", null) }
149 when: 'the method to onboard model is called'
150 objectUnderTest.onboardSubscriptionModel(yangResourceToContentMap)
151 then: 'no exception thrown'
153 and: 'the log message contains the correct exception message'
154 def infoMessage = appender.list[0].toString()
155 assert infoMessage.contains("already exists")
158 def 'Create anchor fails due to any other exception'() {
159 given: 'creating an anchor failed'
160 mockCpsAdminService.createAnchor(*_) >>
161 { throw new SchemaSetNotFoundException('NCMP-Admin', 'subscriptions') }
162 when: 'the method to onboard model is called'
163 objectUnderTest.onboardSubscriptionModel(yangResourceToContentMap)
164 then: 'the log message contains the correct exception message'
165 def debugMessage = appender.list[0].toString()
166 assert debugMessage.contains("Schema Set not found")
167 and: 'exception is thrown'
168 thrown(NcmpStartUpException)
171 def 'Create top level node fails due to an AlreadyDefined exception'() {
172 given: 'the saving of the node data will throw an Already Defined exception'
173 mockCpsDataService.saveData(*_) >>
174 { throw AlreadyDefinedException.forDataNode('/xpath', "sampleContextName", null) }
175 when: 'the method to onboard model is called'
176 objectUnderTest.onboardSubscriptionModel(yangResourceToContentMap)
177 then: 'no exception thrown'
179 and: 'the log message contains the correct exception message'
180 def infoMessage = appender.list[0].toString()
181 assert infoMessage.contains("already exists")
184 def 'Create top level node fails due to any other exception'() {
185 given: 'the saving of the node data will throw an exception'
186 mockCpsDataService.saveData(*_) >>
187 { throw new DataValidationException("Invalid JSON", "JSON Data is invalid") }
188 when: 'the method to onboard model is called'
189 objectUnderTest.onboardSubscriptionModel(yangResourceToContentMap)
190 then: 'the log message contains the correct exception message'
191 def debugMessage = appender.list[0].toString()
192 assert debugMessage.contains("Creating data node for subscription model failed: Invalid JSON")
193 and: 'exception is thrown'
194 thrown(NcmpStartUpException)
197 def 'Get file content as string'() {
198 when: 'the method to get yang content is called'
199 objectUnderTest.getFileContentAsString('NonExistingFile')
200 then: 'exception is thrown'
201 thrown(NcmpStartUpException)