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