Search in sources :

Example 1 with ModelValidationException

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

the class AbstractModelParser method validateModel.

/**
 * Validate DOM document
 *
 * @param document the DOM document to validate
 */
public void validateModel(DomDocument document) {
    Schema schema = getSchema(document);
    if (schema == null) {
        return;
    }
    Validator validator = schema.newValidator();
    try {
        synchronized (document) {
            validator.validate(document.getDomSource());
        }
    } catch (IOException e) {
        throw new ModelValidationException("Error during DOM document validation", e);
    } catch (SAXException e) {
        throw new ModelValidationException("DOM document is not valid", e);
    }
}
Also used : ModelValidationException(org.camunda.bpm.model.xml.ModelValidationException) Schema(javax.xml.validation.Schema) IOException(java.io.IOException) Validator(javax.xml.validation.Validator) SAXException(org.xml.sax.SAXException)

Example 2 with ModelValidationException

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

the class DefinitionsTest method shouldAddChildElementsInCorrectOrder.

@Test
public void shouldAddChildElementsInCorrectOrder() {
    // create an empty model
    BpmnModelInstance bpmnModelInstance = Bpmn.createEmptyModel();
    // add definitions
    Definitions definitions = bpmnModelInstance.newInstance(Definitions.class);
    definitions.setTargetNamespace("Examples");
    bpmnModelInstance.setDefinitions(definitions);
    // create a Process element and add it to the definitions
    Process process = bpmnModelInstance.newInstance(Process.class);
    process.setId("some-process-id");
    definitions.getRootElements().add(process);
    // create an Import element and add it to the definitions
    Import importElement = bpmnModelInstance.newInstance(Import.class);
    importElement.setNamespace("Imports");
    importElement.setLocation("here");
    importElement.setImportType("example");
    definitions.getImports().add(importElement);
    // create another Process element and add it to the definitions
    process = bpmnModelInstance.newInstance(Process.class);
    process.setId("another-process-id");
    definitions.getRootElements().add(process);
    // create another Import element and add it to the definitions
    importElement = bpmnModelInstance.newInstance(Import.class);
    importElement.setNamespace("Imports");
    importElement.setLocation("there");
    importElement.setImportType("example");
    definitions.getImports().add(importElement);
    // validate model
    try {
        Bpmn.validateModel(bpmnModelInstance);
    } catch (ModelValidationException e) {
        Assert.fail();
    }
}
Also used : ModelValidationException(org.camunda.bpm.model.xml.ModelValidationException) Process(org.camunda.bpm.model.bpmn.instance.Process) Test(org.junit.Test)

Example 3 with ModelValidationException

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

the class DefinitionsTest method shouldAddParentChildElementInCorrectOrder.

@Test
public void shouldAddParentChildElementInCorrectOrder() {
    // create empty model
    BpmnModelInstance bpmnModelInstance = Bpmn.createEmptyModel();
    // add definitions to model
    Definitions definitions = bpmnModelInstance.newInstance(Definitions.class);
    definitions.setTargetNamespace("Examples");
    bpmnModelInstance.setDefinitions(definitions);
    // add process
    Process process = bpmnModelInstance.newInstance(Process.class);
    process.setId("messageEventDefinition");
    definitions.getRootElements().add(process);
    // add start event
    StartEvent startEvent = bpmnModelInstance.newInstance(StartEvent.class);
    startEvent.setId("theStart");
    process.getFlowElements().add(startEvent);
    // create and add message
    Message message = bpmnModelInstance.newInstance(Message.class);
    message.setId("start-message-id");
    definitions.getRootElements().add(message);
    // add message event definition to start event
    MessageEventDefinition startEventMessageEventDefinition = bpmnModelInstance.newInstance(MessageEventDefinition.class);
    startEventMessageEventDefinition.setMessage(message);
    startEvent.getEventDefinitions().add(startEventMessageEventDefinition);
    // add property after message event definition
    Property property = bpmnModelInstance.newInstance(Property.class);
    startEvent.getProperties().add(property);
    // finally add an extensions element
    ExtensionElements extensionElements = bpmnModelInstance.newInstance(ExtensionElements.class);
    process.setExtensionElements(extensionElements);
    // validate model
    try {
        Bpmn.validateModel(bpmnModelInstance);
    } catch (ModelValidationException e) {
        Assert.fail();
    }
}
Also used : ModelValidationException(org.camunda.bpm.model.xml.ModelValidationException) Process(org.camunda.bpm.model.bpmn.instance.Process) Test(org.junit.Test)

Example 4 with ModelValidationException

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

the class DefinitionsTest method shouldAddMessageAndMessageEventDefinition.

