@Override protected void setUp() throws Exception { parser = new FormatOptionsKvpParser(); }
/** * Tests format options with embedded separators * * @throws ParseException if the string can't be parsed. */ public void testErrors() throws Exception { Map<String, String> expected = new HashMap<String, String>() { { put("key1", "value:1"); put("key2", "value:2"); put("key3", "value:3"); } }; Map<String, String> actual = (Map<String, String>) parser.parse("key1:value:1;key2:value:2;key3:value:3"); assertEquals(expected.size(), actual.size()); assertEquals(expected, actual); } }
private Hints getQueryHints(String viewParams) { if (viewParams != null && !viewParams.isEmpty()) { FormatOptionsKvpParser parser = new FormatOptionsKvpParser(); Map<String, String> params; try { params = (Map<String, String>) parser.parse(viewParams); return new Hints(Hints.VIRTUAL_TABLE_PARAMETERS, params); } catch (Exception e) { throw new RestException("Invalid viewparams", HttpStatus.INTERNAL_SERVER_ERROR); } } return null; }
@Override public Operation operationDispatched(Request request, Operation operation) { String sim = Optional.ofNullable(request.getRawKvp().get(KVP)) .map(String.class::cast) .orElse(null); if (sim == null) { return operation; } Map<String, Object> simOpts; if (sim.contains(";")) { try { simOpts = (Map<String, Object>) new FormatOptionsKvpParser().parse(sim.toString()); } catch (Exception e) { throw new RuntimeException( "Illegal syntax for simulate options: simulate=<key>:<val>[;<key>:<val>]*", e); } } else { simOpts = Collections.emptyMap(); } throw new HttpErrorCodeException(202, toJSON(operation, simOpts)) .setContentType("application/json"); }
static void assertMapsEqual(Map<String, Object> expected, Map<String, Object> actual) throws Exception { for (Map.Entry<String, Object> entry : expected.entrySet()) { if (entry.getKey().equalsIgnoreCase("format_options")) { FormatOptionsKvpParser parser = new FormatOptionsKvpParser(); Map expectedFormatOptions = (Map) parser.parse((String) entry.getValue()); Map actualFormatOptions = (Map) parser.parse((String) actual.get(entry.getKey())); for (Object o : expectedFormatOptions.entrySet()) { Map.Entry formatOption = (Map.Entry) o; assertEquals( formatOption.getValue(), actualFormatOptions.get(formatOption.getKey())); } for (Object key : actualFormatOptions.keySet()) { assertTrue( "found unexpected key '" + key + "' in format options", expectedFormatOptions.containsKey(key)); } // special treatment for the format options } else { assertEquals(entry.getValue(), actual.get(entry.getKey())); } } for (String key : actual.keySet()) { assertTrue(expected.containsKey(key)); } }
/** * Tests format options with embedded separators * * @throws ParseException if the string can't be parsed. */ public void testEmbeddedSeparators() throws Exception { Map<String, String> expected = new HashMap<String, String>() { { put("key1", "value:1"); put("key2", "value:2"); put("key3", "value:3:ZZ;XX"); } }; Map<String, String> actual = (Map<String, String>) parser.parse("key1:value:1;key2:value:2;key3:value:3\\:ZZ\\;XX"); assertEquals(expected, actual); }
/** * Tests format options with escaped separators * * @throws ParseException if the string can't be parsed. */ public void testEscapedSeparators() throws Exception { Map<String, String> expected = new HashMap<String, String>() { { put("key1", "value:1"); put("key2", "value:2"); put("key3", "value:3;ZZZ"); } }; Map<String, String> actual = (Map<String, String>) parser.parse("key1:value\\:1;key2:value\\:2;key3:value\\:3\\;ZZZ"); assertEquals(expected, actual); }
/** * Tests normal-style format options * * @throws ParseException if the string can't be parsed. */ public void testPairs() throws Exception { Map<String, String> expected = new HashMap<String, String>() { { put("key1", "value1"); put("key2", "value2"); put("key3", "true"); put("key4", "value4"); } }; Map<String, String> actual = (Map<String, String>) parser.parse("key1:value1;key2:value2;key3;key4:value4"); assertEquals(expected, actual); }