Search in sources :

Example 1 with User

use of dev.morphia.test.models.User in project morphia by mongodb.

the class AggregationTest method testDateToString.

@Test
public void testDateToString() {
    LocalDate joined = LocalDate.parse("2016-05-01 UTC", DateTimeFormatter.ofPattern("yyyy-MM-dd z"));
    getDs().save(new User("John Doe", joined));
    Aggregation<User> pipeline = getDs().aggregate(User.class).project(project().include("string", dateToString().format("%Y-%m-%d").date(field("joined"))));
    for (Iterator<StringDates> it = pipeline.execute(StringDates.class); it.hasNext(); ) {
        Assert.assertEquals(it.next().string, "2016-05-01");
    }
}
Also used : User(dev.morphia.test.models.User) LocalDate(java.time.LocalDate) Test(org.testng.annotations.Test)

Example 2 with User

use of dev.morphia.test.models.User in project morphia by mongodb.

the class AggregationTest method testUnwind.

@Test
public void testUnwind() {
    DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    getDs().save(asList(new User("jane", LocalDate.parse("2011-03-02", format), "golf", "racquetball"), new User("joe", LocalDate.parse("2012-07-02", format), "tennis", "golf", "swimming"), new User("john", LocalDate.parse("2012-07-02", format))));
    Iterator<User> aggregate = getDs().aggregate(User.class).project(project().include("name").include("joined").include("likes")).unwind(unwind("likes")).execute(User.class);
    int count = 0;
    while (aggregate.hasNext()) {
        User user = aggregate.next();
        switch(count) {
            case 0:
                Assert.assertEquals(user.name, "jane");
                Assert.assertEquals(user.likes.get(0), "golf");
                break;
            case 1:
                Assert.assertEquals(user.name, "jane");
                Assert.assertEquals(user.likes.get(0), "racquetball");
                break;
            case 2:
                Assert.assertEquals(user.name, "joe");
                Assert.assertEquals(user.likes.get(0), "tennis");
                break;
            case 3:
                Assert.assertEquals(user.name, "joe");
                Assert.assertEquals(user.likes.get(0), "golf");
                break;
            case 4:
                Assert.assertEquals(user.name, "joe");
                Assert.assertEquals(user.likes.get(0), "swimming");
                break;
            default:
                Assert.fail("Should only find 5 elements");
        }
        count++;
    }
    aggregate = getDs().aggregate(User.class).project(project().include("name").include("joined").include("likes")).unwind(unwind("likes").preserveNullAndEmptyArrays(true)).execute(User.class);
    count = 0;
    while (aggregate.hasNext()) {
        User user = aggregate.next();
        switch(count) {
            case 0:
                Assert.assertEquals(user.name, "jane");
                Assert.assertEquals(user.likes.get(0), "golf");
                break;
            case 1:
                Assert.assertEquals(user.name, "jane");
                Assert.assertEquals(user.likes.get(0), "racquetball");
                break;
            case 2:
                Assert.assertEquals(user.name, "joe");
                Assert.assertEquals(user.likes.get(0), "tennis");
                break;
            case 3:
                Assert.assertEquals(user.name, "joe");
                Assert.assertEquals(user.likes.get(0), "golf");
                break;
            case 4:
                Assert.assertEquals(user.name, "joe");
                Assert.assertEquals(user.likes.get(0), "swimming");
                break;
            case 5:
                Assert.assertEquals(user.name, "john");
                Assert.assertNull(user.likes);
                break;
            default:
                Assert.fail("Should only find 6 elements");
        }
        count++;
    }
}
Also used : User(dev.morphia.test.models.User) DateTimeFormatter(java.time.format.DateTimeFormatter) Point(com.mongodb.client.model.geojson.Point) Test(org.testng.annotations.Test)

Example 3 with User

use of dev.morphia.test.models.User in project morphia by mongodb.

the class TestTransactions method manual.

