2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Bell Canada. All rights reserved.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 * SPDX-License-Identifier: Apache-2.0
17 * ============LICENSE_END=========================================================
20 package org.onap.cps.notification
22 import org.onap.cps.event.model.CpsDataUpdatedEvent
23 import org.spockframework.spring.SpringBean
24 import org.springframework.beans.factory.annotation.Autowired
25 import org.springframework.boot.context.properties.EnableConfigurationProperties
26 import org.springframework.boot.test.context.SpringBootTest
27 import org.springframework.test.context.ContextConfiguration
28 import spock.lang.Shared
29 import spock.lang.Specification
32 @EnableConfigurationProperties
33 @ContextConfiguration(classes = [NotificationProperties])
34 class NotificationServiceSpec extends Specification {
37 NotificationPublisher mockNotificationPublisher = Mock()
39 NotificationErrorHandler spyNotificationErrorHandler = Spy(new NotificationErrorHandler())
41 CpsDataUpdatedEventFactory mockCpsDataUpdatedEventFactory = Mock()
44 NotificationProperties notificationProperties
45 NotificationProperties spyNotificationProperties
48 def myDataspacePublishedName = 'my-dataspace-published'
49 def myAnchorName = 'my-anchorname'
51 def 'Skip sending notification when disabled.'() {
52 given: 'notification is disabled'
53 def objectUnderTest = createNotificationService(false)
54 when: 'dataUpdatedEvent is received'
55 objectUnderTest.processDataUpdatedEvent(myDataspacePublishedName, myAnchorName)
56 then: 'the notification is not sent'
57 0 * mockNotificationPublisher.sendNotification(_)
60 def 'Send notification when enabled: #scenario.'() {
61 given: 'notification is enabled'
62 def objectUnderTest = createNotificationService(true)
63 and: 'event factory can create event successfully'
64 def cpsDataUpdatedEvent = new CpsDataUpdatedEvent()
65 mockCpsDataUpdatedEventFactory.createCpsDataUpdatedEvent(dataspaceName, myAnchorName) >> cpsDataUpdatedEvent
66 when: 'dataUpdatedEvent is received'
67 objectUnderTest.processDataUpdatedEvent(dataspaceName, myAnchorName)
68 then: 'notification is sent'
69 expectedSendNotificationCount * mockNotificationPublisher.sendNotification(cpsDataUpdatedEvent)
71 scenario | dataspaceName || expectedSendNotificationCount
72 'dataspace name does not match filter' | 'does-not-match-pattern' || 0
73 'dataspace name matches filter' | myDataspacePublishedName || 1
76 def 'Error handling in notification service.'() {
77 given: 'notification is enabled'
78 def objectUnderTest = createNotificationService(true)
79 and: 'event factory can not create event successfully'
80 mockCpsDataUpdatedEventFactory.createCpsDataUpdatedEvent(myDataspacePublishedName, myAnchorName) >>
81 { throw new Exception("Could not create event") }
82 when: 'event is sent for processing'
83 objectUnderTest.processDataUpdatedEvent(myDataspacePublishedName, myAnchorName)
84 then: 'error is handled and not thrown to caller'
86 1 * spyNotificationErrorHandler.onException(_, _, _, _)
89 NotificationService createNotificationService(boolean notificationEnabled) {
90 spyNotificationProperties = Spy(notificationProperties)
91 spyNotificationProperties.isEnabled() >> notificationEnabled
92 return new NotificationService(spyNotificationProperties, mockNotificationPublisher,
93 mockCpsDataUpdatedEventFactory, spyNotificationErrorHandler)