Search in sources :

Example 1 with ValidationException

use of dev.morphia.query.ValidationException in project morphia by mongodb.

the class IndexHelperTest method findField.

@Test
public void findField() {
    getMapper().map(MappedInterface.class, MappedInterfaceImpl.class, AbstractParent.class, IndexedClass.class);
    EntityModel model = getMapper().getEntityModel(IndexedClass.class);
    IndexOptions options = indexOptionsBuilder().build();
    assertEquals(getIndexHelper().findField(model, options, "indexName"), "indexName");
    assertEquals(getIndexHelper().findField(model, options, "nested.name"), "nest.name");
    assertEquals(getIndexHelper().findField(model, options, "nest.name"), "nest.name");
    try {
        assertEquals(getIndexHelper().findField(model, options, "nest.whatsit"), "nest.whatsit");
        fail("Should have failed on the bad index path");
    } catch (ValidationException e) {
    // alles ist gut
    }
    assertEquals(getIndexHelper().findField(model, indexOptionsBuilder().disableValidation(true).build(), "nest.whatsit.nested.more.deeply.than.the.object.model"), "nest.whatsit.nested.more.deeply.than.the.object.model");
}
Also used : ValidationException(dev.morphia.query.ValidationException) IndexOptions(dev.morphia.annotations.IndexOptions) EntityModel(dev.morphia.mapping.codec.pojo.EntityModel) Test(org.testng.annotations.Test)

Example 2 with ValidationException

use of dev.morphia.query.ValidationException in project morphia by mongodb.

the class TestQuery method testProject.

@Test
public void testProject() {
    getDs().save(new ContainsRenamedFields("Frank", "Zappa"));
    ContainsRenamedFields found = getDs().find(ContainsRenamedFields.class).iterator(new FindOptions().projection().include("first_name").limit(1)).tryNext();
    assertNotNull(found.firstName);
    assertNull(found.lastName);
    found = getDs().find(ContainsRenamedFields.class).iterator(new FindOptions().projection().include("first_name").limit(1)).tryNext();
    assertNotNull(found.firstName);
    assertNull(found.lastName);
    try {
        getDs().find(ContainsRenamedFields.class).iterator(new FindOptions().projection().include("bad field name").limit(1)).tryNext();
        fail("Validation should have caught the bad field");
    } catch (ValidationException e) {
    // success!
    }
    Query<ContainsRenamedFields> query = getDs().find(ContainsRenamedFields.class);
}
Also used : FindOptions(dev.morphia.query.FindOptions) ValidationException(dev.morphia.query.ValidationException) Test(org.testng.annotations.Test)

Example 3 with ValidationException

use of dev.morphia.query.ValidationException in project morphia by mongodb.

the class TestQuery method testRetrievedFields.

@Test
public void testRetrievedFields() {
    getDs().save(new ContainsRenamedFields("Frank", "Zappa"));
    ContainsRenamedFields found = getDs().find(ContainsRenamedFields.class).iterator(new FindOptions().projection().include("first_name").limit(1)).tryNext();
    assertNotNull(found.firstName);
    assertNull(found.lastName);
    found = getDs().find(ContainsRenamedFields.class).iterator(new FindOptions().projection().include("firstName").limit(1)).tryNext();
    assertNotNull(found.firstName);
    assertNull(found.lastName);
    try {
        getDs().find(ContainsRenamedFields.class).iterator(new FindOptions().projection().include("bad field name").limit(1)).tryNext();
        fail("Validation should have caught the bad field");
    } catch (ValidationException e) {
    // success!
    }
}
Also used : FindOptions(dev.morphia.query.FindOptions) ValidationException(dev.morphia.query.ValidationException) Test(org.testng.annotations.Test)

Example 4 with ValidationException

use of dev.morphia.query.ValidationException in project morphia by mongodb.

the class TestLegacyQuery method testMixedProjection.

@Test
public void testMixedProjection() {
    getDs().save(new ContainsRenamedFields("Frank", "Zappa"));
    try {
        getDs().find(ContainsRenamedFields.class).execute(new FindOptions().projection().include("first_name").projection().exclude("last_name"));
        fail("An exception should have been thrown indication a mixed projection");
    } catch (ValidationException e) {
    // all good
    }
    try {
        getDs().find(ContainsRenamedFields.class).execute(new FindOptions().projection().include("first_name", "last_name").projection().exclude("_id"));
    } catch (ValidationException e) {
        fail("An exception should not have been thrown indication a mixed projection because _id suppression is a special case");
    }
    try {
        getDs().find(ContainsRenamedFields.class).execute(new FindOptions().projection().exclude("first_name", "last_name").projection().include("_id"));
        fail("An exception should have been thrown indication a mixed projection");
    } catch (ValidationException e) {
    // all good
    }
    try {
        getDs().find(IntVector.class).execute(new FindOptions().projection().exclude("name").projection().project("scalars", new ArraySlice(5)));
        fail("An exception should have been thrown indication a mixed projection");
    } catch (ValidationException e) {
    // all good
    }
}
Also used : FindOptions(dev.morphia.query.FindOptions) ValidationException(dev.morphia.query.ValidationException) ArraySlice(dev.morphia.query.ArraySlice) Test(org.testng.annotations.Test)

Example 5 with ValidationException

use of dev.morphia.query.ValidationException in project morphia by mongodb.

the class TestLegacyQuery method testProject.

@Test
public void testProject() {
    getDs().save(new ContainsRenamedFields("Frank", "Zappa"));
    ContainsRenamedFields found = getDs().find(ContainsRenamedFields.class).execute(new FindOptions().projection().include("first_name").limit(1)).tryNext();
    assertNotNull(found.firstName);
    assertNull(found.lastName);
    found = getDs().find(ContainsRenamedFields.class).execute(new FindOptions().projection().include("first_name").limit(1)).tryNext();
    assertNotNull(found.firstName);
    assertNull(found.lastName);
    try {
        getDs().find(ContainsRenamedFields.class).execute(new FindOptions().projection().include("bad field name").limit(1)).tryNext();
        fail("Validation should have caught the bad field");
    } catch (ValidationException e) {
    // success!
    }
}
Also used : FindOptions(dev.morphia.query.FindOptions) ValidationException(dev.morphia.query.ValidationException) Test(org.testng.annotations.Test)

Aggregations

ValidationException (dev.morphia.query.ValidationException)6 Test (org.testng.annotations.Test)6 FindOptions (dev.morphia.query.FindOptions)5 ArraySlice (dev.morphia.query.ArraySlice)2 IndexOptions (dev.morphia.annotations.IndexOptions)1 EntityModel (dev.morphia.mapping.codec.pojo.EntityModel)1