Search in sources :

Example 1 with PatchTree

use of com.linkedin.data.transform.patch.request.PatchTree in project rest.li by linkedin.

the class TestPatchCreation method testDiffPatchCreationMixed.

@Test
public void testDiffPatchCreationMixed() throws Exception {
    DataMap map = new DataMap(asMap("qux", 42, "bar", new DataMap(asMap("baz", "The quick brown fox"))));
    DataMap map2 = map.copy();
    map2.remove("foo");
    ((DataMap) map2.get("bar")).remove("baz");
    map2.put("foo", 42);
    map2.remove("qux");
    PatchTree patch = new PatchTree();
    patch.addOperation(new PathSpec("foo"), PatchOpFactory.setFieldOp(42));
    patch.addOperation(new PathSpec("bar", "baz"), PatchOpFactory.REMOVE_FIELD_OP);
    patch.addOperation(new PathSpec("qux"), PatchOpFactory.REMOVE_FIELD_OP);
    //"{$set={foo=42}, bar={$delete=[baz]}, $delete=[qux]}"
    final DataMap fooMap = new DataMap();
    fooMap.put("foo", 42);
    final DataMap deleteMap = new DataMap();
    deleteMap.put(PatchConstants.DELETE_COMMAND, new DataList(Arrays.asList("baz")));
    final DataMap setBarDeleteMap = new DataMap();
    setBarDeleteMap.put(PatchConstants.SET_COMMAND, fooMap);
    setBarDeleteMap.put("bar", deleteMap);
    setBarDeleteMap.put(PatchConstants.DELETE_COMMAND, new DataList(Arrays.asList("qux")));
    assertEquals(patch.getDataMap(), setBarDeleteMap, "PatchTree DataMap must be correct");
}
Also used : DataList(com.linkedin.data.DataList) PathSpec(com.linkedin.data.schema.PathSpec) PatchTree(com.linkedin.data.transform.patch.request.PatchTree) DataMap(com.linkedin.data.DataMap) Test(org.testng.annotations.Test)

Example 2 with PatchTree

use of com.linkedin.data.transform.patch.request.PatchTree in project rest.li by linkedin.

the class TestPatchCreation method testDiffPatchCreationRemove.

@Test
public void testDiffPatchCreationRemove() throws Exception {
    DataMap map = new DataMap(asMap("foo", 42, "bar", new DataMap(asMap("baz", "The quick brown fox"))));
    DataMap map2 = map.copy();
    map2.remove("foo");
    ((DataMap) map2.get("bar")).remove("baz");
    PatchTree patch = PatchCreator.diff(map, map2);
    Assert.assertEquals(patch.toString(), "{bar={$delete=[baz]}, $delete=[foo]}");
}
Also used : PatchTree(com.linkedin.data.transform.patch.request.PatchTree) DataMap(com.linkedin.data.DataMap) Test(org.testng.annotations.Test)

Example 3 with PatchTree

use of com.linkedin.data.transform.patch.request.PatchTree in project rest.li by linkedin.

the class TestPatchCreation method testDiffPatchCreationSet.

@Test
public void testDiffPatchCreationSet() throws Exception {
    DataMap map = new DataMap();
    DataMap map2 = map.copy();
    map2.put("foo", 42);
    map2.put("bar", new DataMap());
    ((DataMap) map2.get("bar")).put("baz", "The quick brown fox");
    PatchTree patch = PatchCreator.diff(map, map2);
    //"{$set={foo=42, bar={baz=The quick brown fox}}}"
    final DataMap bazMap = new DataMap();
    bazMap.put("baz", "The quick brown fox");
    final DataMap fooBarMap = new DataMap();
    fooBarMap.put("foo", 42);
    fooBarMap.put("bar", bazMap);
    final DataMap setMap = new DataMap();
    setMap.put(PatchConstants.SET_COMMAND, fooBarMap);
    assertEquals(patch.getDataMap(), setMap, "PatchTree DataMap must be correct");
}
Also used : PatchTree(com.linkedin.data.transform.patch.request.PatchTree) DataMap(com.linkedin.data.DataMap) Test(org.testng.annotations.Test)

Example 4 with PatchTree

use of com.linkedin.data.transform.patch.request.PatchTree in project rest.li by linkedin.

