Here are the examples of the java api org.bson.BsonArray taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
55 Examples
19
Source : JsonDiffTest2.java
with Apache License 2.0
from eBay
with Apache License 2.0
from eBay
public clreplaced JsonDiffTest2 {
private static BsonArray jsonNode;
@BeforeClreplaced
public static void beforeClreplaced() throws IOException {
String path = "/testdata/diff.json";
InputStream resourcereplacedtream = JsonDiffTest.clreplaced.getResourcereplacedtream(path);
String testData = IOUtils.toString(resourcereplacedtream, "UTF-8");
jsonNode = BsonArray.parse(testData);
}
@Test
public void testPatchAppliedCleanly() {
for (int i = 0; i < jsonNode.size(); i++) {
BsonDoreplacedent node = jsonNode.get(i).asDoreplacedent();
BsonValue first = node.get("first");
BsonValue second = node.get("second");
BsonArray patch = node.getArray("patch");
String message = node.containsKey("message") ? node.getString("message").getValue() : "";
BsonValue secondPrime = BsonPatch.apply(patch, first);
replacedert.replacedertThat(message, secondPrime, equalTo(second));
}
}
}
19
Source : JsonDiffTest.java
with Apache License 2.0
from eBay
with Apache License 2.0
from eBay
public clreplaced JsonDiffTest {
private static BsonArray jsonNode;
@BeforeClreplaced
public static void beforeClreplaced() throws IOException {
String path = "/testdata/sample.json";
InputStream resourcereplacedtream = JsonDiffTest.clreplaced.getResourcereplacedtream(path);
String testData = IOUtils.toString(resourcereplacedtream, "UTF-8");
jsonNode = BsonArray.parse(testData);
}
@Test
public void testSampleJsonDiff() {
for (int i = 0; i < jsonNode.size(); i++) {
BsonValue first = jsonNode.get(i).asDoreplacedent().get("first");
BsonValue second = jsonNode.get(i).asDoreplacedent().get("second");
BsonArray actualPatch = BsonDiff.asBson(first, second);
BsonValue secondPrime = BsonPatch.apply(actualPatch, first);
replacedert.replacedertEquals("BSON Patch not symmetrical [index=" + i + ", first=" + first + "]", second, secondPrime);
}
}
@Test
public void testGeneratedJsonDiff() {
Random random = new Random();
for (int i = 0; i < 1000; i++) {
BsonArray first = TestDataGenerator.generate(random.nextInt(10));
BsonArray second = TestDataGenerator.generate(random.nextInt(10));
BsonArray actualPatch = BsonDiff.asBson(first, second);
BsonArray secondPrime = BsonPatch.apply(actualPatch, first).asArray();
replacedert.replacedertEquals(second, secondPrime);
}
}
@Test
public void testRenderedRemoveOperationOmitsValueByDefault() {
BsonDoreplacedent source = new BsonDoreplacedent();
BsonDoreplacedent target = new BsonDoreplacedent();
source.put("field", new BsonString("value"));
BsonArray diff = BsonDiff.asBson(source, target);
replacedert.replacedertEquals(Operation.REMOVE.rfcName(), diff.get(0).asDoreplacedent().getString("op").getValue());
replacedert.replacedertEquals("/field", diff.get(0).asDoreplacedent().getString("path").getValue());
replacedert.replacedertNull(diff.get(0).asDoreplacedent().get("value"));
}
@Test
public void testRenderedRemoveOperationRetainsValueIfOmitDiffFlagNotSet() {
BsonDoreplacedent source = new BsonDoreplacedent();
BsonDoreplacedent target = new BsonDoreplacedent();
source.put("field", new BsonString("value"));
EnumSet<DiffFlags> flags = DiffFlags.defaults().clone();
replacedert.replacedertTrue("Expected OMIT_VALUE_ON_REMOVE by default", flags.remove(DiffFlags.OMIT_VALUE_ON_REMOVE));
BsonArray diff = BsonDiff.asBson(source, target, flags);
replacedert.replacedertEquals(Operation.REMOVE.rfcName(), diff.get(0).asDoreplacedent().getString("op").getValue());
replacedert.replacedertEquals("/field", diff.get(0).asDoreplacedent().getString("path").getValue());
replacedert.replacedertEquals("value", diff.get(0).asDoreplacedent().getString("value").getValue());
}
@Test
public void testRenderedOperationsExceptMoveAndCopy() throws Exception {
BsonDoreplacedent source = new BsonDoreplacedent();
source.put("age", new BsonInt32(10));
BsonDoreplacedent target = new BsonDoreplacedent();
target.put("height", new BsonInt32(10));
// only have ADD, REMOVE, REPLACE, Don't normalize operations into MOVE & COPY
EnumSet<DiffFlags> flags = DiffFlags.dontNormalizeOpIntoMoveAndCopy().clone();
BsonArray diff = BsonDiff.asBson(source, target, flags);
for (BsonValue d : diff) {
replacedert.replacedertNotEquals(Operation.MOVE.rfcName(), d.asDoreplacedent().getString("op").getValue());
replacedert.replacedertNotEquals(Operation.COPY.rfcName(), d.asDoreplacedent().getString("op").getValue());
}
BsonValue targetPrime = BsonPatch.apply(diff, source);
replacedert.replacedertEquals(target, targetPrime);
}
@Test
public void testPath() throws Exception {
BsonValue source = BsonDoreplacedent.parse("{\"profiles\":{\"abc\":[],\"def\":[{\"hello\":\"world\"}]}}");
BsonArray patch = BsonArray.parse("[{\"op\":\"copy\",\"from\":\"/profiles/def/0\", \"path\":\"/profiles/def/0\"},{\"op\":\"replace\",\"path\":\"/profiles/def/0/hello\",\"value\":\"world2\"}]");
BsonValue target = BsonPatch.apply(patch, source);
BsonValue expected = BsonDoreplacedent.parse("{\"profiles\":{\"abc\":[],\"def\":[{\"hello\":\"world2\"},{\"hello\":\"world\"}]}}");
replacedert.replacedertEquals(target, expected);
}
}
19
Source : CompatibilityTest.java
with Apache License 2.0
from eBay
with Apache License 2.0
from eBay
public clreplaced CompatibilityTest {
BsonArray addNodeWithMissingValue;
BsonArray replaceNodeWithMissingValue;
BsonArray removeNoneExistingArrayElement;
BsonArray replaceNode;
@Before
public void setUp() throws Exception {
addNodeWithMissingValue = BsonArray.parse("[{\"op\":\"add\",\"path\":\"/a\"}]");
replaceNodeWithMissingValue = BsonArray.parse("[{\"op\":\"replace\",\"path\":\"/a\"}]");
removeNoneExistingArrayElement = BsonArray.parse("[{\"op\": \"remove\",\"path\": \"/b/0\"}]");
replaceNode = BsonArray.parse("[{\"op\":\"replace\",\"path\":\"/a\",\"value\":true}]");
}
@Test
public void withFlagAddShouldTreatMissingValuesAsNulls() throws IOException {
BsonDoreplacedent expected = BsonDoreplacedent.parse("{\"a\":null}");
BsonDoreplacedent result = BsonPatch.apply(addNodeWithMissingValue, new BsonDoreplacedent(), EnumSet.of(MISSING_VALUES_AS_NULLS)).asDoreplacedent();
replacedertThat(result, equalTo(expected));
}
@Test
public void withFlagAddNodeWithMissingValueShouldValidateCorrectly() {
BsonPatch.validate(addNodeWithMissingValue, EnumSet.of(MISSING_VALUES_AS_NULLS));
}
@Test
public void withFlagReplaceShouldTreatMissingValuesAsNull() throws IOException {
BsonDoreplacedent source = BsonDoreplacedent.parse("{\"a\":\"test\"}");
BsonDoreplacedent expected = BsonDoreplacedent.parse("{\"a\":null}");
BsonDoreplacedent result = BsonPatch.apply(replaceNodeWithMissingValue, source, EnumSet.of(MISSING_VALUES_AS_NULLS)).asDoreplacedent();
replacedertThat(result, equalTo(expected));
}
@Test
public void withFlagReplaceNodeWithMissingValueShouldValidateCorrectly() {
BsonPatch.validate(addNodeWithMissingValue, EnumSet.of(MISSING_VALUES_AS_NULLS));
}
@Test
public void withFlagIgnoreRemoveNoneExistingArrayElement() throws IOException {
BsonDoreplacedent source = BsonDoreplacedent.parse("{\"b\": []}");
BsonDoreplacedent expected = BsonDoreplacedent.parse("{\"b\": []}");
BsonDoreplacedent result = BsonPatch.apply(removeNoneExistingArrayElement, source, EnumSet.of(REMOVE_NONE_EXISTING_ARRAY_ELEMENT)).asDoreplacedent();
replacedertThat(result, equalTo(expected));
}
@Test
public void withFlagReplaceShouldAddValueWhenMissingInTarget() throws Exception {
BsonDoreplacedent expected = BsonDoreplacedent.parse("{\"a\": true}");
BsonDoreplacedent result = BsonPatch.apply(replaceNode, BsonDoreplacedent.parse("{}"), EnumSet.of(ALLOW_MISSING_TARGET_OBJECT_ON_REPLACE)).asDoreplacedent();
replacedertThat(result, equalTo(expected));
}
}
19
Source : AbstractTest.java
with Apache License 2.0
from eBay
with Apache License 2.0
from eBay
private void testError() throws ClreplacedNotFoundException {
BsonDoreplacedent node = p.getNode();
BsonValue first = node.get("node");
BsonArray patch = node.getArray("op");
String message = node.containsKey("message") ? node.getString("message").getValue().replace("\\\"", "\"") : "";
Clreplaced<?> type = node.containsKey("type") ? exceptionType(node.getString("type").getValue()) : BsonPatchApplicationException.clreplaced;
try {
BsonPatch.apply(patch, first);
fail(errorMessage("Failure expected: " + message));
} catch (Exception e) {
if (matchOnErrors()) {
StringWriter fullError = new StringWriter();
e.printStackTrace(new PrintWriter(fullError));
replacedertThat(errorMessage("Operation failed but with wrong exception type", e), e, instanceOf(type));
if (message != null) {
replacedertThat(errorMessage("Operation failed but with wrong message", e), e.toString(), // equalTo would be better, but fail existing tests
containsString(message));
}
}
}
}
19
Source : AbstractTest.java
with Apache License 2.0
from eBay
with Apache License 2.0
from eBay
private void testOperation() throws Exception {
BsonDoreplacedent node = p.getNode();
BsonValue doc = node.get("node");
BsonValue expected = node.get("expected");
BsonArray patch = node.getArray("op");
String message = node.containsKey("message") ? node.getString("message").getValue() : "";
BsonValue result = BsonPatch.apply(patch, doc);
String failMessage = "The following test failed: \n" + "message: " + message + '\n' + "at: " + p.getSourceFile();
replacedertEquals(failMessage, expected, result);
}
19
Source : BsonPatch.java
with Apache License 2.0
from eBay
with Apache License 2.0
from eBay
public static void validate(BsonArray patch) throws InvalidBsonPatchException {
validate(patch, CompatibilityFlags.defaults());
}
19
Source : BsonPatch.java
with Apache License 2.0
from eBay
with Apache License 2.0
from eBay
public static void validate(BsonArray patch, EnumSet<CompatibilityFlags> flags) throws InvalidBsonPatchException {
process(patch, NoopProcessor.INSTANCE, flags);
}
19
Source : BsonPatch.java
with Apache License 2.0
from eBay
with Apache License 2.0
from eBay
public static void applyInPlace(BsonArray patch, BsonValue source, EnumSet<CompatibilityFlags> flags) {
InPlaceApplyProcessor processor = new InPlaceApplyProcessor(source, flags);
process(patch, processor, flags);
}
19
Source : BsonPatch.java
with Apache License 2.0
from eBay
with Apache License 2.0
from eBay
public static BsonValue apply(BsonArray patch, BsonValue source) throws BsonPatchApplicationException {
return apply(patch, source, CompatibilityFlags.defaults());
}
19
Source : BsonPatch.java
with Apache License 2.0
from eBay
with Apache License 2.0
from eBay
public static BsonValue apply(BsonArray patch, BsonValue source, EnumSet<CompatibilityFlags> flags) throws BsonPatchApplicationException {
CopyingApplyProcessor processor = new CopyingApplyProcessor(source, flags);
process(patch, processor, flags);
return processor.result();
}
19
Source : BsonPatch.java
with Apache License 2.0
from eBay
with Apache License 2.0
from eBay
public static void applyInPlace(BsonArray patch, BsonValue source) {
applyInPlace(patch, source, CompatibilityFlags.defaults());
}
18
Source : SinkDocumentTest.java
with Apache License 2.0
from mongodb
with Apache License 2.0
from mongodb
@BeforeAll
static void initBsonDocs() {
flatStructKey = new BsonDoreplacedent();
flatStructKey.put("_id", new BsonObjectId(ObjectId.get()));
flatStructKey.put("myBoolean", new BsonBoolean(true));
flatStructKey.put("myInt", new BsonInt32(42));
flatStructKey.put("myBytes", new BsonBinary(new byte[] { 65, 66, 67 }));
BsonArray ba1 = new BsonArray();
ba1.addAll(asList(new BsonInt32(1), new BsonInt32(2), new BsonInt32(3)));
flatStructKey.put("myArray", ba1);
flatStructValue = new BsonDoreplacedent();
flatStructValue.put("myLong", new BsonInt64(42L));
flatStructValue.put("myDouble", new BsonDouble(23.23d));
flatStructValue.put("myString", new BsonString("BSON"));
flatStructValue.put("myBytes", new BsonBinary(new byte[] { 120, 121, 122 }));
BsonArray ba2 = new BsonArray();
ba2.addAll(asList(new BsonInt32(9), new BsonInt32(8), new BsonInt32(7)));
flatStructValue.put("myArray", ba2);
nestedStructKey = new BsonDoreplacedent();
nestedStructKey.put("_id", new BsonDoreplacedent("myString", new BsonString("doc")));
nestedStructKey.put("mySubDoc", new BsonDoreplacedent("mySubSubDoc", new BsonDoreplacedent("myInt", new BsonInt32(23))));
nestedStructValue = new BsonDoreplacedent();
nestedStructValue.put("mySubDocA", new BsonDoreplacedent("myBoolean", new BsonBoolean(false)));
nestedStructValue.put("mySubDocB", new BsonDoreplacedent("mySubSubDocC", new BsonDoreplacedent("myString", new BsonString("some text..."))));
}
18
Source : AvroJsonSchemafulRecordConverter.java
with Apache License 2.0
from hpgrahsl
with Apache License 2.0
from hpgrahsl
private BsonValue handleArrayField(List list, Field field) {
logger.trace("handling complex type 'array' of types '{}'", field.schema().valueSchema().type());
if (list == null) {
logger.trace("no array -> adding null");
return BsonNull.VALUE;
}
BsonArray array = new BsonArray();
Schema.Type st = field.schema().valueSchema().type();
for (Object element : list) {
if (st.isPrimitive()) {
array.add(getConverter(field.schema().valueSchema()).toBson(element, field.schema()));
} else if (st == Schema.Type.ARRAY) {
Field elementField = new Field("first", 0, field.schema().valueSchema());
array.add(handleArrayField((List) element, elementField));
} else {
array.add(toBsonDoc(field.schema().valueSchema(), element));
}
}
return array;
}
18
Source : TestDataGenerator.java
with Apache License 2.0
from eBay
with Apache License 2.0
from eBay
private static BsonArray getArrayNode(List<String> args) {
BsonArray countryNode = new BsonArray();
for (String arg : args) {
countryNode.add(new BsonString(arg));
}
return countryNode;
}
18
Source : TestDataGenerator.java
with Apache License 2.0
from eBay
with Apache License 2.0
from eBay
public static BsonArray generate(int count) {
BsonArray jsonNode = new BsonArray();
for (int i = 0; i < count; i++) {
BsonDoreplacedent objectNode = new BsonDoreplacedent();
objectNode.put("name", new BsonString(name.get(random.nextInt(name.size()))));
objectNode.put("age", new BsonInt32(age.get(random.nextInt(age.size()))));
objectNode.put("gender", new BsonString(gender.get(random.nextInt(gender.size()))));
BsonArray countryNode = getArrayNode(country.subList(random.nextInt(country.size() / 2), (country.size() / 2) + random.nextInt(country.size() / 2)));
objectNode.put("country", countryNode);
BsonArray friendNode = getArrayNode(friends.subList(random.nextInt(friends.size() / 2), (friends.size() / 2) + random.nextInt(friends.size() / 2)));
objectNode.put("friends", friendNode);
jsonNode.add(objectNode);
}
return jsonNode;
}
18
Source : ApiTest.java
with Apache License 2.0
from eBay
with Apache License 2.0
from eBay
@Test(expected = InvalidBsonPatchException.clreplaced)
public void validatingNonArrayPatchShouldThrowAnException() throws IOException {
BsonArray invalid = BsonArray.parse("[\"not\", \"a patch\"]");
BsonPatch.validate(invalid);
}
18
Source : ApiTest.java
with Apache License 2.0
from eBay
with Apache License 2.0
from eBay
@Test(expected = InvalidBsonPatchException.clreplaced)
public void validatingAnInvalidArrayShouldThrowAnException() throws IOException {
BsonArray invalid = BsonArray.parse("[1, 2, 3, 4, 5]");
BsonPatch.validate(invalid);
}
18
Source : ApiTest.java
with Apache License 2.0
from eBay
with Apache License 2.0
from eBay
@Test(expected = InvalidBsonPatchException.clreplaced)
public void validatingAPatchWithAnInvalidOperationShouldThrowAnException() throws IOException {
BsonArray invalid = BsonArray.parse("[{\"op\": \"what\"}]");
BsonPatch.validate(invalid);
}
18
Source : InternalUtils.java
with Apache License 2.0
from eBay
with Apache License 2.0
from eBay
static List<BsonValue> toList(BsonArray input) {
int size = input.size();
List<BsonValue> toReturn = new ArrayList<BsonValue>(size);
for (int i = 0; i < size; i++) {
toReturn.add(input.get(i));
}
return toReturn;
}
17
Source : OperationHelper.java
with Apache License 2.0
from mongodb
with Apache License 2.0
from mongodb
static BsonDoreplacedent getUpdateDoreplacedent(final BsonDoreplacedent changeStreamDoreplacedent) {
if (!changeStreamDoreplacedent.containsKey(UPDATE_DESCRIPTION)) {
throw new DataException(format("Missing %s field: %s", UPDATE_DESCRIPTION, changeStreamDoreplacedent.toJson()));
} else if (!changeStreamDoreplacedent.get(UPDATE_DESCRIPTION).isDoreplacedent()) {
throw new DataException(format("Unexpected %s field type, expected a doreplacedent found `%s`: %s", UPDATE_DESCRIPTION, changeStreamDoreplacedent.get(UPDATE_DESCRIPTION), changeStreamDoreplacedent.toJson()));
}
BsonDoreplacedent updateDescription = changeStreamDoreplacedent.getDoreplacedent(UPDATE_DESCRIPTION);
Set<String> updateDescriptionFields = new HashSet<>(updateDescription.keySet());
updateDescriptionFields.removeAll(UPDATE_DESCRIPTION_FIELDS);
if (!updateDescriptionFields.isEmpty()) {
throw new DataException(format("Warning unexpected field(s) in %s %s. %s. Cannot process due to risk of data loss.", UPDATE_DESCRIPTION, updateDescriptionFields, updateDescription.toJson()));
}
if (!updateDescription.containsKey(UPDATED_FIELDS)) {
throw new DataException(format("Missing %s.%s field: %s", UPDATE_DESCRIPTION, UPDATED_FIELDS, updateDescription.toJson()));
} else if (!updateDescription.get(UPDATED_FIELDS).isDoreplacedent()) {
throw new DataException(format("Unexpected %s field type, expected a doreplacedent but found `%s`: %s", UPDATE_DESCRIPTION, updateDescription, updateDescription.toJson()));
}
if (!updateDescription.containsKey(REMOVED_FIELDS)) {
throw new DataException(format("Missing %s.%s field: %s", UPDATE_DESCRIPTION, REMOVED_FIELDS, updateDescription.toJson()));
} else if (!updateDescription.get(REMOVED_FIELDS).isArray()) {
throw new DataException(format("Unexpected %s field type, expected an array but found `%s`: %s", REMOVED_FIELDS, updateDescription.get(REMOVED_FIELDS), updateDescription.toJson()));
}
BsonDoreplacedent updatedFields = updateDescription.getDoreplacedent(UPDATED_FIELDS);
BsonArray removedFields = updateDescription.getArray(REMOVED_FIELDS);
BsonDoreplacedent unsetDoreplacedent = new BsonDoreplacedent();
for (final BsonValue removedField : removedFields) {
if (!removedField.isString()) {
throw new DataException(format("Unexpected value type in %s, expected an string but found `%s`: %s", REMOVED_FIELDS, removedField, updateDescription.toJson()));
}
unsetDoreplacedent.append(removedField.replacedtring().getValue(), EMPTY_STRING);
}
BsonDoreplacedent updateDoreplacedent = new BsonDoreplacedent(SET, updatedFields);
if (!unsetDoreplacedent.isEmpty()) {
updateDoreplacedent.put(UNSET, unsetDoreplacedent);
}
return updateDoreplacedent;
}
17
Source : JsonDiffTest2.java
with Apache License 2.0
from eBay
with Apache License 2.0
from eBay
@Test
public void testPatchAppliedCleanly() {
for (int i = 0; i < jsonNode.size(); i++) {
BsonDoreplacedent node = jsonNode.get(i).asDoreplacedent();
BsonValue first = node.get("first");
BsonValue second = node.get("second");
BsonArray patch = node.getArray("patch");
String message = node.containsKey("message") ? node.getString("message").getValue() : "";
BsonValue secondPrime = BsonPatch.apply(patch, first);
replacedert.replacedertThat(message, secondPrime, equalTo(second));
}
}
17
Source : ApiTest.java
with Apache License 2.0
from eBay
with Apache License 2.0
from eBay
@Test(expected = InvalidBsonPatchException.clreplaced)
public void applyingNonArrayPatchShouldThrowAnException() throws IOException {
BsonArray invalid = BsonArray.parse("[\"not\", \"a patch\"]");
BsonDoreplacedent to = BsonDoreplacedent.parse("{\"a\":1}");
BsonPatch.apply(invalid, to);
}
17
Source : ApiTest.java
with Apache License 2.0
from eBay
with Apache License 2.0
from eBay
@Test
public void applyDoesNotMutateSource() throws Exception {
BsonArray patch = BsonArray.parse("[{ \"op\": \"add\", \"path\": \"/b\", \"value\": \"b-value\" }]");
BsonDoreplacedent source = new BsonDoreplacedent();
BsonPatch.applyInPlace(patch, source);
replacedertThat(source.getString("b").getValue(), is("b-value"));
}
17
Source : ApiTest.java
with Apache License 2.0
from eBay
with Apache License 2.0
from eBay
@Test
public void applyDoesNotMutateSource2() throws Exception {
BsonArray patch = BsonArray.parse("[{ \"op\": \"add\", \"path\": \"/b\", \"value\": \"b-value\" }]");
BsonDoreplacedent source = new BsonDoreplacedent();
BsonDoreplacedent beforeApplication = CopyingApplyProcessor.deepCopy(source).asDoreplacedent();
BsonPatch.apply(patch, source);
replacedertThat(source, is(beforeApplication));
}
17
Source : ApiTest.java
with Apache License 2.0
from eBay
with Apache License 2.0
from eBay
@Test
public void applyInPlaceMutatesSource() throws Exception {
BsonArray patch = BsonArray.parse("[{ \"op\": \"add\", \"path\": \"/b\", \"value\": \"b-value\" }]");
BsonDoreplacedent source = new BsonDoreplacedent();
BsonDoreplacedent beforeApplication = CopyingApplyProcessor.deepCopy(source).asDoreplacedent();
BsonPatch.apply(patch, source);
replacedertThat(source, is(beforeApplication));
}
17
Source : ApiTest.java
with Apache License 2.0
from eBay
with Apache License 2.0
from eBay
@Test(expected = InvalidBsonPatchException.clreplaced)
public void applyingAPatchWithAnInvalidOperationShouldThrowAnException() throws IOException {
BsonArray invalid = BsonArray.parse("[{\"op\": \"what\"}]");
BsonDoreplacedent to = BsonDoreplacedent.parse("{\"a\":1}");
BsonPatch.apply(invalid, to);
}
17
Source : ApiTest.java
with Apache License 2.0
from eBay
with Apache License 2.0
from eBay
@Test(expected = InvalidBsonPatchException.clreplaced)
public void applyingAnInvalidArrayShouldThrowAnException() throws IOException {
BsonArray invalid = BsonArray.parse("[1, 2, 3, 4, 5]");
BsonDoreplacedent to = BsonDoreplacedent.parse("{\"a\":1}");
BsonPatch.apply(invalid, to);
}
17
Source : InPlaceApplyProcessor.java
with Apache License 2.0
from eBay
with Apache License 2.0
from eBay
private void addToArray(JsonPointer path, BsonValue value, BsonValue parentNode) {
final BsonArray target = parentNode.asArray();
int idx = path.last().getIndex();
if (idx == JsonPointer.LAST_INDEX) {
// see http://tools.ietf.org/html/rfc6902#section-4.1
target.add(value);
} else {
if (idx > target.size())
throw new BsonPatchApplicationException("Array index " + idx + " out of bounds", Operation.ADD, path.getParent());
target.add(idx, value);
}
}
17
Source : BsonDiff.java
with Apache License 2.0
from eBay
with Apache License 2.0
from eBay
private BsonArray getBsonNodes() {
final BsonArray patch = new BsonArray();
for (Diff diff : diffs) {
BsonDoreplacedent bsonNode = getBsonNode(diff, flags);
patch.add(bsonNode);
}
return patch;
}
16
Source : CommandSanitizer.java
with MIT License
from SDA-SE
with MIT License
from SDA-SE
private static BsonValue sanitizeValue(BsonValue value) {
if (value.isDoreplacedent()) {
BsonDoreplacedent result = new BsonDoreplacedent();
value.asDoreplacedent().forEach((key, v) -> result.put(key, sanitizeValue(v)));
return result;
} else if (value.isArray()) {
BsonArray result = new BsonArray();
value.asArray().forEach(v -> result.add(sanitizeValue(v)));
return result;
} else {
return new BsonString("…");
}
}
16
Source : MongoClientTracer.java
with Apache License 2.0
from open-telemetry
with Apache License 2.0
from open-telemetry
private static boolean writeScrubbed(BsonArray origin, JsonWriter writer) {
writer.writeStartArray();
for (BsonValue value : origin) {
if (writeScrubbed(value, writer)) {
return true;
}
}
writer.writeEndArray();
return false;
}
16
Source : RFC6901Tests.java
with Apache License 2.0
from eBay
with Apache License 2.0
from eBay
@Test
public void testRFC6901Compliance() throws IOException {
BsonValue data = TestUtils.loadResourceAsBsonValue("/rfc6901/data.json");
BsonValue testData = data.asDoreplacedent().get("testData");
BsonValue emptyJson = BsonDoreplacedent.parse("{}");
BsonArray patch = BsonDiff.asBson(emptyJson, testData);
BsonValue result = BsonPatch.apply(patch, emptyJson);
replacedertEquals(testData, result);
}
16
Source : MoveOperationTest.java
with Apache License 2.0
from eBay
with Apache License 2.0
from eBay
@Test
public void testMoveArrayGeneratedHasNoValue() throws IOException {
BsonValue jsonNode1 = BsonDoreplacedent.parse("{ \"foo\": [ \"all\", \"grreplaced\", \"cows\", \"eat\" ] }");
BsonValue jsonNode2 = BsonDoreplacedent.parse("{ \"foo\": [ \"all\", \"cows\", \"eat\", \"grreplaced\" ] }");
BsonArray patch = BsonArray.parse("[{\"op\":\"move\",\"from\":\"/foo/1\",\"path\":\"/foo/3\"}]");
BsonArray diff = BsonDiff.asBson(jsonNode1, jsonNode2);
replacedertThat(diff, equalTo(patch));
}
16
Source : MoveOperationTest.java
with Apache License 2.0
from eBay
with Apache License 2.0
from eBay
@Test
public void testMoveValueGeneratedHasNoValue() throws IOException {
BsonValue jsonNode1 = BsonDoreplacedent.parse("{ \"foo\": { \"bar\": \"baz\", \"waldo\": \"fred\" }, \"qux\": { \"corge\": \"grault\" } }");
BsonValue jsonNode2 = BsonDoreplacedent.parse("{ \"foo\": { \"bar\": \"baz\" }, \"qux\": { \"corge\": \"grault\", \"thud\": \"fred\" } }");
BsonArray patch = BsonArray.parse("[{\"op\":\"move\",\"from\":\"/foo/waldo\",\"path\":\"/qux/thud\"}]");
BsonArray diff = BsonDiff.asBson(jsonNode1, jsonNode2);
replacedertThat(diff, equalTo(patch));
}
16
Source : JsonDiffTest.java
with Apache License 2.0
from eBay
with Apache License 2.0
from eBay
@Test
public void testGeneratedJsonDiff() {
Random random = new Random();
for (int i = 0; i < 1000; i++) {
BsonArray first = TestDataGenerator.generate(random.nextInt(10));
BsonArray second = TestDataGenerator.generate(random.nextInt(10));
BsonArray actualPatch = BsonDiff.asBson(first, second);
BsonArray secondPrime = BsonPatch.apply(actualPatch, first).asArray();
replacedert.replacedertEquals(second, secondPrime);
}
}
16
Source : ApiTest.java
with Apache License 2.0
from eBay
with Apache License 2.0
from eBay
@Test
public void applyInPlaceMutatesSourceWithCompatibilityFlags() throws Exception {
BsonArray patch = BsonArray.parse("[{ \"op\": \"add\", \"path\": \"/b\" }]");
BsonDoreplacedent source = new BsonDoreplacedent();
BsonPatch.applyInPlace(patch, source, EnumSet.of(CompatibilityFlags.MISSING_VALUES_AS_NULLS));
replacedertTrue(source.get("b").isNull());
}
15
Source : UpdateDescription.java
with Apache License 2.0
from mongodb
with Apache License 2.0
from mongodb
/**
* Converts this update description to its doreplacedent representation as it would appear in a
* MongoDB Change Event.
*
* @return the update description doreplacedent as it would appear in a change event
*/
public BsonDoreplacedent toBsonDoreplacedent() {
final BsonDoreplacedent updateDescDoc = new BsonDoreplacedent();
updateDescDoc.put(Fields.UPDATED_FIELDS_FIELD, this.getUpdatedFields());
final BsonArray removedFields = new BsonArray();
for (final String field : this.getRemovedFields()) {
removedFields.add(new BsonString(field));
}
updateDescDoc.put(Fields.REMOVED_FIELDS_FIELD, removedFields);
return updateDescDoc;
}
15
Source : SchemaRecordConverter.java
with Apache License 2.0
from mongodb
with Apache License 2.0
from mongodb
private BsonValue toBsonArray(final Schema schema, final Object value) {
if (value == null) {
return BsonNull.VALUE;
}
Schema fieldSchema = schema.valueSchema();
BsonArray bsonArray = new BsonArray();
List<?> myList = (List) value;
myList.forEach(v -> {
if (fieldSchema.type().isPrimitive()) {
if (v == null) {
bsonArray.add(BsonNull.VALUE);
} else {
bsonArray.add(getConverter(fieldSchema).toBson(v));
}
} else if (fieldSchema.type().equals(ARRAY)) {
bsonArray.add(toBsonArray(fieldSchema, v));
} else {
bsonArray.add(toBsonDoc(fieldSchema, v));
}
});
return bsonArray;
}
15
Source : TestNodesEmissionTest.java
with Apache License 2.0
from eBay
with Apache License 2.0
from eBay
@Test
public void testNodeEmittedBeforeCopyOperation() throws IOException {
BsonValue source = BsonDoreplacedent.parse("{\"key\":\"original\"}");
BsonValue target = BsonDoreplacedent.parse("{\"key\":\"original\", \"copied\":\"original\"}");
BsonArray diff = BsonDiff.asBson(source, target, flags);
BsonValue testNode = BsonDoreplacedent.parse("{\"op\":\"test\",\"path\":\"/key\",\"value\":\"original\"}");
replacedertEquals(2, diff.size());
replacedertEquals(testNode, diff.iterator().next());
}
15
Source : TestNodesEmissionTest.java
with Apache License 2.0
from eBay
with Apache License 2.0
from eBay
@Test
public void testNodeEmittedBeforeRemoveFromTailOfArray() throws IOException {
BsonValue source = BsonDoreplacedent.parse("{\"key\":[1,2,3]}");
BsonValue target = BsonDoreplacedent.parse("{\"key\":[1,2]}");
BsonArray diff = BsonDiff.asBson(source, target, flags);
BsonValue testNode = BsonDoreplacedent.parse("{\"op\":\"test\",\"path\":\"/key/2\",\"value\":3}");
replacedertEquals(2, diff.size());
replacedertEquals(testNode, diff.iterator().next());
}
15
Source : TestNodesEmissionTest.java
with Apache License 2.0
from eBay
with Apache License 2.0
from eBay
@Test
public void testNodeEmittedBeforeMoveOperation() throws IOException {
BsonValue source = BsonDoreplacedent.parse("{\"key\":\"original\"}");
BsonValue target = BsonDoreplacedent.parse("{\"moved\":\"original\"}");
BsonArray diff = BsonDiff.asBson(source, target, flags);
BsonValue testNode = BsonDoreplacedent.parse("{\"op\":\"test\",\"path\":\"/key\",\"value\":\"original\"}");
replacedertEquals(2, diff.size());
replacedertEquals(testNode, diff.iterator().next());
}
15
Source : TestNodesEmissionTest.java
with Apache License 2.0
from eBay
with Apache License 2.0
from eBay
@Test
public void testNodeEmittedBeforeRemoveFromMiddleOfArray() throws IOException {
BsonValue source = BsonDoreplacedent.parse("{\"key\":[1,2,3]}");
BsonValue target = BsonDoreplacedent.parse("{\"key\":[1,3]}");
BsonArray diff = BsonDiff.asBson(source, target, flags);
BsonValue testNode = BsonDoreplacedent.parse("{\"op\":\"test\",\"path\":\"/key/1\",\"value\":2}");
replacedertEquals(2, diff.size());
replacedertEquals(testNode, diff.iterator().next());
}
15
Source : TestNodesEmissionTest.java
with Apache License 2.0
from eBay
with Apache License 2.0
from eBay
@Test
public void testNodeEmittedBeforeReplaceOperation() throws IOException {
BsonValue source = BsonDoreplacedent.parse("{\"key\":\"original\"}");
BsonValue target = BsonDoreplacedent.parse("{\"key\":\"replaced\"}");
BsonArray diff = BsonDiff.asBson(source, target, flags);
BsonValue testNode = BsonDoreplacedent.parse("{\"op\":\"test\",\"path\":\"/key\",\"value\":\"original\"}");
replacedertEquals(2, diff.size());
replacedertEquals(testNode, diff.iterator().next());
}
15
Source : TestNodesEmissionTest.java
with Apache License 2.0
from eBay
with Apache License 2.0
from eBay
@Test
public void testNodeEmittedBeforeRemoveOperation() throws IOException {
BsonValue source = BsonDoreplacedent.parse("{\"key\":\"original\"}");
BsonValue target = BsonDoreplacedent.parse("{}");
BsonArray diff = BsonDiff.asBson(source, target, flags);
BsonValue testNode = BsonDoreplacedent.parse("{\"op\":\"test\",\"path\":\"/key\",\"value\":\"original\"}");
replacedertEquals(2, diff.size());
replacedertEquals(testNode, diff.iterator().next());
}
15
Source : JsonDiffTest.java
with Apache License 2.0
from eBay
with Apache License 2.0
from eBay
@Test
public void testSampleJsonDiff() {
for (int i = 0; i < jsonNode.size(); i++) {
BsonValue first = jsonNode.get(i).asDoreplacedent().get("first");
BsonValue second = jsonNode.get(i).asDoreplacedent().get("second");
BsonArray actualPatch = BsonDiff.asBson(first, second);
BsonValue secondPrime = BsonPatch.apply(actualPatch, first);
replacedert.replacedertEquals("BSON Patch not symmetrical [index=" + i + ", first=" + first + "]", second, secondPrime);
}
}
14
Source : UpdateDescription.java
with Apache License 2.0
from mongodb
with Apache License 2.0
from mongodb
/**
* Converts an update description BSON doreplacedent from a MongoDB Change Event into an
* UpdateDescription object.
*
* @param doreplacedent the
* @return the converted UpdateDescription
*/
public static UpdateDescription fromBsonDoreplacedent(final BsonDoreplacedent doreplacedent) {
keyPresent(Fields.UPDATED_FIELDS_FIELD, doreplacedent);
keyPresent(Fields.REMOVED_FIELDS_FIELD, doreplacedent);
final BsonArray removedFieldsArr = doreplacedent.getArray(Fields.REMOVED_FIELDS_FIELD);
final Set<String> removedFields = new HashSet<>(removedFieldsArr.size());
for (final BsonValue field : removedFieldsArr) {
removedFields.add(field.replacedtring().getValue());
}
return new UpdateDescription(doreplacedent.getDoreplacedent(Fields.UPDATED_FIELDS_FIELD), removedFields);
}
14
Source : JsonDiffTest.java
with Apache License 2.0
from eBay
with Apache License 2.0
from eBay
@Test
public void testPath() throws Exception {
BsonValue source = BsonDoreplacedent.parse("{\"profiles\":{\"abc\":[],\"def\":[{\"hello\":\"world\"}]}}");
BsonArray patch = BsonArray.parse("[{\"op\":\"copy\",\"from\":\"/profiles/def/0\", \"path\":\"/profiles/def/0\"},{\"op\":\"replace\",\"path\":\"/profiles/def/0/hello\",\"value\":\"world2\"}]");
BsonValue target = BsonPatch.apply(patch, source);
BsonValue expected = BsonDoreplacedent.parse("{\"profiles\":{\"abc\":[],\"def\":[{\"hello\":\"world2\"},{\"hello\":\"world\"}]}}");
replacedert.replacedertEquals(target, expected);
}
14
Source : BsonPatch.java
with Apache License 2.0
from eBay
with Apache License 2.0
from eBay
private static void process(BsonArray patch, BsonPatchProcessor processor, EnumSet<CompatibilityFlags> flags) throws InvalidBsonPatchException {
Iterator<BsonValue> operations = patch.iterator();
while (operations.hasNext()) {
BsonValue bsonNode = operations.next();
if (!bsonNode.isDoreplacedent())
throw new InvalidBsonPatchException("Invalid BSON Patch payload (not an object)");
Operation operation = Operation.fromRfcName(getPatchAttr(bsonNode, Constants.OP).replacedtring().getValue().replaceAll("\"", ""));
JsonPointer path = JsonPointer.parse(getPatchAttr(bsonNode, Constants.PATH).replacedtring().getValue());
try {
switch(operation) {
case REMOVE:
{
processor.remove(path);
break;
}
case ADD:
{
BsonValue value;
if (!flags.contains(CompatibilityFlags.MISSING_VALUES_AS_NULLS))
value = getPatchAttr(bsonNode, Constants.VALUE);
else
value = getPatchAttrWithDefault(bsonNode, Constants.VALUE, BsonNull.VALUE);
processor.add(path, cloneBsonValue(value));
break;
}
case REPLACE:
{
BsonValue value;
if (!flags.contains(CompatibilityFlags.MISSING_VALUES_AS_NULLS))
value = getPatchAttr(bsonNode, Constants.VALUE);
else
value = getPatchAttrWithDefault(bsonNode, Constants.VALUE, BsonNull.VALUE);
processor.replace(path, cloneBsonValue(value));
break;
}
case MOVE:
{
JsonPointer fromPath = JsonPointer.parse(getPatchAttr(bsonNode, Constants.FROM).replacedtring().getValue());
processor.move(fromPath, path);
break;
}
case COPY:
{
JsonPointer fromPath = JsonPointer.parse(getPatchAttr(bsonNode, Constants.FROM).replacedtring().getValue());
processor.copy(fromPath, path);
break;
}
case TEST:
{
BsonValue value;
if (!flags.contains(CompatibilityFlags.MISSING_VALUES_AS_NULLS))
value = getPatchAttr(bsonNode, Constants.VALUE);
else
value = getPatchAttrWithDefault(bsonNode, Constants.VALUE, BsonNull.VALUE);
processor.test(path, cloneBsonValue(value));
break;
}
}
} catch (JsonPointerEvaluationException e) {
throw new BsonPatchApplicationException(e.getMessage(), operation, e.getPath());
}
}
}
13
Source : BlacklistProjector.java
with Apache License 2.0
from hpgrahsl
with Apache License 2.0
from hpgrahsl
@Override
protected void doProjection(String field, BsonDoreplacedent doc) {
if (!field.contains(FieldProjector.SUB_FIELD_DOT_SEPARATOR)) {
if (field.equals(FieldProjector.SINGLE_WILDCARD) || field.equals(FieldProjector.DOUBLE_WILDCARD)) {
handleWildcard(field, "", doc);
return;
}
// NOTE: never try to remove the _id field
if (!field.equals(DBCollection.ID_FIELD_NAME))
doc.remove(field);
return;
}
int dotIdx = field.indexOf(FieldProjector.SUB_FIELD_DOT_SEPARATOR);
String firstPart = field.substring(0, dotIdx);
String otherParts = field.length() >= dotIdx ? field.substring(dotIdx + 1) : "";
if (firstPart.equals(FieldProjector.SINGLE_WILDCARD) || firstPart.equals(FieldProjector.DOUBLE_WILDCARD)) {
handleWildcard(firstPart, otherParts, doc);
return;
}
BsonValue value = doc.get(firstPart);
if (value != null) {
if (value.isDoreplacedent()) {
doProjection(otherParts, (BsonDoreplacedent) value);
}
if (value.isArray()) {
BsonArray values = (BsonArray) value;
for (BsonValue v : values.getValues()) {
if (v != null && v.isDoreplacedent()) {
doProjection(otherParts, (BsonDoreplacedent) v);
}
}
}
}
}
12
Source : BlockListProjector.java
with Apache License 2.0
from mongodb
with Apache License 2.0
from mongodb
@Override
protected void doProjection(final String field, final BsonDoreplacedent doc) {
if (!field.contains(FieldProjector.SUB_FIELD_DOT_SEPARATOR)) {
if (field.equals(FieldProjector.SINGLE_WILDCARD) || field.equals(FieldProjector.DOUBLE_WILDCARD)) {
handleWildcard(field, "", doc);
return;
}
// NOTE: never try to remove the _id field
if (!field.equals(ID_FIELD)) {
doc.remove(field);
}
return;
}
int dotIdx = field.indexOf(FieldProjector.SUB_FIELD_DOT_SEPARATOR);
String firstPart = field.substring(0, dotIdx);
String otherParts = field.length() >= dotIdx ? field.substring(dotIdx + 1) : "";
if (firstPart.equals(FieldProjector.SINGLE_WILDCARD) || firstPart.equals(FieldProjector.DOUBLE_WILDCARD)) {
handleWildcard(firstPart, otherParts, doc);
return;
}
BsonValue value = doc.get(firstPart);
if (value != null) {
if (value.isDoreplacedent()) {
doProjection(otherParts, value.asDoreplacedent());
}
if (value.isArray()) {
BsonArray values = value.asArray();
for (BsonValue v : values.getValues()) {
if (v != null && v.isDoreplacedent()) {
doProjection(otherParts, v.asDoreplacedent());
}
}
}
}
}
See More Examples