Search in sources :

Example 1 with InvalidPropertyException

use of ca.corefacility.bioinformatics.irida.exceptions.InvalidPropertyException in project irida by phac-nml.

the class CRUDServiceImplTest method testUpdateWithBadPropertyType.

@Test
public void testUpdateWithBadPropertyType() {
    IdentifiableTestEntity entity = new IdentifiableTestEntity();
    entity.setId(new Long(1));
    Map<String, Object> updatedProperties = new HashMap<>();
    updatedProperties.put("integerValue", new Object());
    when(crudRepository.findOne(1L)).thenReturn(entity);
    try {
        crudService.updateFields(entity.getId(), updatedProperties);
        fail();
    } catch (InvalidPropertyException ex) {
        assertNotNull(ex.getAffectedClass());
    }
}
Also used : IdentifiableTestEntity(ca.corefacility.bioinformatics.irida.utils.model.IdentifiableTestEntity) HashMap(java.util.HashMap) InvalidPropertyException(ca.corefacility.bioinformatics.irida.exceptions.InvalidPropertyException) Test(org.junit.Test)

Example 2 with InvalidPropertyException

use of ca.corefacility.bioinformatics.irida.exceptions.InvalidPropertyException in project irida by phac-nml.

the class CRUDServiceImplTest method testUpdateWithBadPropertyName.

@Test
public void testUpdateWithBadPropertyName() {
    IdentifiableTestEntity entity = new IdentifiableTestEntity();
    entity.setId(1L);
    Map<String, Object> updatedProperties = new HashMap<>();
    updatedProperties.put("noSuchField", new Object());
    when(crudRepository.findOne(1L)).thenReturn(entity);
    try {
        crudService.updateFields(entity.getId(), updatedProperties);
        fail();
    } catch (InvalidPropertyException ex) {
        assertNotNull(ex.getAffectedClass());
    }
}
Also used : IdentifiableTestEntity(ca.corefacility.bioinformatics.irida.utils.model.IdentifiableTestEntity) HashMap(java.util.HashMap) InvalidPropertyException(ca.corefacility.bioinformatics.irida.exceptions.InvalidPropertyException) Test(org.junit.Test)

Example 3 with InvalidPropertyException

use of ca.corefacility.bioinformatics.irida.exceptions.InvalidPropertyException in project irida by phac-nml.

the class ControllerExceptionHandlerTest method testHandleInvalidPropertyException.

@Test
public void testHandleInvalidPropertyException() {
    ResponseEntity<ErrorResponse> response = controller.handleInvalidPropertyException(new InvalidPropertyException("invalid property"));
    assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());
}
Also used : InvalidPropertyException(ca.corefacility.bioinformatics.irida.exceptions.InvalidPropertyException) ErrorResponse(ca.corefacility.bioinformatics.irida.web.controller.api.exception.ErrorResponse) Test(org.junit.Test)

Example 4 with InvalidPropertyException

use of ca.corefacility.bioinformatics.irida.exceptions.InvalidPropertyException in project irida by phac-nml.

the class CRUDServiceImpl method updateFields.

/**
 * {@inheritDoc}
 */
@Override
@Transactional
public ValueType updateFields(KeyType id, Map<String, Object> updatedFields) throws ConstraintViolationException, EntityExistsException, InvalidPropertyException {
    // check if you can actually update the properties requested
    ValueType instance = read(id);
    for (String key : updatedFields.keySet()) {
        Object value = updatedFields.get(key);
        try {
            // setProperty doesn't throw an exception if the field name
            // can't be found, so we have to force an exception to be thrown
            // by calling getProperty manually first.
            DirectFieldAccessor dfa = new DirectFieldAccessor(instance);
            dfa.setPropertyValue(key, value);
        } catch (IllegalArgumentException | NotWritablePropertyException | TypeMismatchException e) {
            throw new InvalidPropertyException("Unable to access field [" + key + "]", valueType, e);
        }
    }
    // now that you know all of the requested methods exist, validate the
    // supplied values
    Set<ConstraintViolation<ValueType>> constraintViolations = new HashSet<>();
    // updated.
    for (String propertyName : updatedFields.keySet()) {
        Set<ConstraintViolation<ValueType>> propertyViolations = validator.validateValue(valueType, propertyName, updatedFields.get(propertyName));
        constraintViolations.addAll(propertyViolations);
    }
    // if any validations fail, throw a constraint violation exception.
    if (!constraintViolations.isEmpty()) {
        throw new ConstraintViolationException(constraintViolations);
    }
    // check if the entity exists in the database
    if (!exists(id)) {
        throw new EntityNotFoundException("Entity not found.");
    }
    // return repository.update(id, updatedFields);
    return repository.save(instance);
}
Also used : TypeMismatchException(org.springframework.beans.TypeMismatchException) EntityNotFoundException(ca.corefacility.bioinformatics.irida.exceptions.EntityNotFoundException) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) NotWritablePropertyException(org.springframework.beans.NotWritablePropertyException) ConstraintViolation(javax.validation.ConstraintViolation) InvalidPropertyException(ca.corefacility.bioinformatics.irida.exceptions.InvalidPropertyException) ConstraintViolationException(javax.validation.ConstraintViolationException) HashSet(java.util.HashSet) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

InvalidPropertyException (ca.corefacility.bioinformatics.irida.exceptions.InvalidPropertyException)4 Test (org.junit.Test)3 IdentifiableTestEntity (ca.corefacility.bioinformatics.irida.utils.model.IdentifiableTestEntity)2 HashMap (java.util.HashMap)2 EntityNotFoundException (ca.corefacility.bioinformatics.irida.exceptions.EntityNotFoundException)1 ErrorResponse (ca.corefacility.bioinformatics.irida.web.controller.api.exception.ErrorResponse)1 HashSet (java.util.HashSet)1 ConstraintViolation (javax.validation.ConstraintViolation)1 ConstraintViolationException (javax.validation.ConstraintViolationException)1 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)1 NotWritablePropertyException (org.springframework.beans.NotWritablePropertyException)1 TypeMismatchException (org.springframework.beans.TypeMismatchException)1 Transactional (org.springframework.transaction.annotation.Transactional)1