Search in sources :

Example 21 with PatchTree

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

the class TestPatchGeneration method testDiffFromNull.

@Test
public void testDiffFromNull() throws Exception {
    Group g1 = new Group();
    Group g2 = new Group(g1.data().copy());
    g2.setId(42);
    g2.setName("Some Group");
    PatchTree update = PatchCreator.diff(g1, g2);
    //"{$set={id=42, name=Some Group}}"
    final DataMap internalSetMap = new DataMap();
    internalSetMap.put("id", 42);
    internalSetMap.put("name", "Some Group");
    final DataMap setMap = new DataMap();
    setMap.put(PatchConstants.SET_COMMAND, internalSetMap);
    assertEquals(update.getDataMap(), setMap, "PatchTree DataMap should be correct");
}
Also used : Group(com.linkedin.restli.examples.groups.api.Group) PatchTree(com.linkedin.data.transform.patch.request.PatchTree) DataMap(com.linkedin.data.DataMap) Test(org.testng.annotations.Test)

Example 22 with PatchTree

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

the class TestPatchGeneration method testRoundtripAddEscapedField.

@Test
void testRoundtripAddEscapedField() throws Exception {
    Group g1 = new Group();
    g1.setId(17);
    g1.setDescription("Some description");
    Group g2 = new Group(g1.data().copy());
    g2.data().put("$foo", "value");
    PatchTree update = PatchCreator.diff(g1, g2);
    //"{$set={$foo=value}}"
    final DataMap setMap = new DataMap();
    final DataMap fooMap = new DataMap();
    fooMap.put("$foo", "value");
    setMap.put(PatchConstants.SET_COMMAND, fooMap);
    assertEquals(update.getDataMap(), setMap, "PatchTree DataMap should be correct");
    assertFalse(g1.equals(g2));
    DataComplexProcessor processor = new DataComplexProcessor(new Patch(), update.getDataMap(), g1.data());
    processor.run(false);
    assertEquals(g1, g2);
}
Also used : Group(com.linkedin.restli.examples.groups.api.Group) DataComplexProcessor(com.linkedin.data.transform.DataComplexProcessor) Patch(com.linkedin.data.transform.patch.Patch) PatchTree(com.linkedin.data.transform.patch.request.PatchTree) DataMap(com.linkedin.data.DataMap) Test(org.testng.annotations.Test)

Example 23 with PatchTree

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

the class TestPatchGeneration method testExplicitUpdate.

@Test
public void testExplicitUpdate() {
    PatchTree explicitUpdateSpec = new PatchTree();
    explicitUpdateSpec.addOperation(Group.fields().id(), PatchOpFactory.setFieldOp(42));
    explicitUpdateSpec.addOperation(Group.fields().name(), PatchOpFactory.setFieldOp("Foo"));
    explicitUpdateSpec.addOperation(Group.fields().description(), PatchOpFactory.REMOVE_FIELD_OP);
    //"{$set={id=42, name=Foo}, $delete=[description]}"
    final DataMap setDeleteMap = new DataMap();
    final DataMap idNameMap = new DataMap();
    idNameMap.put("id", 42);
    idNameMap.put("name", "Foo");
    setDeleteMap.put(PatchConstants.SET_COMMAND, idNameMap);
    setDeleteMap.put(PatchConstants.DELETE_COMMAND, new DataList(Arrays.asList("description")));
    assertEquals(explicitUpdateSpec.getDataMap(), setDeleteMap, "PatchTree DataMap should be correct");
}
Also used : DataList(com.linkedin.data.DataList) PatchTree(com.linkedin.data.transform.patch.request.PatchTree) DataMap(com.linkedin.data.DataMap) Test(org.testng.annotations.Test)

Example 24 with PatchTree

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

the class TestPatchGeneration method testDiffFromOverwrittenNested.

@Test
public void testDiffFromOverwrittenNested() throws Exception {
    Group g1 = new Group();
    Location loc1 = new Location();
    loc1.setLatitude(0.0f);
    loc1.setLongitude(0.0f);
    g1.setLocation(loc1);
    Group g2 = new Group(g1.data().copy());
    Location loc2 = new Location();
    loc2.setLatitude(42.0f);
    loc2.setLongitude(17.0f);
    g2.setLocation(loc2);
    PatchTree update = PatchCreator.diff(g1, g2);
    //"{location={$set={longitude=17.0, latitude=42.0}}}"
    final DataMap setMap = new DataMap();
    final DataMap longLatMap = new DataMap();
    longLatMap.put("longitude", 17.0f);
    longLatMap.put("latitude", 42.0f);
    setMap.put(PatchConstants.SET_COMMAND, longLatMap);
    final DataMap locationMap = new DataMap();
    locationMap.put("location", setMap);
    Assert.assertEquals(update.getDataMap(), locationMap, "PatchTree DataMap should be correct");
}
Also used : Group(com.linkedin.restli.examples.groups.api.Group) PatchTree(com.linkedin.data.transform.patch.request.PatchTree) Location(com.linkedin.restli.examples.groups.api.Location) DataMap(com.linkedin.data.DataMap) Test(org.testng.annotations.Test)

Example 25 with PatchTree

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

the class TestPatchGeneration method testDiffRemove.

@Test
void testDiffRemove() throws Exception {
    Group g1 = new Group();
    g1.setId(17);
    g1.setDescription("Some description");
    Group g2 = new Group(g1.data().copy());
    g2.removeDescription();
    PatchTree update = PatchCreator.diff(g1, g2);
    //"{$delete=[description]}"
    final DataMap deleteMap = new DataMap();
    final DataList descriptionList = new DataList();
    descriptionList.add("description");
    deleteMap.put(PatchConstants.DELETE_COMMAND, descriptionList);
    assertEquals(update.getDataMap(), deleteMap, "PatchTree DataMap should be correct");
}
Also used : Group(com.linkedin.restli.examples.groups.api.Group) DataList(com.linkedin.data.DataList) PatchTree(com.linkedin.data.transform.patch.request.PatchTree) DataMap(com.linkedin.data.DataMap) 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