Search in sources :

Example 6 with ConstraintViolationException

use of org.qi4j.api.constraint.ConstraintViolationException in project qi4j-sdk by Qi4j.

the class PropertyConstraintTest method givenConstraintOnPropertyWhenInvalidValueThenThrowException.

@org.junit.Test
public void givenConstraintOnPropertyWhenInvalidValueThenThrowException() throws Throwable {
    TransientBuilder<Test> builder = module.newTransientBuilder(Test.class);
    builder.prototype().test().set("XXXXXX");
    Test test = builder.newInstance();
    try {
        test.test().set("YY");
        fail("Should have thrown a ConstraintViolationException.");
    } catch (ConstraintViolationException e) {
        Collection<ConstraintViolation> violations = e.constraintViolations();
        assertEquals(2, violations.size());
    }
}
Also used : AbstractQi4jTest(org.qi4j.test.AbstractQi4jTest) ConstraintViolationException(org.qi4j.api.constraint.ConstraintViolationException) Collection(java.util.Collection)

Example 7 with ConstraintViolationException

use of org.qi4j.api.constraint.ConstraintViolationException in project qi4j-sdk by Qi4j.

the class ConstraintTest method testNotEmptyFail.

@Test
public void testNotEmptyFail() {
    TransientBuilder<TestCaseComposite> cb = module.newTransientBuilder(TestCaseComposite.class);
    try {
        cb.prototype().notEmptyString().set("");
        fail("Should have thrown exception");
    } catch (ConstraintViolationException e) {
    }
    try {
        cb.prototype().notEmptyCollection().set(new ArrayList());
        fail("Should have thrown exception");
    } catch (ConstraintViolationException e) {
    }
    try {
        cb.prototype().notEmptyList().set(new ArrayList());
        fail("Should have thrown exception");
    } catch (ConstraintViolationException e) {
    }
}
Also used : ArrayList(java.util.ArrayList) ConstraintViolationException(org.qi4j.api.constraint.ConstraintViolationException) AbstractQi4jTest(org.qi4j.test.AbstractQi4jTest) Test(org.junit.Test)

Example 8 with ConstraintViolationException

use of org.qi4j.api.constraint.ConstraintViolationException in project qi4j-sdk by Qi4j.

the class HelloWorldCompositeTest method testComposite.

@Test
public void testComposite() {
    SingletonAssembler assembler = new SingletonAssembler() {

        @Override
        public void assemble(ModuleAssembly module) throws AssemblyException {
            module.transients(HelloWorldComposite.class, HelloWorldComposite2.class).withMixins(ScalaTraitMixin.class).withConcerns(ExclamationGenericConcern.class);
        }
    };
    HelloWorldComposite composite = assembler.module().newTransient(HelloWorldComposite.class);
    Assert.assertEquals("Do stuff!", composite.doStuff());
    Assert.assertEquals("Hello there World!", composite.sayHello("World"));
    try {
        composite.sayHello("AReallyReallyLongName");
    } catch (ConstraintViolationException e) {
    // Ok!
    }
    HelloWorldComposite2 composite2 = assembler.module().newTransient(HelloWorldComposite2.class);
    Assert.assertEquals("Do custom stuff!", composite2.doStuff());
}
Also used : ModuleAssembly(org.qi4j.bootstrap.ModuleAssembly) ScalaTraitMixin(org.qi4j.lang.scala.ScalaTraitMixin) SingletonAssembler(org.qi4j.bootstrap.SingletonAssembler) ConstraintViolationException(org.qi4j.api.constraint.ConstraintViolationException) Test(org.junit.Test)

Example 9 with ConstraintViolationException

use of org.qi4j.api.constraint.ConstraintViolationException in project qi4j-sdk by Qi4j.

the class ContextResource method handleException.

