b60d093236f500b504e433ee93a254e3053d61d9
[cps.git] / cps-service / src / test / groovy / org / onap / cps / notification / NotificationServiceSpec.groovy
1 /*
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
8  *
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.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19
20 package org.onap.cps.notification
21
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
30
31 @SpringBootTest
32 @EnableConfigurationProperties
33 @ContextConfiguration(classes = [NotificationProperties])
34 class NotificationServiceSpec extends Specification {
35
36     @SpringBean
37     NotificationPublisher mockNotificationPublisher = Mock()
38     @SpringBean
39     NotificationErrorHandler spyNotificationErrorHandler = Spy(new NotificationErrorHandler())
40     @SpringBean
41     CpsDataUpdatedEventFactory mockCpsDataUpdatedEventFactory = Mock()
42
43     @Autowired
44     NotificationProperties notificationProperties
45     NotificationProperties spyNotificationProperties
46
47     @Shared
48     def myDataspacePublishedName = 'my-dataspace-published'
49     def myAnchorName = 'my-anchorname'
50
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(_)
58     }
59
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)
70         where:
71             scenario                               | dataspaceName            || expectedSendNotificationCount
72             'dataspace name does not match filter' | 'does-not-match-pattern' || 0
73             'dataspace name matches filter'        | myDataspacePublishedName || 1
74     }
75
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'
85             notThrown Exception
86             1 * spyNotificationErrorHandler.onException(_, _, _, _)
87     }
88
89     NotificationService createNotificationService(boolean notificationEnabled) {
90         spyNotificationProperties = Spy(notificationProperties)
91         spyNotificationProperties.isEnabled() >> notificationEnabled
92         return new NotificationService(spyNotificationProperties, mockNotificationPublisher,
93             mockCpsDataUpdatedEventFactory, spyNotificationErrorHandler)
94     }
95 }