package io.wealthwizards.appsynctesting.utils;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

import java.util.Map;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;

@DisplayName("$util.xml")
public class XmlTest {
    @Test
    @DisplayName(".toMap")
    void shouldConvertToMap() {
        var xml = new Xml();

        var raw = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<posts>\n" +
                "<post>\n" +
                " <id>1</id>\n" +
                " <title>Getting Started with GraphQL</title>\n" +
                "</post>\n" +
                "<post>\n" +
                " <id>2</id>\n" +
                " <title>Getting Started with AWS AppSync</title>\n" +
                "</post>\n" +
                "</posts>";

        var actual = xml.toMap(raw);
        var expected = Map.of(
                "posts", Map.of(
                        "post", List.of(
                                Map.of(
                                        "id", 1,
                                        "title", "Getting Started with GraphQL"
                                ),
                                Map.of(
                                        "id", 2,
                                        "title", "Getting Started with AWS AppSync"
                                )
                        )
                )
        );

        assertEquals(expected, actual);
    }

    @Nested
    @DisplayName(".toJsonString")
    class ToJsonString {
        @Test
        @DisplayName("with original config, inferring types")
        void shouldConvertAndInferTypes() {
            var xml = new Xml();

            var raw = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                    "<posts>\n" +
                    "<post>\n" +
                    " <id>1</id>\n" +
                    " <title>Getting Started with GraphQL</title>\n" +
                    "</post>\n" +
                    "<post>\n" +
                    " <id>2</id>\n" +
                    " <title>Getting Started with AWS AppSync</title>\n" +
                    "</post>\n" +
                    "</posts>";

            var actual = xml.toJsonString(raw);
            var expected = "{\"posts\":{\"post\":[{\"id\":1,\"title\":\"Getting Started with GraphQL\"},{\"id\":2,\"title\":\"Getting Started with AWS AppSync\"}]}}";

            assertEquals(expected, actual);
        }

        @Test
        @DisplayName("with keep strings")
        void shouldConvertWithOriginalStrings() {
            var xml = new Xml();

            var raw = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                    "<posts>\n" +
                    "<post>\n" +
                    " <id>1</id>\n" +
                    " <title>Getting Started with GraphQL</title>\n" +
                    "</post>\n" +
                    "<post>\n" +
                    " <id>2</id>\n" +
                    " <title>Getting Started with AWS AppSync</title>\n" +
                    "</post>\n" +
                    "</posts>";

            var actual = xml.toJsonString(raw, true);
            var expected = "{\"posts\":{\"post\":[{\"id\":\"1\",\"title\":\"Getting Started with GraphQL\"},{\"id\":\"2\",\"title\":\"Getting Started with AWS AppSync\"}]}}";

            assertEquals(expected, actual);
        }
    }
}
