/** as {@link #get(Class)} but always wrapped in a {@link Maybe}, absent if null * @throws ClassCastException if object exists here but of the wrong type */ public @Nonnull <V> Maybe<V> getMaybe(Class<V> type) { return Maybe.fromNullable(get(type)); }
@Test public void testAddStringToRoot() { Navigator<MutableMap<Object, Object>> j = Jsonya.newInstance().add("a"); Assert.assertEquals( j.toString(), "\"a\""); Assert.assertEquals( j.get(), "a"); }
@SuppressWarnings("unchecked") @Test public void testJsonyaMapNew() { MutableMap<Object, Object> m = europeMap().getRootMap(); Assert.assertEquals(Jsonya.of(m).get("europe", "uk", "edinburgh", "population"), 500*1000); Assert.assertEquals(Jsonya.of(m).at("europe", "uk", "edinburgh", "population").get(), 500*1000); Assert.assertEquals(((Map<Object,Object>)Jsonya.of(m).get("europe")).keySet(), ImmutableSet.of("uk", "france")); Assert.assertEquals(Jsonya.of(m).at("europe").getFocusMap().keySet(), ImmutableSet.of("uk", "france")); }
runList, (Map<String, Object>) attrs.root().get()));
runList, (Map<String, Object>) attrs.root().get()));
@SuppressWarnings({ "unchecked", "rawtypes", "deprecation" }) protected void startWithKnifeAsync() { // TODO prestart, ports (as above); also, note, some aspects of this are untested as we need a chef server String primary = getPrimaryCookbook(); // put all config under brooklyn/cookbook/config Navigator<MutableMap<Object, Object>> attrs = Jsonya.newInstancePrimitive().at("brooklyn"); if (Strings.isNonBlank(primary)) attrs.at(primary); attrs.at("config"); attrs.put( entity().config().getBag().getAllConfig() ); // and put launch attrs at root try { attrs.root().put((Map<?,?>)Tasks.resolveDeepValue(entity().getConfig(CHEF_LAUNCH_ATTRIBUTES), Object.class, entity().getExecutionContext())); } catch (Exception e) { Exceptions.propagate(e); } Collection<? extends String> runList = entity().getConfig(CHEF_LAUNCH_RUN_LIST); if (runList==null) runList = entity().getConfig(CHEF_RUN_LIST); if (runList==null) { if (Strings.isNonBlank(primary)) runList = ImmutableList.of(primary+"::"+"start"); else throw new IllegalStateException("Require a primary cookbook or a run_list to effect "+"start"+" on "+entity()); } DynamicTasks.queue( ChefServerTasks.knifeConvergeTask() .knifeNodeName(getNodeName()) .knifeRunList(Strings.join(runList, ",")) .knifeAddAttributes((Map) attrs.root().get()) .knifeRunTwice(entity().getConfig(CHEF_RUN_CONVERGE_TWICE)) ); }
@SuppressWarnings({ "unchecked", "rawtypes", "deprecation" }) protected void startWithKnifeAsync() { // TODO prestart, ports (as above); also, note, some aspects of this are untested as we need a chef server String primary = getPrimaryCookbook(); // put all config under brooklyn/cookbook/config Navigator<MutableMap<Object, Object>> attrs = Jsonya.newInstancePrimitive().at("brooklyn"); if (Strings.isNonBlank(primary)) attrs.at(primary); attrs.at("config"); attrs.put( entity().config().getBag().getAllConfig() ); // and put launch attrs at root try { attrs.root().put((Map<?,?>)Tasks.resolveDeepValue(entity().getConfig(CHEF_LAUNCH_ATTRIBUTES), Object.class, entity().getExecutionContext())); } catch (Exception e) { Exceptions.propagate(e); } Collection<? extends String> runList = entity().getConfig(CHEF_LAUNCH_RUN_LIST); if (runList==null) runList = entity().getConfig(CHEF_RUN_LIST); if (runList==null) { if (Strings.isNonBlank(primary)) runList = ImmutableList.of(primary+"::"+"start"); else throw new IllegalStateException("Require a primary cookbook or a run_list to effect "+"start"+" on "+entity()); } DynamicTasks.queue( ChefServerTasks.knifeConvergeTask() .knifeNodeName(getNodeName()) .knifeRunList(Strings.join(runList, ",")) .knifeAddAttributes((Map) attrs.root().get()) .knifeRunTwice(entity().getConfig(CHEF_RUN_CONVERGE_TWICE)) ); }
@Test public void testJsonyaPushPop() { Navigator<MutableMap<Object, Object>> m = europeMap(); Assert.assertTrue(m.getFocusMap().containsKey("europe")); Assert.assertFalse(m.getFocusMap().containsKey("edinburgh")); m.push(); m.at("europe", "uk"); Assert.assertTrue(m.getFocusMap().containsKey("edinburgh")); Assert.assertFalse(m.getFocusMap().containsKey("europe")); m.pop(); Assert.assertTrue(m.getFocusMap().containsKey("europe")); Assert.assertFalse(m.getFocusMap().containsKey("edinburgh")); // also check 'get' does not change focus m.get("europe", "uk"); Assert.assertTrue(m.getFocusMap().containsKey("europe")); Assert.assertFalse(m.getFocusMap().containsKey("edinburgh")); }
@SuppressWarnings("rawtypes") @Test public void testJsonyaWithList() { Navigator<MutableMap<Object, Object>> n = europeMap(); n.at("europe", "uk", "neighbours").list().add("ireland") .root().at("europe", "france", "neighbours").list().add("spain", "germany").add("switzerland") .root().at("europe", "france", "neighbours").add("lux"); Object l = n.root().at("europe", "france", "neighbours").get(); Assert.assertTrue(l instanceof List); Assert.assertEquals(((List)l).size(), 4); // this wants a map, so it creates a map in the list n.put("east", "germany", "south", "spain"); Assert.assertEquals(((List)l).size(), 5); Map nd = (Map) ((List)l).get(4); Assert.assertEquals(nd.size(), 2); Map nd2 = (Map) n.root().get("europe", "france", "neighbours", 4); Assert.assertEquals(nd2.size(), 2); }
@Test public void testJsonyaDeepMoreComplicated() { Navigator<MutableMap<Object, Object>> n = Jsonya.of(europeMap()).at("asia") .list().add("china", "japan") .root().add( Jsonya.newInstance().at("europe", "uk", "glasgow").put("weather", "even wetter").getRootMap() ); Assert.assertEquals( n.getRootMap().size(), 2 ); Assert.assertTrue( n.root().at("asia").get(List.class).contains("china") ); Assert.assertTrue( ((List<?>)n.root().get("asia")).contains("japan") ); Assert.assertEquals(n.root().at("europe", "uk").get(Map.class).size(), 2); Assert.assertEquals(n.root().at("europe", "uk", "edinburgh", "weather").get(), "wet"); Assert.assertEquals(n.root().at("europe", "uk", "glasgow", "weather").get(), "even wetter"); }
@Test public void testJsonyaDeepSimple() { Navigator<MutableMap<Object, Object>> n = Jsonya.of(europeMap()) .at("europe").add("spain", "plains"); Assert.assertEquals( n.root().get("europe", "spain"), "plains" ); Assert.assertEquals( n.getRootMap().size(), 1 ); Assert.assertEquals( n.root().at("europe").getFocusMap().size(), 3 ); }
/** gets the object at the indicated path from the current focus * (without changing the path to that focus; use {@link #at(Object, Object...)} to change focus) */ // Jun 2014, semantics changed so that focus does not change, which is more natural public Object get(Object pathSegment, Object ...furtherPathSegments) { push(); at(pathSegment, furtherPathSegments); Object result = get(); pop(); return result; }
@SuppressWarnings("rawtypes") @Test public void testJsonyaMapExistingAndRootModification() { Navigator<MutableMap<Object, Object>> n = Jsonya.of(europeMap().getRootMap()).at("asia") .put(MutableMap.of("china", null)) .put(MutableMap.of("japan", null)); Assert.assertTrue( n.root().at("asia").get(Map.class).containsKey("china") ); Assert.assertTrue( ((Map)n.root().get("asia")).containsKey("japan") ); }
public void testAddStringAtKeySequence() { Navigator<MutableMap<Object, Object>> j = Jsonya.newInstance().at("1", "2").add("value"); Assert.assertEquals( j.get(), MutableMap.of("1", MutableMap.of("2", "value"))); } }
@Test public void testAddStringAtKey() { Navigator<MutableMap<Object, Object>> j = Jsonya.newInstance().at("root").add("value").root(); Assert.assertEquals( j.get(), MutableMap.of("root", "value")); } public void testAddStringAtKeySequence() {
@Test public void testAddStringToListAtRoot() { Navigator<MutableMap<Object, Object>> j = Jsonya.newInstance().list().add("a", "b").root(); Assert.assertEquals( j.toString(), "[ \"a\", \"b\" ]" ); Assert.assertEquals( j.get(), MutableList.of("a", "b") ); }
/** Returns JSON serialized output for given focus in the given jsonya; * applies a naive toString for specialized types */ @Override public String toString() { return render(get()); } }