AttributesHelper { public static boolean applyAttributeChange( final RepositoryContent content, final AttributeChange change, final String key, @Nullable final Object value) { return applyAttributeChange(content.attributes(), change, key, value); } private  AttributesHelper(); static boolean applyAttributeChange(
      final RepositoryContent content,
      final AttributeChange change,
      final String key,
      @Nullable final Object value); static boolean applyAttributeChange(
      final AttributesMap attributes,
      final AttributeChange change,
      final String key,
      @Nullable final Object value);  }
@Test public void testSimpleChanges() { assertThat(attributes.get("myvalue"), is(nullValue())); assertTrue(applyAttributeChange(attributes, SET, "myvalue", 1)); assertFalse(applyAttributeChange(attributes, SET, "myvalue", 1)); assertTrue(applyAttributeChange(attributes, REMOVE, "myvalue", null)); assertFalse(applyAttributeChange(attributes, REMOVE, "myvalue", null)); }
@Test public void testListChanges() { assertThat(attributes.get("mylist"), is(nullValue())); assertTrue(applyAttributeChange(attributes, APPEND, "mylist", 2)); assertThat((List<?>) attributes.get("mylist"), contains(2)); assertTrue(applyAttributeChange(attributes, PREPEND, "mylist", 3)); assertThat((List<?>) attributes.get("mylist"), contains(3, 2)); assertTrue(applyAttributeChange(attributes, APPEND, "mylist", 1)); assertThat((List<?>) attributes.get("mylist"), contains(3, 2, 1)); try { applyAttributeChange(attributes, APPEND, "notalist", 1); fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException e) { assertThat(e.getMessage(), is("Cannot append to non-list attribute")); } try { applyAttributeChange(attributes, PREPEND, "notalist", 1); fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException e) { assertThat(e.getMessage(), is("Cannot prepend to non-list attribute")); } }
@Test @SuppressWarnings("unchecked") public void testMapChanges() throws IOException { assertThat(attributes.get("mymap"), is(nullValue())); Map<String, Object> overlay1 = objectMapper.readValue( "{" + "\"one\" : {" + "\"alpha\" : {" + "\"beta\" : {" + "\"test\" : \"value1\"" + "}}}," + "\"two\" : {" + "\"alpha\" : {" + "\"beta\" : {" + "\"test\" : \"value2\"" + "}}}," + "\"three\" : {" + "\"alpha\" : {" + "\"beta\" : {" + "\"test\" : \"value3\"" + "}}}}", Map.class); Map<String, Object> overlay2 = objectMapper.readValue( "{" + "\"one\" : {" + "\"added\" : \"value!\"" + "}," + "\"two\" : {" + "\"alpha\" : {" + "\"beta\" : {" + "\"added\" : \"value!\"" + "}}}," + "\"added\" : \"value!\"" + "}", Map.class); Map<String, Object> overlay3 = objectMapper.readValue( "{" + "\"two\" : {" + "\"alpha\" : {" + "\"beta\" : {" + "\"test\" : \"value2 - overwrite not allow!\"" + "}}}}", Map.class); Map<String, Object> expected = objectMapper.readValue( "{" + "\"one\" : {" + "\"alpha\" : {" + "\"beta\" : {" + "\"test\" : \"value1\"" + "}}," + "\"added\" : \"value!\"" + "}," + "\"two\" : {" + "\"alpha\" : {" + "\"beta\" : {" + "\"test\" : \"value2\"," + "\"added\" : \"value!\"" + "}}}," + "\"three\" : {" + "\"alpha\" : {" + "\"beta\" : {" + "\"test\" : \"value3\"" + "}}}," + "\"added\" : \"value!\"" + "}", Map.class); assertTrue(applyAttributeChange(attributes, OVERLAY, "mymap", overlay1)); assertThat((Map<?, ?>) attributes.get("mymap"), is(overlay1)); assertFalse(applyAttributeChange(attributes, OVERLAY, "mymap", overlay1)); assertTrue(applyAttributeChange(attributes, OVERLAY, "mymap", overlay2)); assertThat((Map<?, ?>) attributes.get("mymap"), is(expected)); assertFalse(applyAttributeChange(attributes, OVERLAY, "mymap", overlay2)); try { applyAttributeChange(attributes, OVERLAY, "mymap", "text"); fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException e) { assertThat(e.getMessage(), containsString("Conflict: cannot overlay ")); } assertTrue(applyAttributeChange(attributes, OVERLAY, "mymap", overlay3)); assertFalse(applyAttributeChange(attributes, OVERLAY, "mymap", overlay3)); try { applyAttributeChange(attributes, OVERLAY, "notamap", overlay1); fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException e) { assertThat(e.getMessage(), containsString("Conflict: cannot overlay ")); } }
@Test public void testNullsNotAllowed() { try { applyAttributeChange(attributes, null, "myvalue", 1); fail("Expected NullPointerException"); } catch (NullPointerException e) { } try { applyAttributeChange(attributes, SET, null, 1); fail("Expected NullPointerException"); } catch (NullPointerException e) { } try { applyAttributeChange(attributes, SET, "myvalue", null); fail("Expected NullPointerException"); } catch (NullPointerException e) { } try { applyAttributeChange(attributes, APPEND, "mylist", null); } catch (NullPointerException e) { } try { applyAttributeChange(attributes, PREPEND, "mylist", null); } catch (NullPointerException e) { } try { applyAttributeChange(attributes, OVERLAY, "mymap", null); } catch (NullPointerException e) { } }