Skip to content

Commit 8bcff23

Browse files
author
Mark Robinson
committed
Partial testing of controllers using Spring MockMVC
1 parent aeb0b9b commit 8bcff23

File tree

4 files changed

+270
-1
lines changed

4 files changed

+270
-1
lines changed

src/main/java/org/commonwl/view/workflow/WorkflowController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public ModelAndView newWorkflowFromGithubURL(@Valid WorkflowForm workflowForm, B
9494
// Run validator which checks the github URL is valid
9595
GithubDetails githubInfo = workflowFormValidator.validateAndParse(workflowForm, bindingResult);
9696

97-
if (bindingResult.hasErrors()) {
97+
if (bindingResult.hasErrors() || githubInfo == null) {
9898
// Go back to index if there are validation errors
9999
return new ModelAndView("index");
100100
} else {

src/main/java/org/commonwl/view/workflow/WorkflowForm.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ public class WorkflowForm {
2727

2828
private String githubURL;
2929

30+
public WorkflowForm() {}
31+
3032
public WorkflowForm(String githubURL) {
3133
if (githubURL != null) {
3234
this.githubURL = trimTrailingSlashes(githubURL);
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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;
21+
22+
import org.junit.Before;
23+
import org.junit.Test;
24+
import org.springframework.test.web.servlet.MockMvc;
25+
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
26+
27+
import static org.hamcrest.beans.HasPropertyWithValue.hasProperty;
28+
import static org.hamcrest.core.Is.is;
29+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
30+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
31+
32+
public class PageControllerTest {
33+
34+
private MockMvc mockMvc;
35+
36+
@Before
37+
public void setup() {
38+
mockMvc = MockMvcBuilders
39+
.standaloneSetup(new PageController())
40+
.build();
41+
}
42+
43+
/**
44+
* Standard homepage
45+
*/
46+
@Test
47+
public void homePage() throws Exception {
48+
mockMvc.perform(get("/"))
49+
.andExpect(status().isOk())
50+
.andExpect(view().name("index"))
51+
.andExpect(model().attributeExists("workflowForm"));
52+
}
53+
54+
/**
55+
* Homepage with default URL to be used in form
56+
*/
57+
@Test
58+
public void homePageWithURL() throws Exception {
59+
mockMvc.perform(get("/").param("url", "https://github.com/test/repo"))
60+
.andExpect(status().isOk())
61+
.andExpect(view().name("index"))
62+
.andExpect(model().attributeExists("workflowForm"))
63+
.andExpect(model().attribute("workflowForm",
64+
hasProperty("githubURL", is("https://github.com/test/repo"))));
65+
}
66+
67+
}
Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
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

Comments
 (0)