@Test public void canUpdateWithACustomTypeId() throws Exception { ExternalFriend friend = new ExternalFriend(999, "Robert"); collection.save(friend); friend.setName("Robert"); collection.save(friend); ExternalFriend result = collection.findOne().as(ExternalFriend.class); assertThat(result.getId()).isEqualTo(999); }
@Test public void canUpdateAnEntity() throws Exception { Friend john = new Friend("John", "21 Jump Street"); collection.save(john); john.setAddress("new address"); collection.save(john); ObjectId johnId = john.getId(); Friend johnWithNewAddress = collection.findOne(johnId).as(Friend.class); assertThat(johnWithNewAddress.getId()).isEqualTo(johnId); assertThat(johnWithNewAddress.getAddress()).isEqualTo("new address"); }
@Test public void canUpdateWithObjectIdAsString() throws Exception { String id = ObjectId.get().toString(); ExposableFriend robert = new ExposableFriend(id, "Robert"); collection.save(robert); String johnId = robert.getId(); robert.setName("Hue"); // <-- "famous" french Robert collection.save(robert); ExposableFriend robertHue = collection.findOne("{_id:#}", johnId).as(ExposableFriend.class); assertThat(robertHue.getId()).isEqualTo(johnId); assertThat(robertHue.getName()).isEqualTo("Hue"); }
public static void main(String[] args) throws Exception { Configuration config = new Configuration(); Mongo mongo = new MongoImpl(config, "127.0.0.1:27018", "127.0.0.1:27017", "127.0.0.1:27019"); MongoCollection collection = mongo.getDB("goojia").getCollection("goojia_common.ppc_map"); BSONDocument doc = new MongoDocument(); doc.put("city", "sh"); doc.put("category", 1); doc.put("relate_id", 123); doc.put("relate_id_2", 0); collection.save(doc); Thread.sleep(20000); doc = new MongoDocument(); doc.put("city", "bj"); doc.put("category", 12); doc.put("relate_id", 12344); doc.put("relate_id_2", 0); collection.save(doc); mongo.close(); }
@Test public void shouldUseDefaultWriteConcern() throws Exception { Friend friend = new Friend("John", "22 Wall Street Avenue"); WriteResult writeResult = collection.save(friend); assertThat(writeResult.getLastConcern()) .isEqualTo(collection.getDBCollection().getWriteConcern()); }
@Test public void canSaveWithACustomTypeId() throws Exception { ExternalFriend john = new ExternalFriend(999, "Robert"); collection.save(john); ExternalFriend result = collection.findOne().as(ExternalFriend.class); assertThat(result.getId()).isEqualTo(999); }
@Test public void shouldFailWhenMarshallerFail() throws Exception { try { collection.save(new ErrorObject()); Assert.fail(); } catch (IllegalArgumentException e) { assertThat(e.getMessage()).contains("Unable to save object"); } }
@Test public void canSaveWithAnEmptyObjectIdAsString() throws Exception { ExposableFriend john = new ExposableFriend("Robert"); collection.save(john); ExposableFriend result = collection.findOne().as(ExposableFriend.class); assertThat(result).isNotNull(); assertThat(result.getId()).isNotNull(); }
@Test public void canSaveAnObjectWithoutIdAnnotation() throws Exception { Coordinate noId = new Coordinate(123, 1); collection.save(noId); Coordinate result = collection.findOne().as(Coordinate.class); assertThat(result).isNotNull(); assertThat(result.lat).isEqualTo(123); }
@Test public void canSaveWithObjectId() throws Exception { ObjectId oid = ObjectId.get(); Friend john = new Friend(oid, "John"); collection.save(john); Friend result = collection.findOne(oid).as(Friend.class); assertThat(result.getId()).isEqualTo(oid); assertThat(oid.isNew()).isFalse(); // insert }
@Test public void canFindUsingSubProperty() throws Exception { /* given */ collection.save(new Friend("John", new Coordinate(2, 31))); /* when */ Iterator<Friend> results = collection.find("{'coordinate.lat':2}").as(Friend.class).iterator(); /* then */ assertThat(results.next().getCoordinate().lat).isEqualTo(2); assertThat(results.hasNext()).isFalse(); }
@Test public void canSaveWithObjectIdAsString() throws Exception { String id = ObjectId.get().toString(); ExposableFriend john = new ExposableFriend(id, "Robert"); collection.save(john); ExposableFriend result = collection.findOne().as(ExposableFriend.class); assertThat(result).isNotNull(); assertThat(result.getId()).isEqualTo(id); }
@Test public void canSave() throws Exception { Friend friend = new Friend("John", "22 Wall Street Avenue"); collection.save(friend); Friend john = collection.findOne("{name:'John'}").as(Friend.class); assertThat(john).isNotNull(); assertThat(john.getId()).isNotNull(); assertThat(john.getId().isNew()).isFalse(); }
@Test public void canFind() throws Exception { /* given */ Friend friend = new Friend(new ObjectId(), "John"); collection.save(friend); /* when */ Iterator<Friend> friends = collection.find("{name:'John'}").as(Friend.class).iterator(); /* then */ assertThat(friends.hasNext()).isTrue(); assertThat(friends.next().getName()).isEqualTo("John"); assertThat(friends.hasNext()).isFalse(); }
@Test public void canFindWithOid() throws Exception { /* given */ ObjectId id = new ObjectId(); Friend john = new Friend(id, "John"); collection.save(john); Iterator<Friend> friends = collection.find("{_id:{$oid:#}}", id.toString()).as(Friend.class).iterator(); /* then */ assertThat(friends.hasNext()).isTrue(); assertThat(friends.next().getId()).isEqualTo(id); }
@Test public void canFindWithStringAsObjectId() { /* given */ String id = ObjectId.get().toString(); ExposableFriend friend = new ExposableFriend(id, "John"); collection.save(friend); /* when */ Iterator<ExposableFriend> friends = collection.find(withOid(friend.getId())).as(ExposableFriend.class).iterator(); /* then */ ExposableFriend john = friends.next(); assertThat(john.getId()).isEqualTo(id); assertThat(john.getName()).isEqualTo("John"); assertThat(friends.hasNext()).isFalse(); }
@Test public void canFindWithReadPreference() throws Exception { /* given */ Friend friend = new Friend(new ObjectId(), "John"); collection.save(friend); /* when */ Iterator<Friend> friends = collection .withReadPreference(ReadPreference.primaryPreferred()) .find("{name:'John'}") .as(Friend.class) .iterator(); /* then */ assertThat(friends.hasNext()).isTrue(); assertThat(friends.next().getName()).isEqualTo("John"); assertThat(friends.hasNext()).isFalse(); // warning: we cannot check that ReadPreference is really used by driver, this unit test only // checks the API }