Merge "Sending Data Updated Event to kafka"
[cps.git] / cps-service / src / test / groovy / org / onap / cps / notification / NotificationServiceSpec.groovy
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Bell Canada. All rights reserved.
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  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19
20 package org.onap.cps.notification
21
22 import org.onap.cps.event.model.CpsDataUpdatedEvent
23 import spock.lang.Specification
24
25 class NotificationServiceSpec extends Specification {
26
27     def mockNotificationPublisher = Mock(NotificationPublisher)
28     def spyNotificationErrorHandler = Spy(new NotificationErrorHandler())
29     def mockCpsDataUpdatedEventFactory = Mock(CpsDataUpdatedEventFactory)
30
31     def objectUnderTest = new NotificationService(true, mockNotificationPublisher,
32             mockCpsDataUpdatedEventFactory, spyNotificationErrorHandler)
33
34     def myDataspaceName = 'my-dataspace'
35     def myAnchorName = 'my-anchorname'
36
37     def 'Skip sending notification when disabled.'() {
38
39         given: 'notification is disabled'
40             objectUnderTest.dataUpdatedEventNotificationEnabled = false
41
42         when: 'dataUpdatedEvent is received'
43             objectUnderTest.processDataUpdatedEvent(myDataspaceName, myAnchorName)
44
45         then: 'the notification is not sent'
46             0 * mockNotificationPublisher.sendNotification(_)
47     }
48
49     def 'Send notification when enabled.'() {
50
51         given: 'notification is enabled'
52             objectUnderTest.dataUpdatedEventNotificationEnabled = true
53         and: 'event factory can create event successfully'
54             def cpsDataUpdatedEvent = new CpsDataUpdatedEvent()
55             mockCpsDataUpdatedEventFactory.createCpsDataUpdatedEvent(myDataspaceName, myAnchorName) >> cpsDataUpdatedEvent
56
57         when: 'dataUpdatedEvent is received'
58             objectUnderTest.processDataUpdatedEvent(myDataspaceName, myAnchorName)
59
60         then: 'notification is sent with correct event'
61             1 * mockNotificationPublisher.sendNotification(cpsDataUpdatedEvent)
62     }
63
64     def 'Error handling in notification service.'(){
65         given: 'event factory can not create event successfully'
66             mockCpsDataUpdatedEventFactory.createCpsDataUpdatedEvent(myDataspaceName, myAnchorName) >>
67                     { throw new Exception("Could not create event") }
68
69         when: 'event is sent for processing'
70             objectUnderTest.processDataUpdatedEvent(myDataspaceName, myAnchorName)
71
72         then: 'error is handled and not thrown to caller'
73             notThrown Exception
74             1 * spyNotificationErrorHandler.onException(_,_,_,_)
75
76     }
77
78 }