Merge "Use PollingConditions to improve intermittent test failure"
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / init / CmDataSubscriptionModelLoaderSpec.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2023-2024 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 import org.onap.cps.ncmp.api.impl.exception.NcmpStartUpException
25 import org.onap.cps.spi.exceptions.AlreadyDefinedException
26
27 import static org.onap.cps.ncmp.api.impl.ncmppersistence.NcmpPersistence.NCMP_DATASPACE_NAME
28
29 import ch.qos.logback.classic.Level
30 import ch.qos.logback.classic.Logger
31 import ch.qos.logback.core.read.ListAppender
32 import org.onap.cps.api.CpsDataspaceService
33 import org.onap.cps.api.CpsDataService
34 import org.onap.cps.api.CpsModuleService
35 import org.onap.cps.spi.model.Dataspace
36 import org.slf4j.LoggerFactory
37 import org.springframework.boot.context.event.ApplicationReadyEvent
38 import org.springframework.context.annotation.AnnotationConfigApplicationContext
39 import spock.lang.Specification
40
41 class CmDataSubscriptionModelLoaderSpec extends Specification {
42
43     def mockCpsDataspaceService = Mock(CpsDataspaceService)
44     def mockCpsModuleService = Mock(CpsModuleService)
45     def mockCpsDataService = Mock(CpsDataService)
46     def mockCpsAnchorService = Mock(CpsAnchorService)
47     def objectUnderTest = new CmDataSubscriptionModelLoader(mockCpsDataspaceService, mockCpsModuleService, mockCpsAnchorService, mockCpsDataService)
48
49     def applicationContext = new AnnotationConfigApplicationContext()
50
51     def expectedYangResourcesToContentMap
52     def logger = (Logger) LoggerFactory.getLogger(objectUnderTest.class)
53     def loggingListAppender
54
55     void setup() {
56         expectedYangResourcesToContentMap = objectUnderTest.createYangResourcesToContentMap('cm-data-subscriptions@2023-11-13.yang')
57         logger.setLevel(Level.DEBUG)
58         loggingListAppender = new ListAppender()
59         logger.addAppender(loggingListAppender)
60         loggingListAppender.start()
61         applicationContext.refresh()
62     }
63
64     void cleanup() {
65         ((Logger) LoggerFactory.getLogger(CmDataSubscriptionModelLoader.class)).detachAndStopAllAppenders()
66         applicationContext.close()
67     }
68
69     def 'Onboard subscription model via application ready event.'() {
70         given:'model loader is enabled'
71             objectUnderTest.subscriptionModelLoaderEnabled = true
72         and: 'dataspace is ready for use'
73             mockCpsDataspaceService.getDataspace(NCMP_DATASPACE_NAME) >> new Dataspace('')
74         when: 'the application is ready'
75             objectUnderTest.onApplicationEvent(Mock(ApplicationReadyEvent))
76         then: 'the module service to create schema set is called once'
77             1 * mockCpsModuleService.createSchemaSet(NCMP_DATASPACE_NAME, 'cm-data-subscriptions', expectedYangResourcesToContentMap)
78         and: 'the admin service to create an anchor set is called once'
79             1 * mockCpsAnchorService.createAnchor(NCMP_DATASPACE_NAME, 'cm-data-subscriptions', 'cm-data-subscriptions')
80         and: 'the data service to create a top level datanode is called once'
81             1 * mockCpsDataService.saveData(NCMP_DATASPACE_NAME, 'cm-data-subscriptions', '{"datastores":{}}', _)
82         and: 'the data service is called once to create datastore for Passthrough-operational'
83             1 * mockCpsDataService.saveData(NCMP_DATASPACE_NAME, 'cm-data-subscriptions', '/datastores',
84                     '{"datastore":[{"name":"ncmp-datastores:passthrough-operational","cm-handles":{}}]}', _, _)
85         and: 'the data service is called once to create datastore for Passthrough-running'
86             1 * mockCpsDataService.saveData(NCMP_DATASPACE_NAME, 'cm-data-subscriptions', '/datastores',
87                     '{"datastore":[{"name":"ncmp-datastores:passthrough-running","cm-handles":{}}]}', _, _)
88     }
89
90     def 'Create node for datastore with already defined exception.'() {
91         given:'the data service throws an Already Defined exception'
92             mockCpsDataService.saveData(*_) >> { throw AlreadyDefinedException.forDataNodes([], 'some context') }
93         when: 'attempt to create datastore'
94             objectUnderTest.createDatastore('some datastore')
95         then: 'the exception is ignored i.e. no exception thrown up'
96             noExceptionThrown()
97         and: 'the exception message is logged'
98             def logs = loggingListAppender.list.toString()
99             logs.contains("Creating new child data node 'some datastore' for data node 'datastores' failed as data node already exists")
100     }
101
102     def 'Create node for datastore with any other exception.'() {
103         given: 'the data service throws an exception'
104             mockCpsDataService.saveData(*_) >> { throw new RuntimeException('test message') }
105         when: 'attempt to create datastore'
106             objectUnderTest.createDatastore('some datastore')
107         then: 'a startup exception with correct message and details is thrown'
108             def thrown = thrown(NcmpStartUpException)
109             assert thrown.message.contains('Creating data node failed')
110             assert thrown.details.contains('test message')
111     }
112
113     def 'Subscription model loader disabled.' () {
114         given: 'model loader is disabled'
115             objectUnderTest.subscriptionModelLoaderEnabled = false
116         when: 'application is ready'
117             objectUnderTest.onApplicationEvent(Mock(ApplicationReadyEvent))
118         then: 'no interaction with admin service'
119             0 * mockCpsDataspaceService.getDataspace(_)
120         then: 'a message is logged that the function is disabled'
121             def logs = loggingListAppender.list.toString()
122             assert logs.contains('Subscription Model Loader is disabled')
123     }
124
125 }