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());
}
}
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());
}
}
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());
}
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);
}
Aggregations