StructHelper { public static <T> T get(Map<String, Object> struct, String dotSeparatedPath, Class<T> expectedType) { final String[] parts = dotSeparatedPath.split("\\."); return get(struct, parts, expectedType, new LinkedList<>(), dotSeparatedPath); }  static T get(Map<String, Object> struct, String dotSeparatedPath, Class<T> expectedType);  }
@Test public void emptyPathShouldReturnCurrent() { assertEquals(map, StructHelper.get(map, "", Map.class)); assertEquals("INSERT_BEFORE", StructHelper.get(map, "patch.operation.", String.class)); }
@Test public void gettingTerminalShouldWork() { assertEquals("INSERT_BEFORE", StructHelper.get(map, "patch.operation", String.class)); }
@Test public void gettingTerminalWithImproperExpectedClassShouldFail() { try { assertEquals(Collections.emptyMap(), StructHelper.get(map, "patch.operation", Map.class)); fail("Should have thrown a ClassCastException"); } catch (ClassCastException e) { } }
@Test public void pathInterruptedByTerminalShouldFail() { final String path = "patch.operation.foo.bar"; try { assertEquals(Collections.emptyMap(), StructHelper.get(map, path, Map.class)); fail("Should have thrown an IllegalArgumentException"); } catch (IllegalArgumentException e) { final String message = e.getMessage(); assertEquals("'" + path + "' is interrupted at 'patch.operation' because 'operation' is a terminal element", message); } }
@Test public void gettingNonExistingTerminalShouldFail() { final String path = "patch.value.typed_config.value.root_id"; try { assertEquals("stats_outbound", StructHelper.get(map, path, String.class)); fail("Should have thrown an IllegalArgumentException"); } catch (IllegalArgumentException e) { final String message = e.getMessage(); assertEquals("'root_id' is not a child of 'patch.value.typed_config.value'. Known children: " + "'config'", message); } }
@Test public void interruptedPathShouldFail() { final String path = "patch.value.value.config"; try { assertEquals(Collections.emptyMap(), StructHelper.get(map, path, Map.class)); fail("Should have thrown an IllegalArgumentException"); } catch (IllegalArgumentException e) { final String message = e.getMessage(); assertEquals("'value' is not a child of 'patch.value.value'. Known children: 'name', " + "'typed_config'", message); } }
@Test public void gettingIntermediateShouldReturnMap() { final Map intermediate = StructHelper.get(StructHelperTest.map, "patch.value.typed_config.value.config", Map.class); assertEquals("stats_outbound", intermediate.get("root_id")); }
@Test public void gettingFromIntermediateShouldBeEquivalentToUsingLongerPath() { final Map intermediate = StructHelper.get(StructHelperTest.map, "patch.value.typed_config.value.config", Map.class); assertEquals(intermediate.get("root_id"), StructHelper.get(StructHelperTest.map, "patch.value.typed_config.value.config.root_id", String.class)); }