25
25
import org .junit .Rule ;
26
26
import org .junit .Test ;
27
27
import org .junit .rules .TemporaryFolder ;
28
+ import org .junit .runner .RunWith ;
29
+ import org .mockito .Matchers ;
28
30
import org .mockito .Mockito ;
31
+ import org .springframework .boot .test .context .SpringBootTest ;
32
+ import org .springframework .data .web .PageableHandlerMethodArgumentResolver ;
33
+ import org .springframework .test .context .junit4 .SpringRunner ;
29
34
import org .springframework .test .web .servlet .MockMvc ;
30
35
import org .springframework .test .web .servlet .setup .MockMvcBuilders ;
36
+ import org .springframework .web .servlet .view .InternalResourceViewResolver ;
31
37
38
+ import java .io .IOException ;
39
+ import java .util .ArrayList ;
40
+ import java .util .Arrays ;
41
+
42
+ import static org .hamcrest .Matchers .*;
43
+ import static org .hamcrest .core .Is .is ;
32
44
import static org .mockito .Matchers .anyObject ;
33
45
import static org .mockito .Matchers .anyString ;
34
46
import static org .mockito .Mockito .when ;
35
47
import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .get ;
36
48
import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .post ;
37
49
import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .*;
38
50
51
+ /**
52
+ * Tests the controller for workflow related functionality
53
+ */
54
+ @ RunWith (SpringRunner .class )
55
+ @ SpringBootTest
39
56
public class WorkflowControllerTest {
40
57
41
58
/**
@@ -44,6 +61,38 @@ public class WorkflowControllerTest {
44
61
@ Rule
45
62
public TemporaryFolder roBundleFolder = new TemporaryFolder ();
46
63
64
+ /**
65
+ * Get the full list of workflows
66
+ * TODO: Mock the repository and test model attributes
67
+ */
68
+ @ Test
69
+ public void getListOfWorkflows () throws Exception {
70
+
71
+ // Mock controller/MVC
72
+ WorkflowController workflowController = new WorkflowController (
73
+ Mockito .mock (WorkflowFormValidator .class ),
74
+ Mockito .mock (WorkflowService .class ),
75
+ Mockito .mock (GraphVizService .class ));
76
+
77
+ // Lots of hassle to make Spring Data Pageable work
78
+ PageableHandlerMethodArgumentResolver pageableArgumentResolver =
79
+ new PageableHandlerMethodArgumentResolver ();
80
+ InternalResourceViewResolver viewResolver = new InternalResourceViewResolver ();
81
+ viewResolver .setPrefix ("/src/main/resources/templates" );
82
+ viewResolver .setSuffix (".html" );
83
+ MockMvc mockMvc = MockMvcBuilders
84
+ .standaloneSetup (workflowController )
85
+ .setCustomArgumentResolvers (pageableArgumentResolver )
86
+ .setViewResolvers (viewResolver )
87
+ .build ();
88
+
89
+ // Simple test to check the view
90
+ mockMvc .perform (get ("/workflows" ))
91
+ .andExpect (status ().isOk ())
92
+ .andExpect (view ().name ("workflows" ));
93
+
94
+ }
95
+
47
96
/**
48
97
* Endpoint for main form submission
49
98
*/
@@ -104,14 +153,70 @@ public void newWorkflowFromGithubURL() throws Exception {
104
153
}
105
154
106
155
/**
107
- * Endpoint for displaying workflows and directories of workflows
156
+ * Displaying workflows
108
157
*/
109
158
@ Test
110
- public void getWorkflowByGithubDetails () throws Exception {
159
+ public void directWorkflowURL () throws Exception {
111
160
112
- // Mock service to return a bundle file and then throw ROBundleNotFoundException
161
+ Workflow mockWorkflow = Mockito .mock (Workflow .class );
162
+ Workflow mockWorkflow2 = Mockito .mock (Workflow .class );
163
+
164
+ // Mock service
113
165
WorkflowService mockWorkflowService = Mockito .mock (WorkflowService .class );
166
+ when (mockWorkflowService .getWorkflow (Matchers .<GithubDetails >anyObject ()))
167
+ .thenReturn (mockWorkflow )
168
+ .thenReturn (null );
169
+ when (mockWorkflowService .createWorkflow (anyObject ()))
170
+ .thenReturn (mockWorkflow2 )
171
+ .thenReturn (null );
172
+
173
+ // Mock controller/MVC
174
+ WorkflowController workflowController = new WorkflowController (
175
+ Mockito .mock (WorkflowFormValidator .class ),
176
+ mockWorkflowService ,
177
+ Mockito .mock (GraphVizService .class ));
178
+ MockMvc mockMvc = MockMvcBuilders
179
+ .standaloneSetup (workflowController )
180
+ .build ();
114
181
182
+ // Workflow already exists in the database
183
+ mockMvc .perform (get ("/workflows/github.com/owner/reponame/tree/branch/path/within/workflow.cwl" ))
184
+ .andExpect (status ().isOk ())
185
+ .andExpect (view ().name ("workflow" ))
186
+ .andExpect (model ().attribute ("workflow" , is (mockWorkflow )));
187
+
188
+ // Workflow needs to be created
189
+ mockMvc .perform (get ("/workflows/github.com/owner/reponame/tree/branch/path/within/workflow.cwl" ))
190
+ .andExpect (status ().isOk ())
191
+ .andExpect (view ().name ("workflow" ))
192
+ .andExpect (model ().attribute ("workflow" , is (mockWorkflow2 )));
193
+
194
+ // Error creating workflow
195
+ mockMvc .perform (get ("/workflows/github.com/owner/reponame/tree/branch/path/within/badworkflow.cwl" ))
196
+ .andExpect (status ().isFound ())
197
+ .andExpect (flash ().attributeExists ("errors" ))
198
+ .andExpect (redirectedUrl ("/?url=https://github.com/owner/reponame/tree/branch/path/within/badworkflow.cwl" ));
199
+
200
+ }
201
+
202
+ /**
203
+ * Displaying directories of workflows
204
+ */
205
+ @ Test
206
+ public void directDirectoryURL () throws Exception {
207
+
208
+ // Workflow overviews for testing
209
+ WorkflowOverview overview1 = new WorkflowOverview ("workflow1.cwl" , "label1" , "doc1" );
210
+ WorkflowOverview overview2 = new WorkflowOverview ("workflow2.cwl" , "label2" , "doc2" );
211
+
212
+ // Mock service to return these overviews
213
+ WorkflowService mockWorkflowService = Mockito .mock (WorkflowService .class );
214
+ when (mockWorkflowService .getWorkflowsFromDirectory (anyObject ()))
215
+ .thenReturn (new ArrayList <>())
216
+ .thenReturn (new ArrayList <>(Arrays .asList (overview1 )))
217
+ .thenReturn (new ArrayList <>(Arrays .asList (overview1 , overview2 )))
218
+ .thenReturn (new ArrayList <>(Arrays .asList (overview1 , overview2 )))
219
+ .thenThrow (new IOException ("Error getting contents" ));
115
220
116
221
// Mock controller/MVC
117
222
WorkflowController workflowController = new WorkflowController (
@@ -122,9 +227,41 @@ public void getWorkflowByGithubDetails() throws Exception {
122
227
.standaloneSetup (workflowController )
123
228
.build ();
124
229
125
- // Redirect with error
230
+ // No workflows in directory, redirect with errors
231
+ mockMvc .perform (get ("/workflows/github.com/owner/reponame/tree/branch/path/within" ))
232
+ .andExpect (status ().isFound ())
233
+ .andExpect (flash ().attributeExists ("errors" ))
234
+ .andExpect (redirectedUrl ("/?url=https://github.com/owner/reponame/tree/branch/path/within" ));
235
+
236
+ // 1 workflow in directory, redirect to it
237
+ mockMvc .perform (get ("/workflows/github.com/common-workflow-language/workflows/tree/master/workflows/lobSTR" ))
238
+ .andExpect (status ().isFound ())
239
+ .andExpect (redirectedUrl ("/workflows/github.com/common-workflow-language/workflows/tree/master/workflows/lobSTR/workflow1.cwl" ));
240
+
241
+ // Multiple workflows in directory, show list
242
+ mockMvc .perform (get ("/workflows/github.com/common-workflow-language/workflows/tree/visu/workflows/scidap" ))
243
+ .andExpect (status ().isOk ())
244
+ .andExpect (view ().name ("selectworkflow" ))
245
+ .andExpect (model ().attribute ("githubDetails" , allOf (
246
+ hasProperty ("owner" , is ("common-workflow-language" )),
247
+ hasProperty ("repoName" , is ("workflows" )),
248
+ hasProperty ("branch" , is ("visu" )),
249
+ hasProperty ("path" , is ("workflows/scidap" ))
250
+ )))
251
+ .andExpect (model ().attribute ("workflowOverviews" ,
252
+ containsInAnyOrder (overview1 , overview2 )));
253
+
254
+ // Workflows at the base of a repository
255
+ mockMvc .perform (get ("/workflows/github.com/genome/arvados_trial/tree/master" ))
256
+ .andExpect (status ().isOk ())
257
+ .andExpect (view ().name ("selectworkflow" ))
258
+ .andExpect (model ().attribute ("githubDetails" ,
259
+ hasProperty ("path" , is ("/" ))));
260
+
261
+ // Error getting contents of Github directory, redirect with errors
126
262
mockMvc .perform (get ("/workflows/github.com/owner/reponame/tree/branch/path/within" ))
127
263
.andExpect (status ().isFound ())
264
+ .andExpect (flash ().attributeExists ("errors" ))
128
265
.andExpect (redirectedUrl ("/?url=https://github.com/owner/reponame/tree/branch/path/within" ));
129
266
130
267
}
0 commit comments