|
20 | 20 | package org.commonwl.view.workflow;
|
21 | 21 |
|
22 | 22 | import org.commonwl.view.cwl.CWLToolStatus;
|
| 23 | +import org.commonwl.view.cwl.CWLValidationException; |
23 | 24 | import org.commonwl.view.github.GithubDetails;
|
24 | 25 | import org.junit.Test;
|
25 | 26 | import org.junit.runner.RunWith;
|
|
31 | 32 | import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
32 | 33 |
|
33 | 34 | 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.*; |
36 | 36 | import static org.mockito.Mockito.when;
|
37 | 37 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
| 38 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; |
38 | 39 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
39 | 40 |
|
40 | 41 | /**
|
|
44 | 45 | @SpringBootTest
|
45 | 46 | public class WorkflowRESTControllerTest {
|
46 | 47 |
|
| 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 | + |
47 | 124 | /**
|
48 | 125 | * Get a workflow from the database and return the JSON format
|
49 | 126 | */
|
|
0 commit comments