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 static org.onap.cps.ncmp.api.impl.ncmppersistence.NcmpPersistence.NCMP_DATASPACE_NAME
25 import ch.qos.logback.classic.Level
26 import ch.qos.logback.classic.Logger
27 import ch.qos.logback.core.read.ListAppender
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.spi.model.Dataspace
32 import org.slf4j.LoggerFactory
33 import org.springframework.boot.context.event.ApplicationReadyEvent
34 import org.springframework.context.annotation.AnnotationConfigApplicationContext
35 import spock.lang.Specification
37 class CmDataSubscriptionModelLoaderSpec extends Specification {
39 def mockCpsAdminService = Mock(CpsAdminService)
40 def mockCpsModuleService = Mock(CpsModuleService)
41 def mockCpsDataService = Mock(CpsDataService)
42 def objectUnderTest = new CmDataSubscriptionModelLoader(mockCpsAdminService, mockCpsModuleService, mockCpsDataService)
44 def applicationContext = new AnnotationConfigApplicationContext()
46 def expectedYangResourcesToContentMap
47 def logger = (Logger) LoggerFactory.getLogger(objectUnderTest.class)
48 def loggingListAppender
51 expectedYangResourcesToContentMap = objectUnderTest.createYangResourcesToContentMap('cm-data-subscriptions@2023-11-13.yang')
52 logger.setLevel(Level.DEBUG)
53 loggingListAppender = new ListAppender()
54 logger.addAppender(loggingListAppender)
55 loggingListAppender.start()
56 applicationContext.refresh()
60 ((Logger) LoggerFactory.getLogger(CmDataSubscriptionModelLoader.class)).detachAndStopAllAppenders()
61 applicationContext.close()
64 def 'Onboard subscription model via application ready event.'() {
65 given:'model loader is enabled'
66 objectUnderTest.subscriptionModelLoaderEnabled = true
67 and: 'dataspace is ready for use'
68 mockCpsAdminService.getDataspace(NCMP_DATASPACE_NAME) >> new Dataspace('')
69 when: 'the application is ready'
70 objectUnderTest.onApplicationEvent(Mock(ApplicationReadyEvent))
71 then: 'the module service to create schema set is called once'
72 1 * mockCpsModuleService.createSchemaSet(NCMP_DATASPACE_NAME, 'cm-data-subscriptions', expectedYangResourcesToContentMap)
73 and: 'the admin service to create an anchor set is called once'
74 1 * mockCpsAdminService.createAnchor(NCMP_DATASPACE_NAME, 'cm-data-subscriptions', 'cm-data-subscriptions')
75 and: 'the data service to create a top level datanode is called once'
76 1 * mockCpsDataService.saveData(NCMP_DATASPACE_NAME, 'cm-data-subscriptions', '{"datastores":{}}', _)
79 def 'Subscription model loader disabled.' () {
80 given: 'model loader is disabled'
81 objectUnderTest.subscriptionModelLoaderEnabled = false
82 when: 'application is ready'
83 objectUnderTest.onApplicationEvent(Mock(ApplicationReadyEvent))
84 then: 'no interaction with admin service'
85 0 * mockCpsAdminService.getDataspace(_)
86 then: 'a message is logged that the function is disabled'
87 def logs = loggingListAppender.list.toString()
88 assert logs.contains('Subscription Model Loader is disabled')