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