package de.timroes; import static org.junit.Assert.*; import de.timroes.axmlrpc.*; import de.timroes.axmlrpc.serializer.SerializerHandler; import org.junit.Test; import java.io.ByteArrayInputStream; import java.io.InputStream; import java.nio.charset.StandardCharsets; import java.util.Date; import java.util.Map; import java.util.TreeMap; public class TestResponseParser { public final static String xmlDecl = ""; 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 + "" + " " + " " + " toto" + " " + " " + ""), false); assertEquals("toto", actual); } @Test public void testWithTrailingWhitespaceInTags() throws Exception { ResponseParser sut = new ResponseParser(); Object actual = sut.parse(sh, strToStream(xmlDecl + "" + " " + " " + " toto" + " " + " " + ""), false); assertEquals("toto", actual); } @Test public void testWithTrailingEndlineInTags() throws Exception { ResponseParser sut = new ResponseParser(); Object actual = sut.parse(sh, strToStream(xmlDecl + "" + " " + " " + " toto" + " " + " " + ""), false); assertEquals("toto", actual); } @Test public void testWithTrailingTabInTags() throws Exception { ResponseParser sut = new ResponseParser(); Object actual = sut.parse(sh, strToStream(xmlDecl + "" + " " + " " + " toto" + " " + " " + ""), false); assertEquals("toto", actual); } @Test public void testResponseWithNonAsciiCharacter() throws Exception { ResponseParser sut = new ResponseParser(); Object actual = sut.parse(sh, strToStream(xmlDecl + "" + " " + " " + " Aéris" + " " + " " + ""), false); assertEquals("Aéris", actual); } @Test public void testUTF16Response() throws Exception { ResponseParser sut = new ResponseParser(); Object actual = sut.parse(sh, bytesToStream((xmlDecl + "" + " " + " " + " toto" + " " + " " + "").getBytes(StandardCharsets.UTF_16)), false); assertEquals("toto", actual); } @Test public void testResponseWithComplexValue() throws Exception { ResponseParser sut = new ResponseParser(); Object actual = sut.parse(sh, strToStream(xmlDecl + "" + " " + " " + " " + " " + " intValue12" + " otherIntValue13" + " boolValue1" + " strValuetoto" + " doubleValue12.4" + " dateValue20200908T0440Z" + // Don't test base64 because it seems assertEqual will do a reference equals on arrray of bytes so it's not easily testable here // so we test it in a test below //" base64ValueQWVyaXM=" + " nestedValue " + " innerStrValueinner" + " " + " " + " " + " " + " " + ""), false); Map expected = new TreeMap<>(); expected.put("intValue", 12); expected.put("otherIntValue", 13); expected.put("boolValue", true); expected.put("strValue", "toto"); expected.put("doubleValue", 12.4); expected.put("dateValue", new Date(1599540000000L)); Map innerStruct = new TreeMap<>(); innerStruct.put("innerStrValue", "inner"); expected.put("nestedValue", innerStruct); assertEquals(expected, actual); } @Test public void testResponseWithBase64Value() throws Exception { ResponseParser sut = new ResponseParser(); Object actual = sut.parse(sh, strToStream(xmlDecl + "" + " " + " " + " " + " QWVyaXM=" + " " + " " + " " + ""), false); String actualAsStr = new String((byte[]) actual, StandardCharsets.UTF_8); assertEquals("Aeris", actualAsStr); } @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("" + " " + " " + " toto" + " " + " " + ""), false); assertEquals("toto", actual); } @Test public void testXmlrpcError() throws Exception { ResponseParser sut = new ResponseParser(); try { sut.parse(sh, strToStream("" + " " + " " + " " + " " + " faultCode" + " 4" + " " + " " + " faultString" + " error X occurred" + " " + " " + " " + " " + ""), false); fail("The previous call should have thrown"); } catch (XMLRPCServerException e){ assertEquals("error X occurred [4]", e.getMessage()); assertEquals(4, e.getErrorNr()); } } @Test public void testResponseWithXmlComment() throws Exception { ResponseParser sut = new ResponseParser(); Object actual = sut.parse(sh, strToStream("" + " " + " " + " " + " tata" + " " + " " + ""), false); assertEquals("tata", actual); } @Test public void testResponseWithInlineXmlComment() throws Exception { ResponseParser sut = new ResponseParser(); Object actual = sut.parse(sh, strToStream("" + " " + " " + " titi" + " " + " " + ""), false); assertEquals("titi", actual); } @Test public void testResponseWithSpecialChars() throws Exception { ResponseParser sut = new ResponseParser(); Object actual = sut.parse(sh, strToStream("" + " " + " " + " to<to" + " " + " " + ""), false); assertEquals("to" + " " + " toto" + " " + " "), false); } @Test(expected = XMLRPCException.class) public void testErrorMissingParamsTag() throws Exception { ResponseParser sut = new ResponseParser(); Object actual = sut.parse(sh, strToStream("" + " " + " toto" + " " + ""), false); } @Test(expected = XMLRPCException.class) public void testErrorMissingParamTag() throws Exception { ResponseParser sut = new ResponseParser(); Object actual = sut.parse(sh, strToStream("" + " " + " toto" + " " + ""), false); } private static InputStream strToStream(String str){ return bytesToStream(str.getBytes(StandardCharsets.UTF_8)); } private static InputStream bytesToStream(byte[] bytes){ return new ByteArrayInputStream(bytes); } }