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