Migrate query tests to integration-test module #6
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / impl / event / avc / SubscriptionEventConsumerSpec.groovy
1 /*
2  * ============LICENSE_START=======================================================
3  * Copyright (c) 2022-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.api.impl.event.avc
22
23 import com.fasterxml.jackson.databind.ObjectMapper
24 import org.onap.cps.ncmp.api.impl.subscriptions.SubscriptionPersistence
25 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelSubscriptionEvent
26 import org.onap.cps.ncmp.api.kafka.MessagingBaseSpec
27 import org.onap.cps.ncmp.event.model.SubscriptionEvent
28 import org.onap.cps.ncmp.utils.TestUtils
29 import org.onap.cps.spi.exceptions.OperationNotYetSupportedException
30 import org.onap.cps.utils.JsonObjectMapper
31 import org.springframework.beans.factory.annotation.Autowired
32 import org.springframework.boot.test.context.SpringBootTest
33
34 @SpringBootTest(classes = [ObjectMapper, JsonObjectMapper])
35 class SubscriptionEventConsumerSpec extends MessagingBaseSpec {
36
37     def mockSubscriptionEventForwarder = Mock(SubscriptionEventForwarder)
38     def mockSubscriptionEventMapper = Mock(SubscriptionEventMapper)
39     def mockSubscriptionPersistence = Mock(SubscriptionPersistence)
40     def objectUnderTest = new SubscriptionEventConsumer(mockSubscriptionEventForwarder, mockSubscriptionEventMapper, mockSubscriptionPersistence)
41
42     def yangModelSubscriptionEvent = new YangModelSubscriptionEvent()
43
44     @Autowired
45     JsonObjectMapper jsonObjectMapper
46
47     def 'Consume, persist and forward valid CM create message'() {
48         given: 'an event with data category CM'
49             def jsonData = TestUtils.getResourceFileContent('avcSubscriptionCreationEvent.json')
50             def testEventSent = jsonObjectMapper.convertJsonString(jsonData, SubscriptionEvent.class)
51         and: 'notifications are enabled'
52             objectUnderTest.notificationFeatureEnabled = true
53         and: 'subscription model loader is enabled'
54             objectUnderTest.subscriptionModelLoaderEnabled = true
55         when: 'the valid event is consumed'
56             objectUnderTest.consumeSubscriptionEvent(testEventSent)
57         then: 'the event is mapped to a yangModelSubscription'
58             1 * mockSubscriptionEventMapper.toYangModelSubscriptionEvent(testEventSent) >> yangModelSubscriptionEvent
59         and: 'the event is persisted'
60             1 * mockSubscriptionPersistence.saveSubscriptionEvent(yangModelSubscriptionEvent)
61         and: 'the event is forwarded'
62             1 * mockSubscriptionEventForwarder.forwardCreateSubscriptionEvent(testEventSent)
63     }
64
65     def 'Consume valid CM create message where notifications and model loader are disabled'() {
66         given: 'an event with data category CM'
67             def jsonData = TestUtils.getResourceFileContent('avcSubscriptionCreationEvent.json')
68             def testEventSent = jsonObjectMapper.convertJsonString(jsonData, SubscriptionEvent.class)
69         and: 'notifications are disabled'
70             objectUnderTest.notificationFeatureEnabled = false
71         and: 'subscription model loader is disabled'
72             objectUnderTest.subscriptionModelLoaderEnabled = false
73         when: 'the valid event is consumed'
74             objectUnderTest.consumeSubscriptionEvent(testEventSent)
75         then: 'the event is not mapped to a yangModelSubscription'
76             0 * mockSubscriptionEventMapper.toYangModelSubscriptionEvent(*_) >> yangModelSubscriptionEvent
77         and: 'the event is not persisted'
78             0 * mockSubscriptionPersistence.saveSubscriptionEvent(*_)
79         and: 'the event is not forwarded'
80             0 * mockSubscriptionEventForwarder.forwardCreateSubscriptionEvent(*_)
81     }
82
83     def 'Consume valid FM message'() {
84         given: 'an event'
85             def jsonData = TestUtils.getResourceFileContent('avcSubscriptionCreationEvent.json')
86             def testEventSent = jsonObjectMapper.convertJsonString(jsonData, SubscriptionEvent.class)
87         and: 'dataCategory is set to FM'
88             testEventSent.getEvent().getDataType().setDataCategory("FM")
89         when: 'the valid event is consumed'
90             objectUnderTest.consumeSubscriptionEvent(testEventSent)
91         then: 'no exception is thrown'
92             noExceptionThrown()
93         and: 'the event is not mapped to a yangModelSubscription'
94             0 * mockSubscriptionEventMapper.toYangModelSubscriptionEvent(testEventSent) >> yangModelSubscriptionEvent
95         and: 'the event is not persisted'
96             0 * mockSubscriptionPersistence.saveSubscriptionEvent(yangModelSubscriptionEvent)
97         and: 'No event is forwarded'
98             0 * mockSubscriptionEventForwarder.forwardCreateSubscriptionEvent(*_)
99     }
100
101     def 'Consume event with wrong datastore causes an exception'() {
102         given: 'an event'
103             def jsonData = TestUtils.getResourceFileContent('avcSubscriptionCreationEvent.json')
104             def testEventSent = jsonObjectMapper.convertJsonString(jsonData, SubscriptionEvent.class)
105         and: 'datastore is set to a non passthrough datastore'
106             testEventSent.getEvent().getPredicates().setDatastore("operational")
107         when: 'the valid event is consumed'
108             objectUnderTest.consumeSubscriptionEvent(testEventSent)
109         then: 'an operation not yet supported exception is thrown'
110             thrown(OperationNotYetSupportedException)
111     }
112
113 }