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.assertEquals;
12 import static org.junit.jupiter.api.Assertions.assertNotNull;
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;
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;
28 public class NotificationTapiServiceSerializerTest extends AbstractTest {
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);
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");