Search in sources :

Example 26 with Datastore

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);
}
Also used : MethodMappedFriend(dev.morphia.test.models.methods.MethodMappedFriend) MethodMappedUser(dev.morphia.test.models.methods.MethodMappedUser) Datastore(dev.morphia.Datastore) Morphia.createDatastore(dev.morphia.Morphia.createDatastore) Test(org.testng.annotations.Test)

Example 27 with Datastore

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());
    }
}
Also used : Datastore(dev.morphia.Datastore) Morphia.createDatastore(dev.morphia.Morphia.createDatastore) FacebookUser(dev.morphia.test.models.FacebookUser) Test(org.testng.annotations.Test)

Example 28 with Datastore

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");
}
Also used : Datastore(dev.morphia.Datastore) Morphia.createDatastore(dev.morphia.Morphia.createDatastore) Test(org.testng.annotations.Test)

Example 29 with Datastore

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

Example 30 with Datastore

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);
}
Also used : Datastore(dev.morphia.Datastore) 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