@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 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 shouldBindEnumParameter() throws Exception { Friend friend = new Friend("John", new Coordinate(2, 31)); friend.setGender(Gender.FEMALE); collection.save(friend); Iterator<Friend> results = collection.find("{'gender':#}", Gender.FEMALE).as(Friend.class).iterator(); assertThat(results.next().getGender()).isEqualTo(Gender.FEMALE); assertThat(results.hasNext()).isFalse(); }
@Test public void canFindWithEmptySelector() throws Exception { /* given */ collection.insert("{name:'John'}"); collection.insert("{name:'Smith'}"); collection.insert("{name:'Peter'}"); /* when */ Iterable<Friend> friends = collection.find().as(Friend.class); /* then */ assertThat(friends.iterator().hasNext()).isTrue(); for (Friend friend : friends) { assertThat(friend.getName()).isIn("John", "Smith", "Peter"); } }
@Test public void canFindWithTwoOid() throws Exception { /* given */ ObjectId id1 = new ObjectId(); Friend john = new Friend(id1, "John"); ObjectId id2 = new ObjectId(); Friend peter = new Friend(id2, "Peter"); collection.save(john); collection.save(peter); Iterable<Friend> friends = collection .find("{$or :[{_id:{$oid:#}},{_id:{$oid:#}}]}", id1.toString(), id2.toString()) .as(Friend.class); /* then */ assertThat(friends.iterator().hasNext()).isTrue(); for (Friend friend : friends) { assertThat(friend.getId()).isIn(id1, id2); } }
@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"); }