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