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