JsonPatchToExpressionSpecBuilder implements Function<JsonPatch, ExpressionSpecBuilder> { @Override public ExpressionSpecBuilder apply(JsonPatch t) { ExpressionSpecBuilder builder = new ExpressionSpecBuilder(); t.getOperations().forEach(operation -> operation.applyToBuilder(builder)); return builder; }  @Override ExpressionSpecBuilder apply(JsonPatch t);  }
@Test public void testEmpty() throws Exception { String patchExpression = "[]"; JsonNode jsonNode = JsonLoader.fromString(patchExpression); JsonPatch jsonPatch = JsonPatch.fromJson(jsonNode); ExpressionSpecBuilder actual = sut.apply(jsonPatch); assertThat(actual, is(notNullValue())); UpdateItemExpressionSpec actualSpec = actual.buildForUpdate(); assertThat(actualSpec.getConditionExpression(), is(nullValue())); assertThat(actualSpec.getUpdateExpression(), is("")); assertThat(actualSpec.getNameMap(), is(nullValue())); assertThat(actualSpec.getValueMap(), is(nullValue())); }
@Test public void test_replace_singlePath_number() throws Exception { String patchExpression = "[ { \"op\": \"replace\", \"path\": \"/a\", \"value\": 1 } ]"; JsonNode jsonNode = JsonLoader.fromString(patchExpression); JsonPatch jsonPatch = JsonPatch.fromJson(jsonNode); UpdateItemExpressionSpec expectedSpec = new ExpressionSpecBuilder() .addUpdate(N("a").set(1)) .buildForUpdate(); ExpressionSpecBuilder actual = sut.apply(jsonPatch); assertThat(actual, is(notNullValue())); UpdateItemExpressionSpec actualSpec = actual.buildForUpdate(); assertThat(actualSpec.getConditionExpression(), is(nullValue())); assertThat(actualSpec.getUpdateExpression(), is(expectedSpec.getUpdateExpression())); assertThat(actualSpec.getNameMap(), is(expectedSpec.getNameMap())); assertThat(actualSpec.getValueMap(), is(expectedSpec.getValueMap())); }
@Test public void test_replace_nestedPath_string() throws Exception { String patchExpression = "[ { \"op\": \"replace\", \"path\": \"/a/b\", \"value\": \"foo\" } ]"; JsonNode jsonNode = JsonLoader.fromString(patchExpression); JsonPatch jsonPatch = JsonPatch.fromJson(jsonNode); UpdateItemExpressionSpec expectedSpec = new ExpressionSpecBuilder() .addUpdate(S("a.b").set("foo")) .buildForUpdate(); ExpressionSpecBuilder actual = sut.apply(jsonPatch); assertThat(actual, is(notNullValue())); UpdateItemExpressionSpec actualSpec = actual.buildForUpdate(); assertThat(actualSpec.getConditionExpression(), is(nullValue())); assertThat(actualSpec.getUpdateExpression(), is(expectedSpec.getUpdateExpression())); assertThat(actualSpec.getNameMap(), is(expectedSpec.getNameMap())); assertThat(actualSpec.getValueMap(), is(expectedSpec.getValueMap())); }
@Test public void test_remove_singlePath() throws Exception { String patchExpression = "[ { \"op\": \"remove\", \"path\": \"/a\" } ]"; JsonNode jsonNode = JsonLoader.fromString(patchExpression); JsonPatch jsonPatch = JsonPatch.fromJson(jsonNode); UpdateItemExpressionSpec expectedSpec = new ExpressionSpecBuilder() .addUpdate(NULL("a").remove()) .buildForUpdate(); ExpressionSpecBuilder actual = sut.apply(jsonPatch); assertThat(actual, is(notNullValue())); UpdateItemExpressionSpec actualSpec = actual.buildForUpdate(); assertThat(actualSpec.getConditionExpression(), is(nullValue())); assertThat(actualSpec.getUpdateExpression(), is(expectedSpec.getUpdateExpression())); assertThat(actualSpec.getNameMap(), is(expectedSpec.getNameMap())); assertThat(actualSpec.getValueMap(), is(expectedSpec.getValueMap())); }