Merge changes from topic "nbinotification"
[transportpce.git] / nbinotifications / src / test / java / org / opendaylight / transportpce / nbinotifications / serialization / NotificationServiceSerializerTest.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.serialization;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12
13 import java.io.IOException;
14 import java.nio.charset.StandardCharsets;
15 import java.nio.file.Files;
16 import java.nio.file.Paths;
17 import java.util.Map;
18 import org.junit.Test;
19 import org.opendaylight.transportpce.common.converter.JsonStringConverter;
20 import org.opendaylight.transportpce.test.AbstractTest;
21 import org.opendaylight.yang.gen.v1.nbi.notifications.rev201130.NotificationService;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
23 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactorySupplier;
24
25 public class NotificationServiceSerializerTest extends AbstractTest {
26
27     @Test
28     public void serializeTest() throws IOException {
29         JsonStringConverter<NotificationService> converter =
30                 new JsonStringConverter<>(getDataStoreContextUtil().getBindingDOMCodecServices());
31         String json = Files.readString(Paths.get("src/test/resources/event.json"));
32         NotificationService notificationService = converter
33                 .createDataObjectFromJsonString(YangInstanceIdentifier.of(NotificationService.QNAME),
34                         json, JSONCodecFactorySupplier.RFC7951);
35         NotificationServiceSerializer serializer = new NotificationServiceSerializer();
36         Map<String, Object> configs = Map.of(ConfigConstants.CONVERTER, converter);
37         serializer.configure(configs, false);
38         byte[] data = serializer.serialize("test", notificationService);
39         serializer.close();
40         assertNotNull("Serialized data should not be null", data);
41         String expectedJson = Files.readString(Paths.get("src/test/resources/expected_event.json"));
42         assertEquals("The event should be equals", expectedJson, new String(data, StandardCharsets.UTF_8));
43     }
44 }