Migration to TAPI 2.4 Step2
[transportpce.git] / nbinotifications / src / test / java / org / opendaylight / transportpce / nbinotifications / listener / NbiNotificationsHandlerTest.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.jupiter.api.Assertions.assertEquals;
11 import static org.junit.jupiter.api.Assertions.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.jupiter.api.BeforeEach;
22 import org.junit.jupiter.api.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.rev230526.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.rev230728.NotificationAlarmService;
31 import org.opendaylight.yang.gen.v1.nbi.notifications.rev230728.NotificationProcessService;
32 import org.opendaylight.yang.gen.v1.nbi.notifications.rev230728.NotificationTapiService;
33 import org.opendaylight.yang.gen.v1.nbi.notifications.rev230728.PublishNotificationAlarmService;
34 import org.opendaylight.yang.gen.v1.nbi.notifications.rev230728.PublishNotificationAlarmServiceBuilder;
35 import org.opendaylight.yang.gen.v1.nbi.notifications.rev230728.PublishNotificationProcessService;
36 import org.opendaylight.yang.gen.v1.nbi.notifications.rev230728.PublishNotificationProcessServiceBuilder;
37 import org.opendaylight.yang.gen.v1.nbi.notifications.rev230728.PublishTapiNotificationService;
38 import org.opendaylight.yang.gen.v1.nbi.notifications.rev230728.PublishTapiNotificationServiceBuilder;
39
40 public class NbiNotificationsHandlerTest 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     @BeforeEach
49     void setUp() throws ExecutionException, InterruptedException {
50         MockitoAnnotations.openMocks(this);
51     }
52
53     @Test
54     void onPublishNotificationServiceTest() {
55         NbiNotificationsHandler listener = new NbiNotificationsHandler(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                 .setIsTempService(false)
62                 .setMessage("Service deleted")
63                 .setOperationalState(State.OutOfService)
64                 .setServiceName("service name")
65                 .build();
66         listener.onPublishNotificationProcessService(notification);
67         verify(publisherService, times(1)).sendEvent(any(), anyString());
68     }
69
70     @Test
71     void onPublishNotificationServiceWrongPublisherTest() {
72         NbiNotificationsHandler listener = new NbiNotificationsHandler(Map.of("test", publisherService),
73                 Map.of("test", publisherAlarm), Map.of("test", publisherTapiService));
74         PublishNotificationProcessService notification = new PublishNotificationProcessServiceBuilder()
75                 .setPublisherName("wrongPublisher")
76                 .setCommonId("commonId")
77                 .setConnectionType(ConnectionType.Service)
78                 .setMessage("Service deleted")
79                 .setOperationalState(State.OutOfService)
80                 .setServiceName("service name")
81                 .build();
82         listener.onPublishNotificationProcessService(notification);
83         verify(publisherService, times(0)).sendEvent(any(), anyString());
84     }
85
86     @Test
87     void onPublishNotificationAlarmServiceTest() {
88         NbiNotificationsHandler listener = new NbiNotificationsHandler(Map.of("test", publisherService),
89                 Map.of("test", publisherAlarm), Map.of("test", publisherTapiService));
90         PublishNotificationAlarmService notification = new PublishNotificationAlarmServiceBuilder()
91                 .setPublisherName("test")
92                 .setConnectionType(ConnectionType.Service)
93                 .setMessage("The service is now inService")
94                 .setOperationalState(State.OutOfService)
95                 .setServiceName("service name")
96                 .build();
97         listener.onPublishNotificationAlarmService(notification);
98         verify(publisherAlarm, times(1)).sendEvent(any(), anyString());
99     }
100
101     @Test
102     void onPublishNotificationAlarmServiceWrongPublisherTest() {
103         NbiNotificationsHandler listener = new NbiNotificationsHandler(Map.of("test", publisherService),
104                 Map.of("test", publisherAlarm), Map.of("test", publisherTapiService));
105         PublishNotificationAlarmService notification = new PublishNotificationAlarmServiceBuilder()
106                 .setPublisherName("wrongPublisher")
107                 .setConnectionType(ConnectionType.Service)
108                 .setMessage("The service is now inService")
109                 .setOperationalState(State.OutOfService)
110                 .setServiceName("service name")
111                 .build();
112         listener.onPublishNotificationAlarmService(notification);
113         verify(publisherAlarm, times(0)).sendEvent(any(), anyString());
114     }
115
116     @Test
117     void onPublishTapiNotificationServiceTest() throws ExecutionException, InterruptedException {
118         NbiNotificationsHandler listener = new NbiNotificationsHandler(Map.of("test", publisherService),
119                 Map.of("test", publisherAlarm), Map.of("test", publisherTapiService));
120
121         PublishTapiNotificationService notification
122             = new PublishTapiNotificationServiceBuilder(NotificationServiceDataUtils.buildReceivedTapiAlarmEvent())
123                 .setTopic("test")
124                 .build();
125         listener.onPublishTapiNotificationService(notification);
126         verify(publisherTapiService, times(1)).sendEvent(any(), anyString());
127     }
128
129     @Test
130     void onPublishTapiNotificationServiceTestWrongPublisherTest() {
131         NbiNotificationsHandler listener = new NbiNotificationsHandler(Map.of("test", publisherService),
132             Map.of("test", publisherAlarm), Map.of("test", publisherTapiService));
133         PublishTapiNotificationService notification
134             = new PublishTapiNotificationServiceBuilder(NotificationServiceDataUtils.buildReceivedTapiAlarmEvent())
135                 .setTopic(UUID.randomUUID().toString())
136                 .build();
137         listener.onPublishTapiNotificationService(notification);
138         verify(publisherTapiService, times(0)).sendEvent(any(), eq(notification.getTopic()));
139     }
140
141     @Test
142     void getTapiPublisherFromTopicTest() {
143         NbiNotificationsHandler listener = new NbiNotificationsHandler(Map.of("test", publisherService),
144                 Map.of("test", publisherAlarm), Map.of("test", publisherTapiService));
145         assertNull(listener.getTapiPublisherFromTopic("toto"));
146         assertEquals(publisherTapiService, listener.getTapiPublisherFromTopic("test"));
147     }
148 }