| 1 | |
package org.codeforamerica.open311.internals.network; |
| 2 | |
|
| 3 | |
import java.io.IOException; |
| 4 | |
import java.net.URL; |
| 5 | |
import java.util.Map; |
| 6 | |
|
| 7 | |
import org.codeforamerica.open311.facade.Format; |
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | 24 | public class MockNetworkManager implements NetworkManager { |
| 15 | 24 | private Format format = Format.XML; |
| 16 | |
|
| 17 | |
@Override |
| 18 | |
public String doGet(URL url) throws IOException { |
| 19 | 33 | if (url.toString().contains("simulateIOException")) { |
| 20 | 0 | throw new IOException(); |
| 21 | |
} |
| 22 | 33 | if (format == Format.XML) { |
| 23 | 25 | return XMLResponse(url); |
| 24 | |
} |
| 25 | 8 | if (format == Format.JSON) { |
| 26 | 8 | return JSONResponse(url); |
| 27 | |
} |
| 28 | 0 | return ""; |
| 29 | |
} |
| 30 | |
|
| 31 | |
@Override |
| 32 | |
public String doPost(URL url, Map<String, String> parameters) |
| 33 | |
throws IOException { |
| 34 | 13 | if (url.toString().contains("simulateIOException")) { |
| 35 | 1 | throw new IOException(); |
| 36 | |
} |
| 37 | 12 | if (format == Format.XML) { |
| 38 | 8 | return XMLPOSTResponse(url); |
| 39 | |
} |
| 40 | 4 | if (format == Format.JSON) { |
| 41 | 4 | return JSONPOSTResponse(url); |
| 42 | |
} |
| 43 | 0 | return ""; |
| 44 | |
|
| 45 | |
} |
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
private String XMLResponse(URL url) { |
| 55 | 25 | if (url.toString().contains("simulateAPIError")) { |
| 56 | 0 | return errorXML(); |
| 57 | |
} |
| 58 | 25 | if (url.toString().contains("discovery")) { |
| 59 | 6 | return discoveryXML(); |
| 60 | |
} |
| 61 | 19 | if (url.toString().contains("services.xml")) { |
| 62 | 5 | return serviceListXML(); |
| 63 | |
} |
| 64 | 14 | if (url.toString().contains("services/")) { |
| 65 | 5 | return serviceDefinitionXML(); |
| 66 | |
} |
| 67 | 9 | if (url.toString().contains("tokens/")) { |
| 68 | 3 | return serviceRequestIdFromATokenXML(); |
| 69 | |
} |
| 70 | 6 | if (url.toString().contains("requests")) { |
| 71 | 6 | return serviceRequestsXML(); |
| 72 | |
} |
| 73 | 0 | return ""; |
| 74 | |
} |
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
private String JSONResponse(URL url) { |
| 84 | 8 | if (url.toString().contains("services.json")) { |
| 85 | 2 | return serviceListJSON(); |
| 86 | |
} |
| 87 | 6 | if (url.toString().contains("services/")) { |
| 88 | 2 | return serviceDefinitionJSON(); |
| 89 | |
} |
| 90 | 4 | if (url.toString().contains("tokens/")) { |
| 91 | 2 | return serviceRequestIdFromATokenJSON(); |
| 92 | |
} |
| 93 | 2 | if (url.toString().contains("requests")) { |
| 94 | 2 | return serviceRequestsJSON(); |
| 95 | |
} |
| 96 | 0 | return ""; |
| 97 | |
} |
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
private String XMLPOSTResponse(URL url) { |
| 107 | 8 | if (url.toString().contains("simulateAPIError")) { |
| 108 | 2 | return errorXML(); |
| 109 | |
} |
| 110 | 6 | if (url.toString().contains("requests.xml")) { |
| 111 | |
|
| 112 | 6 | if (url.toString().contains("api_key")) { |
| 113 | 0 | if (url.toString().contains("api_key=key")) { |
| 114 | 0 | return postServiceRequestResponseXML(); |
| 115 | |
} else |
| 116 | 0 | return errorXML(); |
| 117 | |
} else { |
| 118 | 6 | return postServiceRequestResponseXML(); |
| 119 | |
} |
| 120 | |
} |
| 121 | 0 | return ""; |
| 122 | |
} |
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
private String JSONPOSTResponse(URL url) { |
| 132 | 4 | if (url.toString().contains("simulateAPIError")) { |
| 133 | 1 | return errorJSON(); |
| 134 | |
} |
| 135 | 3 | if (url.toString().contains("requests.json")) { |
| 136 | |
|
| 137 | 2 | if (url.toString().contains("api_key")) { |
| 138 | 0 | if (url.toString().contains("api_key=key")) { |
| 139 | 0 | return postServiceRequestResponseJSON(); |
| 140 | |
} else |
| 141 | 0 | return errorJSON(); |
| 142 | |
} else { |
| 143 | 2 | return postServiceRequestResponseJSON(); |
| 144 | |
} |
| 145 | |
} |
| 146 | 1 | return ""; |
| 147 | |
} |
| 148 | |
|
| 149 | |
@Override |
| 150 | |
public void setFormat(Format format) { |
| 151 | 8 | this.format = format; |
| 152 | 8 | } |
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 159 | |
private String serviceListXML() { |
| 160 | 5 | return "<?xml version=\"1.0\" encoding=\"utf-8\"?><services><service><service_code>" |
| 161 | |
+ "001</service_code><service_name>Cans left out 24x7</service_name><description>" |
| 162 | |
+ "Garbage or recycling cans that have been left out for more than 24 hours after" |
| 163 | |
+ " collection. Violators will be cited.</description><metadata>true</metadata>" |
| 164 | |
+ "<type>realtime</type><keywords>lorem, ipsum, dolor</keywords><group>sanitation" |
| 165 | |
+ "</group></service><service><service_code>002</service_code><metadata>true</metadata>" |
| 166 | |
+ "<type>realtime</type><keywords>lorem, ipsum, dolor</keywords><group>street</group>" |
| 167 | |
+ "<service_name>Construction plate shifted</service_name>" |
| 168 | |
+ "<description>Metal construction plate covering the street or sidewalk has been moved." |
| 169 | |
+ "</description></service></services>"; |
| 170 | |
} |
| 171 | |
|
| 172 | |
private String serviceListJSON() { |
| 173 | 2 | return "[{\"service_code\":\"001\",\"service_name\":\"Cans left out 24x7\",\"description\":\"Garbage or recycling cans that have been left out for more than 24 hours after collection. Violators will be cited.\",\"metadata\":true,\"type\":\"realtime\"," |
| 174 | |
+ "\"keywords\":\"lorem, ipsum, dolor\",\"group\":\"sanitation\"},{\"service_code\":\"002\",\"metadata\":true,\"type\":\"realtime\",\"keywords\":\"lorem, ipsum, dolor\",\"group\":\"street\",\"service_name\":\"Construction plate shifted\"," |
| 175 | |
+ "\"description\":\"Metal construction plate covering the street or sidewalk has been moved.\"}]"; |
| 176 | |
} |
| 177 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
|
| 181 | |
|
| 182 | |
|
| 183 | |
private String serviceDefinitionXML() { |
| 184 | 5 | return "<?xml version=\"1.0\" encoding=\"utf-8\"?><service_definition>" |
| 185 | |
+ "<service_code>DMV66</service_code><attributes><attribute>" |
| 186 | |
+ "<variable>true</variable><code>WHISHETN</code><datatype>singlevaluelist</datatype>" |
| 187 | |
+ "<required>true</required><datatype_description></datatype_description><order>1</order>" |
| 188 | |
+ "<description>What is the ticket/tag/DL number?</description><values><value>" |
| 189 | |
+ "<key>123</key><name>Ford</name></value><value><key>124</key><name>Chrysler</name>" |
| 190 | |
+ "</value></values></attribute></attributes></service_definition>"; |
| 191 | |
} |
| 192 | |
|
| 193 | |
|
| 194 | |
|
| 195 | |
|
| 196 | |
|
| 197 | |
|
| 198 | |
private String serviceDefinitionJSON() { |
| 199 | 2 | return "{\"service_code\":\"DMV66\",\"attributes\":[{\"variable\":true,\"code\":\"WHISHETN\",\"datatype\":\"singlevaluelist\"," |
| 200 | |
+ "\"required\":true,\"datatype_description\":null,\"order\":1,\"description\":\"What is the ticket/tag/DL number?\"," |
| 201 | |
+ "\"values\":[{\"key\":123,\"name\":\"Ford\"},{\"key\":124,\"name\":\"Chrysler\"}]}]}"; |
| 202 | |
} |
| 203 | |
|
| 204 | |
|
| 205 | |
|
| 206 | |
|
| 207 | |
|
| 208 | |
|
| 209 | |
private String serviceRequestIdFromATokenXML() { |
| 210 | 3 | return "<?xml version=\"1.0\" encoding=\"utf-8\"?><service_requests>" |
| 211 | |
+ "<request><service_request_id>638344</service_request_id>" |
| 212 | |
+ "<token>12345</token></request></service_requests>"; |
| 213 | |
} |
| 214 | |
|
| 215 | |
|
| 216 | |
|
| 217 | |
|
| 218 | |
|
| 219 | |
|
| 220 | |
private String serviceRequestIdFromATokenJSON() { |
| 221 | 2 | return "[{\"service_request_id\":638344,\"token\":12345},{\"service_request_id\":111,\"token\":12345}]"; |
| 222 | |
} |
| 223 | |
|
| 224 | |
|
| 225 | |
|
| 226 | |
|
| 227 | |
|
| 228 | |
|
| 229 | |
private String serviceRequestsXML() { |
| 230 | 6 | return "<?xml version=\"1.0\" encoding=\"utf-8\"?><service_requests>" |
| 231 | |
+ "<request><service_request_id>638344</service_request_id>" |
| 232 | |
+ "<status>closed</status><status_notes>Duplicate request." |
| 233 | |
+ "</status_notes><service_name>Sidewalk and Curb Issues</service_name>" |
| 234 | |
+ "<service_code>006</service_code><description></description>" |
| 235 | |
+ "<agency_responsible></agency_responsible><service_notice></service_notice>" |
| 236 | |
+ "<requested_datetime>2010-04-14T06:37:38-08:00</requested_datetime>" |
| 237 | |
+ "<updated_datetime>2010-04-14T06:37:38-08:00</updated_datetime>" |
| 238 | |
+ "<expected_datetime>2010-04-15T06:37:38-08:00</expected_datetime><address>" |
| 239 | |
+ "8TH AVE and JUDAH ST</address>" |
| 240 | |
+ "<address_id>545483</address_id><zipcode>94122</zipcode>" |
| 241 | |
+ "<lat>37.762221815</lat><long>-122.4651145</long>" |
| 242 | |
+ "<media_url>http://city.gov.s3.amazonaws.com/requests/media/638344.jpg " |
| 243 | |
+ "</media_url></request>" |
| 244 | |
+ "<request><service_request_id>638349</service_request_id><status>open</status" |
| 245 | |
+ "><status_notes></status_notes><service_name>Sidewalk and Curb Issues</service_name>" |
| 246 | |
+ "<service_code>006</service_code><description></description><agency_responsible>" |
| 247 | |
+ "</agency_responsible>" |
| 248 | |
+ "<service_notice></service_notice><requested_datetime>2010-04-19T06:37:38-08:00" |
| 249 | |
+ "</requested_datetime><updated_datetime>2010-04-19T06:37:38-08:00</updated_datetime>" |
| 250 | |
+ "<expected_datetime>2010-04-19T06:37:38-08:00</expected_datetime>" |
| 251 | |
+ "<address>8TH AVE and JUDAH ST</address><address_id>545483" |
| 252 | |
+ "</address_id><zipcode>94122</zipcode>" |
| 253 | |
+ "<lat>37.762221815</lat><long>-122.4651145</long>" |
| 254 | |
+ "<media_url>http://city.gov.s3.amazonaws.com/requests/media/638349.jpg </media_url>" |
| 255 | |
+ "</request></service_requests>"; |
| 256 | |
} |
| 257 | |
|
| 258 | |
|
| 259 | |
|
| 260 | |
|
| 261 | |
|
| 262 | |
|
| 263 | |
private String serviceRequestsJSON() { |
| 264 | 2 | return "[{\"service_request_id\":638344,\"status\":\"closed\",\"status_notes\":" |
| 265 | |
+ "\"Duplicate request.\",\"service_name\":\"Sidewalk and Curb Issues\"," |
| 266 | |
+ "\"service_code\":\"006\",\"description\":null,\"agency_responsible\":null," |
| 267 | |
+ "\"service_notice\":null,\"requested_datetime\":\"2010-04-14T06:37:38-08:00\"," |
| 268 | |
+ "\"updated_datetime\":\"2010-04-14T06:37:38-08:00\",\"expected_datetime\":" |
| 269 | |
+ "\"2010-04-15T06:37:38-08:00\",\"address\":\"8TH AVE and JUDAH ST\"," |
| 270 | |
+ "\"address_id\":545483,\"zipcode\":94122,\"lat\":37.762221815,\"long\":" |
| 271 | |
+ "-122.4651145,\"media_url\":\"http://city.gov.s3.amazonaws.com/requests" |
| 272 | |
+ "/media/638344.jpg \"},{\"service_request_id\":638349,\"status\":\"open\"," |
| 273 | |
+ "\"status_notes\":null,\"service_name\":\"Sidewalk and Curb Issues\"," |
| 274 | |
+ "\"service_code\":\"006\",\"description\":null,\"agency_responsible\":null," |
| 275 | |
+ "\"service_notice\":null,\"requested_datetime\":\"2010-04-19T06:37:38-08:00\"," |
| 276 | |
+ "\"updated_datetime\":\"2010-04-19T06:37:38-08:00\",\"expected_datetime\":\"" |
| 277 | |
+ "2010-04-19T06:37:38-08:00\",\"address\":\"8TH AVE and JUDAH ST\",\"address_id\":" |
| 278 | |
+ "545483,\"zipcode\":94122,\"lat\":37.762221815,\"long\":-122.4651145," |
| 279 | |
+ "\"media_url\":\"http://city.gov.s3.amazonaws.com/requests/media/638349.jpg\"}]"; |
| 280 | |
} |
| 281 | |
|
| 282 | |
|
| 283 | |
|
| 284 | |
|
| 285 | |
|
| 286 | |
|
| 287 | |
public String postServiceRequestResponseXML() { |
| 288 | 6 | return "<?xml version=\"1.0\" encoding=\"utf-8\"?><service_requests><request>" |
| 289 | |
+ "<service_request_id>293944</service_request_id><service_notice>" |
| 290 | |
+ "The City will inspect and require the responsible party to correct " |
| 291 | |
+ "within 24 hours and/or issue a Correction Notice or Notice of Violation " |
| 292 | |
+ "of the Public Works Code</service_notice><account_id/></request>" |
| 293 | |
+ "</service_requests>"; |
| 294 | |
} |
| 295 | |
|
| 296 | |
|
| 297 | |
|
| 298 | |
|
| 299 | |
|
| 300 | |
|
| 301 | |
public String postServiceRequestResponseJSON() { |
| 302 | 2 | return "[{\"service_request_id\":293944,\"service_notice\":" |
| 303 | |
+ "\"The City will inspect and require the responsible party to correct within 24" |
| 304 | |
+ " hours and/or issue a Correction Notice or Notice of Violation of the Public Works Code\"," |
| 305 | |
+ "\"account_id\":null}]"; |
| 306 | |
} |
| 307 | |
|
| 308 | |
|
| 309 | |
|
| 310 | |
|
| 311 | |
|
| 312 | |
|
| 313 | |
public String errorXML() { |
| 314 | 2 | return "<?xml version=\"1.0\" encoding=\"utf-8\"?><errors><error><code>403</code><description>Invalid api_key received -- can't proceed with create_request.</description></error>" |
| 315 | |
+ "</errors>"; |
| 316 | |
} |
| 317 | |
|
| 318 | |
|
| 319 | |
|
| 320 | |
|
| 321 | |
|
| 322 | |
|
| 323 | |
public String errorJSON() { |
| 324 | 1 | return "[{\"code\":403,\"description\":\"Invalid api_key received -- can't proceed with create_request.\"}]"; |
| 325 | |
} |
| 326 | |
|
| 327 | |
|
| 328 | |
|
| 329 | |
|
| 330 | |
|
| 331 | |
|
| 332 | |
public String discoveryXML() { |
| 333 | 6 | return "<?xml version=\"1.0\" encoding=\"utf-8\"?><discovery xmlns:m=\"http://org/sfgov/sf311v2/services\" " |
| 334 | |
+ "xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">" |
| 335 | |
+ "<changeset>2011-04-05T17:48:34Z</changeset><contact>Please email " |
| 336 | |
+ "( content.311@sfgov.org ) or call ( 415-701-2311 ) for assistance " |
| 337 | |
+ "or to report bugs</contact><key_service>To get an API_KEY please " |
| 338 | |
+ "visit this website: http://apps.sfgov.org/Open311API/?page_id=486" |
| 339 | |
+ "</key_service><endpoints>" |
| 340 | |
+ "<endpoint><specification>http://wiki.open311.org/GeoReport_v2" |
| 341 | |
+ "</specification><url>https://open311.sfgov.org/dev/v2</url>" |
| 342 | |
+ "<changeset>2011-04-20T17:48:34Z</changeset>" |
| 343 | |
+ "<type>test</type><formats><format>text/XML</format></formats>" |
| 344 | |
+ "</endpoint><endpoint><specification>http://wiki.open311.org/GeoReport_v2</specification>" |
| 345 | |
+ "<url>https://open311.sfgov.org/v2</url><changeset>" |
| 346 | |
+ "2011-04-25T17:48:34Z</changeset><type>production</type><formats>" |
| 347 | |
+ "<format>text/XML</format></formats></endpoint>" |
| 348 | |
+ "<endpoint><specification>http://wiki.open311.org/GeoReport_v1</specification>" |
| 349 | |
+ "<url>https://open311.sfgov.org/dev/v1</url>" |
| 350 | |
+ "<changeset>2011-04-20T17:48:34Z</changeset><type>test</type><formats>" |
| 351 | |
+ "<format>text/XML</format></formats></endpoint>" |
| 352 | |
+ "<endpoint><specification>http://wiki.open311.org/GeoReport_v1" |
| 353 | |
+ "</specification><url>https://open311.sfgov.org/v1</url>" |
| 354 | |
+ "<changeset>2011-04-25T17:48:34Z</changeset>" |
| 355 | |
+ "<type>production</type><formats><format>text/xml</format></formats>" |
| 356 | |
+ "</endpoint></endpoints></discovery>"; |
| 357 | |
} |
| 358 | |
} |