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.onap.cps.ncmp.api.impl.events.EventsPublisher
26 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle
27 import org.onap.cps.ncmp.api.inventory.InventoryPersistence
28 import org.onap.cps.ncmp.api.kafka.MessagingBaseSpec
29 import org.onap.cps.ncmp.event.model.SubscriptionEvent
30 import org.onap.cps.ncmp.utils.TestUtils
31 import org.onap.cps.spi.exceptions.OperationNotYetSupportedException
32 import org.onap.cps.utils.JsonObjectMapper
33 import org.spockframework.spring.SpringBean
34 import org.springframework.beans.factory.annotation.Autowired
35 import org.springframework.boot.test.context.SpringBootTest
36 import spock.util.concurrent.BlockingVariable
38 @SpringBootTest(classes = [ObjectMapper, JsonObjectMapper, SubscriptionEventForwarder])
39 class SubscriptionEventForwarderSpec extends MessagingBaseSpec {
42 SubscriptionEventForwarder objectUnderTest
45 InventoryPersistence mockInventoryPersistence = Mock(InventoryPersistence)
47 EventsPublisher<SubscriptionEvent> mockSubscriptionEventPublisher = Mock(EventsPublisher<SubscriptionEvent>)
49 IMap<String, Set<String>> mockForwardedSubscriptionEventCache = Mock(IMap<String, Set<String>>)
52 JsonObjectMapper jsonObjectMapper
54 def 'Forward valid CM create subscription and simulate timeout where #scenario'() {
56 def jsonData = TestUtils.getResourceFileContent('avcSubscriptionCreationEvent.json')
57 def testEventSent = jsonObjectMapper.convertJsonString(jsonData, SubscriptionEvent.class)
58 and: 'the InventoryPersistence returns private properties for the supplied CM Handles'
59 1 * mockInventoryPersistence.getYangModelCmHandles(["CMHandle1", "CMHandle2", "CMHandle3"]) >> [
60 createYangModelCmHandleWithDmiProperty(1, 1,"shape","circle"),
61 createYangModelCmHandleWithDmiProperty(2, 1,"shape","square"),
62 createYangModelCmHandleWithDmiProperty(3, 2,"shape","triangle")
64 and: 'the thread creation delay is reduced to 2 seconds for testing'
65 objectUnderTest.dmiResponseTimeoutInMs = 2000
66 and: 'a Blocking Variable is used for the Asynchronous call with a timeout of 5 seconds'
67 def block = new BlockingVariable<Object>(5)
68 when: 'the valid event is forwarded'
69 objectUnderTest.forwardCreateSubscriptionEvent(testEventSent)
70 then: 'An asynchronous call is made to the blocking variable'
72 then: 'the event is added to the forwarded subscription event cache'
73 1 * mockForwardedSubscriptionEventCache.put("SCO-9989752cm-subscription-001", ["DMIName1", "DMIName2"] as Set)
74 and: 'the event is forwarded twice with the CMHandle private properties and provides a valid listenable future'
75 1 * mockSubscriptionEventPublisher.publishEvent("ncmp-dmi-cm-avc-subscription-DMIName1", "SCO-9989752-cm-subscription-001-DMIName1",
76 subscriptionEvent -> {
77 Map targets = subscriptionEvent.getEvent().getPredicates().getTargets().get(0)
78 targets["CMHandle1"] == ["shape":"circle"]
79 targets["CMHandle2"] == ["shape":"square"]
82 1 * mockSubscriptionEventPublisher.publishEvent("ncmp-dmi-cm-avc-subscription-DMIName2", "SCO-9989752-cm-subscription-001-DMIName2",
83 subscriptionEvent -> {
84 Map targets = subscriptionEvent.getEvent().getPredicates().getTargets().get(0)
85 targets["CMHandle3"] == ["shape":"triangle"]
88 and: 'a separate thread has been created where the map is polled'
89 1 * mockForwardedSubscriptionEventCache.containsKey("SCO-9989752cm-subscription-001") >> true
90 1 * mockForwardedSubscriptionEventCache.get(_) >> (DMINamesInMap)
91 and: 'the subscription id is removed from the event cache map returning the asynchronous blocking variable'
92 1 * mockForwardedSubscriptionEventCache.remove("SCO-9989752cm-subscription-001") >> {block.set(_)}
94 scenario | DMINamesInMap
95 'there are dmis which have not responded' | ["DMIName1", "DMIName2"] as Set
96 'all dmis have responded ' | [] as Set
99 def 'Forward CM create subscription where target CM Handles are #scenario'() {
101 def jsonData = TestUtils.getResourceFileContent('avcSubscriptionCreationEvent.json')
102 def testEventSent = jsonObjectMapper.convertJsonString(jsonData, SubscriptionEvent.class)
103 and: 'the target CMHandles are set to #scenario'
104 testEventSent.getEvent().getPredicates().setTargets(invalidTargets)
105 when: 'the event is forwarded'
106 objectUnderTest.forwardCreateSubscriptionEvent(testEventSent)
107 then: 'an operation not yet supported exception is thrown'
108 thrown(OperationNotYetSupportedException)
110 scenario | invalidTargets
113 'wildcard' | ['CMHandle*']
116 def 'Forward valid CM create subscription where targets are not associated to any existing CMHandles'() {
118 def jsonData = TestUtils.getResourceFileContent('avcSubscriptionCreationEvent.json')
119 def testEventSent = jsonObjectMapper.convertJsonString(jsonData, SubscriptionEvent.class)
120 and: 'the InventoryPersistence returns no private properties for the supplied CM Handles'
121 1 * mockInventoryPersistence.getYangModelCmHandles(["CMHandle1", "CMHandle2", "CMHandle3"]) >> []
122 and: 'the thread creation delay is reduced to 2 seconds for testing'
123 objectUnderTest.dmiResponseTimeoutInMs = 2000
124 and: 'a Blocking Variable is used for the Asynchronous call with a timeout of 5 seconds'
125 def block = new BlockingVariable<Object>(5)
126 when: 'the valid event is forwarded'
127 objectUnderTest.forwardCreateSubscriptionEvent(testEventSent)
128 then: 'the event is not added to the forwarded subscription event cache'
129 0 * mockForwardedSubscriptionEventCache.put("SCO-9989752cm-subscription-001", ["DMIName1", "DMIName2"] as Set)
130 and: 'the event is forwarded twice with the CMHandle private properties and provides a valid listenable future'
131 0 * mockSubscriptionEventPublisher.publishEvent("ncmp-dmi-cm-avc-subscription-DMIName1", "SCO-9989752-cm-subscription-001-DMIName1",
132 subscriptionEvent -> {
133 Map targets = subscriptionEvent.getEvent().getPredicates().getTargets().get(0)
134 targets["CMHandle1"] == ["shape":"circle"]
135 targets["CMHandle2"] == ["shape":"square"]
138 0 * mockSubscriptionEventPublisher.publishEvent("ncmp-dmi-cm-avc-subscription-DMIName2", "SCO-9989752-cm-subscription-001-DMIName2",
139 subscriptionEvent -> {
140 Map targets = subscriptionEvent.getEvent().getPredicates().getTargets().get(0)
141 targets["CMHandle3"] == ["shape":"triangle"]
144 and: 'a separate thread has been created where the map is polled'
145 0 * mockForwardedSubscriptionEventCache.containsKey("SCO-9989752cm-subscription-001") >> true
146 0 * mockForwardedSubscriptionEventCache.get(_)
147 and: 'the subscription id is removed from the event cache map returning the asynchronous blocking variable'
148 0 * mockForwardedSubscriptionEventCache.remove("SCO-9989752cm-subscription-001") >> {block.set(_)}
151 static def createYangModelCmHandleWithDmiProperty(id, dmiId,propertyName, propertyValue) {
152 return new YangModelCmHandle(id:"CMHandle" + id, dmiDataServiceName: "DMIName" + dmiId, dmiProperties: [new YangModelCmHandle.Property(propertyName,propertyValue)])