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
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.
16 * SPDX-License-Identifier: Apache-2.0
17 * ============LICENSE_END=========================================================
20 package org.onap.cps.notification
22 import org.onap.cps.event.model.CpsDataUpdatedEvent
23 import spock.lang.Specification
25 class NotificationServiceSpec extends Specification {
27 def mockNotificationPublisher = Mock(NotificationPublisher)
28 def spyNotificationErrorHandler = Spy(new NotificationErrorHandler())
29 def mockCpsDataUpdatedEventFactory = Mock(CpsDataUpdatedEventFactory)
31 def objectUnderTest = new NotificationService(true, mockNotificationPublisher,
32 mockCpsDataUpdatedEventFactory, spyNotificationErrorHandler)
34 def myDataspaceName = 'my-dataspace'
35 def myAnchorName = 'my-anchorname'
37 def 'Skip sending notification when disabled.'() {
39 given: 'notification is disabled'
40 objectUnderTest.dataUpdatedEventNotificationEnabled = false
42 when: 'dataUpdatedEvent is received'
43 objectUnderTest.processDataUpdatedEvent(myDataspaceName, myAnchorName)
45 then: 'the notification is not sent'
46 0 * mockNotificationPublisher.sendNotification(_)
49 def 'Send notification when enabled.'() {
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
57 when: 'dataUpdatedEvent is received'
58 objectUnderTest.processDataUpdatedEvent(myDataspaceName, myAnchorName)
60 then: 'notification is sent with correct event'
61 1 * mockNotificationPublisher.sendNotification(cpsDataUpdatedEvent)
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") }
69 when: 'event is sent for processing'
70 objectUnderTest.processDataUpdatedEvent(myDataspaceName, myAnchorName)
72 then: 'error is handled and not thrown to caller'
74 1 * spyNotificationErrorHandler.onException(_,_,_,_)