f9e801ddbbbe1e7d0790421aac80248285ffddfd
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / impl / event / avc / 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.event.avc
22
23 import com.fasterxml.jackson.databind.ObjectMapper
24 import org.onap.cps.ncmp.api.impl.event.EventsPublisher
25 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle
26 import org.onap.cps.ncmp.api.inventory.InventoryPersistence
27 import org.onap.cps.ncmp.api.kafka.MessagingBaseSpec
28 import org.onap.cps.ncmp.event.model.SubscriptionEvent
29 import org.onap.cps.ncmp.utils.TestUtils
30 import org.onap.cps.spi.exceptions.OperationNotYetSupportedException
31 import org.onap.cps.utils.JsonObjectMapper
32 import org.springframework.beans.factory.annotation.Autowired
33 import org.springframework.boot.test.context.SpringBootTest
34 import org.springframework.kafka.core.KafkaTemplate
35 import org.springframework.util.concurrent.ListenableFuture;
36
37 @SpringBootTest(classes = [ObjectMapper, JsonObjectMapper])
38 class SubscriptionEventForwarderSpec extends MessagingBaseSpec {
39
40     def mockInventoryPersistence = Mock(InventoryPersistence)
41     def mockSubscriptionEventPublisher = Mock(EventsPublisher<SubscriptionEvent>)
42     def objectUnderTest = new SubscriptionEventForwarder(mockInventoryPersistence, mockSubscriptionEventPublisher)
43
44     @Autowired
45     JsonObjectMapper jsonObjectMapper
46
47     def 'Forward valid CM create subscription'() {
48         given: 'an event'
49             def jsonData = TestUtils.getResourceFileContent('avcSubscriptionCreationEvent.json')
50             def testEventSent = jsonObjectMapper.convertJsonString(jsonData, SubscriptionEvent.class)
51         and: 'the InventoryPersistence returns private properties for the supplied CM Handles'
52             1 * mockInventoryPersistence.getYangModelCmHandles(["CMHandle1", "CMHandle2", "CMHandle3"]) >> [
53                 createYangModelCmHandleWithDmiProperty(1, 1,"shape","circle"),
54                 createYangModelCmHandleWithDmiProperty(2, 1,"shape","square"),
55                 createYangModelCmHandleWithDmiProperty(3, 2,"shape","triangle")
56             ]
57         when: 'the valid event is forwarded'
58             objectUnderTest.forwardCreateSubscriptionEvent(testEventSent)
59         then: 'the event is forwarded twice with the CMHandle private properties and provides a valid listenable future'
60             1 * mockSubscriptionEventPublisher.publishEvent("ncmp-dmi-cm-avc-subscription-DMIName1", "SCO-9989752-cm-subscription-001-DMIName1",
61                 subscriptionEvent -> {
62                     Map targets = subscriptionEvent.getEvent().getPredicates().getTargets().get(0)
63                     targets["CMHandle1"] == ["shape":"circle"]
64                     targets["CMHandle2"] == ["shape":"square"]
65                 }
66             )
67             1 * mockSubscriptionEventPublisher.publishEvent("ncmp-dmi-cm-avc-subscription-DMIName2", "SCO-9989752-cm-subscription-001-DMIName2",
68                 subscriptionEvent -> {
69                     Map targets = subscriptionEvent.getEvent().getPredicates().getTargets().get(0)
70                     targets["CMHandle3"] == ["shape":"triangle"]
71                 }
72             )
73     }
74
75     def 'Forward CM create subscription where target CM Handles are #scenario'() {
76         given: 'an event'
77             def jsonData = TestUtils.getResourceFileContent('avcSubscriptionCreationEvent.json')
78             def testEventSent = jsonObjectMapper.convertJsonString(jsonData, SubscriptionEvent.class)
79         and: 'the target CMHandles are set to #scenario'
80             testEventSent.getEvent().getPredicates().setTargets(invalidTargets)
81         when: 'the event is forwarded'
82             objectUnderTest.forwardCreateSubscriptionEvent(testEventSent)
83         then: 'an operation not yet supported exception is thrown'
84             thrown(OperationNotYetSupportedException)
85         where:
86             scenario   | invalidTargets
87             'null'     | null
88             'empty'    | []
89             'wildcard' | ['CMHandle*']
90     }
91
92     static def createYangModelCmHandleWithDmiProperty(id, dmiId,propertyName, propertyValue) {
93         return new YangModelCmHandle(id:"CMHandle" + id, dmiDataServiceName: "DMIName" + dmiId, dmiProperties: [new YangModelCmHandle.Property(propertyName,propertyValue)])
94     }
95
96 }