Add new Maven module to manage NBI Notifications
[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.NotificationServiceSerializer;
23 import org.opendaylight.transportpce.test.AbstractTest;
24 import org.opendaylight.yang.gen.v1.nbi.notifications.rev201130.NotificationService;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
26 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactorySupplier;
27
28 public class PublisherTest extends AbstractTest {
29     private JsonStringConverter<NotificationService> converter;
30     private Publisher publisher;
31     private MockProducer<String, NotificationService> mockProducer;
32
33     @Before
34     public void setUp() {
35         NotificationServiceSerializer serializer = new NotificationServiceSerializer();
36         Map<String, Object> properties = Map.of(ConfigConstants.CONVERTER , serializer);
37         serializer.configure(properties, false);
38         mockProducer =  new MockProducer<>(true, new StringSerializer(), serializer);
39         converter = new JsonStringConverter<NotificationService>(
40                 getDataStoreContextUtil().getBindingDOMCodecServices());
41         publisher = new Publisher("test",mockProducer);
42     }
43
44     @Test
45     public void sendEventShouldBeSuccessful() throws IOException {
46         String json = Files.readString(Paths.get("src/test/resources/event.json"));
47         NotificationService notificationService = converter
48                 .createDataObjectFromJsonString(YangInstanceIdentifier.of(NotificationService.QNAME),
49                         json, JSONCodecFactorySupplier.RFC7951);
50         publisher.sendEvent(notificationService);
51         assertEquals("We should have one message", 1, mockProducer.history().size());
52         assertEquals("Key should be test", "test",mockProducer.history().get(0).key());
53     }
54
55 }