Merge "Remove CmHandle in DMI-Registry"
[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 import java.util.concurrent.TimeUnit
36
37 @SpringBootTest
38 @EnableAsync
39 @EnableConfigurationProperties
40 @ContextConfiguration(classes = [NotificationProperties, NotificationService, NotificationErrorHandler, AsyncConfig])
41 class NotificationServiceSpec extends Specification {
42
43     @SpringBean
44     NotificationPublisher mockNotificationPublisher = Mock()
45     @SpringBean
46     CpsDataUpdatedEventFactory mockCpsDataUpdatedEventFactory = Mock()
47     @SpringSpy
48     NotificationErrorHandler spyNotificationErrorHandler
49     @SpringSpy
50     NotificationProperties spyNotificationProperties
51
52     @Autowired
53     NotificationService objectUnderTest
54
55     @Shared
56     def myDataspacePublishedName = 'my-dataspace-published'
57     def myAnchorName = 'my-anchorname'
58
59     def 'Skip sending notification when disabled.'() {
60         given: 'notification is disabled'
61             spyNotificationProperties.isEnabled() >> false
62         when: 'dataUpdatedEvent is received'
63             objectUnderTest.processDataUpdatedEvent(myDataspacePublishedName, myAnchorName)
64         then: 'the notification is not sent'
65             0 * mockNotificationPublisher.sendNotification(_)
66     }
67
68     def 'Send notification when enabled: #scenario.'() {
69         given: 'notification is enabled'
70             spyNotificationProperties.isEnabled() >> true
71         and: 'event factory can create event successfully'
72             def cpsDataUpdatedEvent = new CpsDataUpdatedEvent()
73             mockCpsDataUpdatedEventFactory.createCpsDataUpdatedEvent(dataspaceName, myAnchorName) >> cpsDataUpdatedEvent
74         when: 'dataUpdatedEvent is received'
75             def future = objectUnderTest.processDataUpdatedEvent(dataspaceName, myAnchorName)
76         and: 'wait for async processing is completed'
77             future.get(10, TimeUnit.SECONDS)
78         then: 'async process completed successfully'
79             future.isDone()
80         and: 'notification is sent'
81             expectedSendNotificationCount * mockNotificationPublisher.sendNotification(cpsDataUpdatedEvent)
82         where:
83             scenario                               | dataspaceName            || expectedSendNotificationCount
84             'dataspace name does not match filter' | 'does-not-match-pattern' || 0
85             'dataspace name matches filter'        | myDataspacePublishedName || 1
86     }
87
88     def 'Error handling in notification service.'() {
89         given: 'notification is enabled'
90             spyNotificationProperties.isEnabled() >> true
91         and: 'event factory can not create event successfully'
92             mockCpsDataUpdatedEventFactory.createCpsDataUpdatedEvent(myDataspacePublishedName, myAnchorName) >>
93                 { throw new Exception("Could not create event") }
94         when: 'event is sent for processing'
95             def future = objectUnderTest.processDataUpdatedEvent(myDataspacePublishedName, myAnchorName)
96         and: 'wait for async processing is completed'
97             future.get(10, TimeUnit.SECONDS)
98         then: 'async process completed successfully'
99             future.isDone()
100         and: 'error is handled and not thrown to caller'
101             notThrown Exception
102             1 * spyNotificationErrorHandler.onException(_, _, _, _)
103     }
104
105     NotificationService createNotificationService(boolean notificationEnabled) {
106         spyNotificationProperties = Spy(notificationProperties)
107         spyNotificationProperties.isEnabled() >> notificationEnabled
108         return new NotificationService(spyNotificationProperties, mockNotificationPublisher,
109             mockCpsDataUpdatedEventFactory, spyNotificationErrorHandler)
110     }
111 }