[CPS] Re-structuring the packages for better understanding
[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 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
34
35 @SpringBootTest(classes = [ObjectMapper, JsonObjectMapper])
36 class SubscriptionEventForwarderSpec extends MessagingBaseSpec {
37
38     def mockInventoryPersistence = Mock(InventoryPersistence)
39     def mockSubscriptionEventPublisher = Mock(EventsPublisher<SubscriptionEvent>)
40     def objectUnderTest = new SubscriptionEventForwarder(mockInventoryPersistence, mockSubscriptionEventPublisher)
41
42     @Autowired
43     JsonObjectMapper jsonObjectMapper
44
45     def 'Forward valid CM create subscription'() {
46         given: 'an event'
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")
54             ]
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"]
63                 }
64             )
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"]
69                 }
70             )
71     }
72
73     def 'Forward CM create subscription where target CM Handles are #scenario'() {
74         given: 'an event'
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)
83         where:
84             scenario   | invalidTargets
85             'null'     | null
86             'empty'    | []
87             'wildcard' | ['CMHandle*']
88     }
89
90     static def createYangModelCmHandleWithDmiProperty(id, dmiId,propertyName, propertyValue) {
91         return new YangModelCmHandle(id:"CMHandle" + id, dmiDataServiceName: "DMIName" + dmiId, dmiProperties: [new YangModelCmHandle.Property(propertyName,propertyValue)])
92     }
93
94 }