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

package com.twilio.twiml.voice;

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

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

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

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

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

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

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

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

        builder.stream(new Stream.Builder()
                    .name("name")
                    .connectorName("connector_name")
                    .url("url")
                    .track(Stream.Track.INBOUND_TRACK)
                    .statusCallback("status_callback")
                    .statusCallbackMethod(Stream.StatusCallbackMethod.GET)
                    .build());

        builder.siprec(new Siprec.Builder()
                    .name("name")
                    .connectorName("connector_name")
                    .track(Siprec.Track.INBOUND_TRACK)
                    .build());

        Stop elem = builder.build();

        Assert.assertEquals(
            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
            "<Stop>" +
                "<Stream connectorName=\"connector_name\" name=\"name\" statusCallback=\"status_callback\" statusCallbackMethod=\"GET\" track=\"inbound_track\" url=\"url\"/>" +
                "<Siprec connectorName=\"connector_name\" name=\"name\" track=\"inbound_track\"/>" +
            "</Stop>",
            elem.toXml()
        );
    }

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

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

        Stop elem = builder.build();

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

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

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

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

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

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

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

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

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

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

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

    @Test
    public void testXmlAttributesDeserialization() {
        final Stop elem = new Stop.Builder().build();

        Assert.assertEquals(
            Stop.Builder.fromXml("<Stop/>").build().toXml(),
            elem.toXml()
        );
    }

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

        builder.stream(new Stream.Builder()
                    .name("name")
                    .connectorName("connector_name")
                    .url("url")
                    .track(Stream.Track.INBOUND_TRACK)
                    .statusCallback("status_callback")
                    .statusCallbackMethod(Stream.StatusCallbackMethod.GET)
                    .build());

        builder.siprec(new Siprec.Builder()
                    .name("name")
                    .connectorName("connector_name")
                    .track(Siprec.Track.INBOUND_TRACK)
                    .build());

        final Stop elem = builder.build();

        Assert.assertEquals(
            Stop.Builder.fromXml("<Stop>" +
                "<Stream connectorName=\"connector_name\" name=\"name\" statusCallback=\"status_callback\" statusCallbackMethod=\"GET\" track=\"inbound_track\" url=\"url\"/>" +
                "<Siprec connectorName=\"connector_name\" name=\"name\" track=\"inbound_track\"/>" +
            "</Stop>").build().toXml(),
            elem.toXml()
        );
    }
}