Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
Issue #823
  • Loading branch information
rsoika committed Aug 20, 2024
1 parent 4c2205b commit 3f14430
Show file tree
Hide file tree
Showing 4 changed files with 306 additions and 346 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.openbpmn.bpmn.elements.Activity;
import org.openbpmn.bpmn.elements.DataObject;
import org.openbpmn.bpmn.elements.Event;
import org.openbpmn.bpmn.elements.Message;
import org.openbpmn.bpmn.elements.Signal;
import org.openbpmn.bpmn.elements.core.BPMNElement;
import org.openbpmn.bpmn.elements.core.BPMNElementNode;
Expand Down Expand Up @@ -111,6 +112,7 @@ public static ItemCollection build(BPMNElementNode bpmnElement) {
// resolve DataObjects
if (bpmnElement instanceof Event || bpmnElement instanceof Activity) {
resolveDataObjects(bpmnElement, result);
resolveMessageTags(bpmnElement, result);
}

// support deprecated item values
Expand Down Expand Up @@ -219,6 +221,73 @@ private static void resolveDataObjects(BPMNElementNode elementNode, ItemCollecti
}
}

/**
* This method resolves message tags for an event element. The method pares for
* the text fragment
* <bpmn2:message>...</bpmn2:message> and replaces the tag with the
* corresponding message if available
*
* @param elementNode - the bpmn event element
* @param entity - the ItemCollection of the event
*/
protected static void resolveMessageTags(BPMNElementNode elementNode, ItemCollection entity) {

String[] fieldList = { OpenBPMNUtil.EVENT_ITEM_MAIL_SUBJECT, OpenBPMNUtil.EVENT_ITEM_MAIL_BODY,
"txtmailsubject", "rtfmailbody" };
for (String field : fieldList) {

// Parse for the tag <bpmn2:message>
String value = entity.getItemValueString(field);
int parsingPos = 0;
boolean bNewValue = false;
while (value.indexOf("<bpmn2:message>", parsingPos) > -1) {

int istart = value.indexOf("<bpmn2:message>", parsingPos);
int iend = value.indexOf("</bpmn2:message>", parsingPos);
if (istart > -1 && iend > -1 && iend > istart) {
String messageName = value.substring(istart + 15, iend);

// find the corresponding bpmn2:message object by name....
BPMNModel model = elementNode.getModel();
String message = findMessageByName(model, messageName);
if (message != null) {
value = value.substring(0, istart) + message + value.substring(iend + 16);
bNewValue = true;
}
}

parsingPos = parsingPos + 15;
}

// Update item?
if (bNewValue) {
entity.replaceItemValue(field, value);
}

}

}

/**
* Helper method find a Message object by its name and return the text form the
* bpmn2:documentation tag.
*
* @param model
* @param messageName
* @return
*/
private static String findMessageByName(BPMNModel model, String messageName) {
if (messageName != null && !messageName.isEmpty()) {
for (Message message : model.getMessages()) {
if (messageName.equals(message.getName())) {
return message.getDocumentation();
}
}
}
// no match
return "";
}

