Add generic json String converter
[transportpce.git] / common / src / test / java / org / opendaylight / transportpce / common / converter / JsonStringConverterTest.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.common.converter;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.fail;
13
14 import java.io.FileReader;
15 import java.io.IOException;
16 import java.io.Reader;
17 import java.nio.charset.StandardCharsets;
18 import java.nio.file.Files;
19 import java.nio.file.Paths;
20 import org.junit.Test;
21 import org.opendaylight.transportpce.test.AbstractTest;
22 import org.opendaylight.transportpce.test.converter.DataObjectConverter;
23 import org.opendaylight.transportpce.test.converter.JSONDataObjectConverter;
24 import org.opendaylight.yang.gen.v1.gnpy.gnpy.api.rev190103.GnpyApi;
25 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
28 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
29 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactorySupplier;
30
31 public class JsonStringConverterTest extends AbstractTest {
32
33     @Test
34     public void createJsonStringFromDataObjectTest() {
35         DataObjectConverter dataObjectConverter = JSONDataObjectConverter
36                 .createWithDataStoreUtil(getDataStoreContextUtil());
37         try (Reader reader = new FileReader("src/test/resources/gnpy_request.json", StandardCharsets.UTF_8)) {
38             NormalizedNode<? extends PathArgument, ?> normalizedNode = dataObjectConverter
39                     .transformIntoNormalizedNode(reader).get();
40             GnpyApi gnpyRequest = (GnpyApi) getDataStoreContextUtil().getBindingDOMCodecServices()
41                     .fromNormalizedNode(YangInstanceIdentifier.of(GnpyApi.QNAME), normalizedNode).getValue();
42             JsonStringConverter<GnpyApi> gnpyJsonCOnverter = new JsonStringConverter<GnpyApi>(
43                     getDataStoreContextUtil().getBindingDOMCodecServices());
44             String jsonString = gnpyJsonCOnverter
45                     .createJsonStringFromDataObject(InstanceIdentifier.builder(GnpyApi.class).build(),
46                             gnpyRequest, JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02);
47             assertEquals("Should be a valid request",
48                     Files.readString(Paths.get("src/test/resources/expected_string.json")), jsonString);
49         } catch (IOException e) {
50             fail("Cannot load path description ");
51         }
52     }
53
54     @Test
55     public void createDataObjectFromJsonStringTest() throws IOException {
56         String json = Files.readString(Paths.get("src/test/resources/expected_string.json"));
57         JsonStringConverter<GnpyApi> gnpyJsonCOnverter = new JsonStringConverter<GnpyApi>(
58                 getDataStoreContextUtil().getBindingDOMCodecServices());
59         GnpyApi request = gnpyJsonCOnverter
60                 .createDataObjectFromJsonString(YangInstanceIdentifier.of(GnpyApi.QNAME), json,
61                         JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02);
62         assertNotNull("Should not be null", request);
63     }
64 }