Search in sources :

Example 1 with Datastore

use of dev.morphia.Datastore in project morphia by mongodb.

the class TestUpdateOperations method testUpdateKeyList.

@Test
public void testUpdateKeyList() {
    final ContainsPicKey cpk = new ContainsPicKey();
    cpk.name = "cpk one";
    Datastore ds = getDs();
    ds.save(cpk);
    final Pic pic = new Pic();
    pic.setName("fist again");
    ds.save(pic);
    cpk.keys = MorphiaReference.wrap(List.of(pic));
    // test with Key<Pic>
    Query<ContainsPicKey> query = ds.find(ContainsPicKey.class).filter(eq("name", cpk.name));
    final UpdateResult res = query.update(set("keys", cpk.keys)).execute();
    assertThat(res.getModifiedCount(), is(1L));
    // test reading the object.
    final ContainsPicKey cpk2 = ds.find(ContainsPicKey.class).iterator(new FindOptions().limit(1)).next();
    assertThat(cpk2, is(notNullValue()));
    assertThat(cpk.name, is(cpk2.name));
    MatcherAssert.assertThat(cpk2.keys.get(), Matchers.hasItem(pic));
}
Also used : FindOptions(dev.morphia.query.FindOptions) Datastore(dev.morphia.Datastore) ContainsPic(dev.morphia.test.query.TestQuery.ContainsPic) Pic(dev.morphia.test.query.TestQuery.Pic) UpdateResult(com.mongodb.client.result.UpdateResult) Test(org.testng.annotations.Test)

Example 2 with Datastore

use of dev.morphia.Datastore in project morphia by mongodb.

the class TestUpdateOperations method testUpdateKeyRef.

@Test
public void testUpdateKeyRef() {
    final ContainsPicKey cpk = new ContainsPicKey();
    cpk.name = "cpk one";
    Datastore ds = getDs();
    ds.save(cpk);
    final Pic pic = new Pic();
    pic.setName("fist again");
    ds.save(pic);
    Query<ContainsPicKey> query = ds.find(ContainsPicKey.class).filter(eq("name", cpk.name));
    assertThat(query.update(set("pic", pic)).execute().getModifiedCount(), is(1L));
    // test reading the object.
    final ContainsPicKey cpk2 = ds.find(ContainsPicKey.class).iterator(new FindOptions().limit(1)).next();
    assertThat(cpk2, is(notNullValue()));
    assertThat(cpk.name, is(cpk2.name));
    assertThat(cpk2.pic, is(notNullValue()));
    MatcherAssert.assertThat(pic, CoreMatchers.is(cpk2.pic.get()));
    query.update(set("pic", pic)).execute();
    // test reading the object.
    final ContainsPicKey cpk3 = ds.find(ContainsPicKey.class).iterator(new FindOptions().limit(1)).next();
    assertThat(cpk3, is(notNullValue()));
    assertThat(cpk.name, is(cpk3.name));
    assertThat(cpk3.pic, is(notNullValue()));
    MatcherAssert.assertThat(pic, CoreMatchers.is(cpk3.pic.get()));
}
Also used : FindOptions(dev.morphia.query.FindOptions) Datastore(dev.morphia.Datastore) ContainsPic(dev.morphia.test.query.TestQuery.ContainsPic) Pic(dev.morphia.test.query.TestQuery.Pic) Test(org.testng.annotations.Test)

Example 3 with Datastore

use of dev.morphia.Datastore in project morphia by mongodb.

the class TestUpdateOperations method testMaxKeepsCurrentDocumentValueWhenThisIsLargerThanSuppliedValue.

@Test
public void testMaxKeepsCurrentDocumentValueWhenThisIsLargerThanSuppliedValue() {
    final ObjectId id = new ObjectId();
    final double originalValue = 2D;
    Datastore ds = getDs();
    Query<Circle> query = ds.find(Circle.class).filter(eq("id", id));
    assertInserted(query.update(setOnInsert(Map.of("radius", originalValue))).execute(new UpdateOptions().upsert(true)));
    Assert.assertEquals(query.update(max("radius", 1D)).execute(new UpdateOptions().upsert(true)).getMatchedCount(), 1);
    MatcherAssert.assertThat(ds.find(Circle.class).filter(eq("_id", id)).first().getRadius(), is(originalValue));
}
Also used : Circle(dev.morphia.test.models.Circle) Datastore(dev.morphia.Datastore) ObjectId(org.bson.types.ObjectId) UpdateOptions(dev.morphia.UpdateOptions) Test(org.testng.annotations.Test)

