|
| 1 | +/** |
| 2 | + * RodQueryTestFramework — Integration test for the CityAnalysis ROD query pipeline. |
| 3 | + * |
| 4 | + * Verifies: |
| 5 | + * 1. RodDisclaimerHandler can connect and accept the disclaimer |
| 6 | + * 2. RodQueryHandler reads local data and queries ROD |
| 7 | + * 3. Results are appended to the output CSV after each call |
| 8 | + * |
| 9 | + * Run standalone: java -cp <classpath> city_analysis.RodQueryTestFramework |
| 10 | + * |
| 11 | + * @author Max Rupplin |
| 12 | + * @javaowner Max Rupplin |
| 13 | + * @date June 24 2026 EST |
| 14 | + */ |
| 15 | + |
| 16 | +package city.analysis; |
| 17 | + |
| 18 | +import java.io.*; |
| 19 | +import java.nio.file.*; |
| 20 | + |
| 21 | +public class RodQueryTestFramework |
| 22 | +{ |
| 23 | + private static final String OUTPUT_CSV = "source/city/analysis/data/durham.nc.rod.query.results.csv"; |
| 24 | + private static final String INPUT_CSV = "source/city/analysis/data/durham.nc.addresses.csv"; |
| 25 | + |
| 26 | + private int passed = 0; |
| 27 | + private int failed = 0; |
| 28 | + |
| 29 | + public static void main(String[] args) |
| 30 | + { |
| 31 | + RodQueryTestFramework t = new RodQueryTestFramework(); |
| 32 | + t.run(); |
| 33 | + } |
| 34 | + |
| 35 | + public void run() |
| 36 | + { |
| 37 | + print("=== RodQueryTestFramework — Starting ==="); |
| 38 | + |
| 39 | + testInputCsvExists(); |
| 40 | + testDisclaimerHandlerConnects(); |
| 41 | + testQueryHandlerProducesResults(); |
| 42 | + testCsvAppendedAfterQuery(); |
| 43 | + |
| 44 | + print("=== Results: " + passed + " passed, " + failed + " failed ==="); |
| 45 | + } |
| 46 | + |
| 47 | + private void testInputCsvExists() |
| 48 | + { |
| 49 | + print("[TEST] Input CSV exists and has data"); |
| 50 | + Path input = Path.of(INPUT_CSV); |
| 51 | + if (!Files.exists(input)) { fail("Input CSV not found: " + INPUT_CSV); return; } |
| 52 | + try |
| 53 | + { |
| 54 | + long lines = Files.lines(input).count(); |
| 55 | + if (lines < 2) { fail("Input CSV has no data rows (lines=" + lines + ")"); return; } |
| 56 | + pass("Input CSV has " + (lines - 1) + " data rows"); |
| 57 | + } |
| 58 | + catch (IOException e) { fail("Cannot read input CSV: " + e.getMessage()); } |
| 59 | + } |
| 60 | + |
| 61 | + private void testDisclaimerHandlerConnects() |
| 62 | + { |
| 63 | + print("[TEST] RodDisclaimerHandler accepts disclaimer and returns content"); |
| 64 | + try |
| 65 | + { |
| 66 | + city_analysis.RodDisclaimerHandler handler = new city_analysis.RodDisclaimerHandler(); |
| 67 | + String html = handler.acceptAndFetch(); |
| 68 | + if (html == null) { fail("Disclaimer handler returned null — site unreachable or form changed"); return; } |
| 69 | + if (html.isEmpty()) { fail("Disclaimer handler returned empty content"); return; } |
| 70 | + if (handler.getSessionCookie() == null) { fail("No session cookie acquired"); return; } |
| 71 | + pass("Disclaimer accepted, got " + html.length() + " chars, cookie set"); |
| 72 | + } |
| 73 | + catch (Exception e) { fail("Exception: " + e.getMessage()); } |
| 74 | + } |
| 75 | + |
| 76 | + private void testQueryHandlerProducesResults() |
| 77 | + { |
| 78 | + print("[TEST] RodQueryHandler queries ROD and gets results"); |
| 79 | + |
| 80 | + // Record output CSV size before |
| 81 | + long sizeBefore = getFileSize(OUTPUT_CSV); |
| 82 | + |
| 83 | + try |
| 84 | + { |
| 85 | + city_analysis.RodQueryHandler handler = new city_analysis.RodQueryHandler(); |
| 86 | + int results = handler.queryAndAppend(3); // Query only 3 records for test |
| 87 | + |
| 88 | + if (results < 0) { fail("queryAndAppend returned negative: " + results); return; } |
| 89 | + |
| 90 | + long sizeAfter = getFileSize(OUTPUT_CSV); |
| 91 | + if (results > 0 && sizeAfter <= sizeBefore) |
| 92 | + { |
| 93 | + fail("Handler reported " + results + " results but CSV did not grow"); |
| 94 | + return; |
| 95 | + } |
| 96 | + |
| 97 | + pass("Query returned " + results + " results, CSV size: " + sizeBefore + " -> " + sizeAfter); |
| 98 | + } |
| 99 | + catch (Exception e) { fail("Exception: " + e.getMessage()); } |
| 100 | + } |
| 101 | + |
| 102 | + private void testCsvAppendedAfterQuery() |
| 103 | + { |
| 104 | + print("[TEST] Output CSV has valid header and appended rows"); |
| 105 | + Path output = Path.of(OUTPUT_CSV); |
| 106 | + if (!Files.exists(output)) { fail("Output CSV does not exist after query"); return; } |
| 107 | + |
| 108 | + try |
| 109 | + { |
| 110 | + var lines = Files.readAllLines(output); |
| 111 | + if (lines.isEmpty()) { fail("Output CSV is empty"); return; } |
| 112 | + |
| 113 | + String header = lines.get(0); |
| 114 | + if (!header.contains("PARCEL_ID") || !header.contains("DOCUMENT_TYPE")) |
| 115 | + { |
| 116 | + fail("Output CSV header malformed: " + header); |
| 117 | + return; |
| 118 | + } |
| 119 | + |
| 120 | + if (lines.size() < 2) |
| 121 | + { |
| 122 | + // No data rows yet — might be network issue but header is correct |
| 123 | + pass("Output CSV header valid, no data rows yet (ROD may be unreachable)"); |
| 124 | + return; |
| 125 | + } |
| 126 | + |
| 127 | + // Verify data rows have correct column count (10 columns) |
| 128 | + String dataRow = lines.get(1); |
| 129 | + int commas = (int) dataRow.chars().filter(c -> c == ',').count(); |
| 130 | + if (commas < 5) { fail("Data row has too few columns: " + dataRow); return; } |
| 131 | + |
| 132 | + pass("Output CSV has " + (lines.size() - 1) + " data rows, format valid"); |
| 133 | + } |
| 134 | + catch (IOException e) { fail("Cannot read output CSV: " + e.getMessage()); } |
| 135 | + } |
| 136 | + |
| 137 | + private long getFileSize(String path) |
| 138 | + { |
| 139 | + try { return Files.size(Path.of(path)); } |
| 140 | + catch (IOException e) { return 0; } |
| 141 | + } |
| 142 | + |
| 143 | + private void pass(String msg) |
| 144 | + { |
| 145 | + passed++; |
| 146 | + print(" ✓ PASS: " + msg); |
| 147 | + } |
| 148 | + |
| 149 | + private void fail(String msg) |
| 150 | + { |
| 151 | + failed++; |
| 152 | + print(" ✗ FAIL: " + msg); |
| 153 | + } |
| 154 | + |
| 155 | + private void print(String msg) |
| 156 | + { |
| 157 | + System.out.println("-- : [RodQueryTestFramework] " + msg); |
| 158 | + } |
| 159 | +} |
0 commit comments