Skip to content

Commit d65c213

Browse files
committed
Parameterise packed ID in API
1 parent b343769 commit d65c213

File tree

6 files changed

+84
-22
lines changed

6 files changed

+84
-22
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public class WorkflowForm {
2828
private String url;
2929
private String branch;
3030
private String path;
31+
private String packedId;
3132

3233
public WorkflowForm() {}
3334

@@ -41,6 +42,13 @@ public WorkflowForm(String url, String branch, String path) {
4142
this.path = path;
4243
}
4344

45+
public WorkflowForm(String url, String branch, String path, String packedId) {
46+
setUrl(url);
47+
this.branch = branch;
48+
this.path = path;
49+
this.packedId = packedId;
50+
}
51+
4452
public String getUrl() {
4553
return url;
4654
}
@@ -67,6 +75,14 @@ public void setPath(String path) {
6775
this.path = path;
6876
}
6977

78+
public String getPackedId() {
79+
return packedId;
80+
}
81+
82+
public void setPackedId(String packedId) {
83+
this.packedId = packedId;
84+
}
85+
7086
/**
7187
* Cuts any trailing slashes off a string
7288
* @param url The string to cut the slashes off

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,16 @@ public GitDetails validateAndParse(WorkflowForm form, Errors e) {
7373
String repoUrl = null;
7474
String branch = null;
7575
String path = null;
76+
String packedId = null;
7677
if (!isEmptyOrWhitespace(form.getBranch())) {
7778
branch = form.getBranch();
7879
}
7980
if (!isEmptyOrWhitespace(form.getPath())) {
8081
path = form.getPath();
8182
}
83+
if (!isEmptyOrWhitespace(form.getPackedId())) {
84+
packedId = form.getPackedId();
85+
}
8286

8387
// Github URL
8488
Matcher m = githubCwlPattern.matcher(form.getUrl());
@@ -114,11 +118,15 @@ public GitDetails validateAndParse(WorkflowForm form, Errors e) {
114118

115119
// Split off packed ID if present
116120
if (repoUrl != null) {
117-
String[] pathSplit = path.split("#");
118121
GitDetails details = new GitDetails(repoUrl, branch, path);
119-
if (pathSplit.length > 1) {
120-
details.setPath(pathSplit[pathSplit.length - 2]);
121-
details.setPackedId(pathSplit[pathSplit.length - 1]);
122+
if (packedId != null) {
123+
details.setPackedId(packedId);
124+
} else {
125+
String[] pathSplit = path.split("#");
126+
if (pathSplit.length > 1) {
127+
details.setPath(pathSplit[pathSplit.length - 2]);
128+
details.setPackedId(pathSplit[pathSplit.length - 1]);
129+
}
122130
}
123131
return details;
124132
}
@@ -128,7 +136,9 @@ public GitDetails validateAndParse(WorkflowForm form, Errors e) {
128136
if (!e.hasErrors()) {
129137
m = gitRepoPattern.matcher(form.getUrl());
130138
if (m.find()) {
131-
return new GitDetails(form.getUrl(), form.getBranch(), form.getPath());
139+
GitDetails details = new GitDetails(form.getUrl(), form.getBranch(), form.getPath());
140+
details.setPackedId(form.getPackedId());
141+
return details;
132142
}
133143
}
134144

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,16 +89,18 @@ public Page<Workflow> searchWorkflowsJson(Model model,
8989
* @param url The URL of the workflow
9090
* @param branch The branch where the workflow can be found
9191
* @param path The path within the repository to the workflow file
92+
* @param packedId The ID of the workflow if the file is packed
9293
* @return Appropriate response code and optional JSON string with message
9394
*/
9495
@PostMapping(value = "/workflows", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
9596
public ResponseEntity<?> newWorkflowFromGitURLJson(@RequestParam(value="url") String url,
9697
@RequestParam(value="branch", required=false) String branch,
9798
@RequestParam(value="path", required=false) String path,
99+
@RequestParam(value="packedId", required=false) String packedId,
98100
HttpServletResponse response) {
99101

100102
// Run validator which checks the URL is valid
101-
WorkflowForm workflowForm = new WorkflowForm(url, branch, path);
103+
WorkflowForm workflowForm = new WorkflowForm(url, branch, path, packedId);
102104
BeanPropertyBindingResult errors = new BeanPropertyBindingResult(workflowForm, "errors");
103105
GitDetails gitInfo = workflowFormValidator.validateAndParse(workflowForm, errors);
104106

@@ -130,14 +132,15 @@ public ResponseEntity<?> newWorkflowFromGitURLJson(@RequestParam(value="url") St
130132
}
131133
} else {
132134
// Error with alternatives suggested
135+
List<WorkflowOverview> test = queued.getWorkflowList();
133136
List<String> workflowUris = new ArrayList<>();
134137
for (WorkflowOverview overview : queued.getWorkflowList()) {
135-
workflowUris.add(overview.getFileName());
138+
workflowUris.add(overview.getFileName().substring(1));
136139
}
137140
Map<String, Object> responseMap = new HashMap<>();
138141
responseMap.put("message", "This workflow file is packed and contains multiple workflow " +
139-
"descriptions. Please choose one to add");
140-
responseMap.put("workflows", workflowUris);
142+
"descriptions. Please provide a packedId parameter with one of the following");
143+
responseMap.put("packedId", workflowUris);
141144
return new ResponseEntity<Map>(responseMap, HttpStatus.UNPROCESSABLE_ENTITY);
142145
}
143146
}

