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.cmsubscription
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.utils.CmSubscriptionEventCloudMapper
29 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelSubscriptionEvent
30 import org.onap.cps.ncmp.api.kafka.MessagingBaseSpec
31 import org.onap.cps.ncmp.events.cmsubscription1_0_0.client_to_ncmp.CmSubscriptionNcmpInEvent
32 import org.onap.cps.ncmp.utils.TestUtils
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 CmSubscriptionNcmpInEventConsumerSpec extends MessagingBaseSpec {
40 def mockCmSubscriptionNcmpInEventForwarder = Mock(CmSubscriptionNcmpInEventForwarder)
41 def mockCmSubscriptionNcmpInEventMapper = Mock(CmSubscriptionNcmpInEventMapper)
42 def mockSubscriptionPersistence = Mock(SubscriptionPersistence)
43 def cmSubscriptionEventCloudMapper = new CmSubscriptionEventCloudMapper(new ObjectMapper())
44 def objectUnderTest = new CmSubscriptionNcmpInEventConsumer(mockCmSubscriptionNcmpInEventForwarder, mockCmSubscriptionNcmpInEventMapper, mockSubscriptionPersistence, cmSubscriptionEventCloudMapper)
46 def yangModelSubscriptionEvent = new YangModelSubscriptionEvent()
49 JsonObjectMapper jsonObjectMapper
52 ObjectMapper objectMapper
55 def 'Consume, persist and forward valid CM create message'() {
56 given: 'an event with data category CM'
57 def jsonData = TestUtils.getResourceFileContent('cmSubscriptionNcmpInEvent.json')
58 def testEventSent = jsonObjectMapper.convertJsonString(jsonData, CmSubscriptionNcmpInEvent.class)
59 testEventSent.getData().getDataType().setDataCategory(dataCategory)
60 def testCloudEventSent = CloudEventBuilder.v1()
61 .withData(objectMapper.writeValueAsBytes(testEventSent))
62 .withId('subscriptionCreated')
64 .withSource(URI.create('some-resource'))
65 .withExtension('correlationid', 'test-cmhandle1').build()
66 def consumerRecord = new ConsumerRecord<String, CloudEvent>('topic-name', 0, 0, 'event-key', testCloudEventSent)
67 and: 'notifications are enabled'
68 objectUnderTest.notificationFeatureEnabled = isNotificationEnabled
69 and: 'subscription model loader is enabled'
70 objectUnderTest.subscriptionModelLoaderEnabled = isModelLoaderEnabled
71 when: 'the valid event is consumed'
72 objectUnderTest.consumeSubscriptionEvent(consumerRecord)
73 then: 'the event is mapped to a yangModelSubscription'
74 numberOfTimesToPersist * mockCmSubscriptionNcmpInEventMapper.toYangModelSubscriptionEvent(testEventSent) >> yangModelSubscriptionEvent
75 and: 'the event is persisted'
76 numberOfTimesToPersist * mockSubscriptionPersistence.saveSubscriptionEvent(yangModelSubscriptionEvent)
77 and: 'the event is forwarded'
78 numberOfTimesToForward * mockCmSubscriptionNcmpInEventForwarder.forwardCreateSubscriptionEvent(testEventSent, 'subscriptionCreated')
79 where: 'given values are used'
80 scenario | dataCategory | dataType | isNotificationEnabled | isModelLoaderEnabled || numberOfTimesToForward || numberOfTimesToPersist
81 'Both model loader and notification are enabled' | 'CM' | 'subscriptionCreated' | true | true || 1 || 1
82 'Both model loader and notification are disabled' | 'CM' | 'subscriptionCreated' | false | false || 0 || 0
83 'Model loader enabled and notification disabled' | 'CM' | 'subscriptionCreated' | false | true || 0 || 1
84 'Model loader disabled and notification enabled' | 'CM' | 'subscriptionCreated' | true | false || 1 || 0
85 'Flags are enabled but data category is FM' | 'FM' | 'subscriptionCreated' | true | true || 0 || 0
86 'Flags are enabled but data type is UPDATE' | 'CM' | 'subscriptionUpdated' | true | true || 0 || 1
89 def 'Consume event with wrong datastore causes an exception'() {
91 def jsonData = TestUtils.getResourceFileContent('cmSubscriptionNcmpInEvent.json')
92 def testEventSent = jsonObjectMapper.convertJsonString(jsonData, CmSubscriptionNcmpInEvent.class)
93 and: 'datastore is set to a passthrough-running datastore'
94 testEventSent.getData().getPredicates().setDatastore('operational')
95 def testCloudEventSent = CloudEventBuilder.v1()
96 .withData(objectMapper.writeValueAsBytes(testEventSent))
97 .withId('some-event-id')
98 .withType('some-event-type')
99 .withSource(URI.create('some-resource'))
100 .withExtension('correlationid', 'test-cmhandle1').build()
101 def consumerRecord = new ConsumerRecord<String, CmSubscriptionNcmpInEvent>('topic-name', 0, 0, 'event-key', testCloudEventSent)
102 when: 'the valid event is consumed'
103 objectUnderTest.consumeSubscriptionEvent(consumerRecord)
104 then: 'an operation not supported exception is thrown'
105 thrown(UnsupportedOperationException)