Skip to content

Commit

Permalink
[minor] fix: node relations being rendered twice (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
manikmagar authored Jun 1, 2020
1 parent 5f85d25 commit 0b5064f
Show file tree
Hide file tree
Showing 4 changed files with 512 additions and 17 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ dependencies {
testImplementation 'org.mockito:mockito-core:3.3.3'
testImplementation 'io.github.netmikey.logunit:logunit-core:1.1.0'
testImplementation 'io.github.netmikey.logunit:logunit-logback:1.1.0'
testImplementation 'org.skyscreamer:jsonassert:1.5.0'
}

spotless {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,14 @@ public boolean draw(DrawingContext drawingContext) {
|| component.getName().equalsIgnoreCase(drawingContext.getFlowName())) {
MutableGraph flowGraph = initNewGraph();
MutableNode flowNode =
processComponent(component, flowGraph, drawingContext, flowRefs, mappedFlowKinds);
processComponent(component, drawingContext, flowRefs, mappedFlowKinds);

flowNode.addTo(flowGraph);

if (drawingContext.isGenerateSingles() && component.isaFlow()) {
writeFlowGraph(component, singleFlowDirPath, flowGraph);
}
flowGraph.addTo(rootGraph);
flowNode.addTo(rootGraph);
}
}
if (drawingContext.getFlowName() == null) {
Expand Down Expand Up @@ -112,9 +113,8 @@ private void checkUnusedNodes(MutableGraph graph) {
.forEach(node -> node.add(Color.RED, Style.FILLED, Color.GRAY));
}

MutableNode processComponent(Component component, MutableGraph graph,
DrawingContext drawingContext, Map<String, Component> flowRefs,
List<String> mappedFlowKinds) {
MutableNode processComponent(Component component, DrawingContext drawingContext,
Map<String, Component> flowRefs, List<String> mappedFlowKinds) {
FlowContainer flow = (FlowContainer) component;
MutableNode flowNode = mutNode(flow.qualifiedName()).add(Label.markdown(getNodeLabel(flow)));
if (flow.isaSubFlow()) {
Expand All @@ -141,7 +141,7 @@ MutableNode processComponent(Component component, MutableGraph graph,
} else {
name = refComponent.qualifiedName();
if (!mappedFlowKinds.contains(name)) {
processComponent(refComponent, graph, drawingContext, flowRefs, mappedFlowKinds);
processComponent(refComponent, drawingContext, flowRefs, mappedFlowKinds);
}
}
}
Expand All @@ -158,9 +158,7 @@ MutableNode processComponent(Component component, MutableGraph graph,
mappedFlowKinds.add(name);
}
if (sourceNode != null) {
sourceNode.add(Style.FILLED, Color.CYAN).addLink(to(flowNode).with(Style.BOLD)).addTo(graph);
} else {
flowNode.addTo(graph);
flowNode = sourceNode.add(Style.FILLED, Color.CYAN).addLink(to(flowNode).with(Style.BOLD));
}
return flowNode;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import static guru.nidi.graphviz.model.Factory.mutGraph;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.io.File;
import java.nio.file.Files;
Expand All @@ -12,21 +14,25 @@
import java.util.Arrays;
import java.util.Collections;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.api.io.TempDir;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import org.skyscreamer.jsonassert.JSONAssert;
import org.skyscreamer.jsonassert.JSONCompareMode;
import org.slf4j.event.Level;

import com.javastreets.muleflowdiagrams.model.Component;
import com.javastreets.muleflowdiagrams.model.FlowContainer;
import com.javastreets.muleflowdiagrams.model.MuleComponent;
import com.javastreets.muleflowdiagrams.model.*;

import guru.nidi.graphviz.attribute.Arrow;
import guru.nidi.graphviz.attribute.GraphAttr;
import guru.nidi.graphviz.attribute.Label;
import guru.nidi.graphviz.attribute.Rank;
import guru.nidi.graphviz.engine.Format;
import guru.nidi.graphviz.engine.Graphviz;
import guru.nidi.graphviz.engine.GraphvizV8Engine;
import guru.nidi.graphviz.model.MutableGraph;
import io.github.netmikey.logunit.api.LogCapturer;

Expand Down Expand Up @@ -55,6 +61,11 @@ void draw() {
context.setDiagramType(DiagramType.GRAPH);
context.setOutputFile(output);
FlowContainer flowContainer = new FlowContainer("flow", "test-flow");
MuleComponent source = new MuleComponent("vm", "listner");
source.setSource(true);
source.setConfigRef(Attribute.with("config-ref", "test"));
source.setPath(Attribute.with("queueName", "testVmQ"));
flowContainer.addComponent(source);
flowContainer.addComponent(new MuleComponent("flow-ref", "test-sub-flow"));
FlowContainer subflow = new FlowContainer("sub-flow", "test-sub-flow");

Expand All @@ -71,11 +82,55 @@ void draw() {
System.out.println("Used memory is bytes: " + memory);
System.out.println("Used memory is megabytes: " + bytesToMegabytes(memory));
assertThat(output).exists();
Mockito.verify(graphDiagram, Mockito.times(0)).writeFlowGraph(any(), any(), any());
verify(graphDiagram, Mockito.times(0)).writeFlowGraph(any(), any(), any());
logs.assertContains(
"Detected a possible self loop in sub-flow test-sub-flow. Skipping flow-ref processing.");
}

@Test
@DisplayName("Validate generated graph when generated as JSON.")
void drawToValidateGraph() throws Exception {

File output = new File(".", "output.png");
DrawingContext context = new DrawingContext();
context.setDiagramType(DiagramType.GRAPH);
context.setOutputFile(output);
FlowContainer flowContainer = new FlowContainer("flow", "test-flow");

MuleComponent source = new MuleComponent("vm", "listener");
source.setSource(true);
source.setConfigRef(Attribute.with("config-ref", "test"));
source.setPath(Attribute.with("queueName", "testVmQ"));
flowContainer.addComponent(source);
flowContainer.addComponent(new MuleComponent("flow-ref", "test-sub-flow"));
FlowContainer subflow = new FlowContainer("sub-flow", "test-sub-flow");
// Add reference to same sub-flow, resulting loop
subflow.addComponent(new MuleComponent("flow-ref", "test-sub-flow"));
context.setComponents(Arrays.asList(flowContainer, subflow));

ComponentItem item = new ComponentItem();
item.setPrefix("vm");
item.setOperation("listener");
item.setSource(true);
item.setConfigAttributeName("config-ref");
item.setPathAttributeName("queueName");
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();
Graphviz.useEngine(new GraphvizV8Engine());
String jsonGraph = Graphviz.fromGraph(generatedGraph).render(Format.JSON).toString();
String ref = new String(Files.readAllBytes(Paths.get(
"src/test/java/com/javastreets/muleflowdiagrams/drawings/drawToValidateGraph_Expected.json")));
JSONAssert.assertEquals(ref, jsonGraph, JSONCompareMode.STRICT);
Graphviz.releaseEngine();

}

@Test
void drawWithSinglesGeneration() {
// Get the Java runtime
Expand Down Expand Up @@ -104,8 +159,8 @@ void drawWithSinglesGeneration() {
System.out.println("Used memory is bytes: " + memory);
System.out.println("Used memory is megabytes: " + bytesToMegabytes(memory));
assertThat(output).exists();
Mockito.verify(graphDiagram, Mockito.times(1)).writeFlowGraph(any(), any(), any());
Mockito.verify(graphDiagram, Mockito.times(1)).writeFlowGraph(eq(flowContainer), any(), any());
verify(graphDiagram, Mockito.times(1)).writeFlowGraph(any(), any(), any());
verify(graphDiagram, Mockito.times(1)).writeFlowGraph(eq(flowContainer), any(), any());
logs.assertContains(
"Detected a possible self loop in sub-flow test-sub-flow. Skipping flow-ref processing.");
}
Expand Down Expand Up @@ -142,8 +197,8 @@ void drawASingleFlow() {
System.out.println("Used memory is megabytes: " + bytesToMegabytes(memory));
assertThat(output).exists();
ArgumentCaptor<Component> compArg = ArgumentCaptor.forClass(Component.class);
Mockito.verify(graphDiagram, Mockito.times(2)).processComponent(compArg.capture(),
any(MutableGraph.class), eq(context), anyMap(), anyList());
verify(graphDiagram, Mockito.times(2)).processComponent(compArg.capture(), eq(context),
anyMap(), anyList());
assertThat(compArg.getAllValues()).containsExactly(flowContainer2, subflow);
logs.assertContains(
"Detected a possible self loop in sub-flow test-sub-flow. Skipping flow-ref processing.");
Expand Down
Loading

0 comments on commit 0b5064f

Please sign in to comment.