1
+ /*
2
+ * Licensed to the Apache Software Foundation (ASF) under one
3
+ * or more contributor license agreements. See the NOTICE file
4
+ * distributed with this work for additional information
5
+ * regarding copyright ownership. The ASF licenses this file
6
+ * to you under the Apache License, Version 2.0 (the
7
+ * "License"); you may not use this file except in compliance
8
+ * with the License. You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing,
13
+ * software distributed under the License is distributed on an
14
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
+ * KIND, either express or implied. See the License for the
16
+ * specific language governing permissions and limitations
17
+ * under the License.
18
+ */
19
+
20
+ package org .commonwl .view .workflow ;
21
+
22
+ import org .commonwl .view .github .GithubDetails ;
23
+ import org .commonwl .view .graphviz .GraphVizService ;
24
+ import org .commonwl .view .researchobject .ROBundleNotFoundException ;
25
+ import org .junit .Rule ;
26
+ import org .junit .Test ;
27
+ import org .junit .rules .TemporaryFolder ;
28
+ import org .mockito .Mockito ;
29
+ import org .springframework .test .web .servlet .MockMvc ;
30
+ import org .springframework .test .web .servlet .setup .MockMvcBuilders ;
31
+
32
+ import static org .mockito .Matchers .anyObject ;
33
+ import static org .mockito .Matchers .anyString ;
34
+ import static org .mockito .Mockito .when ;
35
+ import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .get ;
36
+ import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .post ;
37
+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .*;
38
+
39
+ public class WorkflowControllerTest {
40
+
41
+ /**
42
+ * Use a temporary directory for testing
43
+ */
44
+ @ Rule
45
+ public TemporaryFolder roBundleFolder = new TemporaryFolder ();
46
+
47
+ /**
48
+ * Endpoint for main form submission
49
+ */
50
+ @ Test
51
+ public void newWorkflowFromGithubURL () throws Exception {
52
+
53
+ // Validator pass or fail
54
+ WorkflowFormValidator mockValidator = Mockito .mock (WorkflowFormValidator .class );
55
+ when (mockValidator .validateAndParse (anyObject (), anyObject ()))
56
+ .thenReturn (null )
57
+ .thenReturn (new GithubDetails ("owner" , "repoName" , "branch" , "path/within" ))
58
+ .thenReturn (new GithubDetails ("owner" , "repoName" , "branch" , "path/workflow.cwl" ));
59
+
60
+ // The eventual accepted valid workflow
61
+ Workflow mockWorkflow = Mockito .mock (Workflow .class );
62
+ when (mockWorkflow .getRetrievedFrom ())
63
+ .thenReturn (new GithubDetails ("owner" , "repoName" , "branch" , "path/workflow.cwl" ));
64
+
65
+ // Mock workflow service returning valid workflow
66
+ WorkflowService mockWorkflowService = Mockito .mock (WorkflowService .class );
67
+ when (mockWorkflowService .createWorkflow (anyObject ()))
68
+ .thenReturn (null )
69
+ .thenReturn (mockWorkflow );
70
+
71
+ // Mock controller/MVC
72
+ WorkflowController workflowController = new WorkflowController (
73
+ mockValidator ,
74
+ mockWorkflowService ,
75
+ Mockito .mock (GraphVizService .class ));
76
+ MockMvc mockMvc = MockMvcBuilders
77
+ .standaloneSetup (workflowController )
78
+ .build ();
79
+
80
+ // Error in validation, go to index to show error
81
+ mockMvc .perform (post ("/" )
82
+ .param ("githubURL" , "invalidurl" ))
83
+ .andExpect (status ().isOk ())
84
+ .andExpect (view ().name ("index" ));
85
+
86
+ // Valid directory URL redirect
87
+ mockMvc .perform (post ("/" )
88
+ .param ("githubURL" , "https://github.com/owner/repoName/tree/branch/path/within" ))
89
+ .andExpect (status ().isFound ())
90
+ .andExpect (redirectedUrl ("/workflows/github.com/owner/repoName/tree/branch/path/within" ));
91
+
92
+ // Invalid workflow URL, go to index to show error
93
+ mockMvc .perform (post ("/" )
94
+ .param ("githubURL" , "https://github.com/owner/repoName/tree/branch/path/nonexistant.cwl" ))
95
+ .andExpect (status ().isOk ())
96
+ .andExpect (view ().name ("index" ));
97
+
98
+ // Valid workflow URL redirect
99
+ mockMvc .perform (post ("/" )
100
+ .param ("githubURL" , "https://github.com/owner/repoName/tree/branch/path/workflow.cwl" ))
101
+ .andExpect (status ().isFound ())
102
+ .andExpect (redirectedUrl ("/workflows/github.com/owner/repoName/tree/branch/path/workflow.cwl" ));
103
+
104
+ }
105
+
106
+ /**
107
+ * Endpoint for downloading RO bundle for a workflow
108
+ */
109
+ @ Test
110
+ public void downloadROBundle () throws Exception {
111
+
112
+ // Mock service to return a bundle file and then throw ROBundleNotFoundException
113
+ WorkflowService mockWorkflowService = Mockito .mock (WorkflowService .class );
114
+ when (mockWorkflowService .getROBundle (anyString ()))
115
+ .thenReturn (roBundleFolder .newFile ("bundle.zip" ).getAbsoluteFile ())
116
+ .thenThrow (new ROBundleNotFoundException ());
117
+
118
+ // Mock controller/MVC
119
+ WorkflowController workflowController = new WorkflowController (
120
+ Mockito .mock (WorkflowFormValidator .class ),
121
+ mockWorkflowService ,
122
+ Mockito .mock (GraphVizService .class ));
123
+ MockMvc mockMvc = MockMvcBuilders
124
+ .standaloneSetup (workflowController )
125
+ .build ();
126
+
127
+ // Bundle exists and can be downloaded
128
+ mockMvc .perform (get ("/workflows/workflowid/download" ))
129
+ .andExpect (status ().isOk ())
130
+ .andExpect (content ().contentType ("application/vnd.wf4ever.robundle+zip" ));
131
+
132
+ // Bundle does not exist, 404 error
133
+ mockMvc .perform (get ("/workflows/workflowid/download" ))
134
+ .andExpect (status ().isNotFound ());
135
+
136
+ }
137
+
138
+ /**
139
+ * Endpoints for downloading GraphViz graph files
140
+ */
141
+ @ Test
142
+ public void downloadGraphVizFiles () throws Exception {
143
+
144
+ // Mock service to return mock workflow
145
+ Workflow mockWorkflow = Mockito .mock (Workflow .class );
146
+ WorkflowService mockWorkflowService = Mockito .mock (WorkflowService .class );
147
+ when (mockWorkflowService .getWorkflow (anyString ()))
148
+ .thenReturn (mockWorkflow )
149
+ .thenReturn (null )
150
+ .thenReturn (mockWorkflow )
151
+ .thenReturn (null )
152
+ .thenReturn (mockWorkflow )
153
+ .thenReturn (null );
154
+
155
+ // Mock service to return files
156
+ GraphVizService mockGraphVizService = Mockito .mock (GraphVizService .class );
157
+ when (mockGraphVizService .getGraph (anyString (), anyString (), anyString ()))
158
+ .thenReturn (roBundleFolder .newFile ("graph.svg" ).getAbsoluteFile ())
159
+ .thenReturn (roBundleFolder .newFile ("graph.png" ).getAbsoluteFile ())
160
+ .thenReturn (roBundleFolder .newFile ("graph.dot" ).getAbsoluteFile ());
161
+
162
+ // Mock controller/MVC
163
+ WorkflowController workflowController = new WorkflowController (
164
+ Mockito .mock (WorkflowFormValidator .class ),
165
+ mockWorkflowService ,
166
+ mockGraphVizService );
167
+ MockMvc mockMvc = MockMvcBuilders
168
+ .standaloneSetup (workflowController )
169
+ .build ();
170
+
171
+ // Image exists and can be downloaded
172
+ mockMvc .perform (get ("/workflows/workflowid/graph/svg" ))
173
+ .andExpect (status ().isOk ())
174
+ .andExpect (content ().contentType ("image/svg+xml" ));
175
+
176
+ // Image does not exist, 404 error
177
+ mockMvc .perform (get ("/workflows/workflowid/graph/svg" ))
178
+ .andExpect (status ().isNotFound ());
179
+
180
+ // Image exists and can be downloaded
181
+ mockMvc .perform (get ("/workflows/workflowid/graph/png" ))
182
+ .andExpect (status ().isOk ())
183
+ .andExpect (content ().contentType ("image/png" ));
184
+
185
+ // Image does not exist, 404 error
186
+ mockMvc .perform (get ("/workflows/workflowid/graph/png" ))
187
+ .andExpect (status ().isNotFound ());
188
+
189
+ // Image exists and can be downloaded
190
+ mockMvc .perform (get ("/workflows/workflowid/graph/xdot" ))
191
+ .andExpect (status ().isOk ())
192
+ .andExpect (content ().contentType ("text/vnd.graphviz" ));
193
+
194
+ // Image does not exist, 404 error
195
+ mockMvc .perform (get ("/workflows/workflowid/graph/xdot" ))
196
+ .andExpect (status ().isNotFound ());
197
+
198
+ }
199
+
200
+ }
0 commit comments