use of org.molgenis.data.validation.ConstraintViolation in project molgenis by molgenis.
the class RestControllerV2Test method testUpdateEntitiesMolgenisValidationException.
@SuppressWarnings("unchecked")
@Test
public void testUpdateEntitiesMolgenisValidationException() throws Exception {
Exception e = new MolgenisValidationException(Collections.singleton(new ConstraintViolation("Message", 5L)));
doThrow(e).when(dataService).update(eq(ENTITY_NAME), (Stream<Entity>) any(Stream.class));
mockMvc.perform(put(HREF_ENTITY_COLLECTION).content("{entities:[{id:'p1', name:'Example data'}]}").contentType(APPLICATION_JSON)).andExpect(status().isBadRequest()).andExpect(content().contentType(APPLICATION_JSON_UTF8)).andExpect(header().doesNotExist("Location")).andExpect(jsonPath(FIRST_ERROR_MESSAGE, is("Message (entity 5)")));
}
use of org.molgenis.data.validation.ConstraintViolation in project molgenis by molgenis.
the class EntityTypeValidator method validateOwnAttributes.
/**
* Validates the attributes owned by this entity:
* 1) validates that the parent entity doesn't have attributes with the same name
* 2) validates that this entity doesn't have attributes with the same name
* 3) validates that this entity has attributes defined at all
*
* @param entityType entity meta data
* @throws MolgenisValidationException if an attribute is owned by another entity or a parent attribute has the same name
*/
private static void validateOwnAttributes(EntityType entityType) {
// Validate that entity has attributes
if (asStream(entityType.getAllAttributes()).collect(toList()).isEmpty()) {
throw new MolgenisValidationException(new ConstraintViolation(format("Entity [%s] does not contain any attributes. Did you use the correct package+entity name combination in both the entities as well as the attributes sheet?", entityType.getId())));
}
// Validate that entity does not contain multiple attributes with the same name
Multimap<String, Attribute> attrMultiMap = asStream(entityType.getAllAttributes()).collect(MultimapCollectors.toArrayListMultimap(Attribute::getName, Function.identity()));
attrMultiMap.keySet().forEach(attrName -> {
if (attrMultiMap.get(attrName).size() > 1) {
throw new MolgenisValidationException(new ConstraintViolation(format("Entity [%s] contains multiple attributes with name [%s]", entityType.getId(), attrName)));
}
});
// Validate that entity attributes with same name do no exist in parent entity
EntityType extendsEntityType = entityType.getExtends();
if (extendsEntityType != null) {
Map<String, Attribute> extendsAllAttrMap = stream(extendsEntityType.getAllAttributes().spliterator(), false).collect(toMap(Attribute::getName, Function.identity(), (u, v) -> {
throw new IllegalStateException(String.format("Duplicate key %s", u));
}, LinkedHashMap::new));
entityType.getOwnAllAttributes().forEach(attr -> {
if (extendsAllAttrMap.containsKey(attr.getName())) {
throw new MolgenisValidationException(new ConstraintViolation(format("An attribute with name [%s] already exists in entity [%s] or one of its parents", attr.getName(), extendsEntityType.getId())));
}
});
}
}
use of org.molgenis.data.validation.ConstraintViolation in project molgenis by molgenis.
the class TagValidator method validate.
/**
* Validates tag
*
* @param tag tag
* @throws MolgenisValidationException if tag is not valid
*/
@SuppressWarnings("MethodMayBeStatic")
public void validate(Tag tag) {
String relationIri = tag.getRelationIri();
Relation relation = Relation.forIRI(relationIri);
if (relation == null) {
throw new MolgenisValidationException(new ConstraintViolation(format("Unknown relation IRI [%s]", relationIri)));
}
}
use of org.molgenis.data.validation.ConstraintViolation in project molgenis by molgenis.
the class AppRepositoryDecorator method validateResourceZip.
private void validateResourceZip(App app) {
FileMeta appZipMeta = app.getSourceFiles();
if (appZipMeta != null) {
File fileStoreFile = fileStore.getFile(appZipMeta.getId());
if (fileStoreFile == null) {
LOG.error("Resource zip '{}' for app '{}' missing in file store", appZipMeta.getId(), app.getName());
throw new RuntimeException("An error occurred trying to create or update app");
}
ZipFile zipFile;
try {
zipFile = new ZipFile(fileStoreFile);
} catch (ZipException e) {
LOG.error("Error creating zip file object", e);
throw new RuntimeException("An error occurred trying to create or update app");
}
if (!zipFile.isValidZipFile()) {
throw new MolgenisValidationException(new ConstraintViolation(String.format("'%s' is not a valid zip file.", appZipMeta.getFilename())));
}
}
}
use of org.molgenis.data.validation.ConstraintViolation in project molgenis by molgenis.
the class JobScheduler method schedule.
/**
* Schedule a {@link ScheduledJob} with a cron expression defined in the entity.
* <p>
* Reschedules job if the job already exists.
* <p>
* If active is false, it unschedules the job
*
* @param scheduledJob the {@link ScheduledJob} to schedule
*/
public synchronized void schedule(ScheduledJob scheduledJob) {
String id = scheduledJob.getId();
String cronExpression = scheduledJob.getCronExpression();
String name = scheduledJob.getName();
// Validate cron expression
if (!CronExpression.isValidExpression(cronExpression)) {
throw new MolgenisValidationException(singleton(new ConstraintViolation("Invalid cronexpression '" + cronExpression + "'", null)));
}
try {
// If already scheduled, remove it from the quartzScheduler
if (quartzScheduler.checkExists(new JobKey(id, SCHEDULED_JOB_GROUP))) {
unschedule(id);
}
// If not active, do not schedule it
if (!scheduledJob.getBoolean(ScheduledJobMetadata.ACTIVE)) {
return;
}
// Schedule with 'cron' trigger
Trigger trigger = newTrigger().withIdentity(id, SCHEDULED_JOB_GROUP).withSchedule(cronSchedule(cronExpression)).build();
schedule(scheduledJob, trigger);
LOG.info("Scheduled Job '{}' with trigger '{}'", name, trigger);
} catch (SchedulerException e) {
LOG.error("Error schedule job", e);
throw new ScheduledJobException("Error schedule job", e);
}
}
Aggregations