Refactor NBINotification & add ServiceListener tests
[transportpce.git] / nbinotifications / src / test / java / org / opendaylight / transportpce / nbinotifications / listener / NbiNotificationsListenerImplTest.java
1 /*
2  * Copyright © 2021 Orange, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.transportpce.nbinotifications.listener;
9
10 import static org.mockito.ArgumentMatchers.any;
11 import static org.mockito.ArgumentMatchers.anyString;
12 import static org.mockito.Mockito.times;
13 import static org.mockito.Mockito.verify;
14
15 import java.util.Map;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.mockito.Mock;
19 import org.mockito.MockitoAnnotations;
20 import org.opendaylight.transportpce.nbinotifications.producer.Publisher;
21 import org.opendaylight.transportpce.test.AbstractTest;
22 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev190531.ConnectionType;
23 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.state.types.rev181130.State;
24 import org.opendaylight.yang.gen.v1.nbi.notifications.rev210628.NotificationAlarmService;
25 import org.opendaylight.yang.gen.v1.nbi.notifications.rev210628.NotificationService;
26 import org.opendaylight.yang.gen.v1.nbi.notifications.rev210628.PublishNotificationAlarmService;
27 import org.opendaylight.yang.gen.v1.nbi.notifications.rev210628.PublishNotificationAlarmServiceBuilder;
28 import org.opendaylight.yang.gen.v1.nbi.notifications.rev210628.PublishNotificationService;
29 import org.opendaylight.yang.gen.v1.nbi.notifications.rev210628.PublishNotificationServiceBuilder;
30
31 public class NbiNotificationsListenerImplTest extends AbstractTest {
32     @Mock
33     private Publisher<NotificationService> publisherService;
34     @Mock
35     private Publisher<NotificationAlarmService> publisherAlarm;
36
37     @Before
38     public void setUp() {
39         MockitoAnnotations.openMocks(this);
40     }
41
42     @Test
43     public void onPublishNotificationServiceTest() {
44         NbiNotificationsListenerImpl listener = new NbiNotificationsListenerImpl(Map.of("test", publisherService),
45                 Map.of("test", publisherAlarm));
46         PublishNotificationService notification = new PublishNotificationServiceBuilder().setTopic("test")
47                 .setCommonId("commonId").setConnectionType(ConnectionType.Service).setMessage("Service deleted")
48                 .setOperationalState(State.OutOfService).setServiceName("service name").build();
49         listener.onPublishNotificationService(notification);
50         verify(publisherService, times(1)).sendEvent(any(), anyString());
51     }
52
53     @Test
54     public void onPublishNotificationServiceWrongTopicTest() {
55         NbiNotificationsListenerImpl listener = new NbiNotificationsListenerImpl(Map.of("test", publisherService),
56                 Map.of("test", publisherAlarm));
57         PublishNotificationService notification = new PublishNotificationServiceBuilder().setTopic("wrongtopic")
58                 .setCommonId("commonId").setConnectionType(ConnectionType.Service).setMessage("Service deleted")
59                 .setOperationalState(State.OutOfService).setServiceName("service name").build();
60         listener.onPublishNotificationService(notification);
61         verify(publisherService, times(0)).sendEvent(any(), anyString());
62     }
63
64     @Test
65     public void onPublishNotificationAlarmServiceTest() {
66         NbiNotificationsListenerImpl listener = new NbiNotificationsListenerImpl(Map.of("test", publisherService),
67                 Map.of("test", publisherAlarm));
68         PublishNotificationAlarmService notification = new PublishNotificationAlarmServiceBuilder().setTopic("test")
69                 .setConnectionType(ConnectionType.Service).setMessage("The service is now inService")
70                 .setOperationalState(State.OutOfService).setServiceName("service name").build();
71         listener.onPublishNotificationAlarmService(notification);
72         verify(publisherAlarm, times(1)).sendEvent(any(), anyString());
73     }
74
75     @Test
76     public void onPublishNotificationAlarmServiceWrongTopicTest() {
77         NbiNotificationsListenerImpl listener = new NbiNotificationsListenerImpl(Map.of("test", publisherService),
78                 Map.of("test", publisherAlarm));
79         PublishNotificationAlarmService notification = new PublishNotificationAlarmServiceBuilder()
80                 .setTopic("wrongtopic").setConnectionType(ConnectionType.Service)
81                 .setMessage("The service is now inService").setOperationalState(State.OutOfService)
82                 .setServiceName("service name").build();
83         listener.onPublishNotificationAlarmService(notification);
84         verify(publisherAlarm, times(0)).sendEvent(any(), anyString());
85     }
86 }