use of dev.morphia.Datastore in project morphia by mongodb.
the class TestReferences method testMethodMapping.
@Test
public void testMethodMapping() {
Datastore datastore = createDatastore(getMongoClient(), TEST_DB_NAME, MapperOptions.builder().propertyDiscovery(PropertyDiscovery.METHODS).build());
datastore.getMapper().map(MethodMappedUser.class);
MethodMappedUser user = new MethodMappedUser();
MethodMappedFriend friend = new MethodMappedFriend();
user.setFriend(friend);
user.setFriends(MorphiaReference.wrap(List.of(friend)));
datastore.save(List.of(friend, user));
MethodMappedUser loaded = datastore.find(MethodMappedUser.class).first();
assertFalse(loaded.getFriends().isResolved());
assertEquals(loaded.getFriend(), friend);
assertEquals(loaded.getFriends().get().get(0), friend);
assertEquals(loaded, user);
}
use of dev.morphia.Datastore in project morphia by mongodb.
the class TestReferences method testMultipleDatabasesSingleThreaded.
@Test
public void testMultipleDatabasesSingleThreaded() {
getMapper().map(List.of(FacebookUser.class));
final Datastore ds1 = createDatastore(getMongoClient(), "db1");
final Datastore ds2 = createDatastore(getMongoClient(), "db2");
final FacebookUser db1Friend = new FacebookUser(3, "DB1 FaceBook Friend");
ds1.save(db1Friend);
final FacebookUser db1User = new FacebookUser(1, "DB1 FaceBook User");
db1User.friends.add(db1Friend);
ds1.save(db1User);
final FacebookUser db2Friend = new FacebookUser(4, "DB2 FaceBook Friend");
ds2.save(db2Friend);
final FacebookUser db2User = new FacebookUser(2, "DB2 FaceBook User");
db2User.friends.add(db2Friend);
ds2.save(db2User);
testFirstDatastore(ds1);
testSecondDatastore(ds2);
for (int i = 1; i <= 4; i++) {
assertNull(getDs().find(FacebookUser.class).filter(eq("id", i)).first());
}
}
use of dev.morphia.Datastore in project morphia by mongodb.
the class TestReferences method testLazyWithParent.
@Test
public void testLazyWithParent() {
Datastore datastore = getDs();
datastore.getMapper().map(Entity1.class, Entity2.class, EntityBase.class);
Entity1 entity1 = new Entity1("entity1");
datastore.save(List.of(entity1, new Entity2("entity2", entity1)));
var entities = datastore.find(Entity2.class).iterator().toList();
assertNotNull(entities.get(0));
Entity1 reference = entities.get(0).getReference();
assertNotNull(reference);
assertEquals(reference.getName(), "entity1", "name should cause a fetch");
assertNotNull(reference.getId(), "ID shouldn't be null");
}
use of dev.morphia.Datastore in project morphia by mongodb.
the class TestLazyCircularReference method testGetKeyWithoutFetching.
public void testGetKeyWithoutFetching() {
checkForProxyTypes();
RootEntity root = new RootEntity();
final ReferencedEntity reference = new ReferencedEntity();
reference.parent = root;
root.r = reference;
reference.setFoo("bar");
final ObjectId id = getDs().save(reference).getId();
getDs().save(root);
final Datastore datastore = getDs();
root = datastore.find(RootEntity.class).filter(eq("_id", root.getId())).first();
final ReferencedEntity p = root.r;
assertIsProxy(p);
assertNotFetched(p);
p.getFoo();
assertFetched(p);
}
use of dev.morphia.Datastore in project morphia by mongodb.
the class ByteMappingTest method testMapping.
@Test
public void testMapping() {
getMapper().map(Bytes.class);
final Bytes ent = new Bytes();
ent.listWrapperArray.add(new Byte[] { 1, 2 });
ent.listPrimitiveArray.add(new byte[] { 2, 3, 12 });
ent.listWrapper.addAll(Arrays.asList((byte) 148, (byte) 6, (byte) 255));
ent.singlePrimitive = 100;
ent.singleWrapper = 47;
ent.primitiveArray = new byte[] { 5, 93 };
ent.wrapperArray = new Byte[] { 55, 16, 99 };
ent.nestedPrimitiveArray = new byte[][] { { 1, 2 }, { 3, 4 } };
ent.nestedWrapperArray = new Byte[][] { { 1, 2 }, { 3, 4 } };
getDs().save(ent);
final Datastore datastore = getDs();
final Bytes loaded = datastore.find(Bytes.class).filter(eq("_id", ent.id)).first();
Assert.assertNotNull(loaded.id);
Assert.assertEquals(loaded.listWrapperArray.get(0), ent.listWrapperArray.get(0));
Assert.assertEquals(loaded.listPrimitiveArray.get(0), ent.listPrimitiveArray.get(0));
Assert.assertEquals(loaded.listWrapper, ent.listWrapper);
Assert.assertEquals(loaded.singlePrimitive, ent.singlePrimitive, 0);
Assert.assertEquals(loaded.singleWrapper, ent.singleWrapper, 0);
Assert.assertEquals(loaded.primitiveArray, ent.primitiveArray);
Assert.assertEquals(loaded.wrapperArray, ent.wrapperArray);
Assert.assertEquals(loaded.nestedPrimitiveArray, ent.nestedPrimitiveArray);
Assert.assertEquals(loaded.nestedWrapperArray, ent.nestedWrapperArray);
}
Aggregations