Merge "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 static org.onap.cps.ncmp.api.impl.ncmppersistence.NcmpPersistence.NCMP_DATASPACE_NAME
24
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
36
37 class SubscriptionModelLoaderSpec extends Specification {
38
39     def mockCpsAdminService = Mock(CpsAdminService)
40     def mockCpsModuleService = Mock(CpsModuleService)
41     def mockCpsDataService = Mock(CpsDataService)
42     def objectUnderTest = new SubscriptionModelLoader(mockCpsAdminService, mockCpsModuleService, mockCpsDataService)
43
44     def applicationContext = new AnnotationConfigApplicationContext()
45
46     def expectedYangResourceToContentMap
47     def logger = (Logger) LoggerFactory.getLogger(objectUnderTest.class)
48     def loggingListAppender
49
50     void setup() {
51         expectedYangResourceToContentMap = objectUnderTest.createYangResourceToContentMap('subscription.yang')
52         logger.setLevel(Level.DEBUG)
53         loggingListAppender = new ListAppender()
54         logger.addAppender(loggingListAppender)
55         loggingListAppender.start()
56         applicationContext.refresh()
57     }
58
59     void cleanup() {
60         ((Logger) LoggerFactory.getLogger(SubscriptionModelLoader.class)).detachAndStopAllAppenders()
61         applicationContext.close()
62     }
63
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, 'subscriptions', expectedYangResourceToContentMap)
73         and: 'the admin service to create an anchor set is called once'
74             1 * mockCpsAdminService.createAnchor(NCMP_DATASPACE_NAME, 'subscriptions', 'AVC-Subscriptions')
75         and: 'the data service to create a top level datanode is called once'
76             1 * mockCpsDataService.saveData(NCMP_DATASPACE_NAME, 'AVC-Subscriptions', '{"subscription-registry":{}}', _)
77     }
78
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')
89     }
90
91 }