Add ability to see raw response from server when something goes wrong. (OC Transpo...
[frank.git] / test / net / jaekl / qd / http / RequestBrokerTest.java
index 037e079040deba3b6bd0cc8365510f8c87031eef..307fe90f93ba675d6d47fef394fd5ca94f31bd62 100644 (file)
@@ -9,6 +9,7 @@ import net.jaekl.qd.xml.XmlParseException;
 
 import org.apache.http.NameValuePair;
 import org.junit.Test;
+import org.xml.sax.SAXParseException;
 
 public class RequestBrokerTest {
 
@@ -83,11 +84,11 @@ public class RequestBrokerTest {
        // Try submitting a request (with a mocked-out http post component)
        // and validate that we do, in fact, return the expected text (XML).
        @Test
-       public void test_sumbit() throws QDException {
+       public void test_submit() throws QDException {
                ArrayList<NameValuePair> emptyParams = new ArrayList<NameValuePair>();
                
                RequestBrokerMock rbm = new RequestBrokerMock(GATEWAY, emptyParams);
-               rbm.setOutput(METHOD, emptyParams, ROUTE_SUMMARY_FOR_STOP);
+               rbm.setResult(METHOD, emptyParams, ROUTE_SUMMARY_FOR_STOP);
                
                String actual = rbm.submit(METHOD, emptyParams);
                
@@ -101,7 +102,7 @@ public class RequestBrokerTest {
                ArrayList<NameValuePair> emptyParams = new ArrayList<NameValuePair>();
                
                RequestBrokerMock rbm = new RequestBrokerMock(GATEWAY, emptyParams);
-               rbm.setOutput(METHOD, emptyParams, ROUTE_SUMMARY_FOR_STOP);
+               rbm.setResult(METHOD, emptyParams, ROUTE_SUMMARY_FOR_STOP);
                
                ParseResult pr = rbm.submitAndParse(METHOD, emptyParams, RouteSummaryParse.class);
                
@@ -167,4 +168,44 @@ public class RequestBrokerTest {
                Assert.assertEquals(passedParams, rbpem.getParamsPassed());
                Assert.assertEquals(ParseResult.class, rbpem.getParserClassPassed());
        }
+       
+       @Test
+       public void test_submitAndParse_nonXmlResponse() {
+               ArrayList<NameValuePair> emptyParams = new ArrayList<NameValuePair>();
+               
+               RequestBrokerMock rbm = new RequestBrokerMock(GATEWAY, emptyParams);
+               rbm.setResult(METHOD, emptyParams, "No stop number specified");
+               
+               try {
+                       rbm.submitAndParse(METHOD, emptyParams, RouteSummaryParse.class);
+                       Assert.fail("Should have thrown an InvalidResultException");
+               }
+               catch (QDException qde) {
+                       Assert.assertTrue(qde instanceof InvalidResponseException);
+                       Assert.assertTrue(qde.toString().contains(GATEWAY));
+                       Assert.assertTrue(qde.toString().contains(METHOD));
+               }
+       }
+       
+       @Test
+       public void test_submitAndParse_throwsInvalidResultException() {
+               final String RESPONSE = "Must specify stop number.";
+               InvalidResponseException ire = new InvalidResponseException(new SAXParseException("dummy", null), GATEWAY, METHOD, RESPONSE);
+               ArrayList<NameValuePair> emptyParams = new ArrayList<NameValuePair>();
+               
+               RequestBrokerMock rbm = new RequestBrokerMock(GATEWAY, emptyParams);
+               rbm.setResult(METHOD, emptyParams, ire);
+               
+               try {
+                       rbm.submitAndParse(METHOD, emptyParams, RouteSummaryParse.class);
+                       Assert.fail("Should have thrown an exception");
+               }
+               catch (QDException qde) {
+                       Assert.assertTrue(qde instanceof InvalidResponseException);
+                       Assert.assertEquals(ire, qde);
+                       Assert.assertTrue(ire.toString().contains(GATEWAY));
+                       Assert.assertTrue(ire.toString().contains(METHOD));
+                       Assert.assertTrue(ire.toString().contains(RESPONSE));
+               }
+       }
 }