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