Search in sources :

Example 16 with PatchTree

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

the class TestPatchTreeRecorder method testPatchGeneratesDeepCopiesOfInternalState.

@Test
public void testPatchGeneratesDeepCopiesOfInternalState() {
    PatchTreeRecorder<PatchTreeTestModel> pc = makeOne();
    PatchTreeTestModel restCommonTestModel = pc.getRecordingProxy();
    restCommonTestModel.setFooRecordTemplate(new FooRecordTemplate().setBar(10l));
    PatchTree pt1 = pc.generatePatchTree();
    restCommonTestModel.setFooRecordTemplate(new FooRecordTemplate().setBar(20l));
    PatchTree pt2 = pc.generatePatchTree();
    Assert.assertNotEquals(pt1.getDataMap(), pt2.getDataMap());
    Assert.assertEquals(pt1.getDataMap(), diffEmpty(new PatchTreeTestModel().setFooRecordTemplate(new FooRecordTemplate().setBar(10l))));
    Assert.assertEquals(pt2.getDataMap(), diffEmpty(new PatchTreeTestModel().setFooRecordTemplate(new FooRecordTemplate().setBar(20l))));
}
Also used : FooRecordTemplate(com.linkedin.restli.client.util.test.FooRecordTemplate) PatchTreeTestModel(com.linkedin.restli.client.util.test.PatchTreeTestModel) PatchTree(com.linkedin.data.transform.patch.request.PatchTree) Test(org.testng.annotations.Test)

Example 17 with PatchTree

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

the class TestPatchTreeRecorder method testComplexDeepSetAndRemoves.

@Test
public void testComplexDeepSetAndRemoves() {
    PatchTreeRecorder<PatchTreeTestModel> pc = makeOne();
    PatchTreeTestModel testModel = pc.getRecordingProxy();
    PatchTreeTestModel.FooUnion fooUnion = new PatchTreeTestModel.FooUnion();
    fooUnion.setInt(10);
    testModel.setFooRequired(100).setFooUnion(fooUnion).setFooOptional(null, SetMode.REMOVE_IF_NULL);
    testModel.getFooRecordTemplate().setBar(9001l);
    // GetMode should be irrelevant
    testModel.getFooRecordTemplate(GetMode.DEFAULT).setBaz(null, SetMode.REMOVE_IF_NULL);
    PatchTree ptExpect = PatchCreator.diff(new DataMap(), new PatchTreeTestModel().setFooRequired(100).setFooUnion(fooUnion).data());
    // Augment the patch request with the removes in the same order so we get the same patch request.
    ptExpect.addOperation(PatchTreeTestModel.fields().fooOptional(), new RemoveFieldOp());
    ptExpect.addOperation(PatchTreeTestModel.fields().fooRecordTemplate().bar(), new SetFieldOp(9001l));
    ptExpect.addOperation(PatchTreeTestModel.fields().fooRecordTemplate().baz(), new RemoveFieldOp());
    Assert.assertEquals(pc.generatePatchTree().getDataMap(), ptExpect.getDataMap());
}
Also used : SetFieldOp(com.linkedin.data.transform.patch.request.SetFieldOp) PatchTreeTestModel(com.linkedin.restli.client.util.test.PatchTreeTestModel) RemoveFieldOp(com.linkedin.data.transform.patch.request.RemoveFieldOp) PatchTree(com.linkedin.data.transform.patch.request.PatchTree) DataMap(com.linkedin.data.DataMap) Test(org.testng.annotations.Test)

Example 18 with PatchTree

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

the class TestPatchGeneration method testDiffFromNonNull.

@Test
void testDiffFromNonNull() throws Exception {
    Group g1 = new Group();
    g1.setId(17);
    g1.setDescription("Some description");
    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 19 with PatchTree

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

the class TestPatchGeneration method testRoundtripModifyEscapedField.

@Test(groups = { TestConstants.TESTNG_GROUP_KNOWN_ISSUE })
void testRoundtripModifyEscapedField() throws Exception {
    Group g1 = new Group();
    g1.data().put("$foo", new DataMap());
    Group g2 = new Group(g1.data().copy());
    ((DataMap) g2.data().get("$foo")).put("bar", 42);
    PatchTree update = PatchCreator.diff(g1, g2);
    //"{$$foo={$set={bar=42}}}"
    final DataMap setMap = new DataMap();
    final DataMap barMap = new DataMap();
    barMap.put("bar", 42);
    setMap.put(PatchConstants.SET_COMMAND, barMap);
    final DataMap fooMap = new DataMap();
    fooMap.put("$$foo", setMap);
    assertEquals(update.getDataMap(), fooMap, "PatchTree DataMap must 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 20 with PatchTree

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

the class TestPatchGeneration method testRoundtripAddFields.

@Test
void testRoundtripAddFields() throws Exception {
    Group g1 = new Group();
    g1.setId(17);
    g1.setDescription("Some description");
    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 setMap = new DataMap();
    final DataMap idNameMap = new DataMap();
    idNameMap.put("id", 42);
    idNameMap.put("name", "Some Group");
    setMap.put(PatchConstants.SET_COMMAND, idNameMap);
    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)

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