Search in sources :

Example 1 with ModelElementValidator

use of org.camunda.bpm.model.xml.validation.ModelElementValidator in project camunda-xml-model by camunda.

the class ModelInstanceValidator method validate.

@SuppressWarnings({ "unchecked", "rawtypes" })
public ValidationResults validate() {
    ValidationResultsCollectorImpl resultsCollector = new ValidationResultsCollectorImpl();
    for (ModelElementValidator validator : validators) {
        Class<? extends ModelElementInstance> elementType = validator.getElementType();
        Collection<? extends ModelElementInstance> modelElementsByType = modelInstanceImpl.getModelElementsByType(elementType);
        for (ModelElementInstance element : modelElementsByType) {
            resultsCollector.setCurrentElement(element);
            try {
                validator.validate(element, resultsCollector);
            } catch (RuntimeException e) {
                throw new RuntimeException("Validator " + validator + " threw an exception while validating " + element, e);
            }
        }
    }
    return resultsCollector.getResults();
}
Also used : ModelElementValidator(org.camunda.bpm.model.xml.validation.ModelElementValidator) ModelElementInstance(org.camunda.bpm.model.xml.instance.ModelElementInstance)

Example 2 with ModelElementValidator

use of org.camunda.bpm.model.xml.validation.ModelElementValidator in project camunda-bpmn-model by camunda.

the class ValidateProcessTest method validationFailsIfNoStartEventFound.

@Test
public void validationFailsIfNoStartEventFound() {
    List<ModelElementValidator<?>> validators = new ArrayList<ModelElementValidator<?>>();
    validators.add(new ProcessStartEventValidator());
    BpmnModelInstance bpmnModelInstance = Bpmn.createProcess().done();
    ValidationResults validationResults = bpmnModelInstance.validate(validators);
    assertThat(validationResults.hasErrors()).isTrue();
    Map<ModelElementInstance, List<ValidationResult>> results = validationResults.getResults();
    assertThat(results.size()).isEqualTo(1);
    Process process = bpmnModelInstance.getDefinitions().getChildElementsByType(Process.class).iterator().next();
    assertThat(results.containsKey(process)).isTrue();
    List<ValidationResult> resultsForProcess = results.get(process);
    assertThat(resultsForProcess.size()).isEqualTo(1);
    ValidationResult validationResult = resultsForProcess.get(0);
    assertThat(validationResult.getElement()).isEqualTo(process);
    assertThat(validationResult.getCode()).isEqualTo(10);
    assertThat(validationResult.getMessage()).isEqualTo("Process does not have exactly one start event. Got 0.");
    assertThat(validationResult.getType()).isEqualTo(ValidationResultType.ERROR);
}
Also used : ModelElementValidator(org.camunda.bpm.model.xml.validation.ModelElementValidator) ValidationResults(org.camunda.bpm.model.xml.validation.ValidationResults) ModelElementInstance(org.camunda.bpm.model.xml.instance.ModelElementInstance) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Process(org.camunda.bpm.model.bpmn.instance.Process) BpmnModelInstance(org.camunda.bpm.model.bpmn.BpmnModelInstance) ValidationResult(org.camunda.bpm.model.xml.validation.ValidationResult) Test(org.junit.Test)

Aggregations

ModelElementInstance (org.camunda.bpm.model.xml.instance.ModelElementInstance)2 ModelElementValidator (org.camunda.bpm.model.xml.validation.ModelElementValidator)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 BpmnModelInstance (org.camunda.bpm.model.bpmn.BpmnModelInstance)1 Process (org.camunda.bpm.model.bpmn.instance.Process)1 ValidationResult (org.camunda.bpm.model.xml.validation.ValidationResult)1 ValidationResults (org.camunda.bpm.model.xml.validation.ValidationResults)1 Test (org.junit.Test)1