@Test
public void manual() {
    boolean success = false;
    int count = 0;
    while (!success && count < 5) {
        try (MorphiaSession session = getDs().startSession()) {
            session.startTransaction();
            Rectangle rectangle = new Rectangle(1, 1);
            session.save(rectangle);
            session.save(new User("transactions", LocalDate.now()));
            assertNull(getDs().find(Rectangle.class).first());
            assertNull(getDs().find(User.class).first());
            assertNotNull(session.find(Rectangle.class).first());
            assertNotNull(session.find(User.class).first());
            session.commitTransaction();
            success = true;
        } catch (MongoQueryException e) {
            if (e.getErrorCode() == 251 && e.getErrorMessage().contains("has been aborted")) {
                count++;
            } else {
                throw e;
            }
        } catch (RuntimeException e) {
            throw e;
        }
    }
    assertNotNull(getDs().find(Rectangle.class).first());
    assertNotNull(getDs().find(User.class).first());
}
Also used : User(dev.morphia.test.models.User) MorphiaSession(dev.morphia.experimental.MorphiaSession) Rectangle(dev.morphia.test.models.Rectangle) MongoQueryException(com.mongodb.MongoQueryException) Test(org.testng.annotations.Test)

Example 4 with User

use of dev.morphia.test.models.User in project morphia by mongodb.

the class TestUpdateOperations method testMinWithDates.

@Test
public void testMinWithDates() {
    List<User> entities = List.of(new User("User 1", LocalDate.of(2003, 7, 13)), new User("User 2", LocalDate.of(2009, 12, 1)), new User("User 3", LocalDate.of(2015, 8, 19)));
    getDs().save(entities);
    UpdateResult updated = getDs().find(User.class).update(min("joined", LocalDate.of(1985, 10, 12))).execute(new UpdateOptions().multi(true));
    assertEquals(updated.getModifiedCount(), 3);
    getDs().find(User.class).delete();
    getDs().save(entities);
    updated = getDs().find(User.class).update(min("joined", Instant.now().minus(5000, DAYS))).execute(new UpdateOptions().multi(true));
    assertEquals(updated.getModifiedCount(), 2);
    getDs().find(User.class).delete();
    getDs().save(entities);
    Calendar instance = Calendar.getInstance();
    instance.set(86, Calendar.MAY, 13);
    Date date = instance.getTime();
    updated = getDs().find(User.class).update(min("joined", date)).execute(new UpdateOptions().multi(true));
    assertEquals(updated.getModifiedCount(), 3);
}
Also used : User(dev.morphia.test.models.User) Calendar(java.util.Calendar) UpdateResult(com.mongodb.client.result.UpdateResult) UpdateOptions(dev.morphia.UpdateOptions) Date(java.util.Date) LocalDate(java.time.LocalDate) UpdateOperators.currentDate(dev.morphia.query.experimental.updates.UpdateOperators.currentDate) Test(org.testng.annotations.Test)

Example 5 with User

use of dev.morphia.test.models.User in project morphia by mongodb.

the class TestDocumentValidation method testBypassDocumentValidation.

@Test
public void testBypassDocumentValidation() {
    getMapper().map(User.class);
    getDs().enableDocumentValidation();
    final User user = new User("Jim Halpert", LocalDate.now());
    user.age = 5;
    try {
        getDs().save(user);
        fail("Document validation should have rejected the document");
    } catch (MongoWriteException ignored) {
    }
    getDs().save(user, new InsertOneOptions().bypassDocumentValidation(true));
    assertEquals(getDs().find(User.class).count(), 1);
}
Also used : User(dev.morphia.test.models.User) MongoWriteException(com.mongodb.MongoWriteException) InsertOneOptions(dev.morphia.InsertOneOptions) Test(org.testng.annotations.Test)

Aggregations

User (dev.morphia.test.models.User)16 Test (org.testng.annotations.Test)14 Document (org.bson.Document)5 FindOptions (dev.morphia.query.FindOptions)4 LocalDate (java.time.LocalDate)4 UpdateResult (com.mongodb.client.result.UpdateResult)2 UpdateOptions (dev.morphia.UpdateOptions)2 UpdateOperators.currentDate (dev.morphia.query.experimental.updates.UpdateOperators.currentDate)2 Rectangle (dev.morphia.test.models.Rectangle)2 Calendar (java.util.Calendar)2 Date (java.util.Date)2 BeforeMethod (org.testng.annotations.BeforeMethod)2 MongoQueryException (com.mongodb.MongoQueryException)1 MongoWriteException (com.mongodb.MongoWriteException)1 Point (com.mongodb.client.model.geojson.Point)1 InsertOneOptions (dev.morphia.InsertOneOptions)1 MorphiaSession (dev.morphia.experimental.MorphiaSession)1 StringDates (dev.morphia.test.aggregation.experimental.model.StringDates)1 DateTimeFormatter (java.time.format.DateTimeFormatter)1