Search in sources :

Example 11 with InvalidEntityException

use of org.apache.syncope.core.persistence.api.attrvalue.validation.InvalidEntityException in project syncope by apache.

the class RestServiceExceptionMapper method processInvalidEntityExceptions.

private ResponseBuilder processInvalidEntityExceptions(final Exception ex) {
    InvalidEntityException iee = null;
    if (ex instanceof InvalidEntityException) {
        iee = (InvalidEntityException) ex;
    }
    if (ex instanceof TransactionSystemException && ex.getCause() instanceof RollbackException && ex.getCause().getCause() instanceof InvalidEntityException) {
        iee = (InvalidEntityException) ex.getCause().getCause();
    }
    if (iee != null) {
        ClientExceptionType exType;
        if (iee.getEntityClassSimpleName().endsWith("Policy")) {
            exType = ClientExceptionType.InvalidPolicy;
        } else if (iee.getEntityClassSimpleName().equals(PlainAttr.class.getSimpleName())) {
            exType = ClientExceptionType.InvalidValues;
        } else {
            try {
                exType = ClientExceptionType.valueOf("Invalid" + iee.getEntityClassSimpleName());
            } catch (IllegalArgumentException e) {
                // ignore
                exType = ClientExceptionType.InvalidEntity;
            }
        }
        ResponseBuilder builder = Response.status(Response.Status.BAD_REQUEST);
        builder.header(RESTHeaders.ERROR_CODE, exType.name());
        ErrorTO error = new ErrorTO();
        error.setStatus(exType.getResponseStatus().getStatusCode());
        error.setType(exType);
        for (Map.Entry<Class<?>, Set<EntityViolationType>> violation : iee.getViolations().entrySet()) {
            for (EntityViolationType violationType : violation.getValue()) {
                builder.header(RESTHeaders.ERROR_INFO, exType.getInfoHeaderValue(violationType.name() + ": " + violationType.getMessage()));
                error.getElements().add(violationType.name() + ": " + violationType.getMessage());
            }
        }
        return builder;
    }
    return null;
}
Also used : ErrorTO(org.apache.syncope.common.lib.to.ErrorTO) Set(java.util.Set) ClientExceptionType(org.apache.syncope.common.lib.types.ClientExceptionType) TransactionSystemException(org.springframework.transaction.TransactionSystemException) RollbackException(javax.persistence.RollbackException) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) HashMap(java.util.HashMap) Map(java.util.Map) InvalidEntityException(org.apache.syncope.core.persistence.api.attrvalue.validation.InvalidEntityException) EntityViolationType(org.apache.syncope.common.lib.types.EntityViolationType)

Example 12 with InvalidEntityException

use of org.apache.syncope.core.persistence.api.attrvalue.validation.InvalidEntityException in project syncope by apache.

the class ConfTest method issueSYNCOPE418.

@Test
public void issueSYNCOPE418() {
    try {
        PlainSchema failing = entityFactory.newEntity(PlainSchema.class);
        failing.setKey("http://schemas.examples.org/security/authorization/organizationUnit");
        failing.setType(AttrSchemaType.String);
        plainSchemaDAO.save(failing);
        fail("This should not happen");
    } catch (InvalidEntityException e) {
        assertTrue(e.hasViolation(EntityViolationType.InvalidKey));
    }
}
Also used : PlainSchema(org.apache.syncope.core.persistence.api.entity.PlainSchema) InvalidEntityException(org.apache.syncope.core.persistence.api.attrvalue.validation.InvalidEntityException) Test(org.junit.jupiter.api.Test) AbstractTest(org.apache.syncope.core.persistence.jpa.AbstractTest)

Example 13 with InvalidEntityException

use of org.apache.syncope.core.persistence.api.attrvalue.validation.InvalidEntityException in project syncope by apache.

the class RealmTest method saveNullParent.

@Test
public void saveNullParent() {
    Realm realm = entityFactory.newEntity(Realm.class);
    realm.setName("name");
    realm.setParent(null);
    try {
        realmDAO.save(realm);
        fail("This should not happen");
    } catch (InvalidEntityException e) {
        assertTrue(e.hasViolation(EntityViolationType.InvalidRealm));
    }
}
Also used : Realm(org.apache.syncope.core.persistence.api.entity.Realm) InvalidEntityException(org.apache.syncope.core.persistence.api.attrvalue.validation.InvalidEntityException) Test(org.junit.jupiter.api.Test) AbstractTest(org.apache.syncope.core.persistence.jpa.AbstractTest)

