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.CpsModuleService
30 import org.onap.cps.ncmp.api.impl.exception.NcmpStartUpException
31 import org.onap.cps.spi.exceptions.AlreadyDefinedException
32 import org.onap.cps.spi.exceptions.SchemaSetNotFoundException
33 import org.springframework.boot.SpringApplication
34 import org.slf4j.LoggerFactory
35 import org.springframework.boot.context.event.ApplicationReadyEvent
36 import spock.lang.Specification
38 class SubscriptionModelLoaderSpec extends Specification {
40 def mockCpsAdminService = Mock(CpsAdminService)
41 def mockCpsModuleService = Mock(CpsModuleService)
42 def objectUnderTest = new SubscriptionModelLoader(mockCpsAdminService, mockCpsModuleService)
44 def SUBSCRIPTION_DATASPACE_NAME = objectUnderTest.SUBSCRIPTION_DATASPACE_NAME;
45 def SUBSCRIPTION_ANCHOR_NAME = objectUnderTest.SUBSCRIPTION_ANCHOR_NAME;
46 def SUBSCRIPTION_SCHEMASET_NAME = objectUnderTest.SUBSCRIPTION_SCHEMASET_NAME;
48 def sampleYangContentMap = ['subscription.yang':'module subscription { *sample content* }']
50 def applicationReadyEvent = new ApplicationReadyEvent(new SpringApplication(), null, null, null)
57 logger = (Logger) LoggerFactory.getLogger(objectUnderTest.getClass())
58 appender = new ListAppender()
59 logger.setLevel(Level.DEBUG)
61 logger.addAppender(appender)
66 ((Logger) LoggerFactory.getLogger(SubscriptionModelLoader.class)).detachAndStopAllAppenders();
69 def 'Onboard subscription model successfully via application ready event'() {
70 when:'model loader is enabled'
71 objectUnderTest.subscriptionModelLoaderEnabled = true
72 and: 'the application is ready'
73 objectUnderTest.onApplicationEvent(applicationReadyEvent)
74 then: 'the module service to create schema set is called once'
75 1 * mockCpsModuleService.createSchemaSet(SUBSCRIPTION_DATASPACE_NAME, SUBSCRIPTION_SCHEMASET_NAME,sampleYangContentMap)
76 and: 'the admin service to create an anchor set is called once'
77 1 * mockCpsAdminService.createAnchor(SUBSCRIPTION_DATASPACE_NAME, SUBSCRIPTION_SCHEMASET_NAME, SUBSCRIPTION_ANCHOR_NAME)
80 def 'Create schema set from model file'() {
81 given: 'the method to create yang resource to content map returns the correct map'
82 def yangResourceToContentMap = objectUnderTest.createYangResourceToContentMap()
83 when: 'the method to create schema set is called with the following parameters'
84 objectUnderTest.createSchemaSet("myDataspace", "mySchemaSet", yangResourceToContentMap)
85 then: 'yang resource to content map is as expected'
86 assert sampleYangContentMap == yangResourceToContentMap
87 and: 'the module service to create schema set is called once with the correct map'
88 1 * mockCpsModuleService.createSchemaSet(_, _, yangResourceToContentMap)
91 def 'Create schema set fails due to AlreadyDefined exception'() {
92 given: 'the method to create yang resource to content map returns the correct map'
93 def yangResourceToContentMap = objectUnderTest.createYangResourceToContentMap()
94 and: 'creating a schema set throws an exception as it already exists'
95 mockCpsModuleService.createSchemaSet(SUBSCRIPTION_DATASPACE_NAME, SUBSCRIPTION_SCHEMASET_NAME, yangResourceToContentMap) >>
96 { throw AlreadyDefinedException.forSchemaSet(SUBSCRIPTION_SCHEMASET_NAME, "sampleContextName", null) }
97 when: 'the method to onboard model is called'
98 objectUnderTest.onboardSubscriptionModel()
99 then: 'the admin service to create an anchor set is then called once'
100 1 * mockCpsAdminService.createAnchor(SUBSCRIPTION_DATASPACE_NAME, SUBSCRIPTION_SCHEMASET_NAME, SUBSCRIPTION_ANCHOR_NAME)
103 def 'Create schema set fails due to any other exception'() {
104 given: 'the method to create yang resource to content map returns the correct map'
105 def yangResourceToContentMap = objectUnderTest.createYangResourceToContentMap()
106 and: 'creating a schema set throws an exception'
107 mockCpsModuleService.createSchemaSet(SUBSCRIPTION_DATASPACE_NAME, SUBSCRIPTION_SCHEMASET_NAME, yangResourceToContentMap) >>
108 { throw new NcmpStartUpException("Creating schema set failed", ""); }
109 when: 'the method to onboard model is called'
110 objectUnderTest.onboardSubscriptionModel()
111 then: 'the log message contains the correct exception message'
112 def debugMessage = appender.list[0].toString()
113 assert debugMessage.contains("Creating schema set failed")
114 and: 'exception is thrown'
115 thrown(NcmpStartUpException)
118 def 'Create anchor fails due to AlreadyDefined exception'() {
119 given: 'creating anchor throws an exception as it already exists'
120 mockCpsAdminService.createAnchor(SUBSCRIPTION_DATASPACE_NAME, SUBSCRIPTION_SCHEMASET_NAME, SUBSCRIPTION_ANCHOR_NAME) >>
121 { throw AlreadyDefinedException.forSchemaSet(SUBSCRIPTION_SCHEMASET_NAME, "sampleContextName", null) }
122 when: 'the method to onboard model is called'
123 objectUnderTest.onboardSubscriptionModel()
124 then: 'no exception thrown'
128 def 'Create anchor fails due to any other exception'() {
129 given: 'creating an anchor failed'
130 mockCpsAdminService.createAnchor(SUBSCRIPTION_DATASPACE_NAME, SUBSCRIPTION_SCHEMASET_NAME, SUBSCRIPTION_ANCHOR_NAME) >>
131 { throw new SchemaSetNotFoundException(SUBSCRIPTION_DATASPACE_NAME, SUBSCRIPTION_SCHEMASET_NAME) }
132 when: 'the method to onboard model is called'
133 objectUnderTest.onboardSubscriptionModel()
134 then: 'the log message contains the correct exception message'
135 def debugMessage = appender.list[0].toString()
136 assert debugMessage.contains("Schema Set not found")
137 and: 'exception is thrown'
138 thrown(NcmpStartUpException)
141 def 'Get file content as string'() {
142 when: 'the method to get yang content is called'
143 def response = objectUnderTest.getFileContentAsString()
144 then: 'the response is as expected'
145 assert response == 'module subscription { *sample content* }'