/**
 * This code was generated by
 * \ / _    _  _|   _  _
 *  | (_)\/(_)(_|\/| |(/_  v1.0.0
 *       /       /
 */

package com.twilio.twiml.voice;

import com.twilio.http.HttpMethod;
import com.twilio.twiml.GenericNode;
import org.junit.Assert;
import org.junit.Test;

import java.net.URI;

/**
 * Test class for {@link Enqueue}
 */
public class EnqueueTest {
    @Test
    public void testEmptyElement() {
        Enqueue elem = new Enqueue.Builder().build();

        Assert.assertEquals(
            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
            "<Enqueue/>",
            elem.toXml()
        );
    }

    @Test
    public void testEmptyElementUrl() {
        Enqueue elem = new Enqueue.Builder().build();

        Assert.assertEquals("%3C%3Fxml+version%3D%221.0%22+encoding%3D%22UTF-8%22%3F%3E%3CEnqueue%2F%3E", elem.toUrl());
    }

    @Test
    public void testElementWithParams() {
        Enqueue elem = new Enqueue.Builder("name")
            .action(URI.create("https://example.com"))
            .method(HttpMethod.GET)
            .waitUrl(URI.create("https://example.com"))
            .waitUrlMethod(HttpMethod.GET)
            .workflowSid("workflow_sid")
            .build();

        Assert.assertEquals(
            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
            "<Enqueue action=\"https://example.com\" method=\"GET\" waitUrl=\"https://example.com\" waitUrlMethod=\"GET\" workflowSid=\"workflow_sid\">name</Enqueue>",
            elem.toXml()
        );
    }

    @Test
    public void testElementWithExtraAttributes() {
        Enqueue elem = new Enqueue.Builder().option("foo", "bar").option("a", "b").build();

        Assert.assertEquals(
            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
            "<Enqueue a=\"b\" foo=\"bar\"/>",
            elem.toXml()
        );
    }

    @Test
    public void testElementWithChildren() {
        Enqueue.Builder builder = new Enqueue.Builder();

        builder.task(new Task.Builder("body").priority(1).timeout(1).build());

        Enqueue elem = builder.build();

        Assert.assertEquals(
            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
            "<Enqueue>" +
                "<Task priority=\"1\" timeout=\"1\">body</Task>" +
            "</Enqueue>",
            elem.toXml()
        );
    }

    @Test
    public void testElementWithTextNode() {
        Enqueue.Builder builder = new Enqueue.Builder();

        builder.addText("Hey no tags!");

        Enqueue elem = builder.build();

        Assert.assertEquals(
            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
            "<Enqueue>" +
            "Hey no tags!" +
            "</Enqueue>",
            elem.toXml()
        );
    }

    @Test
    public void testMixedContent() {
        GenericNode.Builder child = new GenericNode.Builder("Child");
        child.addText("content");

        Enqueue.Builder builder = new Enqueue.Builder();

        builder.addText("before");
        builder.addChild(child.build());
        builder.addText("after");

        Assert.assertEquals(
            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
            "<Enqueue>" +
            "before" +
            "<Child>content</Child>" +
            "after" +
            "</Enqueue>",
            builder.build().toXml()
        );
    }

    @Test
    public void testElementWithGenericNode() {
        GenericNode.Builder genericBuilder = new GenericNode.Builder("genericTag");
        genericBuilder.addText("Some text");
        GenericNode node = genericBuilder.build();

        Enqueue.Builder builder = new Enqueue.Builder();
        Enqueue elem = builder.addChild(node).build();

        Assert.assertEquals(
            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
            "<Enqueue>" +
            "<genericTag>" +
            "Some text" +
            "</genericTag>" +
            "</Enqueue>",
            elem.toXml()
        );
    }

    @Test
    public void testElementWithGenericNodeAttributes() {
        GenericNode.Builder genericBuilder = new GenericNode.Builder("genericTag");
        GenericNode node = genericBuilder.option("key", "value").addText("someText").build();

        Enqueue.Builder builder = new Enqueue.Builder();
        Enqueue elem = builder.addChild(node).build();

        Assert.assertEquals(
            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
            "<Enqueue>" +
            "<genericTag key=\"value\">" +
            "someText" +
            "</genericTag>" +
            "</Enqueue>",
            elem.toXml()
        );
    }

    @Test
    public void testXmlAttributesDeserialization() {
        final Enqueue elem = new Enqueue.Builder("name")
            .action(URI.create("https://example.com"))
            .method(HttpMethod.GET)
            .waitUrl(URI.create("https://example.com"))
            .waitUrlMethod(HttpMethod.GET)
            .workflowSid("workflow_sid")
            .build();

        Assert.assertEquals(
            Enqueue.Builder.fromXml("<Enqueue action=\"https://example.com\" method=\"GET\" waitUrl=\"https://example.com\" waitUrlMethod=\"GET\" workflowSid=\"workflow_sid\">name</Enqueue>").build().toXml(),
            elem.toXml()
        );
    }

    @Test
    public void testXmlChildrenDeserialization() {
        final Enqueue.Builder builder = new Enqueue.Builder();

        builder.task(new Task.Builder("body").priority(1).timeout(1).build());

        final Enqueue elem = builder.build();

        Assert.assertEquals(
            Enqueue.Builder.fromXml("<Enqueue>" +
                "<Task priority=\"1\" timeout=\"1\">body</Task>" +
            "</Enqueue>").build().toXml(),
            elem.toXml()
        );
    }
}