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 io.cloudevents.CloudEvent
25 import io.cloudevents.core.builder.CloudEventBuilder
26 import org.apache.kafka.clients.consumer.ConsumerRecord
27 import org.onap.cps.ncmp.api.impl.subscriptions.SubscriptionPersistence
28 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelSubscriptionEvent
29 import org.onap.cps.ncmp.api.kafka.MessagingBaseSpec
30 import org.onap.cps.ncmp.events.avcsubscription1_0_0.client_to_ncmp.SubscriptionEvent;
31 import org.onap.cps.ncmp.utils.TestUtils
32 import org.onap.cps.spi.exceptions.OperationNotYetSupportedException
33 import org.onap.cps.utils.JsonObjectMapper
34 import org.springframework.beans.factory.annotation.Autowired
35 import org.springframework.boot.test.context.SpringBootTest
37 @SpringBootTest(classes = [ObjectMapper, JsonObjectMapper])
38 class SubscriptionEventConsumerSpec extends MessagingBaseSpec {
40 def mockSubscriptionEventForwarder = Mock(SubscriptionEventForwarder)
41 def mockSubscriptionEventMapper = Mock(SubscriptionEventMapper)
42 def mockSubscriptionPersistence = Mock(SubscriptionPersistence)
43 def objectUnderTest = new SubscriptionEventConsumer(mockSubscriptionEventForwarder, mockSubscriptionEventMapper, mockSubscriptionPersistence)
45 def yangModelSubscriptionEvent = new YangModelSubscriptionEvent()
48 JsonObjectMapper jsonObjectMapper
51 ObjectMapper objectMapper
54 def 'Consume, persist and forward valid CM create message'() {
55 given: 'an event with data category CM'
56 def jsonData = TestUtils.getResourceFileContent('avcSubscriptionCreationEvent.json')
57 def testEventSent = jsonObjectMapper.convertJsonString(jsonData, SubscriptionEvent.class)
58 testEventSent.getData().getDataType().setDataCategory(dataCategory)
59 def testCloudEventSent = CloudEventBuilder.v1()
60 .withData(objectMapper.writeValueAsBytes(testEventSent))
61 .withId('subscriptionCreated')
63 .withSource(URI.create('some-resource'))
64 .withExtension('correlationid', 'test-cmhandle1').build()
65 def consumerRecord = new ConsumerRecord<String, CloudEvent>('topic-name', 0, 0, 'event-key', testCloudEventSent)
66 and: 'notifications are enabled'
67 objectUnderTest.notificationFeatureEnabled = isNotificationEnabled
68 and: 'subscription model loader is enabled'
69 objectUnderTest.subscriptionModelLoaderEnabled = isModelLoaderEnabled
70 when: 'the valid event is consumed'
71 objectUnderTest.consumeSubscriptionEvent(consumerRecord)
72 then: 'the event is mapped to a yangModelSubscription'
73 numberOfTimesToPersist * mockSubscriptionEventMapper.toYangModelSubscriptionEvent(testEventSent) >> yangModelSubscriptionEvent
74 and: 'the event is persisted'
75 numberOfTimesToPersist * mockSubscriptionPersistence.saveSubscriptionEvent(yangModelSubscriptionEvent)
76 and: 'the event is forwarded'
77 numberOfTimesToForward * mockSubscriptionEventForwarder.forwardCreateSubscriptionEvent(testEventSent, 'subscriptionCreated')
78 where: 'given values are used'
79 scenario | dataCategory | dataType | isNotificationEnabled | isModelLoaderEnabled || numberOfTimesToForward || numberOfTimesToPersist
80 'Both model loader and notification are enabled' | 'CM' | 'subscriptionCreated' | true | true || 1 || 1
81 'Both model loader and notification are disabled' | 'CM' | 'subscriptionCreated' | false | false || 0 || 0
82 'Model loader enabled and notification disabled' | 'CM' | 'subscriptionCreated' | false | true || 0 || 1
83 'Model loader disabled and notification enabled' | 'CM' | 'subscriptionCreated' | true | false || 1 || 0
84 'Flags are enabled but data category is FM' | 'FM' | 'subscriptionCreated' | true | true || 0 || 0
85 'Flags are enabled but data type is UPDATE' | 'CM' | 'subscriptionUpdated' | true | true || 0 || 1
88 def 'Consume event with wrong datastore causes an exception'() {
90 def jsonData = TestUtils.getResourceFileContent('avcSubscriptionCreationEvent.json')
91 def testEventSent = jsonObjectMapper.convertJsonString(jsonData, SubscriptionEvent.class)
92 and: 'datastore is set to a passthrough-running datastore'
93 testEventSent.getData().getPredicates().setDatastore('operational')
94 def testCloudEventSent = CloudEventBuilder.v1()
95 .withData(objectMapper.writeValueAsBytes(testEventSent))
96 .withId('some-event-id')
97 .withType('some-event-type')
98 .withSource(URI.create('some-resource'))
99 .withExtension('correlationid', 'test-cmhandle1').build()
100 def consumerRecord = new ConsumerRecord<String, SubscriptionEvent>('topic-name', 0, 0, 'event-key', testCloudEventSent)
101 when: 'the valid event is consumed'
102 objectUnderTest.consumeSubscriptionEvent(consumerRecord)
103 then: 'an operation not yet supported exception is thrown'
104 def exception = thrown(OperationNotYetSupportedException)
105 exception.details == 'passthrough-running datastores are currently only supported for event subscriptions'