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 'Create schema set from model file'() {
87 given: 'the method to create yang resource to content map returns the correct map'
88 def yangResourceToContentMap = objectUnderTest.createYangResourceToContentMap()
89 when: 'the method to create schema set is called with the following parameters'
90 objectUnderTest.createSchemaSet("myDataspace", "mySchemaSet", yangResourceToContentMap)
91 then: 'yang resource to content map is as expected'
92 assert sampleYangContentMap == yangResourceToContentMap
93 and: 'the module service to create schema set is called once with the correct map'
94 1 * mockCpsModuleService.createSchemaSet(_, _, yangResourceToContentMap)
97 def 'Create schema set fails due to AlreadyDefined exception'() {
98 given: 'the method to create yang resource to content map returns the correct map'
99 def yangResourceToContentMap = objectUnderTest.createYangResourceToContentMap()
100 and: 'creating a schema set throws an exception as it already exists'
101 mockCpsModuleService.createSchemaSet(SUBSCRIPTION_DATASPACE_NAME, SUBSCRIPTION_SCHEMASET_NAME, yangResourceToContentMap) >>
102 { throw AlreadyDefinedException.forSchemaSet(SUBSCRIPTION_SCHEMASET_NAME, "sampleContextName", null) }
103 when: 'the method to onboard model is called'
104 objectUnderTest.onboardSubscriptionModel()
105 then: 'the admin service to create an anchor set is then called once'
106 1 * mockCpsAdminService.createAnchor(SUBSCRIPTION_DATASPACE_NAME, SUBSCRIPTION_SCHEMASET_NAME, SUBSCRIPTION_ANCHOR_NAME)
109 def 'Create schema set fails due to any other exception'() {
110 given: 'the method to create yang resource to content map returns the correct map'
111 def yangResourceToContentMap = objectUnderTest.createYangResourceToContentMap()
112 and: 'creating a schema set throws an exception'
113 mockCpsModuleService.createSchemaSet(SUBSCRIPTION_DATASPACE_NAME, SUBSCRIPTION_SCHEMASET_NAME, yangResourceToContentMap) >>
114 { throw new NcmpStartUpException("Creating schema set failed", ""); }
115 when: 'the method to onboard model is called'
116 objectUnderTest.onboardSubscriptionModel()
117 then: 'the log message contains the correct exception message'
118 def debugMessage = appender.list[0].toString()
119 assert debugMessage.contains("Creating schema set failed")
120 and: 'exception is thrown'
121 thrown(NcmpStartUpException)
124 def 'Create anchor fails due to AlreadyDefined exception'() {
125 given: 'creating anchor throws an exception as it already exists'
126 mockCpsAdminService.createAnchor(SUBSCRIPTION_DATASPACE_NAME, SUBSCRIPTION_SCHEMASET_NAME, SUBSCRIPTION_ANCHOR_NAME) >>
127 { throw AlreadyDefinedException.forSchemaSet(SUBSCRIPTION_SCHEMASET_NAME, "sampleContextName", null) }
128 when: 'the method to onboard model is called'
129 objectUnderTest.onboardSubscriptionModel()
130 then: 'no exception thrown'
134 def 'Create anchor fails due to any other exception'() {
135 given: 'creating an anchor failed'
136 mockCpsAdminService.createAnchor(SUBSCRIPTION_DATASPACE_NAME, SUBSCRIPTION_SCHEMASET_NAME, SUBSCRIPTION_ANCHOR_NAME) >>
137 { throw new SchemaSetNotFoundException(SUBSCRIPTION_DATASPACE_NAME, SUBSCRIPTION_SCHEMASET_NAME) }
138 when: 'the method to onboard model is called'
139 objectUnderTest.onboardSubscriptionModel()
140 then: 'the log message contains the correct exception message'
141 def debugMessage = appender.list[0].toString()
142 assert debugMessage.contains("Schema Set not found")
143 and: 'exception is thrown'
144 thrown(NcmpStartUpException)
147 def 'Create top level node fails due to an AlreadyDefined exception'() {
148 given: 'the saving of the node data will throw an Already Defined exception'
149 mockCpsDataService.saveData(*_) >>
150 { AlreadyDefinedException.forDataNode('/xpath', "sampleContextName", null) }
151 when: 'the method to onboard model is called'
152 objectUnderTest.onboardSubscriptionModel()
153 then: 'no exception thrown'
157 def 'Create top level node fails due to any other exception'() {
158 given: 'the saving of the node data will throw an exception'
159 mockCpsDataService.saveData(*_) >>
160 { throw new DataValidationException("Invalid JSON", "JSON Data is invalid") }
161 when: 'the method to onboard model is called'
162 objectUnderTest.onboardSubscriptionModel()
163 then: 'the log message contains the correct exception message'
164 def debugMessage = appender.list[0].toString()
165 assert debugMessage.contains("Creating data node for subscription model failed: Invalid JSON")
166 and: 'exception is thrown'
167 thrown(NcmpStartUpException)
170 def 'Get file content as string'() {
171 when: 'the method to get yang content is called'
172 def response = objectUnderTest.getFileContentAsString()
173 then: 'the response is as expected'
174 assert response == 'module subscription { *sample content* }'