/** includes the given attributes in the attributes to be passed to chef; * when combining with other attributes, this uses {@link Jsonya} semantics to add * (a deep add, combining lists and maps) */ public KnifeConvergeTaskFactory<RET> knifeAddAttributes(Map<? extends Object, ? extends Object> attributes) { if (attributes!=null && !attributes.isEmpty()) { Jsonya.of(knifeAttributes).add(attributes); } return self(); }
/** includes the given attributes in the attributes to be passed to chef; * when combining with other attributes, this uses {@link Jsonya} semantics to add * (a deep add, combining lists and maps) */ public KnifeConvergeTaskFactory<RET> knifeAddAttributes(Map<? extends Object, ? extends Object> attributes) { if (attributes!=null && !attributes.isEmpty()) { Jsonya.of(knifeAttributes).add(attributes); } return self(); }
@Test public void testAddStringToRoot() { Navigator<MutableMap<Object, Object>> j = Jsonya.newInstance().add("a"); Assert.assertEquals( j.toString(), "\"a\""); Assert.assertEquals( j.get(), "a"); }
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") ); }
@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" ); }
@SuppressWarnings("rawtypes") @Test public void testCreateMapInList1() { MutableMap<Object, Object> map = Jsonya.at("countries").list().map().add("europe", "uk").getRootMap(); List l = (List)map.get("countries"); Assert.assertEquals( ((Map)l.get(0)).get("europe"), "uk" ); } @SuppressWarnings("rawtypes")
private static String toJson(Map<?,?> x) { // was: // return new Gson().toJson(x); // but GSON does funny things with DSL, whereas toString is the right thing to do return Jsonya.newInstance().add(x).toString(); }
@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 testAddOddStringsAtRootIsError() { try { Jsonya.newInstance().add("a", 1, "b"); Asserts.shouldHaveFailedPreviously(); } catch (Exception e) { Asserts.expectedFailureContainsIgnoreCase(e, "odd"); } }
@Test public void testAddListAtRootAddsReference() { MutableList<Object> list = MutableList.<Object>of("a", "b"); Navigator<MutableMap<Object, Object>> j = Jsonya.newInstance().add(list); Assert.assertEquals( j.toString(), "[ \"a\", \"b\" ]"); list.append("c"); Assert.assertEquals( j.toString(), "[ \"a\", \"b\", \"c\" ]"); }
@Test public void testAddListToExistingRootAddsCopy() { MutableList<Object> list = MutableList.<Object>of("a", "b"); Navigator<MutableMap<Object, Object>> j = Jsonya.newInstance().list().add(list); Assert.assertEquals( j.toString(), "[ \"a\", \"b\" ]"); list.append("c"); Assert.assertEquals( j.toString(), "[ \"a\", \"b\" ]"); }
@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 testAddMapToExistingRootAddsCopy() { MutableMap<Object, Object> map = MutableMap.<Object,Object>of("a", 1, "b", 2); Navigator<MutableMap<Object, Object>> j = Jsonya.newInstance().map().add(map); Assert.assertEquals( j.toString(), "{ \"a\": 1, \"b\": 2 }"); map.put("b", 1); Assert.assertEquals( j.toString(), "{ \"a\": 1, \"b\": 2 }"); }
@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 testAddListAddsReference() { MutableList<Object> list = MutableList.<Object>of("a", "b"); Navigator<MutableMap<Object, Object>> j = Jsonya.newInstance().add("root", list); Assert.assertEquals( j.toString(), "{ \"root\": [ \"a\", \"b\" ] }"); list.append("c"); Assert.assertEquals( j.toString(), "{ \"root\": [ \"a\", \"b\", \"c\" ] }"); }
@Test public void testAddMapAddsReference() { MutableMap<Object, Object> map = MutableMap.<Object,Object>of("a", 1, "b", 2); Navigator<MutableMap<Object, Object>> j = Jsonya.newInstance().add("root", map); Assert.assertEquals( j.toString(), "{ \"root\": { \"a\": 1, \"b\": 2 } }"); map.put("b", 1); Assert.assertEquals( j.toString(), "{ \"root\": { \"a\": 1, \"b\": 1 } }"); }
@SuppressWarnings("rawtypes") @Override public Object applyToKeyInMap(MapConfigKey<V> key, Map target) { return key.applyValueToMap(Jsonya.of(key.rawValue(target)).add(this).getRootMap(), target); } };