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 org.onap.cps.ncmp.api.impl.events.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
35 @SpringBootTest(classes = [ObjectMapper, JsonObjectMapper])
36 class SubscriptionEventForwarderSpec extends MessagingBaseSpec {
38 def mockInventoryPersistence = Mock(InventoryPersistence)
39 def mockSubscriptionEventPublisher = Mock(EventsPublisher<SubscriptionEvent>)
40 def objectUnderTest = new SubscriptionEventForwarder(mockInventoryPersistence, mockSubscriptionEventPublisher)
43 JsonObjectMapper jsonObjectMapper
45 def 'Forward valid CM create subscription'() {
47 def jsonData = TestUtils.getResourceFileContent('avcSubscriptionCreationEvent.json')
48 def testEventSent = jsonObjectMapper.convertJsonString(jsonData, SubscriptionEvent.class)
49 and: 'the InventoryPersistence returns private properties for the supplied CM Handles'
50 1 * mockInventoryPersistence.getYangModelCmHandles(["CMHandle1", "CMHandle2", "CMHandle3"]) >> [
51 createYangModelCmHandleWithDmiProperty(1, 1,"shape","circle"),
52 createYangModelCmHandleWithDmiProperty(2, 1,"shape","square"),
53 createYangModelCmHandleWithDmiProperty(3, 2,"shape","triangle")
55 when: 'the valid event is forwarded'
56 objectUnderTest.forwardCreateSubscriptionEvent(testEventSent)
57 then: 'the event is forwarded twice with the CMHandle private properties and provides a valid listenable future'
58 1 * mockSubscriptionEventPublisher.publishEvent("ncmp-dmi-cm-avc-subscription-DMIName1", "SCO-9989752-cm-subscription-001-DMIName1",
59 subscriptionEvent -> {
60 Map targets = subscriptionEvent.getEvent().getPredicates().getTargets().get(0)
61 targets["CMHandle1"] == ["shape":"circle"]
62 targets["CMHandle2"] == ["shape":"square"]
65 1 * mockSubscriptionEventPublisher.publishEvent("ncmp-dmi-cm-avc-subscription-DMIName2", "SCO-9989752-cm-subscription-001-DMIName2",
66 subscriptionEvent -> {
67 Map targets = subscriptionEvent.getEvent().getPredicates().getTargets().get(0)
68 targets["CMHandle3"] == ["shape":"triangle"]
73 def 'Forward CM create subscription where target CM Handles are #scenario'() {
75 def jsonData = TestUtils.getResourceFileContent('avcSubscriptionCreationEvent.json')
76 def testEventSent = jsonObjectMapper.convertJsonString(jsonData, SubscriptionEvent.class)
77 and: 'the target CMHandles are set to #scenario'
78 testEventSent.getEvent().getPredicates().setTargets(invalidTargets)
79 when: 'the event is forwarded'
80 objectUnderTest.forwardCreateSubscriptionEvent(testEventSent)
81 then: 'an operation not yet supported exception is thrown'
82 thrown(OperationNotYetSupportedException)
84 scenario | invalidTargets
87 'wildcard' | ['CMHandle*']
90 static def createYangModelCmHandleWithDmiProperty(id, dmiId,propertyName, propertyValue) {
91 return new YangModelCmHandle(id:"CMHandle" + id, dmiDataServiceName: "DMIName" + dmiId, dmiProperties: [new YangModelCmHandle.Property(propertyName,propertyValue)])