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: 'the application is ready'
71 objectUnderTest.onApplicationEvent(applicationReadyEvent)
72 then: 'the module service to create schema set is called once'
73 1 * mockCpsModuleService.createSchemaSet(SUBSCRIPTION_DATASPACE_NAME, SUBSCRIPTION_SCHEMASET_NAME,sampleYangContentMap)
74 and: 'the admin service to create an anchor set is called once'
75 1 * mockCpsAdminService.createAnchor(SUBSCRIPTION_DATASPACE_NAME, SUBSCRIPTION_SCHEMASET_NAME, SUBSCRIPTION_ANCHOR_NAME)
78 def 'Create schema set from model file'() {
79 given: 'the method to create yang resource to content map returns the correct map'
80 def yangResourceToContentMap = objectUnderTest.createYangResourceToContentMap()
81 when: 'the method to create schema set is called with the following parameters'
82 objectUnderTest.createSchemaSet("myDataspace", "mySchemaSet", yangResourceToContentMap)
83 then: 'yang resource to content map is as expected'
84 assert sampleYangContentMap == yangResourceToContentMap
85 and: 'the module service to create schema set is called once with the correct map'
86 1 * mockCpsModuleService.createSchemaSet(_, _, yangResourceToContentMap)
89 def 'Create schema set fails due to AlreadyDefined exception'() {
90 given: 'the method to create yang resource to content map returns the correct map'
91 def yangResourceToContentMap = objectUnderTest.createYangResourceToContentMap()
92 and: 'creating a schema set throws an exception as it already exists'
93 mockCpsModuleService.createSchemaSet(SUBSCRIPTION_DATASPACE_NAME, SUBSCRIPTION_SCHEMASET_NAME, yangResourceToContentMap) >>
94 { throw AlreadyDefinedException.forSchemaSet(SUBSCRIPTION_SCHEMASET_NAME, "sampleContextName", null) }
95 when: 'the method to onboard model is called'
96 objectUnderTest.onboardSubscriptionModel()
97 then: 'the admin service to create an anchor set is then called once'
98 1 * mockCpsAdminService.createAnchor(SUBSCRIPTION_DATASPACE_NAME, SUBSCRIPTION_SCHEMASET_NAME, SUBSCRIPTION_ANCHOR_NAME)
101 def 'Create schema set fails due to any other exception'() {
102 given: 'the method to create yang resource to content map returns the correct map'
103 def yangResourceToContentMap = objectUnderTest.createYangResourceToContentMap()
104 and: 'creating a schema set throws an exception'
105 mockCpsModuleService.createSchemaSet(SUBSCRIPTION_DATASPACE_NAME, SUBSCRIPTION_SCHEMASET_NAME, yangResourceToContentMap) >>
106 { throw new NcmpStartUpException("Creating schema set failed", ""); }
107 when: 'the method to onboard model is called'
108 objectUnderTest.onboardSubscriptionModel()
109 then: 'the log message contains the correct exception message'
110 def debugMessage = appender.list[0].toString()
111 assert debugMessage.contains("Creating schema set failed")
112 and: 'exception is thrown'
113 thrown(NcmpStartUpException)
116 def 'Create anchor fails due to AlreadyDefined exception'() {
117 given: 'creating anchor throws an exception as it already exists'
118 mockCpsAdminService.createAnchor(SUBSCRIPTION_DATASPACE_NAME, SUBSCRIPTION_SCHEMASET_NAME, SUBSCRIPTION_ANCHOR_NAME) >>
119 { throw AlreadyDefinedException.forSchemaSet(SUBSCRIPTION_SCHEMASET_NAME, "sampleContextName", null) }
120 when: 'the method to onboard model is called'
121 objectUnderTest.onboardSubscriptionModel()
122 then: 'no exception thrown'
126 def 'Create anchor fails due to any other exception'() {
127 given: 'creating an anchor failed'
128 mockCpsAdminService.createAnchor(SUBSCRIPTION_DATASPACE_NAME, SUBSCRIPTION_SCHEMASET_NAME, SUBSCRIPTION_ANCHOR_NAME) >>
129 { throw new SchemaSetNotFoundException(SUBSCRIPTION_DATASPACE_NAME, SUBSCRIPTION_SCHEMASET_NAME) }
130 when: 'the method to onboard model is called'
131 objectUnderTest.onboardSubscriptionModel()
132 then: 'the log message contains the correct exception message'
133 def debugMessage = appender.list[0].toString()
134 assert debugMessage.contains("Schema Set not found")
135 and: 'exception is thrown'
136 thrown(NcmpStartUpException)
139 def 'Get file content as string'() {
140 when: 'the method to get yang content is called'
141 def response = objectUnderTest.getFileContentAsString()
142 then: 'the response is as expected'
143 assert response == 'module subscription { *sample content* }'