Skip to content

Commit 18fb502

Browse files
MarkRobboMark Robinson
authored andcommitted
Add test for adding workflows in API
1 parent 20df607 commit 18fb502

File tree

2 files changed

+86
-4
lines changed

2 files changed

+86
-4
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,13 @@ public ResponseEntity<?> newWorkflowFromGithubURLJson(@RequestParam(value="url")
100100
GithubDetails githubInfo = workflowFormValidator.validateAndParse(workflowForm, errors);
101101

102102
if (errors.hasErrors() || githubInfo == null) {
103-
Map<String, String> message = Collections.singletonMap("message", "Error: " +
104-
errors.getAllErrors().get(0).getDefaultMessage());
103+
String error;
104+
if (errors.hasErrors()) {
105+
error = errors.getAllErrors().get(0).getDefaultMessage();
106+
} else {
107+
error = "Could not parse workflow details from URL";
108+
}
109+
Map<String, String> message = Collections.singletonMap("message", "Error: " + error);
105110
return new ResponseEntity<Map>(message, HttpStatus.BAD_REQUEST);
106111
} else {
107112
if (githubInfo.getPath().endsWith(".cwl")) {

src/test/java/org/commonwl/view/workflow/WorkflowRESTControllerTest.java

Lines changed: 79 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package org.commonwl.view.workflow;
2121

2222
import org.commonwl.view.cwl.CWLToolStatus;
23+
import org.commonwl.view.cwl.CWLValidationException;
2324
import org.commonwl.view.github.GithubDetails;
2425
import org.junit.Test;
2526
import org.junit.runner.RunWith;
@@ -31,10 +32,10 @@
3132
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
3233

3334
import static org.hamcrest.Matchers.is;
34-
import static org.mockito.Matchers.any;
35-
import static org.mockito.Matchers.anyString;
35+
import static org.mockito.Matchers.*;
3636
import static org.mockito.Mockito.when;
3737
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
38+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
3839
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
3940

4041
/**
@@ -44,6 +45,82 @@
4445
@SpringBootTest
4546
public class WorkflowRESTControllerTest {
4647

48+
@Test
49+
public void newWorkflowFromGithubURLJson() throws Exception {
50+
51+
// Validator pass or fail
52+
WorkflowFormValidator mockValidator = Mockito.mock(WorkflowFormValidator.class);
53+
when(mockValidator.validateAndParse(anyObject(), anyObject()))
54+
.thenReturn(null)
55+
.thenReturn(new GithubDetails("owner", "repoName", "branch", "path/within"))
56+
.thenReturn(new GithubDetails("owner", "repoName", "branch", "path/workflow.cwl"));
57+
58+
// The eventual accepted valid workflow
59+
Workflow mockWorkflow = Mockito.mock(Workflow.class);
60+
when(mockWorkflow.getRetrievedFrom())
61+
.thenReturn(new GithubDetails("owner", "repoName", "branch", "path/workflow.cwl"));
62+
QueuedWorkflow mockQueuedWorkflow = Mockito.mock(QueuedWorkflow.class);
63+
when(mockQueuedWorkflow.getId())
64+
.thenReturn("123");
65+
when(mockQueuedWorkflow.getTempRepresentation())
66+
.thenReturn(mockWorkflow);
67+
68+
// Mock workflow service returning valid workflow
69+
WorkflowService mockWorkflowService = Mockito.mock(WorkflowService.class);
70+
when(mockWorkflowService.getWorkflow(any(GithubDetails.class)))
71+
.thenReturn(mockWorkflow)
72+
.thenReturn(null);
73+
when(mockWorkflowService.createQueuedWorkflow(anyObject()))
74+
.thenThrow(new CWLValidationException("Error"))
75+
.thenReturn(mockQueuedWorkflow);
76+
77+
// Mock controller/MVC
78+
WorkflowRESTController workflowRESTController = new WorkflowRESTController(
79+
mockValidator,
80+
mockWorkflowService);
81+
82+
MockMvc mockMvc = MockMvcBuilders
83+
.standaloneSetup(workflowRESTController)
84+
.build();
85+
86+
// Error in validation
87+
mockMvc.perform(post("/workflows")
88+
.param("url", "invalidurl")
89+
.accept(MediaType.APPLICATION_JSON_UTF8))
90+
.andExpect(status().isBadRequest())
91+
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
92+
.andExpect(jsonPath("$.message", is("Error: Could not parse workflow details from URL")));
93+
94+
// Directory URL is error
95+
mockMvc.perform(post("/workflows")
96+
.param("url", "https://github.com/owner/repoName/tree/branch/path/within")
97+
.accept(MediaType.APPLICATION_JSON_UTF8))
98+
.andExpect(status().isBadRequest())
99+
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
100+
.andExpect(jsonPath("$.message", is("Error: URL provided was not a .cwl file")));
101+
102+
// Workflow already exists
103+
mockMvc.perform(post("/workflows")
104+
.param("url", "https://github.com/owner/repoName/tree/branch/path/workflow.cwl")
105+
.accept(MediaType.APPLICATION_JSON_UTF8))
106+
.andExpect(status().isSeeOther())
107+
.andExpect(header().string("Location", is("/workflows/github.com/owner/repoName/tree/branch/path/workflow.cwl")));
108+
109+
// Error creating the workflow
110+
mockMvc.perform(post("/workflows")
111+
.param("url", "https://github.com/owner/repoName/tree/branch/path/workflow.cwl")
112+
.accept(MediaType.APPLICATION_JSON_UTF8))
113+
.andExpect(status().isBadRequest());
114+
115+
// Success
116+
mockMvc.perform(post("/workflows")
117+
.param("url", "https://github.com/owner/repoName/tree/branch/path/workflow.cwl")
118+
.accept(MediaType.APPLICATION_JSON_UTF8))
119+
.andExpect(status().isAccepted())
120+
.andExpect(header().string("Location", is("/queue/123")));
121+
122+
}
123+
47124
/**
48125
* Get a workflow from the database and return the JSON format
49126
*/

0 commit comments

Comments
 (0)