Search in sources :

Example 51 with ObjectId

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));
}
Also used : Circle(org.mongodb.morphia.testmodel.Circle) ObjectId(org.bson.types.ObjectId) Test(org.junit.Test)

Example 52 with ObjectId

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)));
}
Also used : ObjectId(org.bson.types.ObjectId) UpdateResults(org.mongodb.morphia.query.UpdateResults) Test(org.junit.Test)

Example 53 with ObjectId

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));
}
Also used : Circle(org.mongodb.morphia.testmodel.Circle) ObjectId(org.bson.types.ObjectId) Test(org.junit.Test)

Example 54 with ObjectId

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);
}
Also used : Mapper(org.mongodb.morphia.mapping.Mapper) ObjectId(org.bson.types.ObjectId) BasicDAO(org.mongodb.morphia.dao.BasicDAO) DBObject(com.mongodb.DBObject) DefaultEntityCache(org.mongodb.morphia.mapping.cache.DefaultEntityCache) DBObject(com.mongodb.DBObject) Test(org.junit.Test)

Example 55 with ObjectId

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());
}
Also used : ObjectId(org.bson.types.ObjectId) Test(org.junit.Test)

Aggregations

ObjectId (org.bson.types.ObjectId)249 Test (org.junit.Test)157 BasicDBObject (com.mongodb.BasicDBObject)54 Document (org.bson.Document)38 DBObject (com.mongodb.DBObject)35 ArrayList (java.util.ArrayList)28 BsonObjectId (org.bson.BsonObjectId)27 Date (java.util.Date)24 DBRef (com.mongodb.DBRef)15 List (java.util.List)15 StreamRuleMock (org.graylog2.streams.matchers.StreamRuleMock)14 Message (org.graylog2.plugin.Message)13 BasicBSONObject (org.bson.BasicBSONObject)12 Query (org.springframework.data.mongodb.core.query.Query)11 GridFSFile (com.mongodb.client.gridfs.model.GridFSFile)10 Map (java.util.Map)10 Binary (org.bson.types.Binary)10 GridFSFindIterable (com.mongodb.client.gridfs.GridFSFindIterable)8 UsingDataSet (com.lordofthejars.nosqlunit.annotation.UsingDataSet)7 Code (org.bson.types.Code)7