T-API notification JUnit test
[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.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNull;
12 import static org.mockito.ArgumentMatchers.any;
13 import static org.mockito.ArgumentMatchers.anyString;
14 import static org.mockito.ArgumentMatchers.eq;
15 import static org.mockito.Mockito.times;
16 import static org.mockito.Mockito.verify;
17
18 import java.util.Map;
19 import java.util.UUID;
20 import java.util.concurrent.ExecutionException;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.mockito.Mock;
24 import org.mockito.MockitoAnnotations;
25 import org.opendaylight.transportpce.nbinotifications.producer.Publisher;
26 import org.opendaylight.transportpce.nbinotifications.utils.NotificationServiceDataUtils;
27 import org.opendaylight.transportpce.test.AbstractTest;
28 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev211210.ConnectionType;
29 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.state.types.rev191129.State;
30 import org.opendaylight.yang.gen.v1.nbi.notifications.rev211013.NotificationAlarmService;
31 import org.opendaylight.yang.gen.v1.nbi.notifications.rev211013.NotificationProcessService;
32 import org.opendaylight.yang.gen.v1.nbi.notifications.rev211013.NotificationTapiService;
33 import org.opendaylight.yang.gen.v1.nbi.notifications.rev211013.PublishNotificationAlarmService;
34 import org.opendaylight.yang.gen.v1.nbi.notifications.rev211013.PublishNotificationAlarmServiceBuilder;
35 import org.opendaylight.yang.gen.v1.nbi.notifications.rev211013.PublishNotificationProcessService;
36 import org.opendaylight.yang.gen.v1.nbi.notifications.rev211013.PublishNotificationProcessServiceBuilder;
37 import org.opendaylight.yang.gen.v1.nbi.notifications.rev211013.PublishTapiNotificationService;
38 import org.opendaylight.yang.gen.v1.nbi.notifications.rev211013.PublishTapiNotificationServiceBuilder;
39
40 public class NbiNotificationsListenerImplTest extends AbstractTest {
41     @Mock
42     private Publisher<NotificationProcessService> publisherService;
43     @Mock
44     private Publisher<NotificationAlarmService> publisherAlarm;
45     @Mock
46     private Publisher<NotificationTapiService> publisherTapiService;
47
48     @Before
49     public void setUp() throws ExecutionException, InterruptedException {
50         MockitoAnnotations.openMocks(this);
51     }
52
53     @Test
54     public void onPublishNotificationServiceTest() {
55         NbiNotificationsListenerImpl listener = new NbiNotificationsListenerImpl(Map.of("test", publisherService),
56                 Map.of("test", publisherAlarm), Map.of("test", publisherTapiService));
57         PublishNotificationProcessService notification = new PublishNotificationProcessServiceBuilder()
58                 .setPublisherName("test")
59                 .setCommonId("commonId")
60                 .setConnectionType(ConnectionType.Service)
61                 .setMessage("Service deleted")
62                 .setOperationalState(State.OutOfService)
63                 .setServiceName("service name")
64                 .build();
65         listener.onPublishNotificationProcessService(notification);
66         verify(publisherService, times(1)).sendEvent(any(), anyString());
67     }
68
69     @Test
70     public void onPublishNotificationServiceWrongPublisherTest() {
71         NbiNotificationsListenerImpl listener = new NbiNotificationsListenerImpl(Map.of("test", publisherService),
72                 Map.of("test", publisherAlarm), Map.of("test", publisherTapiService));
73         PublishNotificationProcessService notification = new PublishNotificationProcessServiceBuilder()
74                 .setPublisherName("wrongPublisher")
75                 .setCommonId("commonId")
76                 .setConnectionType(ConnectionType.Service)
77                 .setMessage("Service deleted")
78                 .setOperationalState(State.OutOfService)
79                 .setServiceName("service name")
80                 .build();
81         listener.onPublishNotificationProcessService(notification);
82         verify(publisherService, times(0)).sendEvent(any(), anyString());
83     }
84
85     @Test
86     public void onPublishNotificationAlarmServiceTest() {
87         NbiNotificationsListenerImpl listener = new NbiNotificationsListenerImpl(Map.of("test", publisherService),
88                 Map.of("test", publisherAlarm), Map.of("test", publisherTapiService));
89         PublishNotificationAlarmService notification = new PublishNotificationAlarmServiceBuilder()
90                 .setPublisherName("test")
91                 .setConnectionType(ConnectionType.Service)
92                 .setMessage("The service is now inService")
93                 .setOperationalState(State.OutOfService)
94                 .setServiceName("service name")
95                 .build();
96         listener.onPublishNotificationAlarmService(notification);
97         verify(publisherAlarm, times(1)).sendEvent(any(), anyString());
98     }
99
100     @Test
101     public void onPublishNotificationAlarmServiceWrongPublisherTest() {
102         NbiNotificationsListenerImpl listener = new NbiNotificationsListenerImpl(Map.of("test", publisherService),
103                 Map.of("test", publisherAlarm), Map.of("test", publisherTapiService));
104         PublishNotificationAlarmService notification = new PublishNotificationAlarmServiceBuilder()
105                 .setPublisherName("wrongPublisher")
106                 .setConnectionType(ConnectionType.Service)
107                 .setMessage("The service is now inService")
108                 .setOperationalState(State.OutOfService)
109                 .setServiceName("service name")
110                 .build();
111         listener.onPublishNotificationAlarmService(notification);
112         verify(publisherAlarm, times(0)).sendEvent(any(), anyString());
113     }
114
115     @Test
116     public void onPublishTapiNotificationServiceTest() throws ExecutionException, InterruptedException {
117         NbiNotificationsListenerImpl listener = new NbiNotificationsListenerImpl(Map.of("test", publisherService),
118                 Map.of("test", publisherAlarm), Map.of("test", publisherTapiService));
119
120         PublishTapiNotificationService notification
121             = new PublishTapiNotificationServiceBuilder(NotificationServiceDataUtils.buildReceivedTapiAlarmEvent())
122                 .setTopic("test")
123                 .build();
124         listener.onPublishTapiNotificationService(notification);
125         verify(publisherTapiService, times(1)).sendEvent(any(), anyString());
126     }
127
128     @Test
129     public void onPublishTapiNotificationServiceTestWrongPublisherTest() {
130         NbiNotificationsListenerImpl listener = new NbiNotificationsListenerImpl(Map.of("test", publisherService),
131             Map.of("test", publisherAlarm), Map.of("test", publisherTapiService));
132         PublishTapiNotificationService notification
133             = new PublishTapiNotificationServiceBuilder(NotificationServiceDataUtils.buildReceivedTapiAlarmEvent())
134                 .setTopic(UUID.randomUUID().toString())
135                 .build();
136         listener.onPublishTapiNotificationService(notification);
137         verify(publisherTapiService, times(0)).sendEvent(any(), eq(notification.getTopic()));
138     }
139
140     @Test
141     public void getTapiPublisherFromTopicTest() {
142         NbiNotificationsListenerImpl listener = new NbiNotificationsListenerImpl(Map.of("test", publisherService),
143                 Map.of("test", publisherAlarm), Map.of("test", publisherTapiService));
144         assertNull(listener.getTapiPublisherFromTopic("toto"));
145         assertEquals(publisherTapiService, listener.getTapiPublisherFromTopic("test"));
146     }
147 }