ModelNodeHelper { public static ModelNode failSafeGet(ModelNode modelNode, String path) { ModelNode undefined = new ModelNode(); if (Strings.emptyToNull(path) != null) { Iterable<String> keys = Splitter.on('/').omitEmptyStrings().trimResults().split(path); if (!Iterables.isEmpty(keys)) { ModelNode context = modelNode; for (String key : keys) { String safeKey = decodeValue(key); if (context.hasDefined(safeKey)) { context = context.get(safeKey); } else { context = undefined; break; } } return context; } } return undefined; } private  ModelNodeHelper(); @JsIgnore static String encodeValue(String value); @JsIgnore static String decodeValue(String value); static ModelNode failSafeGet(ModelNode modelNode, String path); static boolean failSafeBoolean(ModelNode modelNode, String path); @JsIgnore static Date failSafeDate(ModelNode modelNode, String path); @JsIgnore static List<ModelNode> failSafeList(ModelNode modelNode, String path); @JsIgnore static List<Property> failSafePropertyList(ModelNode modelNode, String path); @JsIgnore static T getOrDefault(ModelNode modelNode, String attribute, Supplier<T> supplier, T defaultValue); @JsIgnore static void storeIndex(List<ModelNode> modelNodes); @JsIgnore static List<NamedNode> asNamedNodes(List<Property> properties); @JsIgnore static E asEnumValue(ModelNode modelNode, String attribute, Function<String, E> valueOf,
            E defaultValue); @JsIgnore static E asEnumValue(ModelNode modelNode, Function<String, E> valueOf, E defaultValue); @JsIgnore static String asAttributeValue(E enumValue); static void move(ModelNode modelNode, String source, String destination); static ModelNode properties(String... properties); @JsMethod(name = "failSafeList") @EsReturn("ModelNode[]") static ModelNode[] jsFailSafeList(ModelNode modelNode, String path); @JsMethod(name = "failSafePropertyList") @EsReturn("Property[]") static Property[] jsFailSafePropertyList(ModelNode modelNode, String path); @JsMethod(name = "asNamedNodes") @EsReturn("NamedNode[]") static NamedNode[] jsAsNamedNodes(@EsParam("Property[]") Property[] properties);  }
@Test public void nullPath() { assertFalse(ModelNodeHelper.failSafeGet(modelNode, null).isDefined()); }
@Test public void emptyPath() { assertFalse(ModelNodeHelper.failSafeGet(modelNode, "").isDefined()); }
@Test public void invalidPath() { assertFalse(ModelNodeHelper.failSafeGet(modelNode, "/").isDefined()); }
@Test public void wrongPath() { assertFalse(ModelNodeHelper.failSafeGet(modelNode, "a").isDefined()); assertFalse(ModelNodeHelper.failSafeGet(modelNode, "a/b").isDefined()); assertFalse(ModelNodeHelper.failSafeGet(modelNode, "foo/bar/baz").isDefined()); }
@Test public void simplePath() { ModelNode node = ModelNodeHelper.failSafeGet(modelNode, "foo"); assertTrue(node.isDefined()); assertEquals(foo, node); }
@Test public void nestedPath() { ModelNode node = ModelNodeHelper.failSafeGet(modelNode, "foo/bar"); assertTrue(node.isDefined()); assertEquals(bar, node); }