the class TestPatchCreation method testExplicitPatchCreationMixed.

@Test
public void testExplicitPatchCreationMixed() {
    PatchTree patch = new PatchTree();
    patch.addOperation(new PathSpec("foo"), PatchOpFactory.setFieldOp(42));
    patch.addOperation(new PathSpec("bar", "baz"), PatchOpFactory.REMOVE_FIELD_OP);
    patch.addOperation(new PathSpec("qux"), PatchOpFactory.REMOVE_FIELD_OP);
    //"{$set={foo=42}, bar={$delete=[baz]}, $delete=[qux]}"
    final DataMap fooMap = new DataMap();
    fooMap.put("foo", 42);
    final DataMap deleteMap = new DataMap();
    deleteMap.put(PatchConstants.DELETE_COMMAND, new DataList(Arrays.asList("baz")));
    final DataMap setBarDeleteMap = new DataMap();
    setBarDeleteMap.put(PatchConstants.SET_COMMAND, fooMap);
    setBarDeleteMap.put("bar", deleteMap);
    setBarDeleteMap.put(PatchConstants.DELETE_COMMAND, new DataList(Arrays.asList("qux")));
    assertEquals(patch.getDataMap(), setBarDeleteMap, "PatchTree DataMap must be correct");
}
Also used : DataList(com.linkedin.data.DataList) PathSpec(com.linkedin.data.schema.PathSpec) PatchTree(com.linkedin.data.transform.patch.request.PatchTree) DataMap(com.linkedin.data.DataMap) Test(org.testng.annotations.Test)

Example 5 with PatchTree

use of com.linkedin.data.transform.patch.request.PatchTree in project rest.li by linkedin.

the class TestPatchTreeRecorder method testSimpleSetRemoveIfNullSetMode.

@Test
public void testSimpleSetRemoveIfNullSetMode() {
    PatchTreeRecorder<PatchTreeTestModel> pc = makeOne();
    PatchTreeTestModel testModel = pc.getRecordingProxy();
    testModel.setFooOptional(null, SetMode.REMOVE_IF_NULL);
    // Augment the patch request with the removes
    PatchTree ptExpect = new PatchTree();
    ptExpect.addOperation(PatchTreeTestModel.fields().fooOptional(), new RemoveFieldOp());
    Assert.assertEquals(pc.generatePatchTree().getDataMap(), ptExpect.getDataMap());
}
Also used : PatchTreeTestModel(com.linkedin.restli.client.util.test.PatchTreeTestModel) RemoveFieldOp(com.linkedin.data.transform.patch.request.RemoveFieldOp) PatchTree(com.linkedin.data.transform.patch.request.PatchTree) Test(org.testng.annotations.Test)

Aggregations

PatchTree (com.linkedin.data.transform.patch.request.PatchTree)27 Test (org.testng.annotations.Test)27 DataMap (com.linkedin.data.DataMap)18 PathSpec (com.linkedin.data.schema.PathSpec)9 Group (com.linkedin.restli.examples.groups.api.Group)9 DataList (com.linkedin.data.DataList)5 PatchTreeTestModel (com.linkedin.restli.client.util.test.PatchTreeTestModel)5 ResourceMethodDescriptor (com.linkedin.restli.internal.server.model.ResourceMethodDescriptor)5 ResourceModel (com.linkedin.restli.internal.server.model.ResourceModel)5 RestLiTestHelper.buildResourceModel (com.linkedin.restli.server.test.RestLiTestHelper.buildResourceModel)5 AfterTest (org.testng.annotations.AfterTest)5 BeforeTest (org.testng.annotations.BeforeTest)5 DataComplexProcessor (com.linkedin.data.transform.DataComplexProcessor)4 Patch (com.linkedin.data.transform.patch.Patch)4 RemoveFieldOp (com.linkedin.data.transform.patch.request.RemoveFieldOp)4 ByteString (com.linkedin.data.ByteString)3 HttpStatus (com.linkedin.restli.common.HttpStatus)3 CustomString (com.linkedin.restli.server.custom.types.CustomString)3 DiscoveredItem (com.linkedin.restli.server.twitter.TwitterTestDataModels.DiscoveredItem)3 DiscoveredItemKey (com.linkedin.restli.server.twitter.TwitterTestDataModels.DiscoveredItemKey)3