]> jaekl.net Git - frank.git/blob - test/net/jaekl/qd/http/RequestBrokerTest.java
Initial
[frank.git] / test / net / jaekl / qd / http / RequestBrokerTest.java
1 package net.jaekl.qd.http;
2
3 import java.util.ArrayList;
4
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;
9
10 import org.apache.http.NameValuePair;
11 import org.junit.Test;
12
13 public class RequestBrokerTest {
14
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"
22                                         + "     <soap:Body>\n"
23                                         + "             <GetRouteSummaryForStopResponse xmlns=\"http://octranspo.com\">\n"
24                                         + "                     <GetRouteSummaryForStopResult>\n"
25                                         + "                             <StopNo xmlns=\"http://tempuri.org/\">1234</StopNo>\n"
26                                         + "                             <StopDescription" 
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"
30                                         + "                                     <Route>\n"
31                                         + "                                             <RouteNo>123</RouteNo>\n"
32                                         + "                                             <DirectionID>0</DirectionID>\n"
33                                         + "                                             <Direction>NORTH</Direction>\n"
34                                         + "                                             <RouteHeading>First Mall</RouteHeading>\n"
35                                         + "                                     </Route>\n"
36                                         + "                                     <Route>\n"
37                                         + "                                             <RouteNo>123</RouteNo>\n"
38                                         + "                                             <DirectionID>1</DirectionID>\n"
39                                         + "                                             <Direction>SOUTH</Direction>\n"
40                                         + "                                             <RouteHeading>Second Mall</RouteHeading>\n"
41                                         + "                                     </Route>\n"
42                                         + "                             </Routes>\n"
43                                         + "                     </GetRouteSummaryForStopResult>\n"
44                                         + "             </GetRouteSummaryForStopResponse>\n"
45                                         + "     </soap:Body>\n"
46                                         + "</soap:Envelope>\n";
47         
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";
51
52                 private static final String[] INTERNAL = { STOP_NO };
53                 private static final Object[][] EXTERNAL = { };
54                 
55                 // Data gleaned from parsing
56                 int m_stopNo;
57                 
58                 public RouteSummaryParse() {
59                         super("GetRouteSummaryForStopResult", INTERNAL, EXTERNAL);
60                         
61                         m_stopNo = 0;
62                 }
63                 
64                 public int getStopNo() { return m_stopNo; }
65
66                 @Override
67                 public void endContents(String uri, String localName, String qName,
68                                 String chars) throws XmlParseException 
69                 {
70                         if (localName.equals(STOP_NO)) {
71                                 m_stopNo = Integer.parseInt(chars);
72                         }
73                 }
74
75                 @Override
76                 public void endExternal(String uri, String localName, String qName)
77                                 throws XmlParseException 
78                 {
79                         Assert.fail("Should not be trying to parse external tags here");
80                 }
81         }
82         
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).
85         @Test
86         public void test_sumbit() throws QDException {
87                 ArrayList<NameValuePair> emptyParams = new ArrayList<NameValuePair>();
88                 
89                 RequestBrokerMock rbm = new RequestBrokerMock(GATEWAY, emptyParams);
90                 rbm.setOutput(METHOD, emptyParams, ROUTE_SUMMARY_FOR_STOP);
91                 
92                 String actual = rbm.submit(METHOD, emptyParams);
93                 
94                 Assert.assertEquals(ROUTE_SUMMARY_FOR_STOP, actual);
95         }
96         
97         // Try submitting a request (with a mocked-out http post component)
98         // and validate that we do, in fact, parse something correctly.
99         @Test
100         public void test_submitAndParse() throws QDException {
101                 ArrayList<NameValuePair> emptyParams = new ArrayList<NameValuePair>();
102                 
103                 RequestBrokerMock rbm = new RequestBrokerMock(GATEWAY, emptyParams);
104                 rbm.setOutput(METHOD, emptyParams, ROUTE_SUMMARY_FOR_STOP);
105                 
106                 ParseResult pr = rbm.submitAndParse(METHOD, emptyParams, RouteSummaryParse.class);
107                 
108                 Assert.assertNotNull(pr);
109                 Assert.assertTrue(pr instanceof RouteSummaryParse);
110                 RouteSummaryParse rsp = (RouteSummaryParse)pr;
111                 Assert.assertEquals(1234, rsp.getStopNo());
112         }
113         
114         
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;
120                 
121                 public RequestBrokerParamExpandMock(String gatewayUrl, ArrayList<NameValuePair> baseParams) {
122                         super(gatewayUrl, baseParams);
123                         m_timesCalled = 0;
124                         m_methodPassed = null;
125                         m_paramsPassed = null;
126                         m_parserClassPassed = null;
127                 }
128                 
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; }
133                 
134                 @Override 
135                 public ParseResult submitAndParse(String method, 
136                                   ArrayList<NameValuePair> passedParams, 
137                                   Class<? extends ParseResult> rootParserClass,
138                                   String rootTagName)
139                          throws QDException
140                 {
141                         Assert.assertNull(rootTagName);
142                         
143                         m_timesCalled += 1;
144                         m_methodPassed = method;
145                         m_paramsPassed = passedParams;
146                         m_parserClassPassed = rootParserClass;
147                         
148                         return null;
149                 }
150         }
151         
152         // submitAndParse(method, passedParams, rootParser) 
153         // should call through to 
154         // submitAndParse(method, passedParams, rootParser, rootTagName=null)
155         @Test
156         public void test_submitAndParse_expandsDefault4thParam() throws QDException {
157                 final String GATEWAY_URL = "http://bogus.net:1234/gateway";
158                 final String METHOD = "MyMethod";
159                 
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);
163                 
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());
169         }
170 }