View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2013-2025 Yegor Bugayenko
3    * SPDX-License-Identifier: MIT
4    */
5   package com.jcabi.github.mock;
6   
7   import com.jcabi.xml.XML;
8   import com.jcabi.xml.XMLDocument;
9   import jakarta.json.JsonObject;
10  import org.hamcrest.MatcherAssert;
11  import org.hamcrest.Matchers;
12  import org.hamcrest.core.IsEqual;
13  import org.junit.jupiter.api.Test;
14  
15  /**
16   * Test case for {@link JsonNode}.
17   * @since 0.5
18   */
19  final class JsonNodeTest {
20  
21      @Test
22      void convertsXmlToJson() {
23          final XML xml = new XMLDocument(
24              "<user><name>Jeff</name><dept><title>IT</title></dept></user>"
25          );
26          final JsonObject json = new JsonNode(xml.nodes("user").get(0)).json();
27          MatcherAssert.assertThat(
28              "Value is null", json, Matchers.notNullValue()
29          );
30          MatcherAssert.assertThat(
31              "Values are not equal",
32              json.getString("name"),
33              Matchers.equalTo("Jeff")
34          );
35          MatcherAssert.assertThat(
36              "Values are not equal",
37              json.getJsonObject("dept").getString("title"),
38              Matchers.equalTo("IT")
39          );
40      }
41  
42      @Test
43      void convertsXmlToJsonArray() {
44          final XML xml = new XMLDocument(
45              // @checkstyle LineLength (1 line)
46              "<users array=\"true\"><item>Jeff</item><item>Bauer</item><item>Iko</item></users>"
47          );
48          final JsonObject json = new JsonNode(xml).json();
49          MatcherAssert.assertThat(
50              "Assertion failed",
51              json.toString(),
52              new IsEqual<>("{\"users\":[\"Jeff\",\"Bauer\",\"Iko\"]}")
53          );
54      }
55  
56  }