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.onap.cps.spi.model.Anchor
27 import org.spockframework.spring.SpringBean
28 import org.spockframework.spring.SpringSpy
29 import org.springframework.beans.factory.annotation.Autowired
30 import org.springframework.boot.context.properties.EnableConfigurationProperties
31 import org.springframework.boot.test.context.SpringBootTest
32 import org.springframework.scheduling.annotation.EnableAsync
33 import org.springframework.test.context.ContextConfiguration
34 import spock.lang.Shared
35 import spock.lang.Specification
37 import java.util.concurrent.TimeUnit
40 @EnableConfigurationProperties
41 @ContextConfiguration(classes = [NotificationProperties, NotificationService, NotificationErrorHandler, AsyncConfig])
42 class NotificationServiceSpec extends Specification {
45 NotificationPublisher mockNotificationPublisher = Mock()
47 CpsDataUpdatedEventFactory mockCpsDataUpdatedEventFactory = Mock()
49 NotificationErrorHandler spyNotificationErrorHandler
51 NotificationProperties spyNotificationProperties
54 NotificationService objectUnderTest
57 def anchor = new Anchor('my-anchorname', 'my-dataspace-published', 'my-schemaset-name')
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(anchor, 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: 'an anchor is in dataspace where #scenario'
73 def anchor = new Anchor('my-anchorname', dataspaceName, 'my-schemaset-name')
74 and: 'event factory can create event successfully'
75 def cpsDataUpdatedEvent = new CpsDataUpdatedEvent()
76 mockCpsDataUpdatedEventFactory.createCpsDataUpdatedEvent(anchor, myObservedTimestamp, Operation.CREATE) >>
78 when: 'dataUpdatedEvent is received'
79 def future = objectUnderTest.processDataUpdatedEvent(anchor, myObservedTimestamp,
80 '/', Operation.CREATE)
81 and: 'wait for async processing to complete'
82 future.get(10, TimeUnit.SECONDS)
83 then: 'async process completed successfully'
85 and: 'notification is sent'
86 expectedSendNotificationCount * mockNotificationPublisher.sendNotification(cpsDataUpdatedEvent)
88 scenario | dataspaceName || expectedSendNotificationCount
89 'dataspace name does not match filter' | 'does-not-match-pattern' || 0
90 'dataspace name matches filter' | 'my-dataspace-published' || 1
93 def 'Send UPDATE operation when non-root data nodes are changed.'() {
94 given: 'notification is enabled'
95 spyNotificationProperties.isEnabled() >> true
96 and: 'event factory creates event if operation is UPDATE'
97 def cpsDataUpdatedEvent = new CpsDataUpdatedEvent()
98 mockCpsDataUpdatedEventFactory.createCpsDataUpdatedEvent(anchor, myObservedTimestamp,
99 Operation.UPDATE) >> cpsDataUpdatedEvent
100 when: 'dataUpdatedEvent is received for non-root xpath'
101 def future = objectUnderTest.processDataUpdatedEvent(anchor, myObservedTimestamp, '/non-root-node',
103 and: 'wait for async processing to complete'
104 future.get(10, TimeUnit.SECONDS)
105 then: 'async process completed successfully'
107 and: 'notification is sent'
108 1 * mockNotificationPublisher.sendNotification(cpsDataUpdatedEvent)
110 operation << [Operation.CREATE, Operation.UPDATE, Operation.DELETE]
113 def 'Send same operation when root nodes are changed.'() {
114 given: 'notification is enabled'
115 spyNotificationProperties.isEnabled() >> true
116 and: 'event factory creates event if operation is #operation'
117 def cpsDataUpdatedEvent = new CpsDataUpdatedEvent()
118 mockCpsDataUpdatedEventFactory.createCpsDataUpdatedEvent(anchor, myObservedTimestamp, operation) >>
120 when: 'dataUpdatedEvent is received for root xpath'
121 def future = objectUnderTest.processDataUpdatedEvent(anchor, myObservedTimestamp, '/', operation)
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(anchor, myObservedTimestamp, Operation.CREATE) >>
138 { throw new Exception("Could not create event") }
139 when: 'event is sent for processing'
140 def future = objectUnderTest.processDataUpdatedEvent(anchor, myObservedTimestamp, '/', Operation.CREATE)
141 and: 'wait for async processing to complete'
142 future.get(10, TimeUnit.SECONDS)
143 then: 'async process completed successfully'
145 and: 'error is handled and not thrown to caller'
147 1 * spyNotificationErrorHandler.onException(_, _, _, '/', Operation.CREATE)