private BsonDocument getInsertDocument(R record, BsonDocument key) { BsonDocument valueDocument = BsonDocument.parse(record.value().toString()); valueDocument.remove("_id"); valueDocument.append("id", key.get("id")); return valueDocument; }
for (Entry<String, BsonValue> valuePairsForSchema : valuePairs) { if (valuePairsForSchema.getKey().equalsIgnoreCase("$set")) { BsonDocument val1 = BsonDocument.parse(valuePairsForSchema.getValue().toString()); Set<Entry<String, BsonValue>> keyValuesForSetSchema = val1.entrySet(); for (Entry<String, BsonValue> keyValuesForSetSchemaEntry : keyValuesForSetSchema) { for (Entry<String, BsonValue> valuePairsForStruct : valuePairs) { if (valuePairsForStruct.getKey().equalsIgnoreCase("$set")) { BsonDocument val1 = BsonDocument.parse(valuePairsForStruct.getValue().toString()); Set<Entry<String, BsonValue>> keyValueForSetStruct = val1.entrySet(); for (Entry<String, BsonValue> keyValueForSetStructEntry : keyValueForSetStruct) {
@Test(expected = ConnectException.class) public void shouldDetectHeterogenousArray() throws Exception { final MongoDataConverter converter = new MongoDataConverter(ArrayEncoding.ARRAY); final BsonDocument val = BsonDocument.parse(HETEROGENOUS_ARRAY); for (Entry<String, BsonValue> entry : val.entrySet()) { converter.addFieldSchema(entry, builder); } }
@Test(expected = ConnectException.class) public void shouldDetectHeterogenousDocumentInArray() throws Exception { final MongoDataConverter converter = new MongoDataConverter(ArrayEncoding.ARRAY); final BsonDocument val = BsonDocument.parse(HETEROGENOUS_DOCUMENT_IN_ARRAY); for (Entry<String, BsonValue> entry : val.entrySet()) { converter.addFieldSchema(entry, builder); } }
@Before public void setup() throws Exception { record = getFile("restaurants5.json"); val = BsonDocument.parse(record); builder = SchemaBuilder.struct().name("pub"); converter = new MongoDataConverter(ArrayEncoding.ARRAY); }
@Test public void query() { final PersonRepository.Criteria criteria = repository.criteria(); repository.find(criteria) .orderByAliases() .orderByNameDesceding(); validate(criteria.name("foo"), BsonDocument.parse("{\"name\":\"foo\"}")); validate(criteria.name("foo").age(8), BsonDocument.parse("{\"name\":\"foo\", \"age\": 8}")); validate(criteria.name("foo").ageLessThan(8), BsonDocument.parse("{\"name\":\"foo\", \"age\": {\"$lt\": 8}}")); validate(criteria.ageLessThan(8).ageNot(5), BsonDocument.parse("{\"$and\": [{\"age\":{ \"$lt\": 8}}, {\"age\": {\"$ne\": 5}} ] }")); // pass as-is without optimizations validate(criteria.name("foo").name("bar"), BsonDocument.parse("{\"$and\": [{\"name\": \"foo\"}, {\"name\": \"bar\"}]}")); validate(criteria.name("foo").name("bar").name("qux"), BsonDocument.parse("{\"$and\": [{\"name\": \"foo\"}, {\"name\": \"bar\"}, {\"name\":\"qux\"}]}")); validate(criteria.name("foo").or().nameNot("bar"), BsonDocument.parse("{\"$or\": [{\"name\": \"foo\"}, {\"name\": {\"$ne\": \"bar\"}} ] }")); validate(criteria.nameIn("foo", "bar").name("bar"), BsonDocument.parse("{\"$and\":[ {\"name\": {\"$in\": [\"foo\", \"bar\"]}}, {\"name\": \"bar\"} ] }")); validate(criteria.name("foo").age(22).or().name("bar").age(33), BsonDocument.parse("{\"$or\": [" + "{\"name\": \"foo\", \"age\": 22}, " + "{\"name\": \"bar\", \"age\": 33}" + "]}")); }
private BsonDocument getUpdateDocument(R patchRecord, BsonDocument keyDocument) { BsonDocument valueDocument = new BsonDocument(); BsonDocument document = BsonDocument.parse(patchRecord.value().toString());
@Before public void setup() throws Exception { record = getFile("complex_objects.json"); val = BsonDocument.parse(record); builder = SchemaBuilder.struct().name("complex"); avroData = new AvroData(100); converter = new MongoDataConverter(ArrayEncoding.ARRAY); }
final R keyRecord = keyExtractor.apply(record); BsonDocument keyDocument = BsonDocument.parse("{ \"id\" : " + keyRecord.key().toString() + "}"); BsonDocument valueDocument = new BsonDocument();
@Test public void shouldCreateStructForEmptyArrayEncodingArray() throws Exception { final BsonDocument val = BsonDocument.parse(EMPTY_ARRAY); final MongoDataConverter arrayConverter = new MongoDataConverter(ArrayEncoding.ARRAY); for (Entry<String, BsonValue> entry : val.entrySet()) { arrayConverter.addFieldSchema(entry, builder); } final Schema arraySchema = builder.build(); final Struct struct = new Struct(arraySchema); for (Entry<String, BsonValue> entry : val.entrySet()) { arrayConverter.convertRecord(entry, arraySchema, struct); } assertThat(struct.toString()).isEqualTo( "Struct{" + "_id=1," + "f=[]" + "}" ); }
@Test public void shouldCreateStructForEmptyArrayEncodingDocument() throws Exception { final BsonDocument val = BsonDocument.parse(EMPTY_ARRAY); final MongoDataConverter documentConverter = new MongoDataConverter(ArrayEncoding.DOCUMENT); for (Entry<String, BsonValue> entry : val.entrySet()) { documentConverter.addFieldSchema(entry, builder); } final Schema documentSchema = builder.build(); final Struct struct = new Struct(documentSchema); for (Entry<String, BsonValue> entry : val.entrySet()) { documentConverter.convertRecord(entry, documentSchema, struct); } assertThat(struct.toString()).isEqualTo( "Struct{" + "_id=1," + "f=Struct{}}" ); }
@Test public void shouldCreateStructForHeterogenousArray() throws Exception { final MongoDataConverter converter = new MongoDataConverter(ArrayEncoding.DOCUMENT); final BsonDocument val = BsonDocument.parse(HETEROGENOUS_ARRAY); for (Entry<String, BsonValue> entry : val.entrySet()) { converter.addFieldSchema(entry, builder); } final Schema finalSchema = builder.build(); final Struct struct = new Struct(finalSchema); for (Entry<String, BsonValue> entry : val.entrySet()) { converter.convertRecord(entry, finalSchema, struct); } assertThat(struct.toString()).isEqualTo( "Struct{" + "_id=1," + "a2=Struct{_0=11,_1=abc}}" ); }
@Test public void shouldCreateStructForHeterogenousDocumentInArray() throws Exception { final MongoDataConverter converter = new MongoDataConverter(ArrayEncoding.DOCUMENT); final BsonDocument val = BsonDocument.parse(HETEROGENOUS_DOCUMENT_IN_ARRAY); for (Entry<String, BsonValue> entry : val.entrySet()) { converter.addFieldSchema(entry, builder); } final Schema finalSchema = builder.build(); final Struct struct = new Struct(finalSchema); for (Entry<String, BsonValue> entry : val.entrySet()) { converter.convertRecord(entry, finalSchema, struct); } assertThat(struct.toString()).isEqualTo( "Struct{" + "_id=1," + "a1=Struct{" + "_0=Struct{a=1}," + "_1=Struct{a=c}" + "}" + "}" ); } }
@Test public void shouldCreateSchemaForEmptyArrayEncodingArray() throws Exception { final BsonDocument val = BsonDocument.parse(EMPTY_ARRAY); final MongoDataConverter arrayConverter = new MongoDataConverter(ArrayEncoding.ARRAY); for (Entry<String, BsonValue> entry : val.entrySet()) { arrayConverter.addFieldSchema(entry, builder); } final Schema arraySchema = builder.build(); assertThat(arraySchema) .isEqualTo( SchemaBuilder.struct().name("array") .field("_id", Schema.OPTIONAL_INT32_SCHEMA) .field("f", SchemaBuilder.array(Schema.OPTIONAL_STRING_SCHEMA).optional().build()) .build()); }
@Test public void shouldCreateStructForHomogenousArray() throws Exception { final MongoDataConverter converter = new MongoDataConverter(ArrayEncoding.ARRAY); final BsonDocument val = BsonDocument.parse(HOMOGENOUS_ARRAYS); final SchemaBuilder builder = SchemaBuilder.struct().name("array"); for (Entry<String, BsonValue> entry : val.entrySet()) { converter.addFieldSchema(entry, builder); } final Schema finalSchema = builder.build(); final Struct struct = new Struct(finalSchema); for (Entry<String, BsonValue> entry : val.entrySet()) { converter.convertRecord(entry, finalSchema, struct); } assertThat(struct.toString()).isEqualTo( "Struct{" + "_id=1," + "a1=[" + "Struct{a=1}, " + "Struct{b=c}" + "]," + "a2=[11, abc]," + "empty=[]}" ); }
@Test public void shouldCreateSchemaForEmptyArrayEncodingDocument() throws Exception { final BsonDocument val = BsonDocument.parse(EMPTY_ARRAY); final MongoDataConverter documentConverter = new MongoDataConverter(ArrayEncoding.DOCUMENT); for (Entry<String, BsonValue> entry : val.entrySet()) { documentConverter.addFieldSchema(entry, builder); } final Schema documentSchema = builder.build(); assertThat(documentSchema) .isEqualTo( SchemaBuilder.struct().name("array") .field("_id", Schema.OPTIONAL_INT32_SCHEMA) .field("f", SchemaBuilder.struct().name("array.f").optional().build()) .build()); }
@Test public void shouldCreateSchemaForHeterogenousArray() throws Exception { final MongoDataConverter converter = new MongoDataConverter(ArrayEncoding.DOCUMENT); final BsonDocument val = BsonDocument.parse(HETEROGENOUS_ARRAY); for (Entry<String, BsonValue> entry : val.entrySet()) { converter.addFieldSchema(entry, builder); } final Schema finalSchema = builder.build(); assertThat(finalSchema) .isEqualTo( SchemaBuilder.struct().name("array") .field("_id", Schema.OPTIONAL_INT32_SCHEMA) .field("a2", SchemaBuilder.struct().name("array.a2").optional() .field("_0", Schema.OPTIONAL_INT32_SCHEMA) .field("_1", Schema.OPTIONAL_STRING_SCHEMA) .build() ).build() ); }
@Test public void shouldCreateSchemaForHeterogenousDocumentInArray() throws Exception { final MongoDataConverter converter = new MongoDataConverter(ArrayEncoding.DOCUMENT); final BsonDocument val = BsonDocument.parse(HETEROGENOUS_DOCUMENT_IN_ARRAY); for (Entry<String, BsonValue> entry : val.entrySet()) { converter.addFieldSchema(entry, builder); } final Schema finalSchema = builder.build(); assertThat(finalSchema) .isEqualTo( SchemaBuilder.struct().name("array") .field("_id", Schema.OPTIONAL_INT32_SCHEMA) .field("a1", SchemaBuilder.struct().name("array.a1").optional() .field("_0", SchemaBuilder.struct().name("array.a1._0").optional() .field("a", Schema.OPTIONAL_INT32_SCHEMA) .build()) .field("_1", SchemaBuilder.struct().name("array.a1._1").optional() .field("a", Schema.OPTIONAL_STRING_SCHEMA) .build()) .build() ) .build() ); }
@Test public void shouldCreateSchemaForHomogenousArray() throws Exception { final MongoDataConverter converter = new MongoDataConverter(ArrayEncoding.ARRAY); final BsonDocument val = BsonDocument.parse(HOMOGENOUS_ARRAYS); for (Entry<String, BsonValue> entry : val.entrySet()) { converter.addFieldSchema(entry, builder); } final Schema finalSchema = builder.build(); assertThat(finalSchema) .isEqualTo( SchemaBuilder.struct().name("array") .field("_id", Schema.OPTIONAL_INT32_SCHEMA) .field("a1", SchemaBuilder.array(SchemaBuilder.struct().name("array.a1").optional() .field("a", Schema.OPTIONAL_INT32_SCHEMA) .field("b", Schema.OPTIONAL_STRING_SCHEMA) .build() ).optional().build()) .field("a2", SchemaBuilder.array(Schema.OPTIONAL_STRING_SCHEMA).optional().build()) .field("empty", SchemaBuilder.array(Schema.OPTIONAL_STRING_SCHEMA).optional().build()) .build()); }
@Test @FixFor("DBZ-928") public void shouldProcessNullValue() { val = BsonDocument.parse("{\n" + " \"_id\" : ObjectId(\"51e5619ee4b01f9fbdfba9fc\"),\n"+ " \"delivery\" : {\n" +