Skip to content

Commit

Permalink
feat: mule core known components support
Browse files Browse the repository at this point in the history
Allow configuring core components in known components csv file and identifying corresponding components. Fixes #301 by rendering schedulers from mule core components.
  • Loading branch information
manikmagar committed Nov 6, 2022
1 parent c8ef1e5 commit c6e70e3
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/main/java/com/javastreets/mulefd/MuleXmlElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ public static boolean isanErrorHandler(Element element) {
return ELEMENT_ERROR_HANDLER.equalsIgnoreCase(element.getNodeName());
}

public static boolean isMuleCoreElement(Element element) {
return "mule".equalsIgnoreCase(element.getNodeName());
}

public static boolean isFlowRef(MuleComponent component) {
return component.getType().equalsIgnoreCase(ELEMENT_FLOW_REF);
}
Expand Down Expand Up @@ -91,7 +95,6 @@ public static List<MuleComponent> fillComponents(Element flowElement,
if (isanErrorHandler(element)) {
muleComponentList.addAll(parseContainerElement(element, knownComponents));
}

processKnownComponents(knownComponents, muleComponentList, element);

}
Expand All @@ -115,6 +118,8 @@ static void processKnownComponents(Map<String, ComponentItem> knownComponents,
String[] wildcards = null;
if (knownComponents.containsKey(keyName)) {
item = knownComponents.get(keyName);
} else if (!element.getNodeName().contains(":")) {
item = knownComponents.get(MuleComponent.toMuleCoreComponentName(element.getNodeName()));
} else if (element.getTagName().contains(":")) {
wildcards = element.getTagName().split(":");
String wildcardEntry = wildcards[0] + ":*";
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/com/javastreets/mulefd/model/MuleComponent.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.javastreets.mulefd.model;

import java.util.Objects;


public class MuleComponent extends Component {
private boolean async;
private boolean source;
Expand Down Expand Up @@ -30,6 +33,17 @@ public void setSource(boolean source) {
this.source = source;
}

/**
* Prefixes the provided name with mule namespace.
*
* @param name {@link String} of a component
* @return String mule prefixed name
*/
public static String toMuleCoreComponentName(String name) {
Objects.requireNonNull(name, "Component name cannot be null");
return "mule:" + name;
}

public Attribute<String, String> getConfigRef() {
return configRef;
}
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/default-mule-components.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
prefix,operation,sourceFlag,path,configName,async
mule,scheduler,true,,,false
http,listener,true,path,config-ref,true
http,request,false,path,config-ref,false
apikit,*,false,api,config-ref,false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -393,4 +393,36 @@ void writeFlowGraphWithSubFlow() {
}



@Test
@DisplayName("Mule Core component flows - Scheduler")
void github_issue_301() throws Exception {

List flows =
DiagramRendererTestUtil.getFlows(Paths.get("src/test/resources/gh-issues/iss-301.xml"));
File output = new File(".", "output.png");
DrawingContext context = new DrawingContext();
context.setDiagramType(DiagramType.GRAPH);
context.setOutputFile(output);
context.setComponents(flows);

ComponentItem item = new ComponentItem();
item.setPrefix("mule");
item.setOperation("scheduler");
item.setSource(true);
context.setKnownComponents(Collections.singletonMap(item.qualifiedName(), item));

GraphDiagram graphDiagram = Mockito.spy(new GraphDiagram());
when(graphDiagram.getDiagramHeaderLines()).thenReturn(new String[] {"Test Diagram"});
graphDiagram.draw(context);
ArgumentCaptor<MutableGraph> graphArgumentCaptor = ArgumentCaptor.forClass(MutableGraph.class);
verify(graphDiagram).writGraphToFile(any(File.class), graphArgumentCaptor.capture());
MutableGraph generatedGraph = graphArgumentCaptor.getValue();
String generated = GraphvizEngineHelper.generate(generatedGraph, Format.DOT);
Path path = Paths.get("src/test/resources/gh-issues/iss-301.dot");
GraphvizEngineHelper.generate(generatedGraph, Format.DOT);
String ref = new String(Files.readAllBytes(path));
assertThat(generated).as("DOT Graph").isEqualToNormalizingNewlines(ref);
}

}
13 changes: 13 additions & 0 deletions src/test/java/com/javastreets/mulefd/model/MuleComponentTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.javastreets.mulefd.model;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.catchThrowableOfType;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -29,4 +30,16 @@ void parentType() {

}

@Test
void toMuleCoreComponentName_withValidName() {
assertThat(MuleComponent.toMuleCoreComponentName("test")).isEqualTo("mule:test");
}

@SuppressWarnings("ResultOfMethodCallIgnored")
@Test
void toMuleCoreComponentName_withNullName() {
NullPointerException npe = catchThrowableOfType(
() -> MuleComponent.toMuleCoreComponentName(null), NullPointerException.class);
assertThat(npe).isNotNull().hasMessage("Component name cannot be null");
}
}
41 changes: 41 additions & 0 deletions src/test/resources/gh-issues/iss-301.dot
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
digraph "mule" {
edge ["dir"="forward"]
graph ["rankdir"="LR","splines"="spline","pad"="1.0,0.5","dpi"="150","label"=<Test Diagram<br/>>,"labelloc"="t"]
edge ["arrowhead"="vee","dir"="forward"]
subgraph "cluster_legend" {
edge ["dir"="forward"]
graph ["label"=<<b>Legend</b>>,"style"="dashed"]
"flow" ["fixedsize"="true","width"="1.0","height"="0.25","shape"="rectangle","color"="blue"]
"sub-flow" ["fixedsize"="true","width"="1.0","height"="0.25","color"="black","shape"="ellipse"]
"connector:operation" ["shape"="component"]
"Unused sub/-flow" ["fixedsize"="true","width"="2.0","height"="0.25","color"="gray","style"="filled"]
"Flow A" ["fixedsize"="true","width"="1.0","height"="0.25"]
"sub-flow-1" ["fixedsize"="true","width"="1.25","height"="0.25"]
"Flow C" ["fixedsize"="true","width"="1.0","height"="0.25"]
"sub-flow-C1" ["fixedsize"="true","width"="1.25","height"="0.25"]
"flow source" ["fixedsize"="true","width"="1.5","height"="0.25","shape"="hexagon","style"="filled","color"="cyan","sourceNode"="true"]
"flow self-call" ["fixedsize"="true","width"="1.25","height"="0.25","shape"="rectangle","color"="blue"]
"sub-flow self-call" ["fixedsize"="true","width"="2.0","height"="0.25","color"="black","shape"="ellipse"]
"flow" -> "sub-flow" ["style"="invis"]
"sub-flow" -> "Unused sub/-flow" ["style"="invis"]
"Flow A" -> "sub-flow-1" ["style"="solid","label"="(1)","taillabel"="Call Sequence\n","labelangle"="-5.0","labeldistance"="8.0"]
"Flow C" -> "sub-flow-C1" ["style"="dashed,bold","xlabel"="(1) Async","color"="lightblue3","taillabel"="Asynchronous call\n","labelangle"="-5.0","labeldistance"="8.0"]
"flow source" -> "flow self-call" ["style"="invis"]
"flow self-call" -> "flow self-call"
"flow self-call" -> "sub-flow self-call" ["style"="invis"]
"sub-flow self-call" -> "sub-flow self-call"
}
subgraph "cluster_legend-space" {
edge ["dir"="none"]
graph ["label"="","style"="invis"]
"" ["shape"="none","width"="2.0","height"="1.0"]
}
subgraph "cluster_mule" {
edge ["dir"="forward"]
graph ["rankdir"="LR","splines"="spline","pad"="1.0,0.5","dpi"="150","label"=<Application graph<br/>>,"labelloc"="t","style"="invis"]
edge ["arrowhead"="vee","dir"="forward"]
"mule:scheduler" ["shape"="hexagon","style"="filled","color"="cyan","sourceNode"="true","label"=<<b>mule: scheduler</b><br/><br/>>]
"flow:scheduler-configFlow" ["label"=<<b>flow</b>: scheduler-configFlow>,"shape"="rectangle","color"="blue"]
"mule:scheduler" -> "flow:scheduler-configFlow" ["style"="bold"]
}
}
14 changes: 14 additions & 0 deletions src/test/resources/gh-issues/iss-301.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd">
<flow name="scheduler-configFlow" doc:id="3a8687e8-ee8d-42fd-b3a0-d1740b1da621" >
<scheduler doc:name="Scheduler" doc:id="8a2088ba-a947-438c-97e5-b124f4386b18" >
<scheduling-strategy >
<cron expression="${cron.expreesion}" />
</scheduling-strategy>
</scheduler>
<logger level="INFO" doc:name="Logger" doc:id="2062d553-4a9a-4c40-b3a1-01ca6c6b305f" />
</flow>
</mule>

0 comments on commit c6e70e3

Please sign in to comment.