085f57e40caf4e51eac2f7d067ba2e4a1bc6f5e6
[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.JSONDataObjectConverter;
23 import org.opendaylight.yang.gen.v1.gnpy.gnpy.api.rev220221.Request;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
26 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactorySupplier;
27
28 public class JsonStringConverterTest extends AbstractTest {
29
30     @Test
31     public void createJsonStringFromDataObjectTest() {
32         try (Reader reader = new FileReader("src/test/resources/gnpy_request.json", StandardCharsets.UTF_8)) {
33             assertEquals(
34                 "Should be a valid request",
35                 Files.readString(Paths.get("src/test/resources/expected_string.json")),
36                 new JsonStringConverter<Request>(getDataStoreContextUtil().getBindingDOMCodecServices())
37                     .createJsonStringFromDataObject(
38                         InstanceIdentifier.builder(Request.class).build(),
39                         //gnpyRequest
40                         (Request) getDataStoreContextUtil()
41                             .getBindingDOMCodecServices()
42                             .fromNormalizedNode(
43                                 YangInstanceIdentifier.of(Request.QNAME),
44                                 JSONDataObjectConverter
45                                     .createWithDataStoreUtil(getDataStoreContextUtil())
46                                     .transformIntoNormalizedNode(reader)
47                                     .get())
48                             .getValue(),
49                         JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02));
50         } catch (IOException e) {
51             fail("Cannot load path description ");
52         }
53     }
54
55     @Test
56     public void createDataObjectFromJsonStringTest() throws IOException {
57         assertNotNull(
58             "Should not be null",
59             new JsonStringConverter<Request>(getDataStoreContextUtil().getBindingDOMCodecServices())
60                 .createDataObjectFromJsonString(
61                     YangInstanceIdentifier.of(Request.QNAME),
62                     Files.readString(Paths.get("src/test/resources/expected_string.json")),
63                     JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02));
64     }
65 }