2 * ============LICENSE_START=======================================================
3 * Copyright (c) 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 com.hazelcast.map.IMap
25 import io.cloudevents.CloudEvent
26 import io.cloudevents.core.builder.CloudEventBuilder
27 import org.apache.kafka.clients.consumer.ConsumerRecord
28 import org.onap.cps.ncmp.api.impl.subscriptions.SubscriptionPersistenceImpl
29 import org.onap.cps.ncmp.api.impl.utils.SubscriptionEventResponseCloudMapper
30 import org.onap.cps.ncmp.api.kafka.MessagingBaseSpec
31 import org.onap.cps.ncmp.events.cmsubscription1_0_0.dmi_to_ncmp.CmSubscriptionDmiOutEvent
32 import org.onap.cps.ncmp.utils.TestUtils
33 import org.onap.cps.spi.model.DataNodeBuilder
34 import org.onap.cps.utils.JsonObjectMapper
35 import org.springframework.beans.factory.annotation.Autowired
36 import org.springframework.boot.test.context.SpringBootTest
38 @SpringBootTest(classes = [ObjectMapper, JsonObjectMapper])
39 class CmSubscriptionDmiOutEventConsumerSpec extends MessagingBaseSpec {
42 JsonObjectMapper jsonObjectMapper
45 ObjectMapper objectMapper
47 IMap<String, Set<String>> mockForwardedSubscriptionEventCache = Mock(IMap<String, Set<String>>)
48 def mockSubscriptionPersistence = Mock(SubscriptionPersistenceImpl)
49 def mockSubscriptionEventResponseMapper = Mock(CmSubscriptionDmiOutEventToYangModelSubscriptionEventMapper)
50 def mockSubscriptionEventResponseOutcome = Mock(CmSubscriptionNcmpOutEventPublisher)
51 def mockSubscriptionEventResponseCloudMapper = new SubscriptionEventResponseCloudMapper(new ObjectMapper())
53 def objectUnderTest = new CmSubscriptionDmiOutEventConsumer(mockForwardedSubscriptionEventCache,
54 mockSubscriptionPersistence, mockSubscriptionEventResponseMapper, mockSubscriptionEventResponseOutcome, mockSubscriptionEventResponseCloudMapper)
56 def 'Consume Subscription Event Response where all DMIs have responded'() {
57 given: 'a consumer record including cloud event having subscription response'
58 def consumerRecordWithCloudEventAndSubscriptionResponse = getConsumerRecord()
59 and: 'a subscription response event'
60 def subscriptionResponseEvent = getSubscriptionResponseEvent()
61 and: 'a subscription event response and notifications are enabled'
62 objectUnderTest.notificationFeatureEnabled = notificationEnabled
63 and: 'subscription model loader is enabled'
64 objectUnderTest.subscriptionModelLoaderEnabled = modelLoaderEnabled
65 and: 'subscription persistence service returns data node includes no pending cm handle'
66 mockSubscriptionPersistence.getCmHandlesForSubscriptionEvent(*_) >> [getDataNode()]
67 when: 'the valid event is consumed'
68 objectUnderTest.consumeSubscriptionEventResponse(consumerRecordWithCloudEventAndSubscriptionResponse)
69 then: 'the forwarded subscription event cache returns only the received dmiName existing for the subscription create event'
70 1 * mockForwardedSubscriptionEventCache.containsKey('SCO-9989752cm-subscription-001') >> true
71 1 * mockForwardedSubscriptionEventCache.get('SCO-9989752cm-subscription-001') >> (['some-dmi-name'] as Set)
72 and: 'the forwarded subscription event cache returns an empty Map when the dmiName has been removed'
73 1 * mockForwardedSubscriptionEventCache.get('SCO-9989752cm-subscription-001') >> ([] as Set)
74 and: 'the response event is map to yang model'
75 numberOfTimeToPersist * mockSubscriptionEventResponseMapper.toYangModelSubscriptionEvent(_)
76 and: 'the response event is persisted into the db'
77 numberOfTimeToPersist * mockSubscriptionPersistence.saveSubscriptionEvent(_)
78 and: 'the subscription event is removed from the map'
79 numberOfTimeToRemove * mockForwardedSubscriptionEventCache.remove('SCO-9989752cm-subscription-001')
80 and: 'a response outcome has been created'
81 numberOfTimeToResponse * mockSubscriptionEventResponseOutcome.sendResponse(subscriptionResponseEvent, 'subscriptionCreated')
82 where: 'the following values are used'
83 scenario | modelLoaderEnabled | notificationEnabled || numberOfTimeToPersist || numberOfTimeToRemove || numberOfTimeToResponse
84 'Both model loader and notification are enabled' | true | true || 1 || 1 || 1
85 'Both model loader and notification are disabled' | false | false || 0 || 0 || 0
86 'Model loader enabled and notification disabled' | true | false || 1 || 0 || 0
87 'Model loader disabled and notification enabled' | false | true || 0 || 1 || 1
90 def 'Consume Subscription Event Response where another DMI has not yet responded'() {
91 given: 'a subscription event response and notifications are enabled'
92 objectUnderTest.notificationFeatureEnabled = notificationEnabled
93 and: 'subscription model loader is enabled'
94 objectUnderTest.subscriptionModelLoaderEnabled = modelLoaderEnabled
95 when: 'the valid event is consumed'
96 objectUnderTest.consumeSubscriptionEventResponse(getConsumerRecord())
97 then: 'the forwarded subscription event cache returns only the received dmiName existing for the subscription create event'
98 1 * mockForwardedSubscriptionEventCache.containsKey('SCO-9989752cm-subscription-001') >> true
99 1 * mockForwardedSubscriptionEventCache.get('SCO-9989752cm-subscription-001') >> (['responded-dmi', 'non-responded-dmi'] as Set)
100 and: 'the forwarded subscription event cache returns an empty Map when the dmiName has been removed'
101 1 * mockForwardedSubscriptionEventCache.get('SCO-9989752cm-subscription-001') >> (['non-responded-dmi'] as Set)
102 and: 'the response event is map to yang model'
103 numberOfTimeToPersist * mockSubscriptionEventResponseMapper.toYangModelSubscriptionEvent(_)
104 and: 'the response event is persisted into the db'
105 numberOfTimeToPersist * mockSubscriptionPersistence.saveSubscriptionEvent(_)
106 and: 'the subscription event is removed from the map'
107 and: 'the subscription event is not removed from the map'
108 0 * mockForwardedSubscriptionEventCache.remove(_)
109 and: 'a response outcome has not been created'
110 0 * mockSubscriptionEventResponseOutcome.sendResponse(*_)
111 where: 'the following values are used'
112 scenario | modelLoaderEnabled | notificationEnabled || numberOfTimeToPersist
113 'Both model loader and notification are enabled' | true | true || 1
114 'Both model loader and notification are disabled' | false | false || 0
115 'Model loader enabled and notification disabled' | true | false || 1
116 'Model loader disabled and notification enabled' | false | true || 0
119 def getSubscriptionResponseEvent() {
120 def subscriptionResponseJsonData = TestUtils.getResourceFileContent('cmSubscriptionDmiOutEvent.json')
121 return jsonObjectMapper.convertJsonString(subscriptionResponseJsonData, CmSubscriptionDmiOutEvent.class)
124 def getCloudEventHavingSubscriptionResponseEvent() {
125 return CloudEventBuilder.v1()
126 .withData(objectMapper.writeValueAsBytes(getSubscriptionResponseEvent()))
128 .withType('subscriptionCreated')
129 .withSource(URI.create('NCMP')).build()
132 def getConsumerRecord() {
133 return new ConsumerRecord<String, CloudEvent>('topic-name', 0, 0, 'event-key', getCloudEventHavingSubscriptionResponseEvent())
137 def leaves = [status:'ACCEPTED', cmHandleId:'cmhandle1'] as Map
138 return new DataNodeBuilder().withDataspace('NCMP-Admin')
139 .withAnchor('AVC-Subscriptions').withXpath('/subscription-registry/subscription')
140 .withLeaves(leaves).build()