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 io.cloudevents.CloudEvent
26 import io.cloudevents.core.CloudEventUtils
27 import io.cloudevents.core.data.PojoCloudEventData
28 import io.cloudevents.jackson.PojoCloudEventDataMapper
29 import org.mapstruct.factory.Mappers
30 import org.onap.cps.ncmp.api.impl.events.EventsPublisher
31 import org.onap.cps.ncmp.api.impl.subscriptions.SubscriptionPersistence
32 import org.onap.cps.ncmp.api.impl.subscriptions.SubscriptionStatus
33 import org.onap.cps.ncmp.api.impl.utils.SubscriptionEventCloudMapper
34 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle
35 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelSubscriptionEvent.TargetCmHandle
36 import org.onap.cps.ncmp.api.inventory.InventoryPersistence
37 import org.onap.cps.ncmp.api.kafka.MessagingBaseSpec
38 import org.onap.cps.ncmp.events.avcsubscription1_0_0.client_to_ncmp.SubscriptionEvent
39 import org.onap.cps.ncmp.events.avcsubscription1_0_0.dmi_to_ncmp.Data
40 import org.onap.cps.ncmp.events.avcsubscription1_0_0.dmi_to_ncmp.SubscriptionEventResponse
41 import org.onap.cps.ncmp.events.avcsubscription1_0_0.ncmp_to_dmi.CmHandle;
42 import org.onap.cps.ncmp.utils.TestUtils
43 import org.onap.cps.utils.JsonObjectMapper
44 import org.spockframework.spring.SpringBean
45 import org.springframework.beans.factory.annotation.Autowired
46 import org.springframework.boot.test.context.SpringBootTest
47 import spock.util.concurrent.BlockingVariable
48 import java.util.concurrent.TimeUnit
50 @SpringBootTest(classes = [ObjectMapper, JsonObjectMapper, SubscriptionEventForwarder])
51 class SubscriptionEventForwarderSpec extends MessagingBaseSpec {
54 SubscriptionEventForwarder objectUnderTest
57 InventoryPersistence mockInventoryPersistence = Mock(InventoryPersistence)
59 EventsPublisher<CloudEvent> mockSubscriptionEventPublisher = Mock(EventsPublisher<CloudEvent>)
61 IMap<String, Set<String>> mockForwardedSubscriptionEventCache = Mock(IMap<String, Set<String>>)
63 SubscriptionEventCloudMapper subscriptionEventCloudMapper = new SubscriptionEventCloudMapper(new ObjectMapper())
65 SubscriptionEventResponseOutcome mockSubscriptionEventResponseOutcome = Mock(SubscriptionEventResponseOutcome)
67 SubscriptionPersistence mockSubscriptionPersistence = Mock(SubscriptionPersistence)
69 SubscriptionEventMapper subscriptionEventMapper = Mappers.getMapper(SubscriptionEventMapper)
71 ClientSubscriptionEventMapper clientSubscriptionEventMapper = Mappers.getMapper(ClientSubscriptionEventMapper)
73 JsonObjectMapper jsonObjectMapper
75 ObjectMapper objectMapper
77 def 'Forward valid CM create subscription and simulate timeout'() {
79 def jsonData = TestUtils.getResourceFileContent('avcSubscriptionCreationEvent.json')
80 def testEventSent = jsonObjectMapper.convertJsonString(jsonData, SubscriptionEvent.class)
81 and: 'the InventoryPersistence returns private properties for the supplied CM Handles'
82 1 * mockInventoryPersistence.getYangModelCmHandles(["CMHandle1", "CMHandle2", "CMHandle3"]) >> [
83 createYangModelCmHandleWithDmiProperty(1, 1,"shape","circle"),
84 createYangModelCmHandleWithDmiProperty(2, 1,"shape","square")
86 and: 'the thread creation delay is reduced to 2 seconds for testing'
87 objectUnderTest.dmiResponseTimeoutInMs = 2000
88 and: 'a Blocking Variable is used for the Asynchronous call with a timeout of 5 seconds'
89 def block = new BlockingVariable<Object>(5)
90 when: 'the valid event is forwarded'
91 objectUnderTest.forwardCreateSubscriptionEvent(testEventSent, 'subscriptionCreated')
92 then: 'An asynchronous call is made to the blocking variable'
94 then: 'the event is added to the forwarded subscription event cache'
95 1 * mockForwardedSubscriptionEventCache.put("SCO-9989752cm-subscription-001", ["DMIName1"] as Set, 600, TimeUnit.SECONDS)
96 and: 'the event is forwarded twice with the CMHandle private properties and provides a valid listenable future'
97 1 * mockSubscriptionEventPublisher.publishCloudEvent("ncmp-dmi-cm-avc-subscription-DMIName1", "SCO-9989752-cm-subscription-001-DMIName1",
99 def targets = toSubscriptionEvent(cloudEvent).getData().getPredicates().getTargets()
100 def cmHandle2 = createCmHandle('CMHandle2', ['shape':'square'] as Map)
101 def cmHandle1 = createCmHandle('CMHandle1', ['shape':'circle'] as Map)
102 targets == [cmHandle2, cmHandle1]
105 and: 'a separate thread has been created where the map is polled'
106 1 * mockForwardedSubscriptionEventCache.containsKey("SCO-9989752cm-subscription-001") >> true
107 1 * mockSubscriptionEventResponseOutcome.sendResponse(*_)
108 and: 'the subscription id is removed from the event cache map returning the asynchronous blocking variable'
109 1 * mockForwardedSubscriptionEventCache.remove("SCO-9989752cm-subscription-001") >> {block.set(_)}
112 def 'Forward CM create subscription where target CM Handles are #scenario'() {
114 def jsonData = TestUtils.getResourceFileContent('avcSubscriptionCreationEvent.json')
115 def testEventSent = jsonObjectMapper.convertJsonString(jsonData, SubscriptionEvent.class)
116 and: 'the target CMHandles are set to #scenario'
117 testEventSent.getData().getPredicates().setTargets(invalidTargets)
118 when: 'the event is forwarded'
119 objectUnderTest.forwardCreateSubscriptionEvent(testEventSent, 'some-event-type')
120 then: 'an operation not supported exception is thrown'
121 thrown(UnsupportedOperationException)
123 scenario | invalidTargets
126 'wildcard' | ['CMHandle*']
129 def 'Forward valid CM create subscription where targets are not associated to any existing CMHandles'() {
131 def jsonData = TestUtils.getResourceFileContent('avcSubscriptionCreationEvent.json')
132 def testEventSent = jsonObjectMapper.convertJsonString(jsonData, SubscriptionEvent.class)
133 and: 'a subscription event response'
134 def emptySubscriptionEventResponse = new SubscriptionEventResponse().withData(new Data());
135 emptySubscriptionEventResponse.getData().setSubscriptionName('cm-subscription-001');
136 emptySubscriptionEventResponse.getData().setClientId('SCO-9989752');
137 and: 'the cm handles will be rejected'
138 def rejectedCmHandles = [new TargetCmHandle('CMHandle1', SubscriptionStatus.REJECTED, 'Cm handle does not exist'),
139 new TargetCmHandle('CMHandle2',SubscriptionStatus.REJECTED, 'Cm handle does not exist'),
140 new TargetCmHandle('CMHandle3',SubscriptionStatus.REJECTED, 'Cm handle does not exist')]
141 and: 'a yang model subscription event will be saved into the db with rejected cm handles'
142 def yangModelSubscriptionEvent = subscriptionEventMapper.toYangModelSubscriptionEvent(testEventSent)
143 yangModelSubscriptionEvent.getPredicates().setTargetCmHandles(rejectedCmHandles)
144 and: 'the InventoryPersistence returns no private properties for the supplied CM Handles'
145 1 * mockInventoryPersistence.getYangModelCmHandles(["CMHandle1", "CMHandle2", "CMHandle3"]) >> []
146 and: 'the thread creation delay is reduced to 2 seconds for testing'
147 objectUnderTest.dmiResponseTimeoutInMs = 2000
148 and: 'a Blocking Variable is used for the Asynchronous call with a timeout of 5 seconds'
149 def block = new BlockingVariable<Object>(5)
150 when: 'the valid event is forwarded'
151 objectUnderTest.forwardCreateSubscriptionEvent(testEventSent, 'subscriptionCreatedStatus')
152 then: 'the event is not added to the forwarded subscription event cache'
153 0 * mockForwardedSubscriptionEventCache.put("SCO-9989752cm-subscription-001", ["DMIName1", "DMIName2"] as Set)
154 and: 'the event is not being forwarded with the CMHandle private properties and does not provides a valid listenable future'
155 0 * mockSubscriptionEventPublisher.publishCloudEvent("ncmp-dmi-cm-avc-subscription-DMIName1", "SCO-9989752-cm-subscription-001-DMIName1",
157 def targets = toSubscriptionEvent(cloudEvent).getData().getPredicates().getTargets()
158 def cmHandle2 = createCmHandle('CMHandle2', ['shape':'square'] as Map)
159 def cmHandle1 = createCmHandle('CMHandle1', ['shape':'circle'] as Map)
160 targets == [cmHandle2, cmHandle1]
163 0 * mockSubscriptionEventPublisher.publishCloudEvent("ncmp-dmi-cm-avc-subscription-DMIName2", "SCO-9989752-cm-subscription-001-DMIName2",
165 def targets = toSubscriptionEvent(cloudEvent).getData().getPredicates().getTargets()
166 def cmHandle3 = createCmHandle('CMHandle3', ['shape':'triangle'] as Map)
167 targets == [cmHandle3]
170 and: 'a separate thread has been created where the map is polled'
171 0 * mockForwardedSubscriptionEventCache.containsKey("SCO-9989752cm-subscription-001") >> true
172 0 * mockForwardedSubscriptionEventCache.get(_)
173 and: 'the subscription id is removed from the event cache map returning the asynchronous blocking variable'
174 0 * mockForwardedSubscriptionEventCache.remove("SCO-9989752cm-subscription-001") >> {block.set(_)}
175 and: 'the persistence service save target cm handles of the yang model subscription event as rejected '
176 1 * mockSubscriptionPersistence.saveSubscriptionEvent(yangModelSubscriptionEvent)
177 and: 'subscription outcome has been sent'
178 1 * mockSubscriptionEventResponseOutcome.sendResponse(emptySubscriptionEventResponse, 'subscriptionCreatedStatus')
181 static def createYangModelCmHandleWithDmiProperty(id, dmiId,propertyName, propertyValue) {
182 return new YangModelCmHandle(id:"CMHandle" + id, dmiDataServiceName: "DMIName" + dmiId, dmiProperties: [new YangModelCmHandle.Property(propertyName,propertyValue)])
185 static def createCmHandle(id, additionalProperties) {
186 def cmHandle = new CmHandle();
188 cmHandle.setAdditionalProperties(additionalProperties)
192 def toSubscriptionEvent(cloudEvent) {
193 final PojoCloudEventData<org.onap.cps.ncmp.events.avcsubscription1_0_0.ncmp_to_dmi.SubscriptionEvent> deserializedCloudEvent = CloudEventUtils
194 .mapData(cloudEvent, PojoCloudEventDataMapper.from(objectMapper,
195 org.onap.cps.ncmp.events.avcsubscription1_0_0.ncmp_to_dmi.SubscriptionEvent.class));
196 if (deserializedCloudEvent == null) {
199 return deserializedCloudEvent.getValue();