2 * Copyright © 2021 Nokia, Inc. and others. All rights reserved.
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
8 package org.opendaylight.transportpce.nbinotifications.serialization;
11 import static org.junit.jupiter.api.Assertions.assertNotNull;
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;
20 import org.json.JSONException;
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.rev230728.NotificationTapiService;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
26 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactorySupplier;
27 import org.skyscreamer.jsonassert.JSONAssert;
29 public class NotificationTapiServiceSerializerTest extends AbstractTest {
32 void serializeTest() throws IOException, JSONException {
33 JsonStringConverter<NotificationTapiService> converter =
34 new JsonStringConverter<>(getDataStoreContextUtil().getBindingDOMCodecServices());
35 String json = Files.readString(Paths.get("src/test/resources/tapi_event.json"));
36 NotificationTapiService notificationService = converter
37 .createDataObjectFromJsonString(YangInstanceIdentifier.of(NotificationTapiService.QNAME),
38 json, JSONCodecFactorySupplier.RFC7951);
39 TapiNotificationSerializer serializer = new TapiNotificationSerializer();
40 Map<String, Object> configs = Map.of(ConfigConstants.CONVERTER, converter);
41 serializer.configure(configs, false);
42 byte[] data = serializer.serialize("test", notificationService);
44 assertNotNull(data, "Serialized data should not be null");
45 String expectedJson = Files.readString(Paths.get("src/test/resources/expected_tapi_event.json"));
46 // Minify the json string
47 expectedJson = new ObjectMapper().readValue(expectedJson, JsonNode.class).toString();
48 JSONAssert.assertEquals(expectedJson, new String(data, StandardCharsets.UTF_8), true);