summaryrefslogtreecommitdiffstats
path: root/cps-service/src/test/groovy
diff options
context:
space:
mode:
authorNiamh Core <niamh.core@est.tech>2021-08-19 08:23:44 +0000
committerGerrit Code Review <gerrit@onap.org>2021-08-19 08:23:44 +0000
commitdb89e9c72388a7eb0a2132f7c25a213a925a0478 (patch)
tree4c7caf7a1e1526fe3e7b36b33db362ae7db93cff /cps-service/src/test/groovy
parent24112c0a500e94dc6068be71105874f8d81678b7 (diff)
parent8f01bf67befd7f73d3208a8279e7d2ea8cc89423 (diff)
Merge "Filter data updated events based on configured pattern"
Diffstat (limited to 'cps-service/src/test/groovy')
-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)
+ }
}