@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") ); }
@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(groups="Integration") public void testProvisioningPropertiesViaJsonya() throws Exception { Entity app = createAndStartApplication( Jsonya.newInstance() .put("location", "localhost") .at("services").list() .put("type", EmptySoftwareProcess.class.getName()) .at("provisioning.properties").put("minRam", 16384) .root().toString()); waitForApplicationTasks(app); log.info("App started:"); Entities.dumpInfo(app); EmptySoftwareProcess entity = (EmptySoftwareProcess) app.getChildren().iterator().next(); Map<String, Object> pp = entity.getConfig(EmptySoftwareProcess.PROVISIONING_PROPERTIES); Assert.assertEquals(pp.get("minRam"), 16384); }
@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)) ); }
private MockResponse applicationStatusResponse(String status) { String body = Jsonya.newInstance() .put("status", status) .at("spec", "locations").list().add("localhost") .root() .toString(); return newJsonResponse() .setBody(body); }
@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 ); }
public static JsonElement europeMap() { Navigator<MutableMap<Object, Object>> europe = Jsonya.newInstance().at("europe", "uk", "edinburgh") .put("population", 500*1000) .put("weather", "wet", "lighting", "dark") .root().at("europe").at("france").put("population", 80*1000*1000) .root(); return new JsonParser().parse( europe.toString() ); }
@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 testAddStringToList() { Navigator<MutableMap<Object, Object>> j = Jsonya.newInstance().at("root").list().add("a", "b").root(); Assert.assertEquals( j.toString(), "{ \"root\": [ \"a\", \"b\" ] }"); }
@SuppressWarnings("rawtypes") @Test public void testCreateMapInList2() { MutableMap<Object, Object> map = Jsonya.at("countries").list().map().add("europe", "uk") .root().at("countries").add("antarctica") .root().at("countries").map().add("asia", (Object)null) .at("asia").list().add("china", "japan").getRootMap(); List l = (List)map.get("countries"); Assert.assertEquals( ((Map)l.get(0)).get("europe"), "uk" ); }
@Test public void testAddMapToExistingAddsCopy() { MutableMap<Object, Object> map = MutableMap.<Object,Object>of("a", 1, "b", 2); Navigator<MutableMap<Object, Object>> j = Jsonya.newInstance().at("root").map().add(map).root(); Assert.assertEquals( j.toString(), "{ \"root\": { \"a\": 1, \"b\": 2 } }"); map.put("b", 1); Assert.assertEquals( j.toString(), "{ \"root\": { \"a\": 1, \"b\": 2 } }"); }
@Test public void testJsonyaMaybe() { Navigator<MutableMap<Object, Object>> m = europeMap(); Assert.assertEquals( m.at("europe", "spain", "barcelona").getMaybe().or("norealabc"), "norealabc" ); Assert.assertEquals(m.root().at("europe").getFocusMap().keySet(), MutableSet.of("uk", "france")); }
@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") ); }
@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 testAddListToExistingAddsCopy() { MutableList<Object> list = MutableList.<Object>of("a", "b"); Navigator<MutableMap<Object, Object>> j = Jsonya.newInstance().at("root").list().add(list).root(); Assert.assertEquals( j.toString(), "{ \"root\": [ \"a\", \"b\" ] }"); list.append("c"); Assert.assertEquals( j.toString(), "{ \"root\": [ \"a\", \"b\" ] }"); }
@Test public void testJsonyaBadPathNull() { Navigator<MutableMap<Object, Object>> m = europeMap(); // does not create (but if we 'pushed' it would) Assert.assertNull( m.at("europe", "spain", "barcelona").get() ); Assert.assertNull( m.root().at("europe").at("spain").at("barcelona").get() ); } @Test