From d2f2b59da3a03b08de5c282457e75c769b22d7ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Gu=C3=A9rout?= Date: Mon, 1 Mar 2021 21:57:04 +0100 Subject: [PATCH 1/3] Revert "Remove JongoNative stuff" This reverts commit 287606f59b89f4c6335cadae35a33c5cb199ed79. --- src/main/java/org/jongo/Jongo.java | 8 + src/main/java/org/jongo/JongoCodec.java | 88 +++++++++ .../java/org/jongo/JongoCodecProvider.java | 41 +++++ src/main/java/org/jongo/JongoNative.java | 78 ++++++++ .../jongo/use_native/AggregateNativeTest.java | 62 +++++++ .../org/jongo/use_native/BsonNativeTest.java | 97 ++++++++++ .../jongo/use_native/DocumentNativeTest.java | 66 +++++++ .../jongo/use_native/InsertNativeTest.java | 170 ++++++++++++++++++ .../org/jongo/use_native/NativeTestBase.java | 57 ++++++ .../use_native/ParametersNativeTest.java | 57 ++++++ .../jongo/use_native/UpdateNativeTest.java | 135 ++++++++++++++ 11 files changed, 859 insertions(+) create mode 100644 src/main/java/org/jongo/JongoCodec.java create mode 100644 src/main/java/org/jongo/JongoCodecProvider.java create mode 100644 src/main/java/org/jongo/JongoNative.java create mode 100644 src/test/java/org/jongo/use_native/AggregateNativeTest.java create mode 100644 src/test/java/org/jongo/use_native/BsonNativeTest.java create mode 100644 src/test/java/org/jongo/use_native/DocumentNativeTest.java create mode 100644 src/test/java/org/jongo/use_native/InsertNativeTest.java create mode 100644 src/test/java/org/jongo/use_native/NativeTestBase.java create mode 100644 src/test/java/org/jongo/use_native/ParametersNativeTest.java create mode 100644 src/test/java/org/jongo/use_native/UpdateNativeTest.java diff --git a/src/main/java/org/jongo/Jongo.java b/src/main/java/org/jongo/Jongo.java index 13919e6e..7d2f70d7 100644 --- a/src/main/java/org/jongo/Jongo.java +++ b/src/main/java/org/jongo/Jongo.java @@ -68,4 +68,12 @@ public Command runCommand(String query) { public Command runCommand(String query, Object... parameters) { return new Command(database, mapper.getUnmarshaller(), mapper.getQueryFactory(), query, parameters); } + + public static JongoNative useNative(MongoDatabase database) { + return new JongoNative(database); + } + + public static JongoNative useNative(MongoDatabase database, Mapper mapper) { + return new JongoNative(database, mapper); + } } diff --git a/src/main/java/org/jongo/JongoCodec.java b/src/main/java/org/jongo/JongoCodec.java new file mode 100644 index 00000000..dfd8c625 --- /dev/null +++ b/src/main/java/org/jongo/JongoCodec.java @@ -0,0 +1,88 @@ +/* + * Copyright (C) 2011 Benoit GUEROUT and Yves AMSELLEM + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jongo; + + +import org.bson.*; +import org.bson.codecs.Codec; +import org.bson.codecs.CollectibleCodec; +import org.bson.codecs.DecoderContext; +import org.bson.codecs.EncoderContext; +import org.bson.codecs.configuration.CodecRegistry; +import org.bson.types.ObjectId; +import org.jongo.bson.Bson; +import org.jongo.marshall.Marshaller; +import org.jongo.marshall.Unmarshaller; + +public class JongoCodec implements CollectibleCodec { + + private final Codec codec; + private final Class clazz; + private final ObjectIdUpdater objectIdUpdater; + private final Unmarshaller unmarshaller; + private final Marshaller marshaller; + + public JongoCodec(Mapper mapper, Class clazz, CodecRegistry codecRegistry) { + this.codec = codecRegistry.get(RawBsonDocument.class); + this.clazz = clazz; + objectIdUpdater = mapper.getObjectIdUpdater(); + unmarshaller = mapper.getUnmarshaller(); + marshaller = mapper.getMarshaller(); + } + + public T decode(BsonReader reader, DecoderContext decoderContext) { + RawBsonDocument raw = codec.decode(reader, decoderContext); + org.jongo.bson.BsonDocument bsonDocument = Bson.createDocument(raw.getByteBuffer().array()); + return unmarshaller.unmarshall(bsonDocument, clazz); + } + + public void encode(BsonWriter writer, Object pojo, EncoderContext encoderContext) { + org.jongo.bson.BsonDocument document = marshaller.marshall(pojo); + codec.encode(writer, new RawBsonDocument(document.toByteArray()), encoderContext); + } + + public Class getEncoderClass() { + return this.clazz; + } + + public T generateIdIfAbsentFromDocument(T document) { + if (objectIdUpdater.mustGenerateObjectId(document)) { + ObjectId newOid = ObjectId.get(); + objectIdUpdater.setObjectId(document, newOid); + } + return document; + } + + public boolean documentHasId(T document) { + return objectIdUpdater.mustGenerateObjectId(document); + } + + public BsonValue getDocumentId(T document) { + + Object id = objectIdUpdater.getId(document); + + if (id instanceof BsonValue) { + return (BsonValue) id; + } + if (id instanceof ObjectId) { + return new BsonObjectId((ObjectId) id); + } + + throw new UnsupportedOperationException("Unable to get document id"); + } + +} diff --git a/src/main/java/org/jongo/JongoCodecProvider.java b/src/main/java/org/jongo/JongoCodecProvider.java new file mode 100644 index 00000000..c2ac2198 --- /dev/null +++ b/src/main/java/org/jongo/JongoCodecProvider.java @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2011 Benoit GUEROUT and Yves AMSELLEM + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jongo; + + +import org.bson.codecs.Codec; +import org.bson.codecs.configuration.CodecProvider; +import org.bson.codecs.configuration.CodecRegistry; + +import static org.jongo.marshall.jackson.JacksonMapper.Builder.jacksonMapper; + +public class JongoCodecProvider implements CodecProvider { + + private final Mapper mapper; + + public JongoCodecProvider() { + this(jacksonMapper().build()); + } + + public JongoCodecProvider(Mapper mapper) { + this.mapper = mapper; + } + + public Codec get(final Class type, final CodecRegistry registry) { + return new JongoCodec(mapper, type, registry); + } +} diff --git a/src/main/java/org/jongo/JongoNative.java b/src/main/java/org/jongo/JongoNative.java new file mode 100644 index 00000000..8c15a6e4 --- /dev/null +++ b/src/main/java/org/jongo/JongoNative.java @@ -0,0 +1,78 @@ +/* + * Copyright (C) 2011 Benoit GUEROUT and Yves AMSELLEM + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jongo; + + +import com.mongodb.MongoClient; +import com.mongodb.client.MongoCollection; +import com.mongodb.client.MongoDatabase; +import org.bson.BsonDocument; +import org.bson.Document; +import org.bson.codecs.BsonValueCodecProvider; +import org.bson.codecs.configuration.CodecRegistries; +import org.bson.codecs.configuration.CodecRegistry; +import org.bson.conversions.Bson; +import org.jongo.query.Query; + +import static org.jongo.marshall.jackson.JacksonMapper.Builder.jacksonMapper; + +public class JongoNative { + + private final Mapper mapper; + private final CodecRegistry codecRegistry; + private final MongoDatabase database; + + public JongoNative(MongoDatabase database) { + this(database, jacksonMapper().build()); + } + + public JongoNative(MongoDatabase database, Mapper mapper) { + this.mapper = mapper; + this.database = database; + this.codecRegistry = createCodecRegistry(mapper); + } + + public MongoCollection getCollection(String collectionName) { + return this.database.getCollection(collectionName); + } + + public MongoCollection getCollection(String collectionName, Class documentClass) { + MongoCollection collection = this.database.getCollection(collectionName, documentClass); + return wrapCollection(collection); + } + + public BsonDocument query(String query, Object... parameters) { + Query q = mapper.getQueryFactory().createQuery(query, parameters); + return q.toBsonDocument(); + } + + public Bson id(Object id) { + return query("{_id:#}", id); + } + + private com.mongodb.client.MongoCollection wrapCollection(com.mongodb.client.MongoCollection collection) { + return collection.withCodecRegistry(codecRegistry); + } + + private CodecRegistry createCodecRegistry(Mapper mapper) { + + CodecRegistry defaultRegistry = MongoClient.getDefaultCodecRegistry(); + CodecRegistry jongoRegistry = CodecRegistries.fromProviders(new BsonValueCodecProvider(), new JongoCodecProvider(mapper)); + + return CodecRegistries.fromRegistries(defaultRegistry, jongoRegistry); + } +} diff --git a/src/test/java/org/jongo/use_native/AggregateNativeTest.java b/src/test/java/org/jongo/use_native/AggregateNativeTest.java new file mode 100644 index 00000000..a3cbe24e --- /dev/null +++ b/src/test/java/org/jongo/use_native/AggregateNativeTest.java @@ -0,0 +1,62 @@ +/* + * Copyright (C) 2011 Benoit GUEROUT and Yves AMSELLEM + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jongo.use_native; + +import com.mongodb.client.MongoCollection; +import org.bson.conversions.Bson; +import org.jongo.model.Article; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.util.List; + +import static java.util.Arrays.asList; +import static org.assertj.core.api.Assertions.assertThat; + +public class AggregateNativeTest extends NativeTestBase { + + private MongoCollection
collection; + + @Before + public void setUp() throws Exception { + collection = jongo.getCollection("articles", Article.class); + collection.insertOne(new Article("Zombie Panic", "Kirsty Mckay", "horror", "virus")); + collection.insertOne(new Article("Apocalypse Zombie", "Maberry Jonathan", "horror", "dead")); + collection.insertOne(new Article("World War Z", "Max Brooks", "horror", "virus", "pandemic")); + } + + @After + public void tearDown() throws Exception { + this.collection.drop(); + } + + @Test + public void canAggregate() throws Exception { + + List pipeline = asList(q("{$match:{tags:'virus'}}")); + Iterable
articles = collection.aggregate(pipeline); + + assertThat(articles.iterator().hasNext()).isTrue(); + int size = 0; + for (Article article : articles) { + assertThat(article.getTags()).contains("virus"); + size++; + } + assertThat(size).isEqualTo(2); + } +} diff --git a/src/test/java/org/jongo/use_native/BsonNativeTest.java b/src/test/java/org/jongo/use_native/BsonNativeTest.java new file mode 100644 index 00000000..ecb8aeaa --- /dev/null +++ b/src/test/java/org/jongo/use_native/BsonNativeTest.java @@ -0,0 +1,97 @@ +/* + * Copyright (C) 2011 Benoit GUEROUT and Yves AMSELLEM + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jongo.use_native; + +import com.mongodb.DBObject; +import com.mongodb.Function; +import com.mongodb.MongoClient; +import com.mongodb.WriteConcern; +import com.mongodb.client.FindIterable; +import com.mongodb.client.MongoCollection; +import org.bson.BsonDocument; +import org.bson.BsonString; +import org.bson.Document; +import org.bson.conversions.Bson; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class BsonNativeTest extends NativeTestBase { + + private MongoCollection collection; + + @Before + public void setUp() throws Exception { + collection = jongo.getCollection("friends", BsonDocument.class).withWriteConcern(WriteConcern.ACKNOWLEDGED); + } + + @After + public void tearDown() throws Exception { + collection.drop(); + } + + @Test + public void canInsert() throws Exception { + + collection.insertOne(q("{name : 'Abby'}")); + + assertThat(collection.countDocuments(q("{name : 'Abby'}"))).isEqualTo(1); + } + + @Test + public void canInsertWithParameters() throws Exception { + + collection.insertOne(q("{name : #}", "Abby")); + + assertThat(collection.countDocuments(q("{name : 'Abby'}"))).isEqualTo(1); + } + + @Test + public void canFindWithProjectionParams() throws Exception { + + collection.insertOne(q("{name : 'Abby'}")); + + FindIterable results = collection.find(q("{name:'Abby'}")).projection(q("{name:#}", 1)); + + assertThat(results).isNotEmpty(); + results.map(new Function() { + + public String apply(BsonDocument bson) { + BsonDocument document = bson.toBsonDocument(DBObject.class, MongoClient.getDefaultCodecRegistry()); + assertThat(document.containsKey("address")).isFalse(); + assertThat(document.containsKey("name")).isTrue(); + return null; + } + }); + } + + @Test + public void canQueryWithNativeDocument() throws Exception { + + BsonDocument document = new BsonDocument("name", new BsonString("Abby")).append("address", new BsonString("123 Wall Street")); + + collection.insertOne(document); + + Bson result = collection.find(new Document("address", "123 Wall Street")).first(); + BsonDocument bsonDocument = result.toBsonDocument(DBObject.class, MongoClient.getDefaultCodecRegistry()); + assertThat(bsonDocument.toJson()).contains("123 Wall Street"); + } + + +} diff --git a/src/test/java/org/jongo/use_native/DocumentNativeTest.java b/src/test/java/org/jongo/use_native/DocumentNativeTest.java new file mode 100644 index 00000000..5187eba1 --- /dev/null +++ b/src/test/java/org/jongo/use_native/DocumentNativeTest.java @@ -0,0 +1,66 @@ +/* + * Copyright (C) 2011 Benoit GUEROUT and Yves AMSELLEM + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jongo.use_native; + +import com.mongodb.client.MongoCollection; +import com.mongodb.client.model.Filters; +import org.bson.Document; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.util.regex.Pattern; + +import static org.assertj.core.api.Assertions.assertThat; + +public class DocumentNativeTest extends NativeTestBase { + + private MongoCollection collection; + + @Before + public void setUp() throws Exception { + collection = jongo.getCollection("friends", Document.class); + } + + @After + public void tearDown() throws Exception { + collection.drop(); + } + + @Test + public void canUseDocumentBuilderFromDriver() throws Exception { + + Document doc = new Document().append("name", "Abby"); + + collection.insertOne(doc); + + assertThat(collection.countDocuments(doc)).isEqualTo(1); + } + + @Test + public void canUseFilterBuilderFromDriver() throws Exception { + + Document doc = new Document().append("name", "Abby"); + + collection.insertOne(doc); + + Document result = collection.find(Filters.regex("name", Pattern.compile("^A"))).first(); + assertThat(result.get("name")).isEqualTo("Abby"); + } + + +} diff --git a/src/test/java/org/jongo/use_native/InsertNativeTest.java b/src/test/java/org/jongo/use_native/InsertNativeTest.java new file mode 100644 index 00000000..b4d89a53 --- /dev/null +++ b/src/test/java/org/jongo/use_native/InsertNativeTest.java @@ -0,0 +1,170 @@ +/* + * Copyright (C) 2011 Benoit GUEROUT and Yves AMSELLEM + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jongo.use_native; + +import com.mongodb.MongoWriteException; +import com.mongodb.client.MongoCollection; +import junit.framework.Assert; +import org.bson.types.ObjectId; +import org.jongo.model.Coordinate; +import org.jongo.model.ExposableFriend; +import org.jongo.model.ExternalFriend; +import org.jongo.model.Friend; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.util.Date; + +import static com.google.common.collect.Lists.newArrayList; +import static org.assertj.core.api.Assertions.assertThat; + +public class InsertNativeTest extends NativeTestBase { + + private MongoCollection collection; + + @Before + public void setUp() throws Exception { + collection = jongo.getCollection("friends", Friend.class); + } + + @After + public void tearDown() throws Exception { + collection.drop(); + } + + @Test + public void canInsert() throws Exception { + + Friend friend = new Friend("John", "22 Wall Street Avenue"); + + collection.insertOne(friend); + long afterSave = new Date().getTime(); + + Friend john = collection.find(q("{name:'John'}")).first(); + assertThat(john).isNotNull(); + assertThat(john.getId()).isNotNull(); + assertThat(john.getId().getDate().getTime()).isLessThan(afterSave); + } + + @Test + public void canInsertWithObjectId() throws Exception { + + ObjectId oid = ObjectId.get(); + Friend john = new Friend(oid, "John"); + + collection.insertOne(john); + long afterSave = new Date().getTime(); + + Friend result = collection.find(id(oid)).first(); + assertThat(result.getId()).isEqualTo(oid); + assertThat(john.getId().getDate().getTime()).isLessThan(afterSave); //insert + } + + @Test + public void canInsertWithACustomTypeId() throws Exception { + + MongoCollection friends = collection.withDocumentClass(ExternalFriend.class); + ExternalFriend john = new ExternalFriend("999", "Robert"); + + friends.insertOne(john); + + ExternalFriend result = friends.find().first(); + assertThat(result.getId()).isEqualTo("999"); + } + + @Test + public void canInsertWithObjectIdAsString() throws Exception { + + MongoCollection friends = collection.withDocumentClass(ExposableFriend.class); + String id = ObjectId.get().toString(); + ExposableFriend john = new ExposableFriend(id, "Robert"); + + friends.insertOne(john); + + ExposableFriend result = friends.find().first(); + assertThat(result).isNotNull(); + assertThat(result.getId()).isEqualTo(id); + } + + @Test + public void canInsertAPojoWithAnEmptyObjectIdAsString() throws Exception { + + MongoCollection friends = collection.withDocumentClass(ExposableFriend.class); + ExposableFriend john = new ExposableFriend("Robert"); + + friends.insertOne(john); + + ExposableFriend result = friends.find().first(); + assertThat(result).isNotNull(); + assertThat(result.getId()).isNotNull(); + } + + @Test + public void canInsertAnObjectWithoutIdAnnotation() throws Exception { + + MongoCollection coordinates = collection.withDocumentClass(Coordinate.class); + Coordinate noId = new Coordinate(123, 1); + + coordinates.insertOne(noId); + + Coordinate result = coordinates.find().first(); + assertThat(result).isNotNull(); + assertThat(result.lat).isEqualTo(123); + } + + @Test + public void canOnlyInsertOnceAPojoWithObjectId() throws Exception { + + ObjectId id = ObjectId.get(); + + collection.insertOne(new Friend(id, "John")); + + try { + collection.insertOne(new Friend(id, "John")); + Assert.fail(); + } catch (MongoWriteException e) { + assertThat(e).hasMessageContaining("E11000"); + } + } + + @Test + public void canOnlyInsertOnceAPojoWithACustomId() throws Exception { + + MongoCollection friends = jongo.getCollection("friends", ExternalFriend.class); + + friends.insertOne(new ExternalFriend("122", "value")); + + try { + friends.insertOne(new ExternalFriend("122", "other value")); + Assert.fail(); + } catch (MongoWriteException e) { + assertThat(e).hasMessageContaining("E11000"); + } + } + + @Test + public void canInsertAListOfDocuments() throws Exception { + + collection.insertMany(newArrayList(new Friend("John"), new Friend("Robert"))); + + assertThat(collection.countDocuments()).isEqualTo(2); + Iterable friends = collection.find(); + assertThat(friends).extracting("name").containsExactly("John", "Robert"); + } + +} diff --git a/src/test/java/org/jongo/use_native/NativeTestBase.java b/src/test/java/org/jongo/use_native/NativeTestBase.java new file mode 100644 index 00000000..bbf43df5 --- /dev/null +++ b/src/test/java/org/jongo/use_native/NativeTestBase.java @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2011 Benoit GUEROUT and Yves AMSELLEM + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jongo.use_native; + +import com.mongodb.client.MongoDatabase; +import org.bson.BsonDocument; +import org.bson.conversions.Bson; +import org.jongo.Jongo; +import org.jongo.JongoNative; +import org.jongo.Mapper; +import org.jongo.util.MongoResource; +import org.junit.BeforeClass; + +import static org.jongo.marshall.jackson.JacksonMapper.Builder.jacksonMapper; + +public abstract class NativeTestBase { + + private static MongoResource MONGO_RESOURCE; + + protected JongoNative jongo; + + public NativeTestBase() { + this(jacksonMapper().build()); + } + + protected NativeTestBase(Mapper mapper) { + MongoDatabase database = MONGO_RESOURCE.getDatabase("test_jongo"); + this.jongo = Jongo.useNative(database, mapper); + } + + @BeforeClass + public static void startMongo() throws Exception { + MONGO_RESOURCE = new MongoResource(); + } + + protected BsonDocument q(String query, Object... parameters) { + return jongo.query(query, parameters); + } + + protected Bson id(Object id) { + return jongo.id(id); + } +} diff --git a/src/test/java/org/jongo/use_native/ParametersNativeTest.java b/src/test/java/org/jongo/use_native/ParametersNativeTest.java new file mode 100644 index 00000000..1b999cc2 --- /dev/null +++ b/src/test/java/org/jongo/use_native/ParametersNativeTest.java @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2011 Benoit GUEROUT and Yves AMSELLEM + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jongo.use_native; + +import com.mongodb.client.MongoCollection; +import org.jongo.model.Friend; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static java.util.Arrays.asList; +import static org.assertj.core.api.Assertions.assertThat; + +public class ParametersNativeTest extends NativeTestBase { + + private MongoCollection collection; + + @Before + public void setUp() throws Exception { + collection = jongo.getCollection("friends", Friend.class); + } + + @After + public void tearDown() throws Exception { + collection.drop(); + } + + @Test + //https://groups.google.com/forum/?fromgroups#!topic/jongo-user/p9CEKnkKX9Q + public void canUpdateIntoAnArray() throws Exception { + + collection.insertMany(asList(new Friend("Peter"), new Friend("Robert"))); + + collection.updateMany(q("{ 'name' : 'Peter' }"), q("{ $set : # }", new Friend("John"))); + + Friend friend = collection.find(q("{ 'name' : 'John' }")).first(); + + assertThat(friend).isNotNull(); + assertThat(friend.getName()).isEqualTo("John"); + } + + +} diff --git a/src/test/java/org/jongo/use_native/UpdateNativeTest.java b/src/test/java/org/jongo/use_native/UpdateNativeTest.java new file mode 100644 index 00000000..e5f90158 --- /dev/null +++ b/src/test/java/org/jongo/use_native/UpdateNativeTest.java @@ -0,0 +1,135 @@ +/* + * Copyright (C) 2011 Benoit GUEROUT and Yves AMSELLEM + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jongo.use_native; + +import com.mongodb.WriteConcern; +import com.mongodb.client.MongoCollection; +import junit.framework.Assert; +import org.bson.types.ObjectId; +import org.jongo.marshall.MarshallingException; +import org.jongo.model.ExposableFriend; +import org.jongo.model.ExternalFriend; +import org.jongo.model.Friend; +import org.jongo.util.ErrorObject; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class UpdateNativeTest extends NativeTestBase { + + private MongoCollection collection; + + @Before + public void setUp() throws Exception { + collection = jongo.getCollection("friends", Friend.class); + } + + @After + public void tearDown() throws Exception { + collection.drop(); + } + + @Test + public void canUpdateAnEntity() throws Exception { + + Friend john = new Friend("John", "21 Jump Street"); + collection.insertOne(john); + + john.setAddress("new address"); + collection.replaceOne(id(john.getId()), john); + + ObjectId johnId = john.getId(); + Friend johnWithNewAddress = collection.find(id(johnId)).first(); + assertThat(johnWithNewAddress.getId()).isEqualTo(johnId); + assertThat(johnWithNewAddress.getAddress()).isEqualTo("new address"); + } + + @Test + public void canUpdateWithACustomTypeId() throws Exception { + + MongoCollection friends = collection.withDocumentClass(ExternalFriend.class); + ExternalFriend friend = new ExternalFriend("999", "Robert"); + friends.insertOne(friend); + + friend.setName("Robert"); + friends.replaceOne(id(friend.getId()), friend); + + ExternalFriend result = friends.find().first(); + assertThat(result.getId()).isEqualTo("999"); + } + + @Test + public void canUpdateWithObjectIdAsString() throws Exception { + + MongoCollection friends = collection.withDocumentClass(ExposableFriend.class); + String id = ObjectId.get().toString(); + ExposableFriend robert = new ExposableFriend(id, "Robert"); + friends.insertOne(robert); + + robert.setName("Hue"); // <-- "famous" french Robert + friends.replaceOne(q("{_id:{$oid:#}}", id), robert); + + ExposableFriend robertHue = friends.find(q("{_id:{$oid:#}}", id)).first(); + assertThat(robertHue.getId()).isEqualTo(id); + assertThat(robertHue.getName()).isEqualTo("Hue"); + } + + @Test + public void canUpdateAPojoWithACustomId() throws Exception { + + MongoCollection friends = collection.withDocumentClass(ExternalFriend.class); + ExternalFriend externalFriend = new ExternalFriend("122", "John"); + MongoCollection safeCollection = friends.withWriteConcern(WriteConcern.ACKNOWLEDGED); + + safeCollection.insertOne(externalFriend); + externalFriend.setName("Robert"); + safeCollection.replaceOne(id(externalFriend.getId()), externalFriend); + + ExternalFriend result = friends.find(q("{name:'Robert'}")).first(); + assertThat(result.getId()).isEqualTo("122"); + } + + @Test + public void canUpdateAPojoWithAnValidObjectIdAsString() { + + MongoCollection friends = collection.withDocumentClass(ExposableFriend.class); + ExposableFriend friend = new ExposableFriend(ObjectId.get().toString(), "Robert"); + + friends.insertOne(friend); + String id = friend.getId(); + assertThat(friend.getId()).isNotNull(); + + friend.setName("John"); + friends.replaceOne(id(friend.getId()), friend); + + assertThat(friend.getId()).isEqualTo(id); + assertThat(friend.getName()).isEqualTo("John"); + } + + @Test + public void shouldFailWhenMarshallerFail() throws Exception { + + try { + collection.withDocumentClass(ErrorObject.class).insertOne(new ErrorObject()); + Assert.fail(); + } catch (MarshallingException e) { + } + } + +} From 85eb3e2ddc560f53747b8cfb4bad4845f4e7e146 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Gu=C3=A9rout?= Date: Mon, 1 Mar 2021 22:00:04 +0100 Subject: [PATCH 2/3] Rename JongoNative into JongoSync --- src/main/java/org/jongo/Jongo.java | 8 ++++---- .../org/jongo/{JongoNative.java => JongoSync.java} | 6 +++--- src/test/java/org/jongo/bench/BenchUtil.java | 4 ++-- .../AggregateSyncTest.java} | 4 ++-- .../BsonNativeTest.java => sync/BsonSyncTest.java} | 6 +++--- .../DocumentSyncTest.java} | 4 ++-- .../InsertSyncTest.java} | 4 ++-- .../ParametersSyncTest.java} | 4 ++-- .../NativeTestBase.java => sync/SyncTestBase.java} | 14 +++++++------- .../UpdateSyncTest.java} | 4 ++-- 10 files changed, 29 insertions(+), 29 deletions(-) rename src/main/java/org/jongo/{JongoNative.java => JongoSync.java} (95%) rename src/test/java/org/jongo/{use_native/AggregateNativeTest.java => sync/AggregateSyncTest.java} (95%) rename src/test/java/org/jongo/{use_native/BsonNativeTest.java => sync/BsonSyncTest.java} (95%) rename src/test/java/org/jongo/{use_native/DocumentNativeTest.java => sync/DocumentSyncTest.java} (95%) rename src/test/java/org/jongo/{use_native/InsertNativeTest.java => sync/InsertSyncTest.java} (98%) rename src/test/java/org/jongo/{use_native/ParametersNativeTest.java => sync/ParametersSyncTest.java} (95%) rename src/test/java/org/jongo/{use_native/NativeTestBase.java => sync/SyncTestBase.java} (84%) rename src/test/java/org/jongo/{use_native/UpdateNativeTest.java => sync/UpdateSyncTest.java} (98%) diff --git a/src/main/java/org/jongo/Jongo.java b/src/main/java/org/jongo/Jongo.java index 7d2f70d7..5dbe4e6e 100644 --- a/src/main/java/org/jongo/Jongo.java +++ b/src/main/java/org/jongo/Jongo.java @@ -69,11 +69,11 @@ public Command runCommand(String query, Object... parameters) { return new Command(database, mapper.getUnmarshaller(), mapper.getQueryFactory(), query, parameters); } - public static JongoNative useNative(MongoDatabase database) { - return new JongoNative(database); + public static JongoSync useSync(MongoDatabase database) { + return new JongoSync(database); } - public static JongoNative useNative(MongoDatabase database, Mapper mapper) { - return new JongoNative(database, mapper); + public static JongoSync useSync(MongoDatabase database, Mapper mapper) { + return new JongoSync(database, mapper); } } diff --git a/src/main/java/org/jongo/JongoNative.java b/src/main/java/org/jongo/JongoSync.java similarity index 95% rename from src/main/java/org/jongo/JongoNative.java rename to src/main/java/org/jongo/JongoSync.java index 8c15a6e4..06f3b766 100644 --- a/src/main/java/org/jongo/JongoNative.java +++ b/src/main/java/org/jongo/JongoSync.java @@ -30,17 +30,17 @@ import static org.jongo.marshall.jackson.JacksonMapper.Builder.jacksonMapper; -public class JongoNative { +public class JongoSync { private final Mapper mapper; private final CodecRegistry codecRegistry; private final MongoDatabase database; - public JongoNative(MongoDatabase database) { + public JongoSync(MongoDatabase database) { this(database, jacksonMapper().build()); } - public JongoNative(MongoDatabase database, Mapper mapper) { + public JongoSync(MongoDatabase database, Mapper mapper) { this.mapper = mapper; this.database = database; this.codecRegistry = createCodecRegistry(mapper); diff --git a/src/test/java/org/jongo/bench/BenchUtil.java b/src/test/java/org/jongo/bench/BenchUtil.java index f8ca0bbc..8db330ba 100644 --- a/src/test/java/org/jongo/bench/BenchUtil.java +++ b/src/test/java/org/jongo/bench/BenchUtil.java @@ -49,8 +49,8 @@ public static DBObject asDBObject(Friend friend) { } public static DBCollection getCollectionFromDriver() throws UnknownHostException { - MongoClient nativeMongo = new MongoClient(); - return nativeMongo.getDB("jongo").getCollection("benchmark"); + MongoClient syncMongo = new MongoClient(); + return syncMongo.getDB("jongo").getCollection("benchmark"); } public static MongoCollection getCollectionFromJongo(Mapper mapper) throws UnknownHostException { diff --git a/src/test/java/org/jongo/use_native/AggregateNativeTest.java b/src/test/java/org/jongo/sync/AggregateSyncTest.java similarity index 95% rename from src/test/java/org/jongo/use_native/AggregateNativeTest.java rename to src/test/java/org/jongo/sync/AggregateSyncTest.java index a3cbe24e..ab04f777 100644 --- a/src/test/java/org/jongo/use_native/AggregateNativeTest.java +++ b/src/test/java/org/jongo/sync/AggregateSyncTest.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.jongo.use_native; +package org.jongo.sync; import com.mongodb.client.MongoCollection; import org.bson.conversions.Bson; @@ -28,7 +28,7 @@ import static java.util.Arrays.asList; import static org.assertj.core.api.Assertions.assertThat; -public class AggregateNativeTest extends NativeTestBase { +public class AggregateSyncTest extends SyncTestBase { private MongoCollection
collection; diff --git a/src/test/java/org/jongo/use_native/BsonNativeTest.java b/src/test/java/org/jongo/sync/BsonSyncTest.java similarity index 95% rename from src/test/java/org/jongo/use_native/BsonNativeTest.java rename to src/test/java/org/jongo/sync/BsonSyncTest.java index ecb8aeaa..e6fb4b5f 100644 --- a/src/test/java/org/jongo/use_native/BsonNativeTest.java +++ b/src/test/java/org/jongo/sync/BsonSyncTest.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.jongo.use_native; +package org.jongo.sync; import com.mongodb.DBObject; import com.mongodb.Function; @@ -32,7 +32,7 @@ import static org.assertj.core.api.Assertions.assertThat; -public class BsonNativeTest extends NativeTestBase { +public class BsonSyncTest extends SyncTestBase { private MongoCollection collection; @@ -82,7 +82,7 @@ public String apply(BsonDocument bson) { } @Test - public void canQueryWithNativeDocument() throws Exception { + public void canQueryWithBsonDocument() throws Exception { BsonDocument document = new BsonDocument("name", new BsonString("Abby")).append("address", new BsonString("123 Wall Street")); diff --git a/src/test/java/org/jongo/use_native/DocumentNativeTest.java b/src/test/java/org/jongo/sync/DocumentSyncTest.java similarity index 95% rename from src/test/java/org/jongo/use_native/DocumentNativeTest.java rename to src/test/java/org/jongo/sync/DocumentSyncTest.java index 5187eba1..4eef0d09 100644 --- a/src/test/java/org/jongo/use_native/DocumentNativeTest.java +++ b/src/test/java/org/jongo/sync/DocumentSyncTest.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.jongo.use_native; +package org.jongo.sync; import com.mongodb.client.MongoCollection; import com.mongodb.client.model.Filters; @@ -27,7 +27,7 @@ import static org.assertj.core.api.Assertions.assertThat; -public class DocumentNativeTest extends NativeTestBase { +public class DocumentSyncTest extends SyncTestBase { private MongoCollection collection; diff --git a/src/test/java/org/jongo/use_native/InsertNativeTest.java b/src/test/java/org/jongo/sync/InsertSyncTest.java similarity index 98% rename from src/test/java/org/jongo/use_native/InsertNativeTest.java rename to src/test/java/org/jongo/sync/InsertSyncTest.java index b4d89a53..aebf11ac 100644 --- a/src/test/java/org/jongo/use_native/InsertNativeTest.java +++ b/src/test/java/org/jongo/sync/InsertSyncTest.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.jongo.use_native; +package org.jongo.sync; import com.mongodb.MongoWriteException; import com.mongodb.client.MongoCollection; @@ -33,7 +33,7 @@ import static com.google.common.collect.Lists.newArrayList; import static org.assertj.core.api.Assertions.assertThat; -public class InsertNativeTest extends NativeTestBase { +public class InsertSyncTest extends SyncTestBase { private MongoCollection collection; diff --git a/src/test/java/org/jongo/use_native/ParametersNativeTest.java b/src/test/java/org/jongo/sync/ParametersSyncTest.java similarity index 95% rename from src/test/java/org/jongo/use_native/ParametersNativeTest.java rename to src/test/java/org/jongo/sync/ParametersSyncTest.java index 1b999cc2..46e327aa 100644 --- a/src/test/java/org/jongo/use_native/ParametersNativeTest.java +++ b/src/test/java/org/jongo/sync/ParametersSyncTest.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.jongo.use_native; +package org.jongo.sync; import com.mongodb.client.MongoCollection; import org.jongo.model.Friend; @@ -25,7 +25,7 @@ import static java.util.Arrays.asList; import static org.assertj.core.api.Assertions.assertThat; -public class ParametersNativeTest extends NativeTestBase { +public class ParametersSyncTest extends SyncTestBase { private MongoCollection collection; diff --git a/src/test/java/org/jongo/use_native/NativeTestBase.java b/src/test/java/org/jongo/sync/SyncTestBase.java similarity index 84% rename from src/test/java/org/jongo/use_native/NativeTestBase.java rename to src/test/java/org/jongo/sync/SyncTestBase.java index bbf43df5..bcdafa86 100644 --- a/src/test/java/org/jongo/use_native/NativeTestBase.java +++ b/src/test/java/org/jongo/sync/SyncTestBase.java @@ -14,32 +14,32 @@ * limitations under the License. */ -package org.jongo.use_native; +package org.jongo.sync; import com.mongodb.client.MongoDatabase; import org.bson.BsonDocument; import org.bson.conversions.Bson; import org.jongo.Jongo; -import org.jongo.JongoNative; +import org.jongo.JongoSync; import org.jongo.Mapper; import org.jongo.util.MongoResource; import org.junit.BeforeClass; import static org.jongo.marshall.jackson.JacksonMapper.Builder.jacksonMapper; -public abstract class NativeTestBase { +public abstract class SyncTestBase { private static MongoResource MONGO_RESOURCE; - protected JongoNative jongo; + protected JongoSync jongo; - public NativeTestBase() { + public SyncTestBase() { this(jacksonMapper().build()); } - protected NativeTestBase(Mapper mapper) { + protected SyncTestBase(Mapper mapper) { MongoDatabase database = MONGO_RESOURCE.getDatabase("test_jongo"); - this.jongo = Jongo.useNative(database, mapper); + this.jongo = Jongo.useSync(database, mapper); } @BeforeClass diff --git a/src/test/java/org/jongo/use_native/UpdateNativeTest.java b/src/test/java/org/jongo/sync/UpdateSyncTest.java similarity index 98% rename from src/test/java/org/jongo/use_native/UpdateNativeTest.java rename to src/test/java/org/jongo/sync/UpdateSyncTest.java index e5f90158..2d2cbaa8 100644 --- a/src/test/java/org/jongo/use_native/UpdateNativeTest.java +++ b/src/test/java/org/jongo/sync/UpdateSyncTest.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.jongo.use_native; +package org.jongo.sync; import com.mongodb.WriteConcern; import com.mongodb.client.MongoCollection; @@ -31,7 +31,7 @@ import static org.assertj.core.api.Assertions.assertThat; -public class UpdateNativeTest extends NativeTestBase { +public class UpdateSyncTest extends SyncTestBase { private MongoCollection collection; From 55d90b9dea07081ce39dfbdcc8af2e068fb5b9e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Gu=C3=A9rout?= Date: Fri, 5 Mar 2021 14:03:40 +0100 Subject: [PATCH 3/3] Rename Bench methods --- src/test/java/org/jongo/bench/BenchUtil.java | 14 +++++++------- src/test/java/org/jongo/bench/FindBench.java | 8 ++++---- src/test/java/org/jongo/bench/SaveBench.java | 5 ++--- 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/src/test/java/org/jongo/bench/BenchUtil.java b/src/test/java/org/jongo/bench/BenchUtil.java index 8db330ba..d08c6193 100644 --- a/src/test/java/org/jongo/bench/BenchUtil.java +++ b/src/test/java/org/jongo/bench/BenchUtil.java @@ -48,20 +48,20 @@ public static DBObject asDBObject(Friend friend) { return dbo; } - public static DBCollection getCollectionFromDriver() throws UnknownHostException { - MongoClient syncMongo = new MongoClient(); - return syncMongo.getDB("jongo").getCollection("benchmark"); + public static DBCollection getDBCollection() throws UnknownHostException { + MongoClient client = new MongoClient(); + return client.getDB("jongo").getCollection("benchmark"); } - public static MongoCollection getCollectionFromJongo(Mapper mapper) throws UnknownHostException { - MongoClient mongo = new MongoClient(); - DB db = mongo.getDB("jongo"); + public static MongoCollection getMongoCollection(Mapper mapper) throws UnknownHostException { + MongoClient client = new MongoClient(); + DB db = client.getDB("jongo"); Jongo jongo = new Jongo(db, mapper); return jongo.getCollection("benchmark"); } public static void injectFriendsIntoDB(int nbDocuments) throws UnknownHostException { - MongoCollection collection = getCollectionFromJongo(jacksonMapper().build()); + MongoCollection collection = getMongoCollection(jacksonMapper().build()); collection.drop(); for (int i = 0; i < nbDocuments; i++) { collection.withWriteConcern(WriteConcern.MAJORITY).save(createFriend(i)); diff --git a/src/test/java/org/jongo/bench/FindBench.java b/src/test/java/org/jongo/bench/FindBench.java index aff8c248..5f49e1ef 100644 --- a/src/test/java/org/jongo/bench/FindBench.java +++ b/src/test/java/org/jongo/bench/FindBench.java @@ -26,8 +26,8 @@ import org.jongo.model.Coordinate; import org.jongo.model.Friend; -import static org.jongo.bench.BenchUtil.getCollectionFromDriver; -import static org.jongo.bench.BenchUtil.getCollectionFromJongo; +import static org.jongo.bench.BenchUtil.getDBCollection; +import static org.jongo.bench.BenchUtil.getMongoCollection; import static org.jongo.marshall.jackson.JacksonMapper.Builder.jacksonMapper; public class FindBench extends SimpleBenchmark { @@ -39,8 +39,8 @@ public class FindBench extends SimpleBenchmark { private DBCollection dbCollection; protected void setUp() throws Exception { - bsonCollection = getCollectionFromJongo(jacksonMapper().build()); - dbCollection = getCollectionFromDriver(); + bsonCollection = getMongoCollection(jacksonMapper().build()); + dbCollection = getDBCollection(); if (dbCollection.count() < NB_DOCUMENTS) { BenchUtil.injectFriendsIntoDB(NB_DOCUMENTS); diff --git a/src/test/java/org/jongo/bench/SaveBench.java b/src/test/java/org/jongo/bench/SaveBench.java index a8720930..6de95f6f 100644 --- a/src/test/java/org/jongo/bench/SaveBench.java +++ b/src/test/java/org/jongo/bench/SaveBench.java @@ -35,9 +35,8 @@ public class SaveBench extends SimpleBenchmark { private MongoCollection bsonCollection; protected void setUp() throws Exception { - - bsonCollection = getCollectionFromJongo(jacksonMapper().build()).withWriteConcern(WriteConcern.MAJORITY); - dbCollection = getCollectionFromDriver(); + bsonCollection = getMongoCollection(jacksonMapper().build()).withWriteConcern(WriteConcern.MAJORITY); + dbCollection = getDBCollection(); bsonCollection.drop(); }