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