248eb8bbe295aeb032ab0848e2c2a16ae37b3997
[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         when: 'the valid event is consumed'
54             objectUnderTest.consumeSubscriptionEvent(testEventSent)
55         then: 'the event is mapped to a yangModelSubscription'
56             1 * mockSubscriptionEventMapper.toYangModelSubscriptionEvent(testEventSent) >> yangModelSubscriptionEvent
57         and: 'the event is persisted'
58             1 * mockSubscriptionPersistence.saveSubscriptionEvent(yangModelSubscriptionEvent)
59         and: 'the event is forwarded'
60             1 * mockSubscriptionEventForwarder.forwardCreateSubscriptionEvent(testEventSent)
61     }
62
63     def 'Consume and persist valid CM create message where notifications are disabled'() {
64         given: 'an event with data category CM'
65             def jsonData = TestUtils.getResourceFileContent('avcSubscriptionCreationEvent.json')
66             def testEventSent = jsonObjectMapper.convertJsonString(jsonData, SubscriptionEvent.class)
67         and: 'notifications are disabled'
68             objectUnderTest.notificationFeatureEnabled = false
69         when: 'the valid event is consumed'
70             objectUnderTest.consumeSubscriptionEvent(testEventSent)
71         then: 'the event is mapped to a yangModelSubscription'
72             1 * mockSubscriptionEventMapper.toYangModelSubscriptionEvent(testEventSent) >> yangModelSubscriptionEvent
73         and: 'the event is persisted'
74             1 * mockSubscriptionPersistence.saveSubscriptionEvent(yangModelSubscriptionEvent)
75         and: 'the event is not forwarded'
76             0 * mockSubscriptionEventForwarder.forwardCreateSubscriptionEvent(*_)
77     }
78
79     def 'Consume valid FM message'() {
80         given: 'an event'
81             def jsonData = TestUtils.getResourceFileContent('avcSubscriptionCreationEvent.json')
82             def testEventSent = jsonObjectMapper.convertJsonString(jsonData, SubscriptionEvent.class)
83         and: 'dataCategory is set to FM'
84             testEventSent.getEvent().getDataType().setDataCategory("FM")
85         when: 'the valid event is consumed'
86             objectUnderTest.consumeSubscriptionEvent(testEventSent)
87         then: 'no exception is thrown'
88             noExceptionThrown()
89         and: 'the event is not mapped to a yangModelSubscription'
90             0 * mockSubscriptionEventMapper.toYangModelSubscriptionEvent(testEventSent) >> yangModelSubscriptionEvent
91         and: 'the event is not persisted'
92             0 * mockSubscriptionPersistence.saveSubscriptionEvent(yangModelSubscriptionEvent)
93         and: 'No event is forwarded'
94             0 * mockSubscriptionEventForwarder.forwardCreateSubscriptionEvent(*_)
95     }
96
97     def 'Consume event with wrong datastore causes an exception'() {
98         given: 'an event'
99             def jsonData = TestUtils.getResourceFileContent('avcSubscriptionCreationEvent.json')
100             def testEventSent = jsonObjectMapper.convertJsonString(jsonData, SubscriptionEvent.class)
101         and: 'datastore is set to a non passthrough datastore'
102             testEventSent.getEvent().getPredicates().setDatastore("operational")
103         when: 'the valid event is consumed'
104             objectUnderTest.consumeSubscriptionEvent(testEventSent)
105         then: 'an operation not yet supported exception is thrown'
106             thrown(OperationNotYetSupportedException)
107     }
108
109 }