2 * ============LICENSE_START=======================================================
3 * Copyright (c) 2021-2022 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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.notification
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
36 import java.util.concurrent.TimeUnit
39 @EnableConfigurationProperties
40 @ContextConfiguration(classes = [NotificationProperties, NotificationService, NotificationErrorHandler, AsyncConfig])
41 class NotificationServiceSpec extends Specification {
44 NotificationPublisher mockNotificationPublisher = Mock()
46 CpsDataUpdatedEventFactory mockCpsDataUpdatedEventFactory = Mock()
48 NotificationErrorHandler spyNotificationErrorHandler
50 NotificationProperties spyNotificationProperties
53 NotificationService objectUnderTest
56 def myDataspacePublishedName = 'my-dataspace-published'
57 def myAnchorName = 'my-anchorname'
58 def myObservedTimestamp = OffsetDateTime.now()
60 def 'Skip sending notification when disabled.'() {
61 given: 'notification is disabled'
62 spyNotificationProperties.isEnabled() >> false
63 when: 'dataUpdatedEvent is received'
64 objectUnderTest.processDataUpdatedEvent(myDataspacePublishedName, myAnchorName, myObservedTimestamp, '/', Operation.CREATE)
65 then: 'the notification is not sent'
66 0 * mockNotificationPublisher.sendNotification(_)
69 def 'Send notification when enabled: #scenario.'() {
70 given: 'notification is enabled'
71 spyNotificationProperties.isEnabled() >> true
72 and: 'event factory can create event successfully'
73 def cpsDataUpdatedEvent = new CpsDataUpdatedEvent()
74 mockCpsDataUpdatedEventFactory.createCpsDataUpdatedEvent(dataspaceName, myAnchorName, myObservedTimestamp,
77 when: 'dataUpdatedEvent is received'
78 def future = objectUnderTest.processDataUpdatedEvent(dataspaceName, myAnchorName, myObservedTimestamp,
79 '/', Operation.CREATE)
80 and: 'wait for async processing to complete'
81 future.get(10, TimeUnit.SECONDS)
82 then: 'async process completed successfully'
84 and: 'notification is sent'
85 expectedSendNotificationCount * mockNotificationPublisher.sendNotification(cpsDataUpdatedEvent)
87 scenario | dataspaceName || expectedSendNotificationCount
88 'dataspace name does not match filter' | 'does-not-match-pattern' || 0
89 'dataspace name matches filter' | myDataspacePublishedName || 1
92 def 'Send UPDATE operation when non-root data nodes are changed.'() {
93 given: 'notification is enabled'
94 spyNotificationProperties.isEnabled() >> true
95 and: 'event factory creates event if operation is UPDATE'
96 def cpsDataUpdatedEvent = new CpsDataUpdatedEvent()
97 mockCpsDataUpdatedEventFactory.createCpsDataUpdatedEvent(myDataspacePublishedName, myAnchorName, myObservedTimestamp,
98 Operation.UPDATE) >> cpsDataUpdatedEvent
99 when: 'dataUpdatedEvent is received for non-root xpath'
100 def future = objectUnderTest.processDataUpdatedEvent(myDataspacePublishedName, myAnchorName, myObservedTimestamp, '/non-root-node',
102 and: 'wait for async processing to complete'
103 future.get(10, TimeUnit.SECONDS)
104 then: 'async process completed successfully'
106 and: 'notification is sent'
107 1 * mockNotificationPublisher.sendNotification(cpsDataUpdatedEvent)
109 operation << [Operation.CREATE, Operation.UPDATE, Operation.DELETE]
112 def 'Send same operation when root nodes are changed.'() {
113 given: 'notification is enabled'
114 spyNotificationProperties.isEnabled() >> true
115 and: 'event factory creates event if operation is #operation'
116 def cpsDataUpdatedEvent = new CpsDataUpdatedEvent()
117 mockCpsDataUpdatedEventFactory.createCpsDataUpdatedEvent(myDataspacePublishedName, myAnchorName, myObservedTimestamp,
118 operation) >> cpsDataUpdatedEvent
119 when: 'dataUpdatedEvent is received for root xpath'
120 def future = objectUnderTest.processDataUpdatedEvent(myDataspacePublishedName, myAnchorName, myObservedTimestamp, '/',
122 and: 'wait for async processing to complete'
123 future.get(10, TimeUnit.SECONDS)
124 then: 'async process completed successfully'
126 and: 'notification is sent'
127 1 * mockNotificationPublisher.sendNotification(cpsDataUpdatedEvent)
129 operation << [Operation.CREATE, Operation.UPDATE, Operation.DELETE]
133 def 'Error handling in notification service.'() {
134 given: 'notification is enabled'
135 spyNotificationProperties.isEnabled() >> true
136 and: 'event factory can not create event successfully'
137 mockCpsDataUpdatedEventFactory.createCpsDataUpdatedEvent(myDataspacePublishedName, myAnchorName,
138 myObservedTimestamp, Operation.CREATE) >>
139 { throw new Exception("Could not create event") }
140 when: 'event is sent for processing'
141 def future = objectUnderTest.processDataUpdatedEvent(myDataspacePublishedName, myAnchorName,
142 myObservedTimestamp, '/', Operation.CREATE)
143 and: 'wait for async processing to complete'
144 future.get(10, TimeUnit.SECONDS)
145 then: 'async process completed successfully'
147 and: 'error is handled and not thrown to caller'
149 1 * spyNotificationErrorHandler.onException(_, _, _, _)