Example 14 with InvalidEntityException

use of org.apache.syncope.core.persistence.api.attrvalue.validation.InvalidEntityException in project syncope by apache.

the class RealmTest method saveInvalidName.

@Test
public void saveInvalidName() {
    Realm realm = entityFactory.newEntity(Realm.class);
    realm.setName(" a name");
    realm.setParent(realmDAO.getRoot());
    try {
        realmDAO.save(realm);
        fail("This should not happen");
    } catch (InvalidEntityException e) {
        assertTrue(e.hasViolation(EntityViolationType.InvalidRealm));
    }
}
Also used : Realm(org.apache.syncope.core.persistence.api.entity.Realm) InvalidEntityException(org.apache.syncope.core.persistence.api.attrvalue.validation.InvalidEntityException) Test(org.junit.jupiter.api.Test) AbstractTest(org.apache.syncope.core.persistence.jpa.AbstractTest)

Example 15 with InvalidEntityException

use of org.apache.syncope.core.persistence.api.attrvalue.validation.InvalidEntityException in project syncope by apache.

the class PlainAttrTest method validateAndSave.

@Test
public void validateAndSave() {
    User user = userDAO.find("1417acbe-cbf6-4277-9372-e75e04f97000");
    PlainSchema emailSchema = plainSchemaDAO.find("email");
    assertNotNull(emailSchema);
    PlainSchema fullnameSchema = plainSchemaDAO.find("fullname");
    assertNotNull(fullnameSchema);
    UPlainAttr attr = entityFactory.newEntity(UPlainAttr.class);
    attr.setOwner(user);
    attr.setSchema(emailSchema);
    UPlainAttrUniqueValue uauv = entityFactory.newEntity(UPlainAttrUniqueValue.class);
    uauv.setAttr(attr);
    uauv.setSchema(fullnameSchema);
    uauv.setStringValue("a value");
    attr.setUniqueValue(uauv);
    user.add(attr);
    InvalidEntityException iee = null;
    try {
        userDAO.save(user);
        fail("This should not happen");
    } catch (InvalidEntityException e) {
        iee = e;
    }
    assertNotNull(iee);
    // for attribute
    assertTrue(iee.hasViolation(EntityViolationType.InvalidValueList));
    // for uauv
    assertTrue(iee.hasViolation(EntityViolationType.InvalidPlainAttr));
}
Also used : User(org.apache.syncope.core.persistence.api.entity.user.User) UPlainAttrUniqueValue(org.apache.syncope.core.persistence.api.entity.user.UPlainAttrUniqueValue) PlainSchema(org.apache.syncope.core.persistence.api.entity.PlainSchema) UPlainAttr(org.apache.syncope.core.persistence.api.entity.user.UPlainAttr) InvalidEntityException(org.apache.syncope.core.persistence.api.attrvalue.validation.InvalidEntityException) Test(org.junit.jupiter.api.Test) AbstractTest(org.apache.syncope.core.persistence.jpa.AbstractTest)

Aggregations

InvalidEntityException (org.apache.syncope.core.persistence.api.attrvalue.validation.InvalidEntityException)18 AbstractTest (org.apache.syncope.core.persistence.jpa.AbstractTest)13 Test (org.junit.jupiter.api.Test)13 User (org.apache.syncope.core.persistence.api.entity.user.User)6 Set (java.util.Set)4 PlainSchema (org.apache.syncope.core.persistence.api.entity.PlainSchema)4 Date (java.util.Date)3 Map (java.util.Map)2 ClientExceptionType (org.apache.syncope.common.lib.types.ClientExceptionType)2 EntityViolationType (org.apache.syncope.common.lib.types.EntityViolationType)2 DerSchema (org.apache.syncope.core.persistence.api.entity.DerSchema)2 Implementation (org.apache.syncope.core.persistence.api.entity.Implementation)2 Realm (org.apache.syncope.core.persistence.api.entity.Realm)2 ExternalResource (org.apache.syncope.core.persistence.api.entity.resource.ExternalResource)2 UPlainAttr (org.apache.syncope.core.persistence.api.entity.user.UPlainAttr)2 JPAUser (org.apache.syncope.core.persistence.jpa.entity.user.JPAUser)2 Transactional (org.springframework.transaction.annotation.Transactional)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1