Refactor NBINotifications and serviceHandlerImpl
[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.rev210813.NotificationAlarmService;
25 import org.opendaylight.yang.gen.v1.nbi.notifications.rev210813.NotificationProcessService;
26 import org.opendaylight.yang.gen.v1.nbi.notifications.rev210813.PublishNotificationAlarmService;
27 import org.opendaylight.yang.gen.v1.nbi.notifications.rev210813.PublishNotificationAlarmServiceBuilder;
28 import org.opendaylight.yang.gen.v1.nbi.notifications.rev210813.PublishNotificationProcessService;
29 import org.opendaylight.yang.gen.v1.nbi.notifications.rev210813.PublishNotificationProcessServiceBuilder;
30
31 public class NbiNotificationsListenerImplTest extends AbstractTest {
32     @Mock
33     private Publisher<NotificationProcessService> 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         PublishNotificationProcessService notification = new PublishNotificationProcessServiceBuilder()
47                 .setPublisherName("test")
48                 .setCommonId("commonId")
49                 .setConnectionType(ConnectionType.Service)
50                 .setMessage("Service deleted")
51                 .setOperationalState(State.OutOfService)
52                 .setServiceName("service name")
53                 .build();
54         listener.onPublishNotificationProcessService(notification);
55         verify(publisherService, times(1)).sendEvent(any(), anyString());
56     }
57
58     @Test
59     public void onPublishNotificationServiceWrongPublisherTest() {
60         NbiNotificationsListenerImpl listener = new NbiNotificationsListenerImpl(Map.of("test", publisherService),
61                 Map.of("test", publisherAlarm));
62         PublishNotificationProcessService notification = new PublishNotificationProcessServiceBuilder()
63                 .setPublisherName("wrongPublisher")
64                 .setCommonId("commonId")
65                 .setConnectionType(ConnectionType.Service)
66                 .setMessage("Service deleted")
67                 .setOperationalState(State.OutOfService)
68                 .setServiceName("service name")
69                 .build();
70         listener.onPublishNotificationProcessService(notification);
71         verify(publisherService, times(0)).sendEvent(any(), anyString());
72     }
73
74     @Test
75     public void onPublishNotificationAlarmServiceTest() {
76         NbiNotificationsListenerImpl listener = new NbiNotificationsListenerImpl(Map.of("test", publisherService),
77                 Map.of("test", publisherAlarm));
78         PublishNotificationAlarmService notification = new PublishNotificationAlarmServiceBuilder()
79                 .setPublisherName("test")
80                 .setConnectionType(ConnectionType.Service)
81                 .setMessage("The service is now inService")
82                 .setOperationalState(State.OutOfService)
83                 .setServiceName("service name")
84                 .build();
85         listener.onPublishNotificationAlarmService(notification);
86         verify(publisherAlarm, times(1)).sendEvent(any(), anyString());
87     }
88
89     @Test
90     public void onPublishNotificationAlarmServiceWrongPublisherTest() {
91         NbiNotificationsListenerImpl listener = new NbiNotificationsListenerImpl(Map.of("test", publisherService),
92                 Map.of("test", publisherAlarm));
93         PublishNotificationAlarmService notification = new PublishNotificationAlarmServiceBuilder()
94                 .setPublisherName("wrongPublisher")
95                 .setConnectionType(ConnectionType.Service)
96                 .setMessage("The service is now inService")
97                 .setOperationalState(State.OutOfService)
98                 .setServiceName("service name")
99                 .build();
100         listener.onPublishNotificationAlarmService(notification);
101         verify(publisherAlarm, times(0)).sendEvent(any(), anyString());
102     }
103 }