Skip to content

Commit

Permalink
added new test cases
Browse files Browse the repository at this point in the history
Issue #345
  • Loading branch information
rsoika committed May 21, 2024
1 parent 4a594b1 commit 499c9d2
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 48 deletions.
147 changes: 147 additions & 0 deletions open-bpmn.metamodel/src/test/java/org/openbpmn/dom/TestNameSpaces.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
package org.openbpmn.dom;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import java.util.logging.Logger;

import org.junit.jupiter.api.Test;
import org.openbpmn.bpmn.BPMNModel;
import org.openbpmn.bpmn.BPMNNS;
import org.openbpmn.bpmn.BPMNTypes;
import org.openbpmn.bpmn.elements.BPMNProcess;
import org.openbpmn.bpmn.exceptions.BPMNModelException;
import org.openbpmn.bpmn.util.BPMNModelFactory;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
* This test class reads a BPMN Model instance and prints the node elements
*
* @author rsoika
*
*/
public class TestNameSpaces {

private static Logger logger = Logger.getLogger(TestNameSpaces.class.getName());

/**
* This Test verifies if a BPMN model with custom namespace URIs can be read.
*
* @throws BPMNModelException
*
*/
@Test
public void testReadEmptyModelWithCustomNamespace() throws BPMNModelException {

logger.info("...read model");

BPMNModel model = BPMNModelFactory.read("/process_1_custom_namespace.bpmn");
System.out.println("Root Element :" + model.getDoc().getDocumentElement().getNodeName());
// next validate the custom BPMN Namespace for bpmn2
assertEquals("http://www.omg.org/spec/BPMN/20100000/MODEL", model.getUri(BPMNNS.BPMN2));

// now we read once again a model with the default namespace
// we expect that the default namespace is set again!
model = BPMNModelFactory.read("/process_1.bpmn");
// next validate the BPMN Default Namespaces
assertEquals("http://www.omg.org/spec/BPMN/20100524/MODEL", model.getUri(BPMNNS.BPMN2));

logger.info("...model read successful");

}

/**
* This Test verifies if a BPMN model with custom namespace URIs can be read if
* no bpmn2: exists
*
* @throws BPMNModelException
*
*/
@Test
public void testReadModelWithoutBPMN2Prefix() throws BPMNModelException {

logger.info("...read model");

BPMNModel model = BPMNModelFactory.read("/process_1_custom_namespace-nopaefix.bpmn");
System.out.println("Root Element :" + model.getDoc().getDocumentElement().getNodeName());
// next validate the default namespace mapped to bpmn2
assertEquals("http://www.omg.org/spec/BPMN/20100524/MODEL", model.getUri(BPMNNS.BPMN2));

// add a new element without a prefix....

logger.info("...model read successful");

}

/**
* This test parses a bpmn file with a default namespace (no prefix) and adds a
* second task element
*
* @throws BPMNModelException
*/
@Test
public void testProcessExample01() throws BPMNModelException {

logger.info("...read model");

BPMNModel model = BPMNModelFactory.read("/process_1_custom_namespace-nopaefix.bpmn");
String out = "src/test/resources/output/process_1_custom_namespace-nopaefix.bpmn";

// next validate the BPMN Default Namespaces
assertEquals("http://www.omg.org/spec/BPMN/20100524/MODEL", model.getUri(BPMNNS.BPMN2));

BPMNProcess process = model.openDefaultProces();
assertNotNull(process);
assertEquals(2, process.getEvents().size()); // we expect 2 events (start and end event)

logger.info("...model read successful");

// Now if we add a new task element to this model the xml namespace should be
// created without a prefix
// 'task' and not 'bpmn2:task'
model.openDefaultProces().addTask("task-example-002", "Example Task 002", BPMNTypes.TASK);

model.save(out);
logger.info("...model update sucessful: " + out);
}

private static void printNode(NodeList nodeList) {

for (int count = 0; count < nodeList.getLength(); count++) {

Node tempNode = nodeList.item(count);

// make sure it's element node.
if (tempNode.getNodeType() == Node.ELEMENT_NODE) {

// get node name and value
System.out.println("\nNode Name =" + tempNode.getNodeName() + " [OPEN]");
System.out.println("Node Value =" + tempNode.getTextContent());

if (tempNode.hasAttributes()) {

// get attributes names and values
NamedNodeMap nodeMap = tempNode.getAttributes();
for (int i = 0; i < nodeMap.getLength(); i++) {
Node node = nodeMap.item(i);
System.out.println("attr name : " + node.getNodeName());
System.out.println("attr value : " + node.getNodeValue());
}

}

if (tempNode.hasChildNodes()) {
// loop again if has child nodes
printNode(tempNode.getChildNodes());
}

System.out.println("Node Name =" + tempNode.getNodeName() + " [CLOSE]");

}

}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,53 +84,6 @@ public void testProcessExample01() throws BPMNModelException {
logger.info("...model update sucessful: " + out);
}

/**
* This Test verifies if a BPMN model with custom namespace URIs can be read.
*
* @throws BPMNModelException
*
*/
@Test
public void testReadEmptyModelWithCustomNamespace() throws BPMNModelException {

logger.info("...read model");

BPMNModel model = BPMNModelFactory.read("/process_1_custom_namespace.bpmn");
System.out.println("Root Element :" + model.getDoc().getDocumentElement().getNodeName());
// next validate the custom BPMN Namespace for bpmn2
assertEquals("http://www.omg.org/spec/BPMN/20100000/MODEL", model.getUri(BPMNNS.BPMN2));

// now we read once again a model with the default namespace
// we expect that the default namespace is set again!
model = BPMNModelFactory.read("/process_1.bpmn");
// next validate the BPMN Default Namespaces
assertEquals("http://www.omg.org/spec/BPMN/20100524/MODEL", model.getUri(BPMNNS.BPMN2));

logger.info("...model read successful");

}

/**
* This Test verifies if a BPMN model with custom namespace URIs can be read if
* no bpmn2: exists
*
* @throws BPMNModelException
*
*/
@Test
public void testReadModelWithoutBPMN2Prefix() throws BPMNModelException {

logger.info("...read model");

BPMNModel model = BPMNModelFactory.read("/process_1_custom_namespace-nopaefix.bpmn");
System.out.println("Root Element :" + model.getDoc().getDocumentElement().getNodeName());
// next validate the default namespace mapped to bpmn2
assertEquals("http://www.omg.org/spec/BPMN/20100524/MODEL", model.getUri(BPMNNS.BPMN2));

logger.info("...model read successful");

}

/**
* This test parses a blank (empty) bpmn file and creaes a default process
*
Expand Down
2 changes: 1 addition & 1 deletion open-bpmn.metamodel/src/test/resources/out_blank.bpmn
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
<bpmn2:definitions xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" xmlns:open-bpmn="http://open-bpmn.org/XMLSchema" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" exporter="org.openbpmn" exporterVersion="1.0.0" targetNamespace="http://open-bpmn.org" xmlns:bpmn2="http://www.omg.org/spec/BPMN/20100524/MODEL">
<bpmn2:process id="process_1" name="Default Process" processType="Public"/>
<bpmndi:BPMNDiagram id="BPMNDiagram_1" name="OpenBPMN Diagram">
<bpmndi:BPMNPlane bpmnElement="process_1" id="BPMNPlane_1"/>
<bpmndi:BPMNPlane id="BPMNPlane_1"/>
</bpmndi:BPMNDiagram>
</bpmn2:definitions>

0 comments on commit 499c9d2

Please sign in to comment.