use of com.linkedin.data.transform.DataComplexProcessor in project rest.li by linkedin.
the class RestLiDataValidator method validatePatch.
/**
* Checks that if the patch is applied to a valid entity, the modified entity will also be valid.
* This method...
* (1) Checks that required/ReadOnly/CreateOnly fields are not deleted.
* (2) Checks that new values for record templates contain all required fields (treating ReadOnly fields as optional).
* (3) Applies the patch to an empty entity and validates the entity for custom validation rules
* and Rest.li annotations, allowing the following exceptions:
* - Allows required fields to be absent by using {@link RequiredMode#IGNORE},
* because a patch does not necessarily contain all fields.
* - Allows array-descendant ReadOnly/CreateOnly fields to be set, since there's currently no way to "patch" arrays.
*
* @param patchRequest the patch
* @return the final validation result
*/
private ValidationResult validatePatch(PatchRequest<?> patchRequest) {
// Instantiate an empty entity.
RecordTemplate entity;
try {
entity = _valueClass.newInstance();
} catch (InstantiationException e) {
return validationResultWithErrorMessage(INSTANTIATION_ERROR);
} catch (IllegalAccessException e) {
return validationResultWithErrorMessage(ILLEGAL_ACCESS_ERROR);
}
// Apply the patch to the entity and get paths that $set and $delete operations were performed on.
@SuppressWarnings("unchecked") PatchRequest<RecordTemplate> patch = (PatchRequest<RecordTemplate>) patchRequest;
DataComplexProcessor processor = new DataComplexProcessor(new Patch(true), patch.getPatchDocument(), entity.data());
MessageList<Message> messages;
try {
messages = processor.runDataProcessing(false);
} catch (DataProcessingException e) {
return validationResultWithErrorMessage("Error while applying patch: " + e.getMessage());
}
// Check that required/ReadOnly/CreateOnly fields are not deleted
ValidationErrorResult checkDeleteResult = new ValidationErrorResult();
checkDeletesAreValid(entity.schema(), messages, checkDeleteResult);
if (!checkDeleteResult.isValid()) {
return checkDeleteResult;
}
// Check that new values for record templates contain all required fields
ValidationResult checkSetResult = checkNewRecordsAreNotMissingFields(entity, messages);
if (checkSetResult != null) {
return checkSetResult;
}
// It's okay if required fields are absent in a partial update request, so use ignore mode.
return ValidateDataAgainstSchema.validate(new SimpleDataElement(entity.data(), entity.schema()), new ValidationOptions(RequiredMode.IGNORE), getValidatorForInputEntityValidation(entity.schema()));
}
use of com.linkedin.data.transform.DataComplexProcessor in project rest.li by linkedin.
the class TestPatchGeneration method testRoundtripDeleteField.
@Test
void testRoundtripDeleteField() 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");
assertFalse(g1.equals(g2));
DataComplexProcessor processor = new DataComplexProcessor(new Patch(), update.getDataMap(), g1.data());
processor.run(false);
assertEquals(g1, g2);
}
use of com.linkedin.data.transform.DataComplexProcessor in project rest.li by linkedin.
the class PatchApplier method applyPatch.
public static <T extends RecordTemplate> void applyPatch(T original, PatchRequest<T> patch) throws DataProcessingException {
DataComplexProcessor processor = new DataComplexProcessor(new Patch(), patch.getPatchDocument(), original.data());
processor.run(false);
}
use of com.linkedin.data.transform.DataComplexProcessor in project rest.li by linkedin.
the class TestPatchOnData method testDeleteAndBeBranchAtSameTime.
@Test
public void testDeleteAndBeBranchAtSameTime() throws JsonParseException, IOException, DataProcessingException {
DataComplexProcessor processor = new DataComplexProcessor(new Patch(), dataMapFromString(//command $set should be used
"{ \"b\": { \"$set\": { \"b\": 1} }, \"$delete\": [\"b\"] }"), dataMapFromString("{\"a\": 1}"));
boolean thrown = false;
try {
processor.run(false);
} catch (DataProcessingException e) {
thrown = true;
}
if (!thrown)
fail("expected DataProcessingException to be thrown");
}
use of com.linkedin.data.transform.DataComplexProcessor in project rest.li by linkedin.
the class TestPatchOnData method testDeleteAndSetSameField.
@Test
public void testDeleteAndSetSameField() throws JsonParseException, IOException, DataProcessingException {
DataComplexProcessor processor = new DataComplexProcessor(new Patch(), dataMapFromString(//command $set should be used
"{ \"$set\": { \"b\": 1}, \"$delete\": [\"b\"] }"), dataMapFromString("{\"a\": 1}"));
boolean thrown = false;
try {
processor.run(false);
} catch (DataProcessingException e) {
thrown = true;
}
if (!thrown)
fail("expected DataProcessingException to be thrown");
}
Aggregations