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 spock.lang.Specification
40 class SubscriptionModelLoaderSpec extends Specification {
42 def mockCpsAdminService = Mock(CpsAdminService)
43 def mockCpsModuleService = Mock(CpsModuleService)
44 def mockCpsDataService = Mock(CpsDataService)
45 def objectUnderTest = new SubscriptionModelLoader(mockCpsAdminService, mockCpsModuleService, mockCpsDataService)
47 def SUBSCRIPTION_DATASPACE_NAME = objectUnderTest.SUBSCRIPTION_DATASPACE_NAME
48 def SUBSCRIPTION_ANCHOR_NAME = objectUnderTest.SUBSCRIPTION_ANCHOR_NAME
49 def SUBSCRIPTION_SCHEMASET_NAME = objectUnderTest.SUBSCRIPTION_SCHEMASET_NAME
50 def SUBSCRIPTION_REGISTRY_DATANODE_NAME = objectUnderTest.SUBSCRIPTION_REGISTRY_DATANODE_NAME
52 def sampleYangContentMap = ['subscription.yang':'module subscription { *sample content* }']
54 def applicationReadyEvent = new ApplicationReadyEvent(new SpringApplication(), null, null, null)
61 logger = (Logger) LoggerFactory.getLogger(objectUnderTest.getClass())
62 appender = new ListAppender()
63 logger.setLevel(Level.DEBUG)
65 logger.addAppender(appender)
70 ((Logger) LoggerFactory.getLogger(SubscriptionModelLoader.class)).detachAndStopAllAppenders()
73 def 'Onboard subscription model successfully via application ready event'() {
74 when:'model loader is enabled'
75 objectUnderTest.subscriptionModelLoaderEnabled = true
76 and: 'the application is ready'
77 objectUnderTest.onApplicationEvent(applicationReadyEvent)
78 then: 'the module service to create schema set is called once'
79 1 * mockCpsModuleService.createSchemaSet(SUBSCRIPTION_DATASPACE_NAME, SUBSCRIPTION_SCHEMASET_NAME,sampleYangContentMap)
80 and: 'the admin service to create an anchor set is called once'
81 1 * mockCpsAdminService.createAnchor(SUBSCRIPTION_DATASPACE_NAME, SUBSCRIPTION_SCHEMASET_NAME, SUBSCRIPTION_ANCHOR_NAME)
82 and: 'the data service to create a top level datanode is called once'
83 1 * mockCpsDataService.saveData(SUBSCRIPTION_DATASPACE_NAME, SUBSCRIPTION_ANCHOR_NAME, '{"' + SUBSCRIPTION_REGISTRY_DATANODE_NAME + '":{}}', _)
86 def 'No subscription model onboarding when subscription model loader is disabled' () {
87 when: 'model loader is disabled'
88 objectUnderTest.subscriptionModelLoaderEnabled = false
89 and: 'application is ready'
90 objectUnderTest.onApplicationEvent(applicationReadyEvent)
91 then: 'the module service to create schema set was not called'
92 0 * mockCpsModuleService.createSchemaSet(*_)
93 and: 'the admin service to create an anchor set was not called'
94 0 * mockCpsAdminService.createAnchor(*_)
95 and: 'the data service to create a top level datanode was not called'
96 0 * mockCpsDataService.saveData(*_)
100 def 'Create schema set from model file'() {
101 given: 'the method to create yang resource to content map returns the correct map'
102 def yangResourceToContentMap = objectUnderTest.createYangResourceToContentMap()
103 when: 'the method to create schema set is called with the following parameters'
104 objectUnderTest.createSchemaSet("myDataspace", "mySchemaSet", yangResourceToContentMap)
105 then: 'yang resource to content map is as expected'
106 assert sampleYangContentMap == yangResourceToContentMap
107 and: 'the module service to create schema set is called once with the correct map'
108 1 * mockCpsModuleService.createSchemaSet(_, _, yangResourceToContentMap)
111 def 'Create schema set fails due to AlreadyDefined exception'() {
112 given: 'the method to create yang resource to content map returns the correct map'
113 def yangResourceToContentMap = objectUnderTest.createYangResourceToContentMap()
114 and: 'creating a schema set throws an exception as it already exists'
115 mockCpsModuleService.createSchemaSet(SUBSCRIPTION_DATASPACE_NAME, SUBSCRIPTION_SCHEMASET_NAME, yangResourceToContentMap) >>
116 { throw AlreadyDefinedException.forSchemaSet(SUBSCRIPTION_SCHEMASET_NAME, "sampleContextName", null) }
117 when: 'the method to onboard model is called'
118 objectUnderTest.onboardSubscriptionModel()
119 then: 'the admin service to create an anchor set is then called once'
120 1 * mockCpsAdminService.createAnchor(SUBSCRIPTION_DATASPACE_NAME, SUBSCRIPTION_SCHEMASET_NAME, SUBSCRIPTION_ANCHOR_NAME)
123 def 'Create schema set fails due to any other exception'() {
124 given: 'the method to create yang resource to content map returns the correct map'
125 def yangResourceToContentMap = objectUnderTest.createYangResourceToContentMap()
126 and: 'creating a schema set throws an exception'
127 mockCpsModuleService.createSchemaSet(SUBSCRIPTION_DATASPACE_NAME, SUBSCRIPTION_SCHEMASET_NAME, yangResourceToContentMap) >>
128 { throw new NcmpStartUpException("Creating schema set failed", "") }
129 when: 'the method to onboard model is called'
130 objectUnderTest.onboardSubscriptionModel()
131 then: 'the log message contains the correct exception message'
132 def debugMessage = appender.list[0].toString()
133 assert debugMessage.contains("Creating schema set failed")
134 and: 'exception is thrown'
135 thrown(NcmpStartUpException)
138 def 'Create anchor fails due to AlreadyDefined exception'() {
139 given: 'creating anchor throws an exception as it already exists'
140 mockCpsAdminService.createAnchor(SUBSCRIPTION_DATASPACE_NAME, SUBSCRIPTION_SCHEMASET_NAME, SUBSCRIPTION_ANCHOR_NAME) >>
141 { throw AlreadyDefinedException.forSchemaSet(SUBSCRIPTION_SCHEMASET_NAME, "sampleContextName", null) }
142 when: 'the method to onboard model is called'
143 objectUnderTest.onboardSubscriptionModel()
144 then: 'no exception thrown'
148 def 'Create anchor fails due to any other exception'() {
149 given: 'creating an anchor failed'
150 mockCpsAdminService.createAnchor(SUBSCRIPTION_DATASPACE_NAME, SUBSCRIPTION_SCHEMASET_NAME, SUBSCRIPTION_ANCHOR_NAME) >>
151 { throw new SchemaSetNotFoundException(SUBSCRIPTION_DATASPACE_NAME, SUBSCRIPTION_SCHEMASET_NAME) }
152 when: 'the method to onboard model is called'
153 objectUnderTest.onboardSubscriptionModel()
154 then: 'the log message contains the correct exception message'
155 def debugMessage = appender.list[0].toString()
156 assert debugMessage.contains("Schema Set not found")
157 and: 'exception is thrown'
158 thrown(NcmpStartUpException)
161 def 'Create top level node fails due to an AlreadyDefined exception'() {
162 given: 'the saving of the node data will throw an Already Defined exception'
163 mockCpsDataService.saveData(*_) >>
164 { throw AlreadyDefinedException.forDataNode('/xpath', "sampleContextName", null) }
165 when: 'the method to onboard model is called'
166 objectUnderTest.onboardSubscriptionModel()
167 then: 'no exception thrown'
171 def 'Create top level node fails due to any other exception'() {
172 given: 'the saving of the node data will throw an exception'
173 mockCpsDataService.saveData(*_) >>
174 { throw new DataValidationException("Invalid JSON", "JSON Data is invalid") }
175 when: 'the method to onboard model is called'
176 objectUnderTest.onboardSubscriptionModel()
177 then: 'the log message contains the correct exception message'
178 def debugMessage = appender.list[0].toString()
179 assert debugMessage.contains("Creating data node for subscription model failed: Invalid JSON")
180 and: 'exception is thrown'
181 thrown(NcmpStartUpException)
184 def 'Get file content as string'() {
185 when: 'the method to get yang content is called'
186 def response = objectUnderTest.getFileContentAsString()
187 then: 'the response is as expected'
188 assert response == 'module subscription { *sample content* }'