Search in sources :

Example 11 with Datastore

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

the class TestEnumMapping method testCustomerWithArrayList.

@Test
public void testCustomerWithArrayList() {
    MapperOptions options = MapperOptions.builder(getMapper().getOptions()).storeEmpties(true).storeNulls(true).build();
    final Datastore datastore = Morphia.createDatastore(getMongoClient(), getDatabase().getName(), options);
    Mapper mapper = datastore.getMapper();
    mapper.map(CustomerWithArrayList.class);
    CustomerWithArrayList customer = new CustomerWithArrayList();
    List<WebTemplate> templates1 = new ArrayList<>();
    templates1.add(new WebTemplate("template #1.1"));
    templates1.add(new WebTemplate("template #1.2"));
    customer.add(WebTemplateType.CrewContract, templates1);
    List<WebTemplate> templates2 = new ArrayList<>();
    templates1.add(new WebTemplate("template #2.1"));
    templates1.add(new WebTemplate("template #2.2"));
    customer.add(WebTemplateType.CrewContractHeader, templates2);
    getDs().save(customer);
    final Datastore datastore1 = getDs();
    CustomerWithArrayList loaded = datastore1.find(CustomerWithArrayList.class).filter(eq("_id", customer.id)).first();
    assertEquals(customer.mapWithArrayList, loaded.mapWithArrayList);
}
Also used : MapperOptions(dev.morphia.mapping.MapperOptions) Mapper(dev.morphia.mapping.Mapper) Datastore(dev.morphia.Datastore) ArrayList(java.util.ArrayList) Test(org.testng.annotations.Test)

Example 12 with Datastore

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

the class TestMapperOptions method lowercaseDefaultCollection.

@Test
public void lowercaseDefaultCollection() {
    DummyEntity entity = new DummyEntity();
    String collectionName = getMapper().getEntityModel(entity.getClass()).getCollectionName();
    assertEquals(collectionName, "dummyEntity", "uppercase");
    Builder builder = MapperOptions.builder(getMapper().getOptions());
    final Datastore datastore = Morphia.createDatastore(getMongoClient(), getDatabase().getName(), builder.collectionNaming(NamingStrategy.lowerCase()).build());
    collectionName = datastore.getMapper().getEntityModel(entity.getClass()).getCollectionName();
    assertEquals(collectionName, "dummyentity", "lowercase");
}
Also used : Datastore(dev.morphia.Datastore) Builder(dev.morphia.mapping.MapperOptions.Builder) Test(org.testng.annotations.Test)

Example 13 with Datastore

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

the class TestGeoQueries method minDistance.

@Test
public void minDistance() {
    // given
    Datastore datastore = getDs();
    City london = new City("London", new Point(new Position(51.5286416, -0.1015987)));
    datastore.save(london);
    datastore.save(List.of(new City("Manchester", new Point(new Position(53.4722454, -2.2235922))), new City("Sevilla", new Point(new Position(37.3753708, -5.9550582)))));
    getDs().ensureIndexes();
    final Point searchPoint = new Point(new Position(50, 0.1278));
    assertThat(datastore.find(City.class).filter(near("location", searchPoint).maxDistance(200000D).minDistance(195000D)).iterator().toList().size(), is(0));
    assertThat(datastore.find(City.class).filter(nearSphere("location", searchPoint).maxDistance(200000D).minDistance(195000D)).iterator().toList().size(), is(0));
}
Also used : Datastore(dev.morphia.Datastore) Position(com.mongodb.client.model.geojson.Position) Point(com.mongodb.client.model.geojson.Point) Test(org.testng.annotations.Test)

Example 14 with Datastore

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

the class TestClassLoader method recreateCollection.

private MongoCollection<BasicEntity> recreateCollection(ClassLoader classLoader) {
    MapperOptions options = MapperOptions.builder().discriminator(DiscriminatorFunction.className()).classLoader(classLoader).build();
    Datastore datastore = Morphia.createDatastore(getMongoClient(), TEST_DB_NAME, options);
    return datastore.getCollection(BasicEntity.class);
}
Also used : MapperOptions(dev.morphia.mapping.MapperOptions) Datastore(dev.morphia.Datastore)

Example 15 with Datastore

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

the class TestEnumMapping method testCustomerWithList.

@Test
public void testCustomerWithList() {
    MapperOptions options = MapperOptions.builder(getMapper().getOptions()).storeEmpties(true).storeNulls(true).build();
    final Datastore datastore = Morphia.createDatastore(getMongoClient(), getDatabase().getName(), options);
    Mapper mapper = datastore.getMapper();
    mapper.map(CustomerWithArrayList.class);
    CustomerWithList customer = new CustomerWithList();
    List<WebTemplate> templates1 = new ArrayList<>();
    templates1.add(new WebTemplate("template #1.1"));
    templates1.add(new WebTemplate("template #1.2"));
    customer.add(WebTemplateType.CrewContract, templates1);
    List<WebTemplate> templates2 = new ArrayList<>();
    templates1.add(new WebTemplate("template #2.1"));
    templates1.add(new WebTemplate("template #2.2"));
    customer.add(WebTemplateType.CrewContractHeader, templates2);
    getDs().save(customer);
    final Datastore datastore1 = getDs();
    CustomerWithList loaded = datastore1.find(CustomerWithList.class).filter(eq("_id", customer.id)).first();
    assertEquals(customer.mapWithList, loaded.mapWithList);
}
Also used : MapperOptions(dev.morphia.mapping.MapperOptions) Mapper(dev.morphia.mapping.Mapper) Datastore(dev.morphia.Datastore) ArrayList(java.util.ArrayList) 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