summaryrefslogtreecommitdiffstats
path: root/cps-service/src/test/groovy/org
diff options
context:
space:
mode:
authorRenu Kumari <renu.kumari@bell.ca>2021-08-17 14:06:53 -0400
committerRenu Kumari <renu.kumari@bell.ca>2021-08-17 15:16:10 -0400
commit8f01bf67befd7f73d3208a8279e7d2ea8cc89423 (patch)
treecbe8b831e0401ee3c89237ff4db5926671f0d855 /cps-service/src/test/groovy/org
parentddf661254f711d78b480b8f5623632443a8f6ebc (diff)
Filter data updated events based on configured pattern
Issue-ID: CPS-469 Signed-off-by: Renu Kumari <renu.kumari@bell.ca> Change-Id: I7810990b54c3140677184ea671164b8835a6afbb
Diffstat (limited to 'cps-service/src/test/groovy/org')
-rw-r--r--cps-service/src/test/groovy/org/onap/cps/notification/NotificationServiceSpec.groovy75
1 files changed, 46 insertions, 29 deletions
diff --git a/cps-service/src/test/groovy/org/onap/cps/notification/NotificationServiceSpec.groovy b/cps-service/src/test/groovy/org/onap/cps/notification/NotificationServiceSpec.groovy
index a74279548e..b60d093236 100644
--- a/cps-service/src/test/groovy/org/onap/cps/notification/NotificationServiceSpec.groovy
+++ b/cps-service/src/test/groovy/org/onap/cps/notification/NotificationServiceSpec.groovy
@@ -20,59 +20,76 @@
package org.onap.cps.notification
import org.onap.cps.event.model.CpsDataUpdatedEvent
+import org.spockframework.spring.SpringBean
+import org.springframework.beans.factory.annotation.Autowired
+import org.springframework.boot.context.properties.EnableConfigurationProperties
+import org.springframework.boot.test.context.SpringBootTest
+import org.springframework.test.context.ContextConfiguration
+import spock.lang.Shared
import spock.lang.Specification
+@SpringBootTest
+@EnableConfigurationProperties
+@ContextConfiguration(classes = [NotificationProperties])
class NotificationServiceSpec extends Specification {
- def mockNotificationPublisher = Mock(NotificationPublisher)
- def spyNotificationErrorHandler = Spy(new NotificationErrorHandler())
- def mockCpsDataUpdatedEventFactory = Mock(CpsDataUpdatedEventFactory)
+ @SpringBean
+ NotificationPublisher mockNotificationPublisher = Mock()
+ @SpringBean
+ NotificationErrorHandler spyNotificationErrorHandler = Spy(new NotificationErrorHandler())
+ @SpringBean
+ CpsDataUpdatedEventFactory mockCpsDataUpdatedEventFactory = Mock()
- def objectUnderTest = new NotificationService(true, mockNotificationPublisher,
- mockCpsDataUpdatedEventFactory, spyNotificationErrorHandler)
+ @Autowired
+ NotificationProperties notificationProperties
+ NotificationProperties spyNotificationProperties
- def myDataspaceName = 'my-dataspace'
+ @Shared
+ def myDataspacePublishedName = 'my-dataspace-published'
def myAnchorName = 'my-anchorname'
def 'Skip sending notification when disabled.'() {
-
given: 'notification is disabled'
- objectUnderTest.dataUpdatedEventNotificationEnabled = false
-
+ def objectUnderTest = createNotificationService(false)
when: 'dataUpdatedEvent is received'
- objectUnderTest.processDataUpdatedEvent(myDataspaceName, myAnchorName)
-
+ objectUnderTest.processDataUpdatedEvent(myDataspacePublishedName, myAnchorName)
then: 'the notification is not sent'
0 * mockNotificationPublisher.sendNotification(_)
}
- def 'Send notification when enabled.'() {
-
+ def 'Send notification when enabled: #scenario.'() {
given: 'notification is enabled'
- objectUnderTest.dataUpdatedEventNotificationEnabled = true
+ def objectUnderTest = createNotificationService(true)
and: 'event factory can create event successfully'
def cpsDataUpdatedEvent = new CpsDataUpdatedEvent()
- mockCpsDataUpdatedEventFactory.createCpsDataUpdatedEvent(myDataspaceName, myAnchorName) >> cpsDataUpdatedEvent
-
+ mockCpsDataUpdatedEventFactory.createCpsDataUpdatedEvent(dataspaceName, myAnchorName) >> cpsDataUpdatedEvent
when: 'dataUpdatedEvent is received'
- objectUnderTest.processDataUpdatedEvent(myDataspaceName, myAnchorName)
-
- then: 'notification is sent with correct event'
- 1 * mockNotificationPublisher.sendNotification(cpsDataUpdatedEvent)
+ objectUnderTest.processDataUpdatedEvent(dataspaceName, myAnchorName)
+ then: 'notification is sent'
+ expectedSendNotificationCount * mockNotificationPublisher.sendNotification(cpsDataUpdatedEvent)
+ where:
+ scenario | dataspaceName || expectedSendNotificationCount
+ 'dataspace name does not match filter' | 'does-not-match-pattern' || 0
+ 'dataspace name matches filter' | myDataspacePublishedName || 1
}
- def 'Error handling in notification service.'(){
- given: 'event factory can not create event successfully'
- mockCpsDataUpdatedEventFactory.createCpsDataUpdatedEvent(myDataspaceName, myAnchorName) >>
- { throw new Exception("Could not create event") }
-
+ def 'Error handling in notification service.'() {
+ given: 'notification is enabled'
+ def objectUnderTest = createNotificationService(true)
+ and: 'event factory can not create event successfully'
+ mockCpsDataUpdatedEventFactory.createCpsDataUpdatedEvent(myDataspacePublishedName, myAnchorName) >>
+ { throw new Exception("Could not create event") }
when: 'event is sent for processing'
- objectUnderTest.processDataUpdatedEvent(myDataspaceName, myAnchorName)
-
+ objectUnderTest.processDataUpdatedEvent(myDataspacePublishedName, myAnchorName)
then: 'error is handled and not thrown to caller'
notThrown Exception
- 1 * spyNotificationErrorHandler.onException(_,_,_,_)
-
+ 1 * spyNotificationErrorHandler.onException(_, _, _, _)
}
+ NotificationService createNotificationService(boolean notificationEnabled) {
+ spyNotificationProperties = Spy(notificationProperties)
+ spyNotificationProperties.isEnabled() >> notificationEnabled
+ return new NotificationService(spyNotificationProperties, mockNotificationPublisher,
+ mockCpsDataUpdatedEventFactory, spyNotificationErrorHandler)
+ }
}