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