Skip to content

Commit bd7a63a

Browse files
author
Mark Robinson
committed
Add test for GraphViz generation
1 parent ae68a46 commit bd7a63a

File tree

2 files changed

+105
-2
lines changed

2 files changed

+105
-2
lines changed

src/test/java/org/commonwl/view/cwl/CWLServiceTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class CWLServiceTest {
2525
@Test
2626
public void parseLobSTRDraft3Workflow() throws Exception {
2727

28-
// Mock githubService class
28+
// Mock githubService class to redirect downloads to resources folder
2929
GitHubService githubService = Mockito.mock(GitHubService.class);
3030
Answer fileAnswer = new Answer<String>() {
3131
@Override
@@ -60,7 +60,7 @@ public String answer(InvocationOnMock invocation) throws Throwable {
6060
@Test
6161
public void parseLobSTRv1Workflow() throws Exception {
6262

63-
// Mock githubService class
63+
// Mock githubService class to redirect downloads to resources folder
6464
GitHubService githubService = Mockito.mock(GitHubService.class);
6565
Answer fileAnswer = new Answer<String>() {
6666
@Override
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package org.commonwl.view.graphviz;
2+
3+
import org.apache.commons.io.FileUtils;
4+
import org.junit.Before;
5+
import org.junit.Rule;
6+
import org.junit.Test;
7+
import org.junit.rules.TemporaryFolder;
8+
9+
import javax.imageio.ImageIO;
10+
import java.awt.image.BufferedImage;
11+
import java.io.File;
12+
13+
import static org.apache.commons.io.FileUtils.readFileToString;
14+
import static org.junit.Assert.*;
15+
16+
17+
public class GraphVizServiceTest {
18+
19+
private GraphVizService graphVizService;
20+
21+
/**
22+
* Use a temporary directory for testing
23+
*/
24+
@Rule
25+
public TemporaryFolder graphvizFolder = new TemporaryFolder();
26+
27+
/**
28+
* Generate a service for testing using the temporary folder
29+
*/
30+
@Before
31+
public void setUp() throws Exception {
32+
graphVizService = new GraphVizService(graphvizFolder.getRoot().getAbsolutePath());
33+
}
34+
35+
/**
36+
* Check that a valid png file can be generated from DOT source
37+
*/
38+
@Test
39+
public void getGraphAsPng() throws Exception {
40+
41+
File dotSource = new File("src/test/resources/graphviz/testWorkflow.dot");
42+
File png = graphVizService.getGraph("workflowid.png",
43+
readFileToString(dotSource), "png");
44+
BufferedImage actualImg = ImageIO.read(png);
45+
46+
// Check a valid image has been created
47+
assertNotNull(actualImg);
48+
assertTrue(actualImg.getWidth() > 0);
49+
assertTrue(actualImg.getHeight() > 0);
50+
51+
}
52+
53+
/**
54+
* Check that a valid svg file can be generated from DOT source
55+
*/
56+
@Test
57+
public void getGraphAsSvg() throws Exception {
58+
59+
File dotSource = new File("src/test/resources/graphviz/testWorkflow.dot");
60+
File svg = graphVizService.getGraph("workflowid.svg",
61+
readFileToString(dotSource), "svg");
62+
String svgString = FileUtils.readFileToString(svg);
63+
64+
assertTrue(svgString.contains("Generated by graphviz"));
65+
66+
}
67+
68+
/**
69+
* Check that an xdot file can be generated from DOT source
70+
*/
71+
@Test
72+
public void getGraphAsXDot() throws Exception {
73+
74+
File dotSource = new File("src/test/resources/graphviz/testWorkflow.dot");
75+
File xdot = graphVizService.getGraph("workflowid.dot",
76+
readFileToString(dotSource), "xdot");
77+
String xdotString = FileUtils.readFileToString(xdot);
78+
79+
assertNotNull(xdotString);
80+
assertTrue(xdotString.length() > 0);
81+
82+
}
83+
84+
/**
85+
* Check that files in the graphVizFolder can be
86+
* deleted with deleteCache()
87+
*/
88+
@Test
89+
public void deleteCache() throws Exception {
90+
91+
File png = graphvizFolder.newFile("exampleid.png");
92+
File svg = graphvizFolder.newFile("exampleid.svg");
93+
File dot = graphvizFolder.newFile("exampleid.dot");
94+
95+
graphVizService.deleteCache("exampleid");
96+
97+
assertFalse(png.exists());
98+
assertFalse(svg.exists());
99+
assertFalse(dot.exists());
100+
101+
}
102+
103+
}

0 commit comments

Comments
 (0)