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 '#scenario are changed with xpath #xpath and operation #operation'() {
94 given: 'notification is enabled'
95 spyNotificationProperties.isEnabled() >> true
96 and: 'event factory creates event if operation is #operation'
97 def cpsDataUpdatedEvent = new CpsDataUpdatedEvent()
98 mockCpsDataUpdatedEventFactory.createCpsDataUpdatedEvent(anchor, myObservedTimestamp, expectedOperationInEvent) >>
100 when: 'dataUpdatedEvent is received for #xpath'
101 def future = objectUnderTest.processDataUpdatedEvent(anchor, myObservedTimestamp, xpath, operation)
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 scenario | xpath | operation || expectedOperationInEvent
110 'Same event is sent when root nodes' | '' | Operation.CREATE || Operation.CREATE
111 'Same event is sent when root nodes' | '' | Operation.UPDATE || Operation.UPDATE
112 'Same event is sent when root nodes' | '' | Operation.DELETE || Operation.DELETE
113 'Same event is sent when root nodes' | '/' | Operation.CREATE || Operation.CREATE
114 'Same event is sent when root nodes' | '/' | Operation.UPDATE || Operation.UPDATE
115 'Same event is sent when root nodes' | '/' | Operation.DELETE || Operation.DELETE
116 'Same event is sent when container nodes' | '/parent' | Operation.CREATE || Operation.CREATE
117 'Same event is sent when container nodes' | '/parent' | Operation.UPDATE || Operation.UPDATE
118 'Same event is sent when container nodes' | '/parent' | Operation.DELETE || Operation.DELETE
119 'UPDATE event is sent when non root nodes' | '/parent/child' | Operation.CREATE || Operation.UPDATE
120 'UPDATE event is sent when non root nodes' | '/parent/child' | Operation.UPDATE || Operation.UPDATE
121 'UPDATE event is sent when non root nodes' | '/parent/child' | Operation.DELETE || Operation.UPDATE
124 def 'Error handling in notification service.'() {
125 given: 'notification is enabled'
126 spyNotificationProperties.isEnabled() >> true
127 and: 'event factory can not create event successfully'
128 mockCpsDataUpdatedEventFactory.createCpsDataUpdatedEvent(anchor, myObservedTimestamp, Operation.CREATE) >>
129 { throw new Exception("Could not create event") }
130 when: 'event is sent for processing'
131 def future = objectUnderTest.processDataUpdatedEvent(anchor, myObservedTimestamp, '/', Operation.CREATE)
132 and: 'wait for async processing to complete'
133 future.get(10, TimeUnit.SECONDS)
134 then: 'async process completed successfully'
136 and: 'error is handled and not thrown to caller'
138 1 * spyNotificationErrorHandler.onException(_, _, _, '/', Operation.CREATE)