src/main/resources/templates/apidocs.html

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ <h4>Parameters</h4>
5959
<tr>
6060
<td>url</td>
6161
<td>String</td>
62-
<td>The URL of the repository. Either a Gitlab or Github URL, or any Git repository URL.</td>
62+
<td>The URL of the repository. Either a Gitlab or Github URL, or any Git repository URL. If a Gitlab or Github repository URL is used, the following fields will take priority over those parsed from the URL if provided.</td>
6363
</tr>
6464
<tr>
6565
<td>branch</td>
@@ -71,6 +71,11 @@ <h4>Parameters</h4>
7171
<td>String</td>
7272
<td>The path within the repository to the workflow file. Required if the URL is not from Gitlab or Github.</td>
7373
</tr>
74+
<tr>
75+
<td>packedId</td>
76+
<td>String</td>
77+
<td>The ID of the workflow, to be provided if the workflow file is packed (contains multiple descriptions with <code>$graph</code>).</td>
78+
</tr>
7479
</table>
7580
<h4>Input</h4>
7681
<pre class="highlight json">
@@ -93,19 +98,19 @@ <h4>Success</h4>
9398
<h4>Already Exists</h4>
9499
<pre class="highlight http">
95100
HTTP/1.1 303 See Other
96-
Location: /workflows/:githubURL
101+
Location: /workflows/:URL
97102
</pre>
98103
<h4>Packed Workflow with Multiple Choices</h4>
99104
<pre class="highlight json">
100105
HTTP/1.1 422 Unprocessable Entity
101106
{
102-
"message": "This workflow file is packed and contains multiple workflow descriptions. Please choose one to add"
103-
"workflows": [
104-
"#workflow.cwl",
105-
"#main",
106-
"#workflow_exome.cwl",
107-
"#align.cwl",
108-
"#workflow.cwl_2"
107+
"message": "This workflow file is packed and contains multiple workflow descriptions. Please provide a packedId parameter with one of the following values"
108+
"packedId": [
109+
"workflow.cwl",
110+
"main",
111+
"workflow_exome.cwl",
112+
"align.cwl",
113+
"workflow.cwl_2"
109114
]
110115
}
111116
</pre>

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040

4141
import java.io.IOException;
4242
import java.util.ArrayList;
43+
import java.util.Collections;
4344
import java.util.List;
4445

4546
import static org.hamcrest.core.Is.is;
@@ -206,11 +207,9 @@ public void directWorkflowURL() throws Exception {
206207
List<WorkflowOverview> listOfTwoOverviews = new ArrayList<>();
207208
listOfTwoOverviews.add(new WorkflowOverview("/workflow1.cwl", "label", "doc"));
208209
listOfTwoOverviews.add(new WorkflowOverview("/workflow2.cwl", "label2", "doc2"));
209-
List<WorkflowOverview> listOfOneOverview = new ArrayList<>();
210-
listOfOneOverview.add(new WorkflowOverview("/workflow1.cwl", "label", "doc"));
211210
when(mockWorkflowService.getWorkflowsFromDirectory(anyObject()))
212211
.thenReturn(listOfTwoOverviews)
213-
.thenReturn(listOfOneOverview);
212+
.thenReturn(Collections.singletonList(new WorkflowOverview("/workflow1.cwl", "label", "doc")));
214213

215214
// Mock controller/MVC
216215
WorkflowController workflowController = new WorkflowController(

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

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@
3131
import org.springframework.test.web.servlet.MockMvc;
3232
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
3333

34+
import java.util.ArrayList;
35+
import java.util.Collections;
36+
import java.util.List;
37+
38+
import static org.hamcrest.Matchers.containsInAnyOrder;
3439
import static org.hamcrest.Matchers.is;
3540
import static org.mockito.Matchers.*;
3641
import static org.mockito.Mockito.when;
@@ -67,6 +72,14 @@ public void newWorkflowFromGithubURLJson() throws Exception {
6772
.thenReturn("123");
6873
when(mockQueuedWorkflow.getTempRepresentation())
6974
.thenReturn(mockWorkflow);
75+
List<WorkflowOverview> listOfTwoOverviews = new ArrayList<>();
76+
listOfTwoOverviews.add(new WorkflowOverview("#packedId1", "label", "doc"));
77+
listOfTwoOverviews.add(new WorkflowOverview("#packedId2", "label2", "doc2"));
78+
when(mockQueuedWorkflow.getWorkflowList())
79+
.thenReturn(null)
80+
.thenReturn(null)
81+
.thenReturn(Collections.singletonList(new WorkflowOverview("#packedId", "Label", "Doc")))
82+
.thenReturn(listOfTwoOverviews);
7083

7184
// Mock workflow service returning valid workflow
7285
WorkflowService mockWorkflowService = Mockito.mock(WorkflowService.class);
@@ -109,11 +122,27 @@ public void newWorkflowFromGithubURLJson() throws Exception {
109122

110123
// Success
111124
mockMvc.perform(post("/workflows")
112-
.param("url", "https://github.com/owner/repoName/tree/branch/path/workflow.cwl")
125+
.param("url", "https://github.com/owner/repoName/tree/branch/path/success.cwl")
113126
.accept(MediaType.APPLICATION_JSON_UTF8))
114127
.andExpect(status().isAccepted())
115128
.andExpect(header().string("Location", is("/queue/123")));
116129

130+
// Packed workflow with one ID is still accepted and parsed using that ID
131+
mockMvc.perform(post("/workflows")
132+
.param("url", "https://github.com/owner/repoName/tree/branch/path/singlePacked.cwl")
133+
.accept(MediaType.APPLICATION_JSON_UTF8))
134+
.andExpect(status().isAccepted())
135+
.andExpect(header().string("Location", is("/queue/123")));
136+
137+
// Packed workflow with multiple IDs is unprocessable
138+
mockMvc.perform(post("/workflows")
139+
.param("url", "https://github.com/owner/repoName/tree/branch/path/multiplePacked.cwl")
140+
.accept(MediaType.APPLICATION_JSON_UTF8))
141+
.andExpect(status().isUnprocessableEntity())
142+
.andExpect(jsonPath("$.message", is("This workflow file is packed and contains multiple workflow " +
143+
"descriptions. Please provide a packedId parameter with one of the following")))
144+
.andExpect(jsonPath("$.packedId", containsInAnyOrder("packedId1", "packedId2")));
145+
117146
}
118147

119148
/**

0 commit comments

Comments
 (0)