use of dev.morphia.Datastore in project morphia by mongodb.
the class ClassMethodPair method invoke.
void invoke(Datastore datastore, Document document, Object entity) {
try {
Object instance;
if (type != null) {
instance = getOrCreateInstance(type);
} else {
instance = entity;
}
final Method method = getMethod();
method.setAccessible(true);
Sofia.logCallingLifecycleMethod(event.getSimpleName(), method, instance);
List<Object> args = new ArrayList<>();
for (Class<?> parameterType : method.getParameterTypes()) {
if (parameterType.equals(Document.class)) {
args.add(document);
} else if (parameterType.equals(Datastore.class)) {
args.add(datastore);
} else {
args.add(entity);
}
}
method.invoke(instance, args.toArray());
} catch (ReflectiveOperationException e) {
throw new IllegalStateException(e.getMessage(), e);
}
}
use of dev.morphia.Datastore in project morphia by mongodb.
the class TestUpdateOperations method testRemoveAllList.
@Test
public void testRemoveAllList() {
LogHolder logs = new LogHolder();
Date date = new Date();
logs.logs.addAll(asList(new Log(1), new Log(2), new Log(3), new Log(1), new Log(2), new Log(3)));
Datastore ds = getDs();
ds.save(logs);
UpdateResult results = ds.find(LogHolder.class).update(pullAll("logs", singletonList(new Log(3)))).execute();
Assert.assertEquals(results.getModifiedCount(), 1);
LogHolder updated = ds.find(LogHolder.class).iterator(new FindOptions().limit(1)).next();
Assert.assertEquals(updated.logs.size(), 4);
Assert.assertTrue(updated.logs.stream().allMatch(log -> log.equals(new Log(1)) || log.equals(new Log(2))));
}
use of dev.morphia.Datastore in project morphia by mongodb.
the class TestUpdateOperations method testSetUnset.
@Test
public void testSetUnset() {
Datastore ds = getDs();
final ObjectId key = ds.save(new Circle(1)).getId();
Query<Circle> circle = ds.find(Circle.class).filter(eq("radius", 1D));
assertUpdated(circle.update(set("radius", 2D)).execute(), 1);
Query<Circle> idQuery = ds.find(Circle.class).filter(eq("_id", key));
MatcherAssert.assertThat(idQuery.first().getRadius(), is(2D));
circle = ds.find(Circle.class).filter(eq("radius", 2D));
assertUpdated(circle.update(unset("radius")).execute(new UpdateOptions().multi(false)), 1);
MatcherAssert.assertThat(idQuery.first().getRadius(), is(0D));
Book article = new Book();
ds.save(article);
Query<Book> query = ds.find(Book.class);
query.update(set("title", "Some Title")).execute();
query.update(unset("title")).execute();
}
use of dev.morphia.Datastore in project morphia by mongodb.
the class TestDatastoreMerge method merge.
@Test
public void merge() {
Datastore ds = getDs();
Test1 test1 = new Test1();
test1.name = "foobar";
ds.save(test1);
Test2 test2 = ds.find(Test2.class).first();
Assert.assertNotNull(test2.id);
test2.blarg = "barfoo";
long version = test2.version;
ds.merge(test2);
Assert.assertEquals(version + 1, test2.version);
test1 = ds.find(Test1.class).filter(eq("_id", test1.id)).first();
Assert.assertNotNull(test1.name);
}
use of dev.morphia.Datastore in project morphia by mongodb.
the class TestGenerics method testMethodMappedGenericEntities.
@Test
public void testMethodMappedGenericEntities() {
Datastore datastore = createDatastore(getMongoClient(), TEST_DB_NAME, MapperOptions.builder().propertyDiscovery(PropertyDiscovery.METHODS).build());
EntityModel entityModel = datastore.getMapper().map(MethodMappedSpecializedEntity.class).get(0);
PropertyModel test = entityModel.getProperty("test");
assertEquals(test.getType(), UUID.class);
MethodMappedSpecializedEntity beforeDB = new MethodMappedSpecializedEntity();
beforeDB.setId(UUID.randomUUID());
beforeDB.setTest(UUID.randomUUID());
datastore.save(beforeDB);
MethodMappedSpecializedEntity loaded = datastore.find(MethodMappedSpecializedEntity.class).filter(eq("_id", beforeDB.getId())).first();
assertEquals(loaded.getId(), beforeDB.getId());
assertEquals(loaded.getTest(), beforeDB.getTest());
}
Aggregations