--- /dev/null
+package net.jaekl.qd.http;
+
+import net.jaekl.qd.QDException;
+
+public class InvalidResponseException extends QDException {
+ private static final long serialVersionUID = 1L;
+
+ String m_url;
+ String m_method;
+
+ public InvalidResponseException(String url, String method, Throwable cause) {
+ super(cause);
+
+ m_url = url;
+ m_method = method;
+ }
+
+ public String getUrl() { return m_url; }
+ public String getMethod() { return m_method; }
+
+ @Override
+ public String toString() {
+ return getClass().getName() + "; " + m_url + "/" + m_method;
+ }
+}
import net.jaekl.qd.QDException;
import net.jaekl.qd.util.ExceptionUtils;
+import net.jaekl.qd.xml.ParseErrorHandler;
import net.jaekl.qd.xml.ParseHandler;
import net.jaekl.qd.xml.ParseResult;
import org.apache.http.impl.client.HttpClientBuilder;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
+import org.xml.sax.SAXParseException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
is = doSubmit(method, passedParams);
XMLReader reader = XMLReaderFactory.createXMLReader();
ParseHandler ph = new ParseHandler(result);
+ ParseErrorHandler peh = new ParseErrorHandler();
reader.setContentHandler(ph);
+ reader.setErrorHandler(peh);
reader.parse(new InputSource(is));
}
+ catch ( SAXParseException saxpe ) {
+ throw new InvalidResponseException(m_gatewayUrl, method, saxpe);
+ }
catch ( InstantiationException
| InvocationTargetException
| IllegalAccessException
--- /dev/null
+// Copyright (C) 2014 Christian Jaekl
+
+// Simple SAX parse error handler.
+// Necessary to avoid printing [Fatal Error] messages to stdout when something goes wrong.
+
+package net.jaekl.qd.xml;
+
+import org.xml.sax.ErrorHandler;
+import org.xml.sax.SAXException;
+import org.xml.sax.SAXParseException;
+
+public class ParseErrorHandler implements ErrorHandler {
+
+ @Override
+ public void error(SAXParseException saxpe) throws SAXException {
+ throw saxpe;
+ }
+
+ @Override
+ public void fatalError(SAXParseException saxpe) throws SAXException {
+ throw saxpe;
+ }
+
+ @Override
+ public void warning(SAXParseException saxpe) throws SAXException {
+ throw saxpe;
+ }
+
+}
ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair(Server.STOP_NO, "1234"));
- broker.setOutput(Server.GET_ROUTE_SUMMARY_FOR_STOP, params, ROUTE_SUMMARY_FOR_STOP);
+ broker.setResult(Server.GET_ROUTE_SUMMARY_FOR_STOP, params, ROUTE_SUMMARY_FOR_STOP);
StopInfo stopInfo = sm.getRouteSummaryForStop(1234);
params.add(new BasicNameValuePair(Server.STOP_NO, "7659"));
params.add(new BasicNameValuePair(Server.ROUTE_NO, "1"));
- broker.setOutput(Server.GET_NEXT_TRIPS_FOR_STOP, params, NEXT_TRIPS_FOR_STOP);
+ broker.setResult(Server.GET_NEXT_TRIPS_FOR_STOP, params, NEXT_TRIPS_FOR_STOP);
StopInfo stopInfo = sm.getNextTripsForStop(7659, 1);
ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair(Server.STOP_NO, "7659"));
- broker.setOutput(Server.GET_NEXT_TRIPS_FOR_STOP_ALL_ROUTES, params, NEXT_TRIPS_FOR_STOP_ALL_ROUTES);
+ broker.setResult(Server.GET_NEXT_TRIPS_FOR_STOP_ALL_ROUTES, params, NEXT_TRIPS_FOR_STOP_ALL_ROUTES);
StopInfo stopInfo = sm.getNextTripsForStopAllRoutes(7659);
}
}
- HashMap<InputParams, String> m_output;
+ HashMap<InputParams, Object> m_result;
public RequestBrokerMock(String gatewayUrl, ArrayList<NameValuePair> baseParams) {
super(gatewayUrl, baseParams);
- m_output = new HashMap<InputParams, String>();
+ m_result = new HashMap<InputParams, Object>();
}
- public void setOutput(String method, ArrayList<NameValuePair> passedParams, String output) {
+ public void setResult(String method, ArrayList<NameValuePair> passedParams, Object result) {
InputParams ip = new InputParams(method, passedParams);
- m_output.put(ip, output);
+ m_result.put(ip, result);
}
@Override
InputStream doSubmit(String method, ArrayList<NameValuePair> passedParams) throws QDException
{
InputParams ip = new InputParams(method, passedParams);
- String output = m_output.get(ip);
+ Object result = m_result.get(ip);
- if (null == output) {
- Assert.fail("No output specified for given inputs.");
+ if (null == result) {
+ Assert.fail("No result specified for given inputs.");
}
+ if (result instanceof QDException) {
+ throw (QDException)result;
+ }
+
+ Assert.assertTrue(result instanceof String);
+
InputStream is = null;
try {
+ String output = (String)result;
is = new ByteArrayInputStream(output.getBytes("UTF-8"));
}
catch (UnsupportedEncodingException uee) {
import org.apache.http.NameValuePair;
import org.junit.Test;
+import org.xml.sax.SAXParseException;
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);
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);
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() {
+ InvalidResponseException ire = new InvalidResponseException(GATEWAY, METHOD, new SAXParseException("dummy", null));
+ 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));
+ }
+ }
}