a271ca43126645ddc5c71fb51721569d7945c0c5
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / init / AbstractModelLoaderSpec.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.ncmp.api.impl.exception.NcmpStartUpException
30 import org.onap.cps.spi.exceptions.AlreadyDefinedException
31 import org.springframework.boot.SpringApplication
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 AbstractModelLoaderSpec extends Specification {
38
39     def mockCpsAdminService = Mock(CpsAdminService)
40     def mockCpsModuleService = Mock(CpsModuleService)
41     def mockCpsDataService = Mock(CpsDataService)
42     def objectUnderTest = Spy(new TestModelLoader(mockCpsAdminService, mockCpsModuleService, mockCpsDataService))
43
44     def applicationContext = new AnnotationConfigApplicationContext()
45
46     def yangResourceToContentMap
47     def logger = (Logger) LoggerFactory.getLogger(AbstractModelLoader)
48     def loggingListAppender
49
50     void setup() {
51         yangResourceToContentMap = 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 'Application ready event'() {
65         when: 'Application (ready) event is triggered'
66             objectUnderTest.onApplicationEvent(Mock(ApplicationReadyEvent))
67         then: 'the onboard/upgrade method is executed'
68             1 * objectUnderTest.onboardOrUpgradeModel()
69     }
70
71     def 'Application ready event with start up exception'() {
72         given: 'a start up exception is thrown doing model onboarding'
73             objectUnderTest.onboardOrUpgradeModel() >> { throw new NcmpStartUpException('test message','details are not logged') }
74         when: 'Application (ready) event is triggered'
75             objectUnderTest.onApplicationEvent(new ApplicationReadyEvent(new SpringApplication(), null, applicationContext, null))
76         then: 'the exception message is logged'
77             def logs = loggingListAppender.list.toString()
78             assert logs.contains('test message')
79     }
80
81     def 'Wait for non-existing dataspace'() {
82         when: 'wait for the dataspace'
83             objectUnderTest.waitUntilDataspaceIsAvailable('some dataspace')
84         then: 'a startup exception is thrown'
85             def thrown = thrown(NcmpStartUpException)
86             assert thrown.message.contains('Retrieval of NCMP dataspace failed')
87     }
88
89     def 'Create schema set.'() {
90         when: 'creating a schema set'
91             objectUnderTest.createSchemaSet('some dataspace','new name','subscription.yang')
92         then: 'the operation is delegated to the admin service'
93             1 * mockCpsModuleService.createSchemaSet('some dataspace','new name',_)
94     }
95
96     def 'Create schema set with already defined exception.'() {
97         given: 'the module service throws an already defined exception'
98             mockCpsModuleService.createSchemaSet(*_) >>  { throw AlreadyDefinedException.forSchemaSet('name','context',null) }
99         when: 'attempt to create a schema set'
100             objectUnderTest.createSchemaSet('some dataspace','new name','subscription.yang')
101         then: 'the exception is ignored i.e. no exception thrown up'
102             noExceptionThrown()
103         and: 'the exception message is logged'
104             def logs = loggingListAppender.list.toString()
105             assert logs.contains('Creating new schema set failed as schema set already exists')
106     }
107
108     def 'Create schema set with non existing yang file.'() {
109         when: 'attempt to create a schema set from a non existing file'
110             objectUnderTest.createSchemaSet('some dataspace','some name','no such yang file')
111         then: 'a startup exception with correct message and details is thrown'
112             def thrown = thrown(NcmpStartUpException)
113             assert thrown.message.contains('Creating schema set failed')
114             assert thrown.details.contains('unable to read file')
115     }
116
117     def 'Create anchor.'() {
118         when: 'creating an anchor'
119             objectUnderTest.createAnchor('some dataspace','some schema set','new name')
120         then: 'thr operation is delegated to the admin service'
121             1 * mockCpsAdminService.createAnchor('some dataspace','some schema set', 'new name')
122     }
123
124     def 'Create anchor with already defined exception.'() {
125         given: 'the admin service throws an already defined exception'
126             mockCpsAdminService.createAnchor(*_)>>  { throw AlreadyDefinedException.forAnchor('name','context',null) }
127         when: 'attempt to create anchor'
128             objectUnderTest.createAnchor('some dataspace','some schema set','new name')
129         then: 'the exception is ignored i.e. no exception thrown up'
130             noExceptionThrown()
131         and: 'the exception message is logged'
132             def logs = loggingListAppender.list.toString()
133             assert logs.contains('Creating new anchor failed as anchor already exists')
134     }
135
136     def 'Create anchor with any other exception.'() {
137         given: 'the admin service throws a exception'
138             mockCpsAdminService.createAnchor(*_)>>  { throw new RuntimeException('test message') }
139         when: 'attempt to create anchor'
140             objectUnderTest.createAnchor('some dataspace','some schema set','new name')
141         then: 'a startup exception with correct message and details is thrown'
142             def thrown = thrown(NcmpStartUpException)
143             assert thrown.message.contains('Creating anchor failed')
144             assert thrown.details.contains('test message')
145     }
146
147     def 'Create top level node.'() {
148         when: 'top level node is created'
149             objectUnderTest.createTopLevelDataNode('dataspace','anchor','new node')
150         then: 'the correct json is saved using the data service'
151             1 * mockCpsDataService.saveData('dataspace','anchor', '{"new node":{}}',_)
152     }
153
154     def 'Create top level node with already defined exception.'() {
155         given: 'the data service throws an Already Defined exception'
156             mockCpsDataService.saveData(*_) >> { throw AlreadyDefinedException.forDataNodes([], 'some context') }
157         when: 'attempt to create top level node'
158             objectUnderTest.createTopLevelDataNode('dataspace','anchor','new node')
159         then: 'the exception is ignored i.e. no exception thrown up'
160             noExceptionThrown()
161         and: 'the exception message is logged'
162             def logs = loggingListAppender.list.toString()
163             assert logs.contains('failed as data node already exists')
164     }
165
166     def 'Create top level node with any other exception.'() {
167         given: 'the data service throws an exception'
168             mockCpsDataService.saveData(*_) >> { throw new RuntimeException('test message') }
169         when: 'attempt to create top level node'
170             objectUnderTest.createTopLevelDataNode('dataspace','anchor','new node')
171         then: 'a startup exception with correct message and details is thrown'
172             def thrown = thrown(NcmpStartUpException)
173             assert thrown.message.contains('Creating data node failed')
174             assert thrown.details.contains('test message')
175     }
176
177     class TestModelLoader extends AbstractModelLoader {
178
179         TestModelLoader(final CpsAdminService cpsAdminService,
180                         final CpsModuleService cpsModuleService,
181                         final CpsDataService cpsDataService) {
182             super(cpsAdminService, cpsModuleService, cpsDataService)
183             super.maximumAttemptCount = 2
184             super.retryTimeMs = 1
185         }
186
187         @Override
188         void onboardOrUpgradeModel() { }
189     }
190
191 }