1d988fc0ce2700b4bd6ca9d43fc7cb168879f181
[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 import static org.junit.Assert.assertEquals;
11
12 import java.io.IOException;
13 import java.nio.file.Files;
14 import java.nio.file.Paths;
15 import java.util.Map;
16 import org.apache.kafka.clients.producer.MockProducer;
17 import org.apache.kafka.common.serialization.StringSerializer;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.opendaylight.transportpce.common.converter.JsonStringConverter;
21 import org.opendaylight.transportpce.nbinotifications.serialization.ConfigConstants;
22 import org.opendaylight.transportpce.nbinotifications.serialization.NotificationAlarmServiceSerializer;
23 import org.opendaylight.transportpce.nbinotifications.serialization.NotificationServiceSerializer;
24 import org.opendaylight.transportpce.test.AbstractTest;
25 import org.opendaylight.yang.gen.v1.nbi.notifications.rev211013.NotificationAlarmService;
26 import org.opendaylight.yang.gen.v1.nbi.notifications.rev211013.NotificationProcessService;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
28 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactorySupplier;
29
30 public class PublisherTest extends AbstractTest {
31     private JsonStringConverter<NotificationProcessService> converterService;
32     private JsonStringConverter<NotificationAlarmService> converterAlarm;
33     private Publisher<NotificationProcessService> publisherService;
34     private Publisher<NotificationAlarmService> publisherAlarm;
35     private MockProducer<String, NotificationProcessService> mockProducer;
36     private MockProducer<String, NotificationAlarmService> mockAlarmProducer;
37
38     @Before
39     public void setUp() {
40         converterService = new JsonStringConverter<>(getDataStoreContextUtil().getBindingDOMCodecServices());
41         converterAlarm = new JsonStringConverter<>(getDataStoreContextUtil().getBindingDOMCodecServices());
42         NotificationServiceSerializer serializerService = new NotificationServiceSerializer();
43         NotificationAlarmServiceSerializer serializerAlarm = new NotificationAlarmServiceSerializer();
44         Map<String, Object> properties = Map.of(ConfigConstants.CONVERTER, converterService);
45         Map<String, Object> propertiesAlarm = Map.of(ConfigConstants.CONVERTER, converterAlarm);
46         serializerService.configure(properties, false);
47         serializerAlarm.configure(propertiesAlarm, false);
48         mockProducer = new MockProducer<>(true, new StringSerializer(), serializerService);
49         mockAlarmProducer = new MockProducer<>(true, new StringSerializer(), serializerAlarm);
50         publisherService = new Publisher<>("test", mockProducer);
51         publisherAlarm = new Publisher<>("test", mockAlarmProducer);
52     }
53
54     @Test
55     public void sendEventServiceShouldBeSuccessful() throws IOException {
56         String json = Files.readString(Paths.get("src/test/resources/event.json"));
57         NotificationProcessService notificationProcessService = converterService
58                 .createDataObjectFromJsonString(YangInstanceIdentifier.of(NotificationProcessService.QNAME),
59                         json, JSONCodecFactorySupplier.RFC7951);
60         publisherService.sendEvent(notificationProcessService, notificationProcessService.getConnectionType().name());
61         assertEquals("We should have one message", 1, mockProducer.history().size());
62         assertEquals("Key should be test", "test", mockProducer.history().get(0).key());
63     }
64
65     @Test
66     public void sendEventAlarmShouldBeSuccessful() throws IOException {
67         String json = Files.readString(Paths.get("src/test/resources/event_alarm_service.json"));
68         NotificationAlarmService notificationAlarmService = converterAlarm
69                 .createDataObjectFromJsonString(YangInstanceIdentifier.of(NotificationAlarmService.QNAME),
70                         json, JSONCodecFactorySupplier.RFC7951);
71         publisherAlarm.sendEvent(notificationAlarmService, "alarm"
72                 + notificationAlarmService.getConnectionType().getName());
73         assertEquals("We should have one message", 1, mockAlarmProducer.history().size());
74         assertEquals("Key should be test", "test", mockAlarmProducer.history().get(0).key());
75     }
76 }