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));
}
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()));
}
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));
}
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"));
}
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));
}
Aggregations