1 package net.jaekl.qd.http;
3 import java.util.ArrayList;
5 import junit.framework.Assert;
6 import net.jaekl.qd.QDException;
7 import net.jaekl.qd.xml.ParseResult;
8 import net.jaekl.qd.xml.XmlParseException;
10 import org.apache.http.NameValuePair;
11 import org.junit.Test;
13 public class RequestBrokerTest {
15 private static final String GATEWAY = "http://hostname.bogus/";
16 private static final String METHOD = "apiMethod";
17 private static final String ROUTE_SUMMARY_FOR_STOP =
18 "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
19 + "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
20 + "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"\n"
21 + "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
23 + " <GetRouteSummaryForStopResponse xmlns=\"http://octranspo.com\">\n"
24 + " <GetRouteSummaryForStopResult>\n"
25 + " <StopNo xmlns=\"http://tempuri.org/\">1234</StopNo>\n"
27 + " xmlns=\"http://tempuri.org/\">ONE-TWO-THREE-FOUR</StopDescription>\n"
28 + " <Error xmlns=\"http://tempuri.org/\"/>\n"
29 + " <Routes xmlns=\"http://tempuri.org/\">\n"
31 + " <RouteNo>123</RouteNo>\n"
32 + " <DirectionID>0</DirectionID>\n"
33 + " <Direction>NORTH</Direction>\n"
34 + " <RouteHeading>First Mall</RouteHeading>\n"
37 + " <RouteNo>123</RouteNo>\n"
38 + " <DirectionID>1</DirectionID>\n"
39 + " <Direction>SOUTH</Direction>\n"
40 + " <RouteHeading>Second Mall</RouteHeading>\n"
43 + " </GetRouteSummaryForStopResult>\n"
44 + " </GetRouteSummaryForStopResponse>\n"
46 + "</soap:Envelope>\n";
48 // Parse sub-tags, handling some internally and some externally
49 public static class RouteSummaryParse extends ParseResult {
50 private static final String STOP_NO = "StopNo";
52 private static final String[] INTERNAL = { STOP_NO };
53 private static final Object[][] EXTERNAL = { };
55 // Data gleaned from parsing
58 public RouteSummaryParse() {
59 super("GetRouteSummaryForStopResult", INTERNAL, EXTERNAL);
64 public int getStopNo() { return m_stopNo; }
67 public void endContents(String uri, String localName, String qName,
68 String chars) throws XmlParseException
70 if (localName.equals(STOP_NO)) {
71 m_stopNo = Integer.parseInt(chars);
76 public void endExternal(String uri, String localName, String qName)
77 throws XmlParseException
79 Assert.fail("Should not be trying to parse external tags here");
83 // Try submitting a request (with a mocked-out http post component)
84 // and validate that we do, in fact, return the expected text (XML).
86 public void test_sumbit() throws QDException {
87 ArrayList<NameValuePair> emptyParams = new ArrayList<NameValuePair>();
89 RequestBrokerMock rbm = new RequestBrokerMock(GATEWAY, emptyParams);
90 rbm.setOutput(METHOD, emptyParams, ROUTE_SUMMARY_FOR_STOP);
92 String actual = rbm.submit(METHOD, emptyParams);
94 Assert.assertEquals(ROUTE_SUMMARY_FOR_STOP, actual);
97 // Try submitting a request (with a mocked-out http post component)
98 // and validate that we do, in fact, parse something correctly.
100 public void test_submitAndParse() throws QDException {
101 ArrayList<NameValuePair> emptyParams = new ArrayList<NameValuePair>();
103 RequestBrokerMock rbm = new RequestBrokerMock(GATEWAY, emptyParams);
104 rbm.setOutput(METHOD, emptyParams, ROUTE_SUMMARY_FOR_STOP);
106 ParseResult pr = rbm.submitAndParse(METHOD, emptyParams, RouteSummaryParse.class);
108 Assert.assertNotNull(pr);
109 Assert.assertTrue(pr instanceof RouteSummaryParse);
110 RouteSummaryParse rsp = (RouteSummaryParse)pr;
111 Assert.assertEquals(1234, rsp.getStopNo());
115 class RequestBrokerParamExpandMock extends RequestBrokerMock {
116 private int m_timesCalled;
117 private String m_methodPassed;
118 private ArrayList<NameValuePair> m_paramsPassed;
119 private Class<? extends ParseResult> m_parserClassPassed;
121 public RequestBrokerParamExpandMock(String gatewayUrl, ArrayList<NameValuePair> baseParams) {
122 super(gatewayUrl, baseParams);
124 m_methodPassed = null;
125 m_paramsPassed = null;
126 m_parserClassPassed = null;
129 public int getTimesCalled() { return m_timesCalled; }
130 public String getMethodPassed() { return m_methodPassed; }
131 public ArrayList<NameValuePair> getParamsPassed() { return m_paramsPassed; }
132 public Class<? extends ParseResult> getParserClassPassed() { return m_parserClassPassed; }
135 public ParseResult submitAndParse(String method,
136 ArrayList<NameValuePair> passedParams,
137 Class<? extends ParseResult> rootParserClass,
141 Assert.assertNull(rootTagName);
144 m_methodPassed = method;
145 m_paramsPassed = passedParams;
146 m_parserClassPassed = rootParserClass;
152 // submitAndParse(method, passedParams, rootParser)
153 // should call through to
154 // submitAndParse(method, passedParams, rootParser, rootTagName=null)
156 public void test_submitAndParse_expandsDefault4thParam() throws QDException {
157 final String GATEWAY_URL = "http://bogus.net:1234/gateway";
158 final String METHOD = "MyMethod";
160 RequestBrokerParamExpandMock rbpem = new RequestBrokerParamExpandMock(GATEWAY_URL, new ArrayList<NameValuePair>() );
161 ArrayList<NameValuePair> passedParams = new ArrayList<NameValuePair>();
162 ParseResult pr = rbpem.submitAndParse(METHOD, passedParams, ParseResult.class);
164 Assert.assertNull(pr);
165 Assert.assertEquals(1, rbpem.getTimesCalled());
166 Assert.assertEquals(METHOD, rbpem.getMethodPassed());
167 Assert.assertEquals(passedParams, rbpem.getParamsPassed());
168 Assert.assertEquals(ParseResult.class, rbpem.getParserClassPassed());