26d4edd538e78beb01dc2354881902b80d5f265b
[transportpce.git] / nbinotifications / src / test / java / org / opendaylight / transportpce / nbinotifications / producer / PublisherTest.java
1 /*
2  * Copyright © 2020 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.producer;
9
10
11 import static org.junit.jupiter.api.Assertions.assertEquals;
12
13 import java.io.IOException;
14 import java.nio.file.Files;
15 import java.nio.file.Paths;
16 import java.util.HashSet;
17 import java.util.List;
18 import java.util.Map;
19 import java.util.concurrent.ExecutionException;
20 import org.apache.kafka.clients.producer.MockProducer;
21 import org.apache.kafka.common.serialization.StringSerializer;
22 import org.junit.jupiter.api.BeforeEach;
23 import org.junit.jupiter.api.Test;
24 import org.mockito.MockitoAnnotations;
25 import org.opendaylight.transportpce.common.converter.JsonStringConverter;
26 import org.opendaylight.transportpce.common.network.NetworkTransactionImpl;
27 import org.opendaylight.transportpce.common.network.NetworkTransactionService;
28 import org.opendaylight.transportpce.nbinotifications.impl.NbiNotificationsImpl;
29 import org.opendaylight.transportpce.nbinotifications.serialization.ConfigConstants;
30 import org.opendaylight.transportpce.nbinotifications.serialization.NotificationAlarmServiceSerializer;
31 import org.opendaylight.transportpce.nbinotifications.serialization.NotificationServiceSerializer;
32 import org.opendaylight.transportpce.nbinotifications.serialization.TapiNotificationSerializer;
33 import org.opendaylight.transportpce.nbinotifications.utils.NotificationServiceDataUtils;
34 import org.opendaylight.transportpce.nbinotifications.utils.TopicManager;
35 import org.opendaylight.transportpce.test.AbstractTest;
36 import org.opendaylight.yang.gen.v1.nbi.notifications.rev230726.NotificationAlarmService;
37 import org.opendaylight.yang.gen.v1.nbi.notifications.rev230726.NotificationProcessService;
38 import org.opendaylight.yang.gen.v1.nbi.notifications.rev230726.NotificationTapiService;
39 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev181210.Uuid;
40 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.notification.rev181210.CreateNotificationSubscriptionServiceInputBuilder;
41 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.notification.rev181210.create.notification.subscription.service.input.SubscriptionFilter;
42 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.notification.rev181210.create.notification.subscription.service.input.SubscriptionFilterBuilder;
43 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
44 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactorySupplier;
45
46 public class PublisherTest extends AbstractTest {
47     private static NetworkTransactionService networkTransactionService;
48
49     private JsonStringConverter<NotificationProcessService> converterService;
50     private JsonStringConverter<NotificationAlarmService> converterAlarm;
51     private JsonStringConverter<NotificationTapiService> converterTapiService;
52     private Publisher<NotificationProcessService> publisherService;
53     private Publisher<NotificationAlarmService> publisherAlarm;
54     private Publisher<NotificationTapiService> publisherTapiService;
55     private MockProducer<String, NotificationProcessService> mockProducer;
56     private MockProducer<String, NotificationAlarmService> mockAlarmProducer;
57     private MockProducer<String, NotificationTapiService> mockTapiProducer;
58     private NbiNotificationsImpl nbiNotificationsImpl;
59     private TopicManager topicManager;
60
61
62     @BeforeEach
63     void setUp() throws ExecutionException, InterruptedException {
64         topicManager = TopicManager.getInstance();
65         converterService = new JsonStringConverter<>(getDataStoreContextUtil().getBindingDOMCodecServices());
66         converterAlarm = new JsonStringConverter<>(getDataStoreContextUtil().getBindingDOMCodecServices());
67         converterTapiService = new JsonStringConverter<>(getDataStoreContextUtil().getBindingDOMCodecServices());
68         NotificationServiceSerializer serializerService = new NotificationServiceSerializer();
69         NotificationAlarmServiceSerializer serializerAlarm = new NotificationAlarmServiceSerializer();
70         TapiNotificationSerializer serializerTapi = new TapiNotificationSerializer();
71         Map<String, Object> properties = Map.of(ConfigConstants.CONVERTER, converterService);
72         Map<String, Object> propertiesAlarm = Map.of(ConfigConstants.CONVERTER, converterAlarm);
73         Map<String, Object> propertiesTapi = Map.of(ConfigConstants.CONVERTER, converterTapiService);
74         serializerService.configure(properties, false);
75         serializerAlarm.configure(propertiesAlarm, false);
76         serializerTapi.configure(propertiesTapi, false);
77         mockProducer = new MockProducer<>(true, new StringSerializer(), serializerService);
78         mockAlarmProducer = new MockProducer<>(true, new StringSerializer(), serializerAlarm);
79         mockTapiProducer = new MockProducer<>(true, new StringSerializer(), serializerTapi);
80         publisherService = new Publisher<>("test", mockProducer);
81         publisherAlarm = new Publisher<>("test", mockAlarmProducer);
82         publisherTapiService = new Publisher<>("test", mockTapiProducer);
83         MockitoAnnotations.openMocks(this);
84         networkTransactionService = new NetworkTransactionImpl(getDataBroker());
85         topicManager.setTapiConverter(converterTapiService);
86         NotificationServiceDataUtils.createTapiContext(networkTransactionService);
87         nbiNotificationsImpl = new NbiNotificationsImpl(converterService, converterAlarm, converterTapiService,
88             "localhost:8080", networkTransactionService, topicManager);
89     }
90
91     @Test
92     void sendEventServiceShouldBeSuccessful() throws IOException {
93         String json = Files.readString(Paths.get("src/test/resources/event.json"));
94         NotificationProcessService notificationProcessService = converterService
95                 .createDataObjectFromJsonString(YangInstanceIdentifier.of(NotificationProcessService.QNAME),
96                         json, JSONCodecFactorySupplier.RFC7951);
97         publisherService.sendEvent(notificationProcessService, notificationProcessService.getConnectionType().name());
98         assertEquals(1, mockProducer.history().size(), "We should have one message");
99         assertEquals("test", mockProducer.history().get(0).key(), "Key should be test");
100     }
101
102     @Test
103     void sendEventAlarmShouldBeSuccessful() throws IOException {
104         String json = Files.readString(Paths.get("src/test/resources/event_alarm_service.json"));
105         NotificationAlarmService notificationAlarmService = converterAlarm
106                 .createDataObjectFromJsonString(YangInstanceIdentifier.of(NotificationAlarmService.QNAME),
107                         json, JSONCodecFactorySupplier.RFC7951);
108         publisherAlarm.sendEvent(notificationAlarmService, "alarm"
109                 + notificationAlarmService.getConnectionType().getName());
110         assertEquals(1, mockAlarmProducer.history().size(), "We should have one message");
111         assertEquals("test", mockAlarmProducer.history().get(0).key(), "Key should be test");
112     }
113
114     @Test
115     void sendTapiEventShouldBeSuccessful() throws IOException {
116         CreateNotificationSubscriptionServiceInputBuilder builder
117             = NotificationServiceDataUtils.buildNotificationSubscriptionServiceInputBuilder();
118         SubscriptionFilter subscriptionFilter = new SubscriptionFilterBuilder(builder.getSubscriptionFilter())
119             .setRequestedObjectIdentifier(new HashSet<>(List.of(new Uuid("76d8f07b-ead5-4132-8eb8-cf3fdef7e079"))))
120             .build();
121         builder.setSubscriptionFilter(subscriptionFilter);
122         nbiNotificationsImpl.createNotificationSubscriptionService(builder.build());
123         String json = Files.readString(Paths.get("src/test/resources/tapi_event.json"));
124         NotificationTapiService notificationTapiService = converterTapiService
125             .createDataObjectFromJsonString(YangInstanceIdentifier.of(NotificationTapiService.QNAME),
126                 json, JSONCodecFactorySupplier.RFC7951);
127         publisherTapiService.sendEvent(notificationTapiService, "");
128         assertEquals(1, mockTapiProducer.history().size(), "We should have one message");
129         assertEquals("test", mockTapiProducer.history().get(0).key(), "Key should be test");
130     }
131 }