private void handleException(Response response, Throwable ex) {
    while (ex instanceof InvocationTargetException) {
        ex = ex.getCause();
    }
    try {
        throw ex;
    } catch (ResourceException e) {
        // IAE (or subclasses) are considered client faults
        response.setEntity(new StringRepresentation(e.getMessage()));
        response.setStatus(e.getStatus());
    } catch (ConstraintViolationException e) {
        try {
            ConstraintViolationMessages cvm = new ConstraintViolationMessages();
            // CVE are considered client faults
            String messages = "";
            Locale locale = ObjectSelection.type(Locale.class);
            for (ConstraintViolation constraintViolation : e.constraintViolations()) {
                if (!messages.isEmpty()) {
                    messages += "\n";
                }
                messages += cvm.getMessage(constraintViolation, locale);
            }
            response.setEntity(new StringRepresentation(messages));
            response.setStatus(Status.CLIENT_ERROR_UNPROCESSABLE_ENTITY);
        } catch (Exception e1) {
            response.setEntity(new StringRepresentation(e.getMessage()));
            response.setStatus(Status.CLIENT_ERROR_UNPROCESSABLE_ENTITY);
        }
    } catch (IllegalArgumentException e) {
        // IAE (or subclasses) are considered client faults
        response.setEntity(new StringRepresentation(e.getMessage()));
        response.setStatus(Status.CLIENT_ERROR_UNPROCESSABLE_ENTITY);
    } catch (RuntimeException e) {
        // RuntimeExceptions are considered server faults
        LoggerFactory.getLogger(getClass()).warn("Exception thrown during processing", e);
        response.setEntity(new StringRepresentation(e.getMessage()));
        response.setStatus(Status.SERVER_ERROR_INTERNAL);
    } catch (Exception e) {
        // Checked exceptions are considered client faults
        String s = e.getMessage();
        if (s == null) {
            s = e.getClass().getSimpleName();
        }
        response.setEntity(new StringRepresentation(s));
        response.setStatus(Status.CLIENT_ERROR_UNPROCESSABLE_ENTITY);
    } catch (Throwable e) {
        // Anything else are considered server faults
        LoggerFactory.getLogger(getClass()).error("Exception thrown during processing", e);
        response.setEntity(new StringRepresentation(e.getMessage()));
        response.setStatus(Status.SERVER_ERROR_INTERNAL);
    }
}
Also used : Locale(java.util.Locale) StringRepresentation(org.restlet.representation.StringRepresentation) ConstraintViolation(org.qi4j.api.constraint.ConstraintViolation) ConstraintViolationException(org.qi4j.api.constraint.ConstraintViolationException) ConstraintViolationMessages(org.qi4j.library.rest.server.restlet.ConstraintViolationMessages) ResourceException(org.restlet.resource.ResourceException) InvocationTargetException(java.lang.reflect.InvocationTargetException) EntityTypeNotFoundException(org.qi4j.api.unitofwork.EntityTypeNotFoundException) ResourceException(org.restlet.resource.ResourceException) InvocationTargetException(java.lang.reflect.InvocationTargetException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NoSuchEntityException(org.qi4j.api.unitofwork.NoSuchEntityException) ConstraintViolationException(org.qi4j.api.constraint.ConstraintViolationException)

Aggregations

ConstraintViolationException (org.qi4j.api.constraint.ConstraintViolationException)9 AccessibleObject (java.lang.reflect.AccessibleObject)3 Member (java.lang.reflect.Member)3 Type (java.lang.reflect.Type)3 TypeVariable (java.lang.reflect.TypeVariable)3 Test (org.junit.Test)3 GenericAssociationInfo (org.qi4j.api.association.GenericAssociationInfo)3 QualifiedName (org.qi4j.api.common.QualifiedName)3 AbstractQi4jTest (org.qi4j.test.AbstractQi4jTest)3 Collection (java.util.Collection)2 ConstraintViolation (org.qi4j.api.constraint.ConstraintViolation)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Locale (java.util.Locale)1 OgnlContext (ognl.OgnlContext)1 OgnlException (ognl.OgnlException)1 Association (org.qi4j.api.association.Association)1 ManyAssociation (org.qi4j.api.association.ManyAssociation)1