/**
* This helper method returns a value list of a given imixs extension element.
* If no values exists, than the method returns an empty List
Expand Down Expand Up @@ -431,21 +500,21 @@ private static void adaptDeprecatedEventProperties(ItemCollection eventEntity) {
/**
* Helper method to adopt a old name into a new one
*
* @param taskEntity
* @param entity
* @param newItemName
* @param oldItemName
*/
private static void adaptDeprecatedItem(ItemCollection taskEntity, String newItemName, String oldItemName) {
private static void adaptDeprecatedItem(ItemCollection entity, String newItemName, String oldItemName) {

// test if old name is provided with a value...
if (taskEntity.getItemValueString(newItemName).isEmpty()
&& !taskEntity.getItemValueString(oldItemName).isEmpty()) {
taskEntity.replaceItemValue(newItemName, taskEntity.getItemValue(oldItemName));
if (entity.getItemValueString(newItemName).isEmpty()
&& !entity.getItemValueString(oldItemName).isEmpty()) {
entity.replaceItemValue(newItemName, entity.getItemValue(oldItemName));
}

// now we support backward compatibility and add the old name if missing
if (taskEntity.getItemValueString(oldItemName).isEmpty()) {
taskEntity.replaceItemValue(oldItemName, taskEntity.getItemValue(newItemName));
if (entity.getItemValueString(oldItemName).isEmpty()) {
entity.replaceItemValue(oldItemName, entity.getItemValue(newItemName));
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
package org.imixs.workflow.bpmn;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.util.List;
import java.util.Set;

import javax.xml.parsers.ParserConfigurationException;

import org.imixs.workflow.ItemCollection;
import org.imixs.workflow.exceptions.ModelException;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.openbpmn.bpmn.BPMNModel;
import org.openbpmn.bpmn.exceptions.BPMNModelException;
import org.openbpmn.bpmn.util.BPMNModelFactory;
import org.xml.sax.SAXException;

/**
Expand All @@ -21,68 +23,62 @@
* @author rsoika
*/
public class TestBPMNParserMessageText {
BPMNModel model = null;
OpenBPMNModelManager openBPMNModelManager = null;

@Before
public void setup() throws ParseException, ParserConfigurationException, SAXException, IOException {
openBPMNModelManager = new OpenBPMNModelManager();

}

@Test
public void testSimple() throws ParseException,
ParserConfigurationException, SAXException, IOException, ModelException {

String VERSION = "1.0.0";

InputStream inputStream = getClass().getResourceAsStream(
"/bpmn/message_example.bpmn");

BPMNModel model = null;
try {
model = BPMNParser.parseModel(inputStream, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
Assert.fail();
} catch (ModelException e) {
openBPMNModelManager.addModel(BPMNModelFactory.read("/bpmn/message_example.bpmn"));
model = openBPMNModelManager.getModel("1.0.0");
Assert.assertNotNull(model);
} catch (ModelException | BPMNModelException e) {
e.printStackTrace();
Assert.fail();
}
Assert.assertNotNull(model);

// Test Environment
ItemCollection profile = model.getDefinition();
Assert.assertNotNull(profile);
Assert.assertEquals("environment.profile",
profile.getItemValueString("txtname"));
Assert.assertEquals("WorkflowEnvironmentEntity",
profile.getItemValueString("type"));
Assert.assertEquals(VERSION,
profile.getItemValueString("$ModelVersion"));
try {
Set<String> groups = openBPMNModelManager.findAllGroups(model);
Assert.assertTrue(groups.contains("Message Example"));

Assert.assertTrue(model.getGroups().contains("Message Example"));
// test count of elements
Assert.assertEquals(2, model.findAllActivities().size());

// test count of elements
Assert.assertEquals(2, model.findAllTasks().size());
// test task 1000
ItemCollection task = openBPMNModelManager.findTaskByID(model, 1000);
Assert.assertNotNull(task);

// test task 1000
ItemCollection task = model.getTask(1000);
Assert.assertNotNull(task);
Assert.assertEquals("1.0.0", task.getItemValueString("$ModelVersion"));
Assert.assertEquals("Message Example",
task.getItemValueString("txtworkflowgroup"));
// test activity for task 1000
List<ItemCollection> events;

// test activity for task 1000
List<ItemCollection> activities = model.findAllEventsByTask(1000);
Assert.assertNotNull(activities);
Assert.assertEquals(1, activities.size());
events = openBPMNModelManager.findEventsByTask(model, 1000);

// test activity 1000.10 submit
ItemCollection activity = model.getEvent(1000, 10);
Assert.assertNotNull(activity);
Assert.assertNotNull(events);
Assert.assertEquals(1, events.size());

Assert.assertEquals("Some MessageMessage-Text",
activity.getItemValueString("txtmailsubject"));
// test event 1000.10 submit
ItemCollection event = openBPMNModelManager.findEventByID(model, 1000, 10);
Assert.assertNotNull(event);

String message = activity.getItemValueString("rtfMailBody");
Assert.assertEquals("Some MessageMessage-Text",
event.getItemValueString(OpenBPMNUtil.EVENT_ITEM_MAIL_SUBJECT));

Assert.assertEquals(
"<h1>Some Message Text</h1>\nThis is some message\nMessage-Text",
message);
String message = event.getItemValueString(OpenBPMNUtil.EVENT_ITEM_MAIL_BODY);

Assert.assertEquals(
"<h1>Some Message Text</h1>\nThis is some message\nMessage-Text",
message);
} catch (BPMNModelException e) {
Assert.fail(e.getMessage());
}
}

}
Loading

0 comments on commit 3f14430

Please sign in to comment.