457eb6fa995b44c3b6ed64a841f00816ab568af4
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / impl / events / avcsubscription / SubscriptionEventForwarderSpec.groovy
1 /*
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
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.ncmp.api.impl.events.avcsubscription
22
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
37
38 @SpringBootTest(classes = [ObjectMapper, JsonObjectMapper, SubscriptionEventForwarder])
39 class SubscriptionEventForwarderSpec extends MessagingBaseSpec {
40
41     @Autowired
42     SubscriptionEventForwarder objectUnderTest
43
44     @SpringBean
45     InventoryPersistence mockInventoryPersistence = Mock(InventoryPersistence)
46     @SpringBean
47     EventsPublisher<SubscriptionEvent> mockSubscriptionEventPublisher = Mock(EventsPublisher<SubscriptionEvent>)
48     @SpringBean
49     IMap<String, Set<String>> mockForwardedSubscriptionEventCache = Mock(IMap<String, Set<String>>)
50
51     @Autowired
52     JsonObjectMapper jsonObjectMapper
53
54     def 'Forward valid CM create subscription and simulate timeout where #scenario'() {
55         given: 'an event'
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")
63             ]
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'
71             block.get()
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"]
80                 }
81             )
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"]
86                 }
87             )
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(_)}
93         where:
94             scenario                                  | DMINamesInMap
95             'there are dmis which have not responded' | ["DMIName1", "DMIName2"] as Set
96             'all dmis have responded '                | [] as Set
97     }
98
99     def 'Forward CM create subscription where target CM Handles are #scenario'() {
100         given: 'an event'
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)
109         where:
110             scenario   | invalidTargets
111             'null'     | null
112             'empty'    | []
113             'wildcard' | ['CMHandle*']
114     }
115
116     static def createYangModelCmHandleWithDmiProperty(id, dmiId,propertyName, propertyValue) {
117         return new YangModelCmHandle(id:"CMHandle" + id, dmiDataServiceName: "DMIName" + dmiId, dmiProperties: [new YangModelCmHandle.Property(propertyName,propertyValue)])
118     }
119
120 }