@Test
public void shouldAddMessageAndMessageEventDefinition() {
    // create empty model
    BpmnModelInstance bpmnModelInstance = Bpmn.createEmptyModel();
    // add definitions to model
    Definitions definitions = bpmnModelInstance.newInstance(Definitions.class);
    definitions.setTargetNamespace("Examples");
    bpmnModelInstance.setDefinitions(definitions);
    // create and add message
    Message message = bpmnModelInstance.newInstance(Message.class);
    message.setId("start-message-id");
    definitions.getRootElements().add(message);
    // create and add message event definition
    MessageEventDefinition messageEventDefinition = bpmnModelInstance.newInstance(MessageEventDefinition.class);
    messageEventDefinition.setId("message-event-def-id");
    messageEventDefinition.setMessage(message);
    definitions.getRootElements().add(messageEventDefinition);
    // test if message was set correctly
    Message setMessage = messageEventDefinition.getMessage();
    assertThat(setMessage).isEqualTo(message);
    // add process
    Process process = bpmnModelInstance.newInstance(Process.class);
    process.setId("messageEventDefinition");
    definitions.getRootElements().add(process);
    // add start event
    StartEvent startEvent = bpmnModelInstance.newInstance(StartEvent.class);
    startEvent.setId("theStart");
    process.getFlowElements().add(startEvent);
    // create and add message event definition to start event
    MessageEventDefinition startEventMessageEventDefinition = bpmnModelInstance.newInstance(MessageEventDefinition.class);
    startEventMessageEventDefinition.setMessage(message);
    startEvent.getEventDefinitions().add(startEventMessageEventDefinition);
    // create another message but do not add it
    Message anotherMessage = bpmnModelInstance.newInstance(Message.class);
    anotherMessage.setId("another-message-id");
    // create a message event definition and try to add last create message
    MessageEventDefinition anotherMessageEventDefinition = bpmnModelInstance.newInstance(MessageEventDefinition.class);
    try {
        anotherMessageEventDefinition.setMessage(anotherMessage);
        Assert.fail("Message should not be added to message event definition, cause it is not part of the model");
    } catch (Exception e) {
        assertThat(e).isInstanceOf(ModelReferenceException.class);
    }
    // first add message to model than to event definition
    definitions.getRootElements().add(anotherMessage);
    anotherMessageEventDefinition.setMessage(anotherMessage);
    startEvent.getEventDefinitions().add(anotherMessageEventDefinition);
    // message event definition and add message by id to it
    anotherMessageEventDefinition = bpmnModelInstance.newInstance(MessageEventDefinition.class);
    startEvent.getEventDefinitions().add(anotherMessageEventDefinition);
    // validate model
    try {
        Bpmn.validateModel(bpmnModelInstance);
    } catch (ModelValidationException e) {
        Assert.fail();
    }
}
Also used : ModelValidationException(org.camunda.bpm.model.xml.ModelValidationException) Process(org.camunda.bpm.model.bpmn.instance.Process) ModelReferenceException(org.camunda.bpm.model.xml.ModelReferenceException) IOException(java.io.IOException) ModelValidationException(org.camunda.bpm.model.xml.ModelValidationException) ModelParseException(org.camunda.bpm.model.xml.ModelParseException) ModelReferenceException(org.camunda.bpm.model.xml.ModelReferenceException) Test(org.junit.Test)

Example 5 with ModelValidationException

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

the class DefinitionsTest method shouldNotAffectComments.

@Test
@BpmnModelResource
public void shouldNotAffectComments() throws IOException {
    Definitions definitions = bpmnModelInstance.getDefinitions();
    assertThat(definitions).isNotNull();
    // create another Process element and add it to the definitions
    Process process = bpmnModelInstance.newInstance(Process.class);
    process.setId("another-process-id");
    definitions.getRootElements().add(process);
    // create another Import element and add it to the definitions
    Import importElement = bpmnModelInstance.newInstance(Import.class);
    importElement.setNamespace("Imports");
    importElement.setLocation("there");
    importElement.setImportType("example");
    definitions.getImports().add(importElement);
    // validate model
    try {
        Bpmn.validateModel(bpmnModelInstance);
    } catch (ModelValidationException e) {
        Assert.fail();
    }
    // convert the model to the XML string representation
    OutputStream outputStream = new ByteArrayOutputStream();
    Bpmn.writeModelToStream(outputStream, bpmnModelInstance);
    InputStream inputStream = IoUtil.convertOutputStreamToInputStream(outputStream);
    String modelString = IoUtil.getStringFromInputStream(inputStream);
    IoUtil.closeSilently(outputStream);
    IoUtil.closeSilently(inputStream);
    // read test process from file as string
    inputStream = getClass().getResourceAsStream("DefinitionsTest.shouldNotAffectCommentsResult.bpmn");
    String fileString = IoUtil.getStringFromInputStream(inputStream);
    IoUtil.closeSilently(inputStream);
    // compare strings
    assertThat(modelString).endsWith(fileString);
}
Also used : ModelValidationException(org.camunda.bpm.model.xml.ModelValidationException) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Process(org.camunda.bpm.model.bpmn.instance.Process) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BpmnModelResource(org.camunda.bpm.model.bpmn.util.BpmnModelResource) Test(org.junit.Test)

Aggregations

ModelValidationException (org.camunda.bpm.model.xml.ModelValidationException)5 Process (org.camunda.bpm.model.bpmn.instance.Process)4 Test (org.junit.Test)4 IOException (java.io.IOException)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 Schema (javax.xml.validation.Schema)1 Validator (javax.xml.validation.Validator)1 BpmnModelResource (org.camunda.bpm.model.bpmn.util.BpmnModelResource)1 ModelParseException (org.camunda.bpm.model.xml.ModelParseException)1 ModelReferenceException (org.camunda.bpm.model.xml.ModelReferenceException)1 SAXException (org.xml.sax.SAXException)1