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