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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.ncmp.api.impl.events.avcsubscription
23 import com.fasterxml.jackson.databind.ObjectMapper
24 import org.apache.kafka.clients.consumer.ConsumerRecord
25 import org.onap.cps.ncmp.api.impl.subscriptions.SubscriptionPersistence
26 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelSubscriptionEvent
27 import org.onap.cps.ncmp.api.kafka.MessagingBaseSpec
28 import org.onap.cps.ncmp.event.model.SubscriptionEvent
29 import org.onap.cps.ncmp.utils.TestUtils
30 import org.onap.cps.spi.exceptions.OperationNotYetSupportedException
31 import org.onap.cps.utils.JsonObjectMapper
32 import org.springframework.beans.factory.annotation.Autowired
33 import org.springframework.boot.test.context.SpringBootTest
35 @SpringBootTest(classes = [ObjectMapper, JsonObjectMapper])
36 class SubscriptionEventConsumerSpec extends MessagingBaseSpec {
38 def mockSubscriptionEventForwarder = Mock(SubscriptionEventForwarder)
39 def mockSubscriptionEventMapper = Mock(SubscriptionEventMapper)
40 def mockSubscriptionPersistence = Mock(SubscriptionPersistence)
41 def objectUnderTest = new SubscriptionEventConsumer(mockSubscriptionEventForwarder, mockSubscriptionEventMapper, mockSubscriptionPersistence)
43 def yangModelSubscriptionEvent = new YangModelSubscriptionEvent()
46 JsonObjectMapper jsonObjectMapper
48 def 'Consume, persist and forward valid CM create message'() {
49 given: 'an event with data category CM'
50 def jsonData = TestUtils.getResourceFileContent('avcSubscriptionCreationEvent.json')
51 def testEventSent = jsonObjectMapper.convertJsonString(jsonData, SubscriptionEvent.class)
52 def consumerRecord = new ConsumerRecord<String, SubscriptionEvent>('topic-name', 0, 0, 'event-key', testEventSent)
53 and: 'notifications are enabled'
54 objectUnderTest.notificationFeatureEnabled = true
55 and: 'subscription model loader is enabled'
56 objectUnderTest.subscriptionModelLoaderEnabled = true
57 when: 'the valid event is consumed'
58 objectUnderTest.consumeSubscriptionEvent(consumerRecord)
59 then: 'the event is mapped to a yangModelSubscription'
60 1 * mockSubscriptionEventMapper.toYangModelSubscriptionEvent(testEventSent) >> yangModelSubscriptionEvent
61 and: 'the event is persisted'
62 1 * mockSubscriptionPersistence.saveSubscriptionEvent(yangModelSubscriptionEvent)
63 and: 'the event is forwarded'
64 1 * mockSubscriptionEventForwarder.forwardCreateSubscriptionEvent(testEventSent, consumerRecord.headers())
67 def 'Consume valid CM create message where notifications and model loader are disabled'() {
68 given: 'an event with data category CM'
69 def jsonData = TestUtils.getResourceFileContent('avcSubscriptionCreationEvent.json')
70 def testEventSent = jsonObjectMapper.convertJsonString(jsonData, SubscriptionEvent.class)
71 def consumerRecord = new ConsumerRecord<String, SubscriptionEvent>('topic-name', 0, 0, 'event-key', testEventSent)
72 and: 'notifications are disabled'
73 objectUnderTest.notificationFeatureEnabled = false
74 and: 'subscription model loader is disabled'
75 objectUnderTest.subscriptionModelLoaderEnabled = false
76 when: 'the valid event is consumed'
77 objectUnderTest.consumeSubscriptionEvent(consumerRecord)
78 then: 'the event is not mapped to a yangModelSubscription'
79 0 * mockSubscriptionEventMapper.toYangModelSubscriptionEvent(*_) >> yangModelSubscriptionEvent
80 and: 'the event is not persisted'
81 0 * mockSubscriptionPersistence.saveSubscriptionEvent(*_)
82 and: 'the event is not forwarded'
83 0 * mockSubscriptionEventForwarder.forwardCreateSubscriptionEvent(*_)
86 def 'Consume valid FM message'() {
88 def jsonData = TestUtils.getResourceFileContent('avcSubscriptionCreationEvent.json')
89 def testEventSent = jsonObjectMapper.convertJsonString(jsonData, SubscriptionEvent.class)
90 def consumerRecord = new ConsumerRecord<String, SubscriptionEvent>('topic-name', 0, 0, 'event-key', testEventSent)
91 and: 'dataCategory is set to FM'
92 testEventSent.getEvent().getDataType().setDataCategory("FM")
93 when: 'the valid event is consumed'
94 objectUnderTest.consumeSubscriptionEvent(consumerRecord)
95 then: 'no exception is thrown'
97 and: 'the event is not mapped to a yangModelSubscription'
98 0 * mockSubscriptionEventMapper.toYangModelSubscriptionEvent(testEventSent) >> yangModelSubscriptionEvent
99 and: 'the event is not persisted'
100 0 * mockSubscriptionPersistence.saveSubscriptionEvent(yangModelSubscriptionEvent)
101 and: 'No event is forwarded'
102 0 * mockSubscriptionEventForwarder.forwardCreateSubscriptionEvent(*_)
105 def 'Consume event with wrong datastore causes an exception'() {
107 def jsonData = TestUtils.getResourceFileContent('avcSubscriptionCreationEvent.json')
108 def testEventSent = jsonObjectMapper.convertJsonString(jsonData, SubscriptionEvent.class)
109 def consumerRecord = new ConsumerRecord<String, SubscriptionEvent>('topic-name', 0, 0, 'event-key', testEventSent)
110 and: 'datastore is set to a non passthrough datastore'
111 testEventSent.getEvent().getPredicates().setDatastore("operational")
112 when: 'the valid event is consumed'
113 objectUnderTest.consumeSubscriptionEvent(consumerRecord)
114 then: 'an operation not yet supported exception is thrown'
115 thrown(OperationNotYetSupportedException)