Example 4 with Datastore

use of dev.morphia.Datastore in project morphia by mongodb.

the class TestMapping method testMethodMapping.

@Test
public void testMethodMapping() {
    Datastore datastore = createDatastore(getMongoClient(), TEST_DB_NAME, MapperOptions.builder().propertyDiscovery(PropertyDiscovery.METHODS).build());
    EntityModel model = datastore.getMapper().map(MethodMappedUser.class).get(0);
    assertTrue(model.getProperties().size() > 0);
    assertNotNull(model.getVersionProperty(), model.getProperties().toString());
    assertNotNull(model.getProperty("dateJoined"));
    assertNotNull(model.getProperty("joined"));
    assertNotNull(model.getProperty("friend_reference"));
    assertNotNull(model.getProperty("morphia_reference"));
}
Also used : MethodMappedUser(dev.morphia.test.models.methods.MethodMappedUser) Datastore(dev.morphia.Datastore) Morphia.createDatastore(dev.morphia.Morphia.createDatastore) EntityModel(dev.morphia.mapping.codec.pojo.EntityModel) Test(org.testng.annotations.Test)

Example 5 with Datastore

use of dev.morphia.Datastore in project morphia by mongodb.

the class TestVersioning method testCanMapAnEntityWithAnAbstractVersionedParent.

@Test
public void testCanMapAnEntityWithAnAbstractVersionedParent() {
    Datastore datastore = Morphia.createDatastore(getMongoClient(), TEST_DB_NAME);
    Mapper mapper = datastore.getMapper();
    mapper.map(VersionedChildEntity.class);
    List<EntityModel> mappedEntities = mapper.getMappedEntities();
    assertEquals(mappedEntities.size(), 2, mappedEntities.toString());
    List<Class<?>> list = new ArrayList<>();
    for (EntityModel entityModel : mappedEntities) {
        list.add(entityModel.getType());
    }
    assertTrue(list.contains(VersionedChildEntity.class));
    assertTrue(list.contains(AbstractVersionedBase.class));
}
Also used : Mapper(dev.morphia.mapping.Mapper) VersionedChildEntity(dev.morphia.test.models.versioned.VersionedChildEntity) Datastore(dev.morphia.Datastore) Morphia.createDatastore(dev.morphia.Morphia.createDatastore) EntityModel(dev.morphia.mapping.codec.pojo.EntityModel) ArrayList(java.util.ArrayList) AbstractVersionedBase(dev.morphia.test.models.versioned.AbstractVersionedBase) Test(org.testng.annotations.Test)

Aggregations

Datastore (dev.morphia.Datastore)46 Test (org.testng.annotations.Test)38 Morphia.createDatastore (dev.morphia.Morphia.createDatastore)12 FindOptions (dev.morphia.query.FindOptions)12 ArrayList (java.util.ArrayList)9 Document (org.bson.Document)9 EntityModel (dev.morphia.mapping.codec.pojo.EntityModel)6 UpdateOptions (dev.morphia.UpdateOptions)5 UpdateResult (com.mongodb.client.result.UpdateResult)4 MapperOptions (dev.morphia.mapping.MapperOptions)4 ObjectId (org.bson.types.ObjectId)4 Mapper (dev.morphia.mapping.Mapper)3 DocumentWriter (dev.morphia.mapping.codec.writer.DocumentWriter)3 Circle (dev.morphia.test.models.Circle)3 Rectangle (dev.morphia.test.models.Rectangle)3 Versioned (dev.morphia.test.models.versioned.Versioned)3 Point (com.mongodb.client.model.geojson.Point)2 Position (com.mongodb.client.model.geojson.Position)2 DeleteOptions (dev.morphia.DeleteOptions)2 Key (dev.morphia.Key)2