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.event.avc
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;
37 @SpringBootTest(classes = [ObjectMapper, JsonObjectMapper])
38 class SubscriptionEventForwarderSpec extends MessagingBaseSpec {
40 def mockInventoryPersistence = Mock(InventoryPersistence)
41 def mockSubscriptionEventPublisher = Mock(EventsPublisher<SubscriptionEvent>)
42 def objectUnderTest = new SubscriptionEventForwarder(mockInventoryPersistence, mockSubscriptionEventPublisher)
45 JsonObjectMapper jsonObjectMapper
47 def 'Forward valid CM create subscription'() {
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")
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"]
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"]
75 def 'Forward CM create subscription where target CM Handles are #scenario'() {
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)
86 scenario | invalidTargets
89 'wildcard' | ['CMHandle*']
92 static def createYangModelCmHandleWithDmiProperty(id, dmiId,propertyName, propertyValue) {
93 return new YangModelCmHandle(id:"CMHandle" + id, dmiDataServiceName: "DMIName" + dmiId, dmiProperties: [new YangModelCmHandle.Property(propertyName,propertyValue)])