package de.timroes;

import static org.junit.Assert.*;

import de.timroes.axmlrpc.ResponseParser;
import de.timroes.axmlrpc.XMLRPCClient;
import de.timroes.axmlrpc.XMLRPCServerException;
import de.timroes.axmlrpc.serializer.SerializerHandler;
import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

public class TestResponseParser {
    public final static String xmlDecl = "<?xml version=\"1.0\"?>";
    public final static SerializerHandler sh = new SerializerHandler(XMLRPCClient.FLAGS_NONE);


    @Test
    public void testSimpleResponse() throws Exception {
        ResponseParser sut = new ResponseParser();
        Object actual = sut.parse(sh, strToStream(xmlDecl +
                "<methodResponse>" +
                "  <params>" +
                "    <param>" +
                "      <value><string>toto</string></value>" +
                "    </param>" +
                "  </params>" +
                "</methodResponse>"), false);
        assertEquals("toto", actual);
    }

    @Test
    /**
     * I'm not sure it really makes sense to support this case. But since I'm adding tests on code which existed
     * for years, I guess it's better to avoid breaking retro compatibility.
     */
    public void testAcceptMissingHeader() throws Exception {
        ResponseParser sut = new ResponseParser();
        Object actual = sut.parse(sh, strToStream("<methodResponse>" +
                "  <params>" +
                "    <param>" +
                "      <value><string>toto</string></value>" +
                "    </param>" +
                "  </params>" +
                "</methodResponse>"), false);
        assertEquals("toto", actual);
    }

    @Test
    public void testXmlrpcError() throws Exception {
        ResponseParser sut = new ResponseParser();
        try {
            sut.parse(sh, strToStream("<methodResponse>" +
                    "  <fault>" +
                    "    <value>" +
                    "      <struct>" +
                    "        <member>" +
                    "          <name>faultCode</name>" +
                    "          <value><int>4</int></value>" +
                    "        </member>" +
                    "        <member>" +
                    "          <name>faultString</name>" +
                    "          <value><string>error X occurred</string></value>" +
                    "        </member>" +
                    "      </struct>" +
                    "    </value>" +
                    "  </fault>" +
                    "</methodResponse>"), false);
            fail("The previous call should have thrown");
        } catch (XMLRPCServerException e){
            assertEquals("error X occurred [4]", e.getMessage());
            assertEquals(4, e.getErrorNr());
        }
}

    private static InputStream strToStream(String str){
        return new ByteArrayInputStream(str.getBytes());
    }
}

@@ -2809,16 +2809,1051 @@
       %7D%0A
+    %7D%0A%0A    @Test%0A    public void testResponseWithXmlComment() throws Exception %7B%0A        ResponseParser sut = new ResponseParser();%0A        Object actual = sut.parse(sh, strToStream(%22%3CmethodResponse%3E%22 +%0A                %22  %3Cparams%3E%22 +%0A                %22    %3Cparam%3E%22 +%0A                %22      %3C!--value%3E%3Cstring%3Etoto%3C/string%3E%3C/value--%3E%22 +%0A                %22      %3Cvalue%3E%3Cstring%3Etata%3C/string%3E%3C/value%3E%22 +%0A                %22    %3C/param%3E%22 +%0A                %22  %3C/params%3E%22 +%0A                %22%3C/methodResponse%3E%22), false);%0A        assertEquals(%22tata%22, actual);%0A    %7D%0A%0A    @Test%0A    public void testResponseWithSpecialChars() throws Exception %7B%0A        ResponseParser sut = new ResponseParser();%0A        Object actual = sut.parse(sh, strToStream(%22%3CmethodResponse%3E%22 +%0A                %22  %3Cparams%3E%22 +%0A                %22    %3Cparam%3E%22 +%0A                %22      %3Cvalue%3E%3Cstring%3Eto&lt;to%3C/string%3E%3C/value%3E%22 +%0A                %22    %3C/param%3E%22 +%0A                %22  %3C/params%3E%22 +%0A                %22%3C/methodResponse%3E%22), false);%0A        assertEquals(%22to%3Cto%22, actual);%0A    
 %7D%0A%0A    p

add more tests