Conditional cps change events
[cps.git] / cps-service / src / test / groovy / org / onap / cps / events / CpsDataUpdateEventsServiceSpec.groovy
index 24b9ab5..1184264 100644 (file)
@@ -1,6 +1,7 @@
 /*
  * ============LICENSE_START=======================================================
  * Copyright (C) 2024 TechMahindra Ltd.
+ * Copyright (C) 2024 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -29,7 +30,6 @@ import io.cloudevents.CloudEvent
 import io.cloudevents.core.CloudEventUtils
 import io.cloudevents.jackson.PojoCloudEventDataMapper
 import org.onap.cps.events.model.CpsDataUpdatedEvent
-import org.onap.cps.events.model.Data
 import org.onap.cps.spi.model.Anchor
 import org.onap.cps.utils.JsonObjectMapper
 import org.springframework.test.context.ContextConfiguration
@@ -40,7 +40,6 @@ import java.time.OffsetDateTime
 @ContextConfiguration(classes = [ObjectMapper, JsonObjectMapper])
 class CpsDataUpdateEventsServiceSpec extends Specification {
     def mockEventsPublisher = Mock(EventsPublisher)
-    def notificationsEnabled = true
     def objectMapper = new ObjectMapper();
 
     def objectUnderTest = new CpsDataUpdateEventsService(mockEventsPublisher)
@@ -52,6 +51,8 @@ class CpsDataUpdateEventsServiceSpec extends Specification {
             def observedTimestamp = OffsetDateTime.now()
         and: 'notificationsEnabled is #notificationsEnabled and it will be true as default'
             objectUnderTest.notificationsEnabled = true
+        and: 'cpsChangeEventNotificationsEnabled is also true'
+            objectUnderTest.cpsChangeEventNotificationsEnabled = true
         when: 'service is called to publish data update event'
             objectUnderTest.topicName = "cps-core-event"
             objectUnderTest.publishCpsDataUpdateEvent(anchor, xpath, operation, observedTimestamp)
@@ -79,18 +80,27 @@ class CpsDataUpdateEventsServiceSpec extends Specification {
         'non root node xpath and delete operation' | '/test/path' | DELETE              || UPDATE
     }
 
-    def 'publish cps update event when notification service is disabled'() {
+    def 'publish cps update event when #scenario'() {
         given: 'an anchor, operation and observed timestamp'
             def anchor = new Anchor('anchor01', 'dataspace01', 'schema01');
             def operation = CREATE
             def observedTimestamp = OffsetDateTime.now()
-        and: 'notificationsEnabled is false'
-            objectUnderTest.notificationsEnabled = false
+        and: 'notificationsEnabled is #notificationsEnabled'
+            objectUnderTest.notificationsEnabled = notificationsEnabled
+        and: 'cpsChangeEventNotificationsEnabled is #cpsChangeEventNotificationsEnabled'
+            objectUnderTest.cpsChangeEventNotificationsEnabled = cpsChangeEventNotificationsEnabled
         when: 'service is called to publish data update event'
             objectUnderTest.topicName = "cps-core-event"
             objectUnderTest.publishCpsDataUpdateEvent(anchor, '/', operation, observedTimestamp)
         then: 'the event contains the required attributes'
-            0 * mockEventsPublisher.publishCloudEvent('cps-core-event', 'dataspace01:anchor01', _)
+            expectedCallToPublisher * mockEventsPublisher.publishCloudEvent('cps-core-event', 'dataspace01:anchor01', _)
+        where: 'below scenarios are present'
+            scenario                                     | notificationsEnabled | cpsChangeEventNotificationsEnabled || expectedCallToPublisher
+            'both notifications enabled'                 | true                 | true                               || 1
+            'both notifications disabled'                 | false                | false                              || 0
+            'only CPS change event notification enabled' | false                | true                               || 0
+            'only overall notification enabled'          | true                 | false                              || 0
+
     }
 
     def 'publish cps update event when no timestamp provided'() {
@@ -100,6 +110,8 @@ class CpsDataUpdateEventsServiceSpec extends Specification {
             def observedTimestamp = null
         and: 'notificationsEnabled is true'
             objectUnderTest.notificationsEnabled = true
+        and: 'cpsChangeEventNotificationsEnabled is true'
+            objectUnderTest.cpsChangeEventNotificationsEnabled = true
         when: 'service is called to publish data update event'
             objectUnderTest.topicName = "cps-core-event"
             objectUnderTest.publishCpsDataUpdateEvent(anchor, '/', operation, observedTimestamp)