/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.getopentest.actions;

import org.getopentest.actions.ReadXml;
import java.util.Map;
import static org.testng.Assert.*;
import org.testng.annotations.Test;

/**
 *
 * @author mc28256
 */
public class ReadXmlNGTest {

    @Test
    public void testWithDefaultArgumentValues() {
        ReadXml readXml = new ReadXml();
        String xml = "<root attr1=\"value1\"><child1 attr1=\"value1\">child1 text</child1></root>";
        readXml.writeArgument("xml", xml);
        readXml.run();
        assertEquals(readXml.readOutputValue("value"), "child1 text");
        assertEquals(readXml.readOutputValue("object"), null);
    }

    @Test
    public void testAttributeValue() {
        ReadXml readXml = new ReadXml();
        String xml = "<root attr1=\"value1\"><child1 attr1=\"value1\">child1 text</child1></root>";
        readXml.writeArgument("xml", xml);
        readXml.writeArgument("xPath", "/root/@attr1");
        readXml.run();
        assertEquals(readXml.readOutputValue("value"), "value1");
        assertEquals(readXml.readOutputValue("object"), null);
    }
    
    @Test
    public void testElementValue() {
        ReadXml readXml = new ReadXml();
        String xml = "<root attr1=\"value1\"><child1 attr1=\"value1\">child1 text</child1></root>";
        readXml.writeArgument("xml", xml);
        readXml.writeArgument("xPath", "/root/child1");
        readXml.run();
        assertEquals(readXml.readOutputValue("value"), "child1 text");
        assertEquals(readXml.readOutputValue("object"), null);
    }

    @Test
    public void testAsObjectAttributeValue() {
        ReadXml readXml = new ReadXml();
        String xml = "<root attr1=\"value1\"><child1 attr1=\"value1\">child1 text</child1></root>";
        readXml.writeArgument("xml", xml);
        readXml.writeArgument("outputAsObject", true);
        readXml.run();
        assertEquals(readXml.readOutputValue("value"), null);
        assertNotNull(readXml.readOutputValue("object"));
        Map<String, Object> output = (Map<String, Object>) readXml.readOutputValue("object");
        assertEquals(output.get("$attr1"), "value1");
    }

    @Test
    public void testAsObjectElementValue() {
        ReadXml readXml = new ReadXml();
        String xml = "<root attr1=\"value1\"><child1 attr1=\"value1\">child1 text</child1></root>";
        readXml.writeArgument("xml", xml);
        readXml.writeArgument("outputAsObject", true);
        readXml.run();
        assertEquals(readXml.readOutputValue("value"), null);
        assertNotNull(readXml.readOutputValue("object"));
        Map<String, Object> output = (Map<String, Object>) readXml.readOutputValue("object");
        Map<String, Object> child1 = (Map<String, Object>) output.get("child1");
        assertEquals(child1.get("$$text"), "child1 text");
    }
}