Async request response NCMP -> Client
[cps.git] / cps-service / src / test / groovy / org / onap / cps / notification / NotificationServiceSpec.groovy
1 /*
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
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.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.test.context.ContextConfiguration
33 import spock.lang.Shared
34 import spock.lang.Specification
35
36 import java.util.concurrent.TimeUnit
37
38 @SpringBootTest
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 anchor = new Anchor('my-anchorname', 'my-dataspace-published', 'my-schemaset-name')
57     def myObservedTimestamp = OffsetDateTime.now()
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(anchor, myObservedTimestamp, '/', Operation.CREATE)
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: 'an anchor is in dataspace where #scenario'
72             def anchor = new Anchor('my-anchorname', dataspaceName, 'my-schemaset-name')
73         and: 'event factory can create event successfully'
74             def cpsDataUpdatedEvent = new CpsDataUpdatedEvent()
75             mockCpsDataUpdatedEventFactory.createCpsDataUpdatedEvent(anchor, myObservedTimestamp, Operation.CREATE) >>
76                     cpsDataUpdatedEvent
77         when: 'dataUpdatedEvent is received'
78             def future = objectUnderTest.processDataUpdatedEvent(anchor, myObservedTimestamp,
79                     '/', Operation.CREATE)
80         and: 'wait for async processing to complete'
81             future.get(10, TimeUnit.SECONDS)
82         then: 'async process completed successfully'
83             future.isDone()
84         and: 'notification is sent'
85             expectedSendNotificationCount * mockNotificationPublisher.sendNotification(cpsDataUpdatedEvent)
86         where:
87             scenario                               | dataspaceName            || expectedSendNotificationCount
88             'dataspace name does not match filter' | 'does-not-match-pattern' || 0
89             'dataspace name matches filter'        | 'my-dataspace-published' || 1
90     }
91
92     def '#scenario are changed with xpath #xpath and operation #operation'() {
93         given: 'notification is enabled'
94             spyNotificationProperties.isEnabled() >> true
95         and: 'event factory creates event if operation is #operation'
96             def cpsDataUpdatedEvent = new CpsDataUpdatedEvent()
97             mockCpsDataUpdatedEventFactory.createCpsDataUpdatedEvent(anchor, myObservedTimestamp, expectedOperationInEvent) >>
98                     cpsDataUpdatedEvent
99         when: 'dataUpdatedEvent is received for #xpath'
100             def future = objectUnderTest.processDataUpdatedEvent(anchor, myObservedTimestamp, xpath, operation)
101         and: 'wait for async processing to complete'
102             future.get(10, TimeUnit.SECONDS)
103         then: 'async process completed successfully'
104             future.isDone()
105         and: 'notification is sent'
106             1 * mockNotificationPublisher.sendNotification(cpsDataUpdatedEvent)
107         where:
108             scenario                                   | xpath           | operation            || expectedOperationInEvent
109             'Same event is sent when root nodes'       | ''              | Operation.CREATE || Operation.CREATE
110             'Same event is sent when root nodes'       | ''              | Operation.UPDATE || Operation.UPDATE
111             'Same event is sent when root nodes'       | ''              | Operation.DELETE || Operation.DELETE
112             'Same event is sent when root nodes'       | '/'             | Operation.CREATE || Operation.CREATE
113             'Same event is sent when root nodes'       | '/'             | Operation.UPDATE || Operation.UPDATE
114             'Same event is sent when root nodes'       | '/'             | Operation.DELETE || Operation.DELETE
115             'Same event is sent when container nodes'  | '/parent'       | Operation.CREATE || Operation.CREATE
116             'Same event is sent when container nodes'  | '/parent'       | Operation.UPDATE || Operation.UPDATE
117             'Same event is sent when container nodes'  | '/parent'       | Operation.DELETE || Operation.DELETE
118             'UPDATE event is sent when non root nodes' | '/parent/child' | Operation.CREATE || Operation.UPDATE
119             'UPDATE event is sent when non root nodes' | '/parent/child' | Operation.UPDATE || Operation.UPDATE
120             'UPDATE event is sent when non root nodes' | '/parent/child' | Operation.DELETE || Operation.UPDATE
121     }
122
123     def 'Error handling in notification service.'() {
124         given: 'notification is enabled'
125             spyNotificationProperties.isEnabled() >> true
126         and: 'event factory can not create event successfully'
127             mockCpsDataUpdatedEventFactory.createCpsDataUpdatedEvent(anchor, myObservedTimestamp, Operation.CREATE) >>
128                     { throw new Exception("Could not create event") }
129         when: 'event is sent for processing'
130             def future = objectUnderTest.processDataUpdatedEvent(anchor, myObservedTimestamp, '/', Operation.CREATE)
131         and: 'wait for async processing to complete'
132             future.get(10, TimeUnit.SECONDS)
133         then: 'async process completed successfully'
134             future.isDone()
135         and: 'error is handled and not thrown to caller'
136             notThrown Exception
137             1 * spyNotificationErrorHandler.onException(_, _, _, '/', Operation.CREATE)
138     }
139 }