use of org.bson.types.ObjectId in project morphia by mongodb.
the class TestUpdateOps method testMaxKeepsCurrentDocumentValueWhenThisIsLargerThanSuppliedValue.
@Test
public void testMaxKeepsCurrentDocumentValueWhenThisIsLargerThanSuppliedValue() throws Exception {
checkMinServerVersion(2.6);
final ObjectId id = new ObjectId();
final double originalValue = 2D;
Datastore ds = getDs();
assertInserted(ds.update(ds.find(Circle.class).field("id").equal(id), ds.createUpdateOperations(Circle.class).setOnInsert("radius", originalValue), new UpdateOptions().upsert(true)));
assertUpdated(ds.update(ds.find(Circle.class).field("id").equal(id), ds.createUpdateOperations(Circle.class).max("radius", 1D), new UpdateOptions().upsert(true)), 1);
assertThat(ds.get(Circle.class, id).getRadius(), is(originalValue));
}
use of org.bson.types.ObjectId in project morphia by mongodb.
the class TestUpdateOps method shouldUpdateAnArrayElement.
@Test
public void shouldUpdateAnArrayElement() {
// given
ObjectId parentId = new ObjectId();
String childName = "Bob";
String updatedLastName = "updatedLastName";
Parent parent = new Parent();
parent.id = parentId;
parent.children.add(new Child("Anthony", "Child"));
parent.children.add(new Child(childName, "originalLastName"));
getDs().save(parent);
// when
Query<Parent> query = getDs().find(Parent.class).field("_id").equal(parentId).field("children.first").equal(childName);
UpdateOperations<Parent> updateOps = getDs().createUpdateOperations(Parent.class).set("children.$.last", updatedLastName);
UpdateResults updateResults = getDs().update(query, updateOps);
// then
assertThat(updateResults.getUpdatedCount(), is(1));
assertThat(getDs().find(Parent.class).filter("id", parentId).get().children, hasItem(new Child(childName, updatedLastName)));
}
use of org.bson.types.ObjectId in project morphia by mongodb.
the class TestUpdateOps method testMinUsesSuppliedValueWhenThisIsSmallerThanCurrentDocumentValue.
@Test
public void testMinUsesSuppliedValueWhenThisIsSmallerThanCurrentDocumentValue() throws Exception {
checkMinServerVersion(2.6);
final ObjectId id = new ObjectId();
final double newLowerValue = 2D;
assertInserted(getDs().update(getDs().find(Circle.class).field("id").equal(id), getDs().createUpdateOperations(Circle.class).setOnInsert("radius", 3D), new UpdateOptions().upsert(true)));
assertUpdated(getDs().update(getDs().find(Circle.class).field("id").equal(id), getDs().createUpdateOperations(Circle.class).min("radius", newLowerValue), new UpdateOptions().upsert(true)), 1);
final Circle updatedCircle = getDs().get(Circle.class, id);
assertThat(updatedCircle, is(notNullValue()));
assertThat(updatedCircle.getRadius(), is(newLowerValue));
}
use of org.bson.types.ObjectId in project morphia by mongodb.
the class TestMapping method testMapping.
@Test
public void testMapping() {
final BasicDAO<User, ObjectId> messageDAO = new BasicDAO<User, ObjectId>(User.class, getDs());
Assert.assertNotNull(messageDAO);
Mapper mapper = new Mapper();
User user = new User();
user.id = 1;
user.userObject = "just a String";
DBObject dbObject = mapper.toDBObject(user);
Object object = mapper.fromDBObject(getDs(), User.class, dbObject, new DefaultEntityCache());
Assert.assertEquals(user.userObject, ((User) object).userObject);
user.userObject = 33;
dbObject = mapper.toDBObject(user);
object = mapper.fromDBObject(getDs(), User.class, dbObject, new DefaultEntityCache());
Assert.assertEquals(user.userObject, ((User) object).userObject);
user.userObject = 33.3;
dbObject = mapper.toDBObject(user);
object = mapper.fromDBObject(getDs(), User.class, dbObject, new DefaultEntityCache());
Assert.assertEquals(user.userObject, ((User) object).userObject);
}
use of org.bson.types.ObjectId in project morphia by mongodb.
the class QueryInTest method testIdOnly.
@Test
public void testIdOnly() {
ReferencedEntity b = new ReferencedEntity();
b.setId(new ObjectId("111111111111111111111111"));
getDs().save(b);
HasIdOnly has = new HasIdOnly();
has.list = new ArrayList<ReferencedEntity>();
has.list.add(b);
has.entity = b;
getDs().save(has);
Query<HasIdOnly> q = getDs().find(HasIdOnly.class);
q.criteria("list").in(Arrays.asList(b));
Assert.assertEquals(1, q.asList().size());
q = getDs().find(HasIdOnly.class);
q.criteria("entity").equal(b.getId());
Assert.assertEquals(1, q.asList().size());
}
Aggregations