From 7549776fb3b82de2d14ae60e1bddf74bf62f8f79 Mon Sep 17 00:00:00 2001
From: lukegleeson <luke.gleeson@est.tech>
Date: Thu, 23 Feb 2023 11:43:13 +0000
Subject: Forward Subscription Information to DMI Plugin(s)

Note: Implemented common EventsPublisher class

Issue-ID: CPS-1431
Signed-off-by: lukegleeson <luke.gleeson@est.tech>
Change-Id: I292a95f2c990a140f5fd63622ca4eba3f8284b9e
---
 .../event/avc/SubscriptionEventConsumerSpec.groovy | 55 +++++++++++--
 .../avc/SubscriptionEventForwarderSpec.groovy      | 96 ++++++++++++++++++++++
 .../impl/event/lcm/LcmEventsPublisherSpec.groovy   |  7 +-
 .../api/impl/event/lcm/LcmEventsServiceSpec.groovy |  5 +-
 .../cps/ncmp/api/kafka/MessagingBaseSpec.groovy    |  4 +-
 5 files changed, 153 insertions(+), 14 deletions(-)
 create mode 100644 cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/event/avc/SubscriptionEventForwarderSpec.groovy

(limited to 'cps-ncmp-service/src/test/groovy/org')

diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/event/avc/SubscriptionEventConsumerSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/event/avc/SubscriptionEventConsumerSpec.groovy
index 20d60e3963..7a9dadebc5 100644
--- a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/event/avc/SubscriptionEventConsumerSpec.groovy
+++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/event/avc/SubscriptionEventConsumerSpec.groovy
@@ -1,6 +1,6 @@
 /*
  * ============LICENSE_START=======================================================
- * Copyright (c) 2022 Nordix Foundation.
+ * Copyright (c) 2022-2023 Nordix Foundation.
  *  ================================================================================
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
@@ -24,29 +24,68 @@ import com.fasterxml.jackson.databind.ObjectMapper
 import org.onap.cps.ncmp.api.kafka.MessagingBaseSpec
 import org.onap.cps.ncmp.event.model.SubscriptionEvent
 import org.onap.cps.ncmp.utils.TestUtils
+import org.onap.cps.spi.exceptions.OperationNotYetSupportedException
 import org.onap.cps.utils.JsonObjectMapper
 import org.springframework.beans.factory.annotation.Autowired
 import org.springframework.boot.test.context.SpringBootTest
 
-@SpringBootTest(classes = [SubscriptionEventConsumer, ObjectMapper, JsonObjectMapper])
+@SpringBootTest(classes = [ObjectMapper, JsonObjectMapper])
 class SubscriptionEventConsumerSpec extends MessagingBaseSpec {
 
-    def objectUnderTest = new SubscriptionEventConsumer()
+    def subscriptionEventForwarder = Mock(SubscriptionEventForwarder)
+    def objectUnderTest = new SubscriptionEventConsumer(subscriptionEventForwarder)
 
     @Autowired
     JsonObjectMapper jsonObjectMapper
 
-    def 'Consume valid message'() {
+    def 'Consume and forward valid CM create message'() {
+        given: 'an event with data category CM'
+            def jsonData = TestUtils.getResourceFileContent('avcSubscriptionCreationEvent.json')
+            def testEventSent = jsonObjectMapper.convertJsonString(jsonData, SubscriptionEvent.class)
+        and: 'notifications are enabled'
+            objectUnderTest.notificationFeatureEnabled = true
+        when: 'the valid event is consumed'
+            objectUnderTest.consumeSubscriptionEvent(testEventSent)
+        then: 'the event is forwarded'
+            1 * subscriptionEventForwarder.forwardCreateSubscriptionEvent(testEventSent)
+    }
+
+    def 'Consume valid CM create message where notifications are disabled'() {
+        given: 'an event with data category CM'
+            def jsonData = TestUtils.getResourceFileContent('avcSubscriptionCreationEvent.json')
+            def testEventSent = jsonObjectMapper.convertJsonString(jsonData, SubscriptionEvent.class)
+        and: 'notifications are disabled'
+            objectUnderTest.notificationFeatureEnabled = false
+        when: 'the valid event is consumed'
+            objectUnderTest.consumeSubscriptionEvent(testEventSent)
+        then: 'the event is forwarded'
+            0 * subscriptionEventForwarder.forwardCreateSubscriptionEvent(testEventSent)
+    }
+
+    def 'Consume valid FM message'() {
         given: 'an event'
             def jsonData = TestUtils.getResourceFileContent('avcSubscriptionCreationEvent.json')
             def testEventSent = jsonObjectMapper.convertJsonString(jsonData, SubscriptionEvent.class)
-        and: 'dataCategory is set'
-            testEventSent.getEvent().getDataType().setDataCategory(dataCategory)
+        and: 'dataCategory is set to FM'
+            testEventSent.getEvent().getDataType().setDataCategory("FM")
         when: 'the valid event is consumed'
             objectUnderTest.consumeSubscriptionEvent(testEventSent)
         then: 'no exception is thrown'
             noExceptionThrown()
-        where: 'data category is changed'
-            dataCategory << [ 'CM' , 'FM' ]
+        and: 'No event is forwarded'
+            0 * subscriptionEventForwarder.forwardCreateSubscriptionEvent(*_)
+    }
+
+    def 'Consume event with wrong datastore causes an exception'() {
+        given: 'an event'
+            def jsonData = TestUtils.getResourceFileContent('avcSubscriptionCreationEvent.json')
+            def testEventSent = jsonObjectMapper.convertJsonString(jsonData, SubscriptionEvent.class)
+        and: 'datastore is set to a non passthrough datastore'
+            testEventSent.getEvent().getPredicates().setDatastore("operational")
+        when: 'the valid event is consumed'
+            objectUnderTest.consumeSubscriptionEvent(testEventSent)
+        then: 'an operation not yet supported exception is thrown'
+            thrown(OperationNotYetSupportedException)
     }
+
 }
diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/event/avc/SubscriptionEventForwarderSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/event/avc/SubscriptionEventForwarderSpec.groovy
new file mode 100644
index 0000000000..f9e801ddbb
--- /dev/null
+++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/event/avc/SubscriptionEventForwarderSpec.groovy
@@ -0,0 +1,96 @@
+/*
+ * ============LICENSE_START=======================================================
+ * Copyright (c) 2023 Nordix Foundation.
+ *  ================================================================================
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an 'AS IS' BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ *  SPDX-License-Identifier: Apache-2.0
+ *  ============LICENSE_END=========================================================
+ */
+
+package org.onap.cps.ncmp.api.impl.event.avc
+
+import com.fasterxml.jackson.databind.ObjectMapper
+import org.onap.cps.ncmp.api.impl.event.EventsPublisher
+import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle
+import org.onap.cps.ncmp.api.inventory.InventoryPersistence
+import org.onap.cps.ncmp.api.kafka.MessagingBaseSpec
+import org.onap.cps.ncmp.event.model.SubscriptionEvent
+import org.onap.cps.ncmp.utils.TestUtils
+import org.onap.cps.spi.exceptions.OperationNotYetSupportedException
+import org.onap.cps.utils.JsonObjectMapper
+import org.springframework.beans.factory.annotation.Autowired
+import org.springframework.boot.test.context.SpringBootTest
+import org.springframework.kafka.core.KafkaTemplate
+import org.springframework.util.concurrent.ListenableFuture;
+
+@SpringBootTest(classes = [ObjectMapper, JsonObjectMapper])
+class SubscriptionEventForwarderSpec extends MessagingBaseSpec {
+
+    def mockInventoryPersistence = Mock(InventoryPersistence)
+    def mockSubscriptionEventPublisher = Mock(EventsPublisher<SubscriptionEvent>)
+    def objectUnderTest = new SubscriptionEventForwarder(mockInventoryPersistence, mockSubscriptionEventPublisher)
+
+    @Autowired
+    JsonObjectMapper jsonObjectMapper
+
+    def 'Forward valid CM create subscription'() {
+        given: 'an event'
+            def jsonData = TestUtils.getResourceFileContent('avcSubscriptionCreationEvent.json')
+            def testEventSent = jsonObjectMapper.convertJsonString(jsonData, SubscriptionEvent.class)
+        and: 'the InventoryPersistence returns private properties for the supplied CM Handles'
+            1 * mockInventoryPersistence.getYangModelCmHandles(["CMHandle1", "CMHandle2", "CMHandle3"]) >> [
+                createYangModelCmHandleWithDmiProperty(1, 1,"shape","circle"),
+                createYangModelCmHandleWithDmiProperty(2, 1,"shape","square"),
+                createYangModelCmHandleWithDmiProperty(3, 2,"shape","triangle")
+            ]
+        when: 'the valid event is forwarded'
+            objectUnderTest.forwardCreateSubscriptionEvent(testEventSent)
+        then: 'the event is forwarded twice with the CMHandle private properties and provides a valid listenable future'
+            1 * mockSubscriptionEventPublisher.publishEvent("ncmp-dmi-cm-avc-subscription-DMIName1", "SCO-9989752-cm-subscription-001-DMIName1",
+                subscriptionEvent -> {
+                    Map targets = subscriptionEvent.getEvent().getPredicates().getTargets().get(0)
+                    targets["CMHandle1"] == ["shape":"circle"]
+                    targets["CMHandle2"] == ["shape":"square"]
+                }
+            )
+            1 * mockSubscriptionEventPublisher.publishEvent("ncmp-dmi-cm-avc-subscription-DMIName2", "SCO-9989752-cm-subscription-001-DMIName2",
+                subscriptionEvent -> {
+                    Map targets = subscriptionEvent.getEvent().getPredicates().getTargets().get(0)
+                    targets["CMHandle3"] == ["shape":"triangle"]
+                }
+            )
+    }
+
+    def 'Forward CM create subscription where target CM Handles are #scenario'() {
+        given: 'an event'
+            def jsonData = TestUtils.getResourceFileContent('avcSubscriptionCreationEvent.json')
+            def testEventSent = jsonObjectMapper.convertJsonString(jsonData, SubscriptionEvent.class)
+        and: 'the target CMHandles are set to #scenario'
+            testEventSent.getEvent().getPredicates().setTargets(invalidTargets)
+        when: 'the event is forwarded'
+            objectUnderTest.forwardCreateSubscriptionEvent(testEventSent)
+        then: 'an operation not yet supported exception is thrown'
+            thrown(OperationNotYetSupportedException)
+        where:
+            scenario   | invalidTargets
+            'null'     | null
+            'empty'    | []
+            'wildcard' | ['CMHandle*']
+    }
+
+    static def createYangModelCmHandleWithDmiProperty(id, dmiId,propertyName, propertyValue) {
+        return new YangModelCmHandle(id:"CMHandle" + id, dmiDataServiceName: "DMIName" + dmiId, dmiProperties: [new YangModelCmHandle.Property(propertyName,propertyValue)])
+    }
+
+}
diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/event/lcm/LcmEventsPublisherSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/event/lcm/LcmEventsPublisherSpec.groovy
index 61bf33d199..f5b58a7536 100644
--- a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/event/lcm/LcmEventsPublisherSpec.groovy
+++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/event/lcm/LcmEventsPublisherSpec.groovy
@@ -1,6 +1,6 @@
 /*
  * ============LICENSE_START=======================================================
- * Copyright (C) 2022 Nordix Foundation
+ * Copyright (C) 2022-2023 Nordix Foundation
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -22,6 +22,7 @@ package org.onap.cps.ncmp.api.impl.event.lcm
 
 import com.fasterxml.jackson.databind.ObjectMapper
 import org.apache.kafka.clients.consumer.KafkaConsumer
+import org.onap.cps.ncmp.api.impl.event.EventsPublisher
 import org.onap.cps.ncmp.api.kafka.MessagingBaseSpec
 import org.onap.cps.ncmp.utils.TestUtils
 import org.onap.cps.utils.JsonObjectMapper
@@ -35,7 +36,7 @@ import org.testcontainers.spock.Testcontainers
 
 import java.time.Duration
 
-@SpringBootTest(classes = [LcmEventsPublisher, ObjectMapper, JsonObjectMapper])
+@SpringBootTest(classes = [EventsPublisher, ObjectMapper, JsonObjectMapper])
 @Testcontainers
 @DirtiesContext
 class LcmEventsPublisherSpec extends MessagingBaseSpec {
@@ -45,7 +46,7 @@ class LcmEventsPublisherSpec extends MessagingBaseSpec {
     def testTopic = 'ncmp-events-test'
 
     @SpringBean
-    LcmEventsPublisher lcmEventsPublisher = new LcmEventsPublisher(kafkaTemplate)
+    EventsPublisher<LcmEvent> lcmEventsPublisher = new EventsPublisher(kafkaTemplate)
 
     @Autowired
     JsonObjectMapper jsonObjectMapper
diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/event/lcm/LcmEventsServiceSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/event/lcm/LcmEventsServiceSpec.groovy
index ef399e1c65..4c632ddf04 100644
--- a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/event/lcm/LcmEventsServiceSpec.groovy
+++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/event/lcm/LcmEventsServiceSpec.groovy
@@ -1,6 +1,6 @@
 /*
  * ============LICENSE_START=======================================================
- * Copyright (C) 2022 Nordix Foundation
+ * Copyright (C) 2022-2023 Nordix Foundation
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -20,13 +20,14 @@
 
 package org.onap.cps.ncmp.api.impl.event.lcm
 
+import org.onap.cps.ncmp.api.impl.event.EventsPublisher
 import org.onap.ncmp.cmhandle.event.lcm.LcmEvent
 import org.springframework.kafka.KafkaException
 import spock.lang.Specification
 
 class LcmEventsServiceSpec extends Specification {
 
-    def mockLcmEventsPublisher = Mock(LcmEventsPublisher)
+    def mockLcmEventsPublisher = Mock(EventsPublisher)
 
     def objectUnderTest = new LcmEventsService(mockLcmEventsPublisher)
 
diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/kafka/MessagingBaseSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/kafka/MessagingBaseSpec.groovy
index bb0ce8745d..337178e128 100644
--- a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/kafka/MessagingBaseSpec.groovy
+++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/kafka/MessagingBaseSpec.groovy
@@ -22,6 +22,7 @@ package org.onap.cps.ncmp.api.kafka
 
 import org.apache.kafka.common.serialization.StringDeserializer
 import org.apache.kafka.common.serialization.StringSerializer
+import org.spockframework.spring.SpringBean
 import org.springframework.kafka.core.DefaultKafkaProducerFactory
 import org.springframework.kafka.core.KafkaTemplate
 import org.springframework.kafka.support.serializer.JsonSerializer
@@ -62,7 +63,8 @@ class MessagingBaseSpec extends Specification {
         ]
     }
 
-    def kafkaTemplate = new KafkaTemplate<>(new DefaultKafkaProducerFactory<Integer, String>(producerConfigProperties()))
+    @SpringBean
+    KafkaTemplate kafkaTemplate = new KafkaTemplate<>(new DefaultKafkaProducerFactory<Integer, String>(producerConfigProperties()))
 
     @DynamicPropertySource
     static void registerKafkaProperties(DynamicPropertyRegistry dynamicPropertyRegistry) {
-- 
cgit