T-API notification JUnit test
[transportpce.git] / nbinotifications / src / test / java / org / opendaylight / transportpce / nbinotifications / serialization / NotificationTapiServiceSerializerTest.java
1 /*
2  * Copyright © 2021 Nokia, 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 com.fasterxml.jackson.databind.JsonNode;
14 import com.fasterxml.jackson.databind.ObjectMapper;
15 import java.io.IOException;
16 import java.nio.charset.StandardCharsets;
17 import java.nio.file.Files;
18 import java.nio.file.Paths;
19 import java.util.Map;
20 import org.junit.Test;
21 import org.opendaylight.transportpce.common.converter.JsonStringConverter;
22 import org.opendaylight.transportpce.test.AbstractTest;
23 import org.opendaylight.yang.gen.v1.nbi.notifications.rev211013.NotificationTapiService;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
25 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactorySupplier;
26
27 public class NotificationTapiServiceSerializerTest extends AbstractTest {
28
29     @Test
30     public void serializeTest() throws IOException {
31         JsonStringConverter<NotificationTapiService> converter =
32                 new JsonStringConverter<>(getDataStoreContextUtil().getBindingDOMCodecServices());
33         String json = Files.readString(Paths.get("src/test/resources/tapi_event.json"));
34         NotificationTapiService notificationService = converter
35                 .createDataObjectFromJsonString(YangInstanceIdentifier.of(NotificationTapiService.QNAME),
36                         json, JSONCodecFactorySupplier.RFC7951);
37         TapiNotificationSerializer serializer = new TapiNotificationSerializer();
38         Map<String, Object> configs = Map.of(ConfigConstants.CONVERTER, converter);
39         serializer.configure(configs, false);
40         byte[] data = serializer.serialize("test", notificationService);
41         serializer.close();
42         assertNotNull("Serialized data should not be null", data);
43         String expectedJson = Files.readString(Paths.get("src/test/resources/expected_tapi_event.json"));
44         // Minify the json string
45         expectedJson = new ObjectMapper().readValue(expectedJson, JsonNode.class).toString();
46         assertEquals("The event should be equals", expectedJson, new String(data, StandardCharsets.UTF_8));
47     }
48 }