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.avcsubscription
23 import com.fasterxml.jackson.databind.ObjectMapper
24 import com.hazelcast.map.IMap
25 import org.apache.kafka.clients.consumer.ConsumerRecord
26 import org.mapstruct.factory.Mappers
27 import org.onap.cps.ncmp.api.impl.events.EventsPublisher
28 import org.onap.cps.ncmp.api.impl.subscriptions.SubscriptionPersistence
29 import org.onap.cps.ncmp.api.impl.subscriptions.SubscriptionStatus
30 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle
31 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelSubscriptionEvent.TargetCmHandle
32 import org.onap.cps.ncmp.api.inventory.InventoryPersistence
33 import org.onap.cps.ncmp.api.kafka.MessagingBaseSpec
34 import org.onap.cps.ncmp.event.model.SubscriptionEvent
35 import org.onap.cps.ncmp.utils.TestUtils
36 import org.onap.cps.spi.exceptions.OperationNotYetSupportedException
37 import org.onap.cps.utils.JsonObjectMapper
38 import org.spockframework.spring.SpringBean
39 import org.springframework.beans.factory.annotation.Autowired
40 import org.springframework.boot.test.context.SpringBootTest
41 import spock.util.concurrent.BlockingVariable
43 import java.util.concurrent.TimeUnit
45 @SpringBootTest(classes = [ObjectMapper, JsonObjectMapper, SubscriptionEventForwarder])
46 class SubscriptionEventForwarderSpec extends MessagingBaseSpec {
49 SubscriptionEventForwarder objectUnderTest
52 InventoryPersistence mockInventoryPersistence = Mock(InventoryPersistence)
54 EventsPublisher<SubscriptionEvent> mockSubscriptionEventPublisher = Mock(EventsPublisher<SubscriptionEvent>)
56 IMap<String, Set<String>> mockForwardedSubscriptionEventCache = Mock(IMap<String, Set<String>>)
58 SubscriptionEventResponseOutcome mockSubscriptionEventResponseOutcome = Mock(SubscriptionEventResponseOutcome)
60 SubscriptionPersistence mockSubscriptionPersistence = Mock(SubscriptionPersistence)
62 SubscriptionEventMapper subscriptionEventMapper = Mappers.getMapper(SubscriptionEventMapper)
64 JsonObjectMapper jsonObjectMapper
66 def 'Forward valid CM create subscription and simulate timeout where #scenario'() {
68 def jsonData = TestUtils.getResourceFileContent('avcSubscriptionCreationEvent.json')
69 def testEventSent = jsonObjectMapper.convertJsonString(jsonData, SubscriptionEvent.class)
70 def consumerRecord = new ConsumerRecord<String, SubscriptionEvent>('topic-name', 0, 0, 'event-key', testEventSent)
71 and: 'the some of the cm handles will be accepted and some of rejected'
72 def cmHandlesToBeSavedInDb = [new TargetCmHandle('CMHandle1', SubscriptionStatus.ACCEPTED),
73 new TargetCmHandle('CMHandle2',SubscriptionStatus.ACCEPTED),
74 new TargetCmHandle('CMHandle3',SubscriptionStatus.REJECTED)]
75 and: 'a yang model subscription event will be saved into the db'
76 def yangModelSubscriptionEventWithAcceptedAndRejectedCmHandles = subscriptionEventMapper.toYangModelSubscriptionEvent(testEventSent)
77 yangModelSubscriptionEventWithAcceptedAndRejectedCmHandles.getPredicates().setTargetCmHandles(cmHandlesToBeSavedInDb)
78 and: 'the InventoryPersistence returns private properties for the supplied CM Handles'
79 1 * mockInventoryPersistence.getYangModelCmHandles(["CMHandle1", "CMHandle2", "CMHandle3"]) >> [
80 createYangModelCmHandleWithDmiProperty(1, 1,"shape","circle"),
81 createYangModelCmHandleWithDmiProperty(2, 1,"shape","square")
83 and: 'the thread creation delay is reduced to 2 seconds for testing'
84 objectUnderTest.dmiResponseTimeoutInMs = 2000
85 and: 'a Blocking Variable is used for the Asynchronous call with a timeout of 5 seconds'
86 def block = new BlockingVariable<Object>(5)
87 when: 'the valid event is forwarded'
88 objectUnderTest.forwardCreateSubscriptionEvent(testEventSent, consumerRecord.headers())
89 then: 'An asynchronous call is made to the blocking variable'
91 then: 'the event is added to the forwarded subscription event cache'
92 1 * mockForwardedSubscriptionEventCache.put("SCO-9989752cm-subscription-001", ["DMIName1"] as Set, 600, TimeUnit.SECONDS)
93 and: 'the event is forwarded twice with the CMHandle private properties and provides a valid listenable future'
94 1 * mockSubscriptionEventPublisher.publishEvent("ncmp-dmi-cm-avc-subscription-DMIName1", "SCO-9989752-cm-subscription-001-DMIName1",
95 consumerRecord.headers(), subscriptionEvent -> {
96 Map targets = subscriptionEvent.getEvent().getPredicates().getTargets().get(0)
97 targets["CMHandle1"] == ["shape":"circle"]
98 targets["CMHandle2"] == ["shape":"square"]
101 and: 'the persistence service save the yang model subscription event'
102 1 * mockSubscriptionPersistence.saveSubscriptionEvent(yangModelSubscriptionEventWithAcceptedAndRejectedCmHandles)
103 and: 'a separate thread has been created where the map is polled'
104 1 * mockForwardedSubscriptionEventCache.containsKey("SCO-9989752cm-subscription-001") >> true
105 1 * mockSubscriptionEventResponseOutcome.sendResponse(*_)
106 and: 'the subscription id is removed from the event cache map returning the asynchronous blocking variable'
107 1 * mockForwardedSubscriptionEventCache.remove("SCO-9989752cm-subscription-001") >> {block.set(_)}
110 def 'Forward CM create subscription where target CM Handles are #scenario'() {
112 def jsonData = TestUtils.getResourceFileContent('avcSubscriptionCreationEvent.json')
113 def testEventSent = jsonObjectMapper.convertJsonString(jsonData, SubscriptionEvent.class)
114 def consumerRecord = new ConsumerRecord<String, SubscriptionEvent>('topic-name', 0, 0, 'event-key', testEventSent)
115 and: 'the target CMHandles are set to #scenario'
116 testEventSent.getEvent().getPredicates().setTargets(invalidTargets)
117 when: 'the event is forwarded'
118 objectUnderTest.forwardCreateSubscriptionEvent(testEventSent, consumerRecord.headers())
119 then: 'an operation not yet supported exception is thrown'
120 thrown(OperationNotYetSupportedException)
122 scenario | invalidTargets
125 'wildcard' | ['CMHandle*']
128 def 'Forward valid CM create subscription where targets are not associated to any existing CMHandles'() {
130 def jsonData = TestUtils.getResourceFileContent('avcSubscriptionCreationEvent.json')
131 def testEventSent = jsonObjectMapper.convertJsonString(jsonData, SubscriptionEvent.class)
132 def consumerRecord = new ConsumerRecord<String, SubscriptionEvent>('topic-name', 0, 0, 'event-key', testEventSent)
133 and: 'the cm handles will be rejected'
134 def rejectedCmHandles = [new TargetCmHandle('CMHandle1', SubscriptionStatus.REJECTED),
135 new TargetCmHandle('CMHandle2',SubscriptionStatus.REJECTED),
136 new TargetCmHandle('CMHandle3',SubscriptionStatus.REJECTED)]
137 and: 'a yang model subscription event will be saved into the db with rejected cm handles'
138 def yangModelSubscriptionEventWithRejectedCmHandles = subscriptionEventMapper.toYangModelSubscriptionEvent(testEventSent)
139 yangModelSubscriptionEventWithRejectedCmHandles.getPredicates().setTargetCmHandles(rejectedCmHandles)
140 and: 'the InventoryPersistence returns no private properties for the supplied CM Handles'
141 1 * mockInventoryPersistence.getYangModelCmHandles(["CMHandle1", "CMHandle2", "CMHandle3"]) >> []
142 and: 'the thread creation delay is reduced to 2 seconds for testing'
143 objectUnderTest.dmiResponseTimeoutInMs = 2000
144 and: 'a Blocking Variable is used for the Asynchronous call with a timeout of 5 seconds'
145 def block = new BlockingVariable<Object>(5)
146 when: 'the valid event is forwarded'
147 objectUnderTest.forwardCreateSubscriptionEvent(testEventSent, consumerRecord.headers())
148 then: 'the event is not added to the forwarded subscription event cache'
149 0 * mockForwardedSubscriptionEventCache.put("SCO-9989752cm-subscription-001", ["DMIName1", "DMIName2"] as Set)
150 and: 'the event is not being forwarded with the CMHandle private properties and does not provides a valid listenable future'
151 0 * mockSubscriptionEventPublisher.publishEvent("ncmp-dmi-cm-avc-subscription-DMIName1", "SCO-9989752-cm-subscription-001-DMIName1",
152 consumerRecord.headers(),subscriptionEvent -> {
153 Map targets = subscriptionEvent.getEvent().getPredicates().getTargets().get(0)
154 targets["CMHandle1"] == ["shape":"circle"]
155 targets["CMHandle2"] == ["shape":"square"]
158 0 * mockSubscriptionEventPublisher.publishEvent("ncmp-dmi-cm-avc-subscription-DMIName2", "SCO-9989752-cm-subscription-001-DMIName2",
159 consumerRecord.headers(),subscriptionEvent -> {
160 Map targets = subscriptionEvent.getEvent().getPredicates().getTargets().get(0)
161 targets["CMHandle3"] == ["shape":"triangle"]
164 and: 'a separate thread has been created where the map is polled'
165 0 * mockForwardedSubscriptionEventCache.containsKey("SCO-9989752cm-subscription-001") >> true
166 0 * mockForwardedSubscriptionEventCache.get(_)
167 and: 'the subscription id is removed from the event cache map returning the asynchronous blocking variable'
168 0 * mockForwardedSubscriptionEventCache.remove("SCO-9989752cm-subscription-001") >> {block.set(_)}
169 and: 'the persistence service save target cm handles of the yang model subscription event as rejected '
170 1 * mockSubscriptionPersistence.saveSubscriptionEvent(yangModelSubscriptionEventWithRejectedCmHandles)
171 and: 'subscription outcome has been sent'
172 1 * mockSubscriptionEventResponseOutcome.sendResponse('SCO-9989752', 'cm-subscription-001')
175 static def createYangModelCmHandleWithDmiProperty(id, dmiId,propertyName, propertyValue) {
176 return new YangModelCmHandle(id:"CMHandle" + id, dmiDataServiceName: "DMIName" + dmiId, dmiProperties: [new YangModelCmHandle.Property(propertyName,propertyValue)])