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