RTD change to document migration to Spring Boot 3.0
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / init / SubscriptionModelLoaderSpec.groovy
1 /*
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
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.ncmp.init
22
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.onap.cps.api.CpsAdminService
27 import org.onap.cps.api.CpsDataService
28 import org.onap.cps.api.CpsModuleService
29 import org.onap.cps.spi.model.Dataspace
30 import org.slf4j.LoggerFactory
31 import org.springframework.boot.context.event.ApplicationReadyEvent
32 import org.springframework.context.annotation.AnnotationConfigApplicationContext
33 import spock.lang.Specification
34
35 class SubscriptionModelLoaderSpec extends Specification {
36
37     def mockCpsAdminService = Mock(CpsAdminService)
38     def mockCpsModuleService = Mock(CpsModuleService)
39     def mockCpsDataService = Mock(CpsDataService)
40     def objectUnderTest = new SubscriptionModelLoader(mockCpsAdminService, mockCpsModuleService, mockCpsDataService)
41
42     def applicationContext = new AnnotationConfigApplicationContext()
43
44     def expectedYangResourceToContentMap
45     def logger = (Logger) LoggerFactory.getLogger(objectUnderTest.class)
46     def loggingListAppender
47
48     void setup() {
49         expectedYangResourceToContentMap = objectUnderTest.createYangResourceToContentMap('subscription.yang')
50         logger.setLevel(Level.DEBUG)
51         loggingListAppender = new ListAppender()
52         logger.addAppender(loggingListAppender)
53         loggingListAppender.start()
54         applicationContext.refresh()
55     }
56
57     void cleanup() {
58         ((Logger) LoggerFactory.getLogger(SubscriptionModelLoader.class)).detachAndStopAllAppenders()
59         applicationContext.close()
60     }
61
62     def 'Onboard subscription model via application ready event.'() {
63         given:'model loader is enabled'
64             objectUnderTest.subscriptionModelLoaderEnabled = true
65         and: 'dataspace is ready for use'
66             mockCpsAdminService.getDataspace('NCMP-Admin') >> new Dataspace('')
67         when: 'the application is ready'
68             objectUnderTest.onApplicationEvent(Mock(ApplicationReadyEvent))
69         then: 'the module service to create schema set is called once'
70             1 * mockCpsModuleService.createSchemaSet('NCMP-Admin', 'subscriptions', expectedYangResourceToContentMap)
71         and: 'the admin service to create an anchor set is called once'
72             1 * mockCpsAdminService.createAnchor('NCMP-Admin', 'subscriptions', 'AVC-Subscriptions')
73         and: 'the data service to create a top level datanode is called once'
74             1 * mockCpsDataService.saveData('NCMP-Admin', 'AVC-Subscriptions', '{"subscription-registry":{}}', _)
75     }
76
77     def 'Subscription model loader disabled.' () {
78         given: 'model loader is disabled'
79             objectUnderTest.subscriptionModelLoaderEnabled = false
80         when: 'application is ready'
81             objectUnderTest.onApplicationEvent(Mock(ApplicationReadyEvent))
82         then: 'no interaction with admin service'
83             0 * mockCpsAdminService.getDataspace(_)
84         then: 'a message is logged that the function is disabled'
85             def logs = loggingListAppender.list.toString()
86             assert logs.contains('Subscription Model Loader is disabled')
87     }
88
89 }