Skip to content

Commit 80753ad

Browse files
author
Mark Robinson
committed
Increase test coverage in various areas
1 parent e11e026 commit 80753ad

File tree

5 files changed

+139
-70
lines changed

5 files changed

+139
-70
lines changed

src/main/java/org/commonwl/view/cwl/CWLService.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ private Map<String, CWLStep> getSteps(JsonNode cwlDoc) {
310310
private Map<String, CWLElement> getInputs(JsonNode cwlDoc) {
311311
if (cwlDoc != null) {
312312
if (cwlDoc.has(INPUTS)) {
313-
// For workflow/draft steps
313+
// For all version workflow inputs/outputs and draft steps
314314
return getInputsOutputs(cwlDoc.get(INPUTS));
315315
} else if (cwlDoc.has(IN)) {
316316
// For V1.0 steps
@@ -327,13 +327,11 @@ private Map<String, CWLElement> getInputs(JsonNode cwlDoc) {
327327
*/
328328
private Map<String, CWLElement> getOutputs(JsonNode cwlDoc) {
329329
if (cwlDoc != null) {
330+
// For all version workflow inputs/outputs and draft steps
330331
if (cwlDoc.has(OUTPUTS)) {
331-
// For workflow/draft steps
332332
return getInputsOutputs(cwlDoc.get(OUTPUTS));
333-
} else if (cwlDoc.has(OUT)) {
334-
// For V1.0 steps
335-
return getStepInputsOutputs(cwlDoc.get(OUT));
336333
}
334+
// Outputs are not gathered for v1 steps
337335
}
338336
return null;
339337
}

src/main/java/org/commonwl/view/github/GithubDetails.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public String getURL() {
9797
*/
9898
public String getURL(String ref) {
9999
String url = "https://github.com/" + owner + "/" + repoName + "/tree/" + ref;
100-
if (path != null && path != "/") {
100+
if (path != null && !path.equals("/")) {
101101
url += "/" + this.path;
102102
}
103103
return url;

src/test/java/org/commonwl/view/cwl/CWLServiceTest.java

Lines changed: 130 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,15 @@
2424
import org.commonwl.view.github.GithubDetails;
2525
import org.commonwl.view.workflow.Workflow;
2626
import org.commonwl.view.workflow.WorkflowOverview;
27+
import org.junit.Rule;
2728
import org.junit.Test;
29+
import org.junit.rules.ExpectedException;
2830
import org.mockito.Mockito;
2931
import org.mockito.invocation.InvocationOnMock;
3032
import org.mockito.stubbing.Answer;
3133

3234
import java.io.File;
35+
import java.io.IOException;
3336
import java.util.Map;
3437

3538
import static org.junit.Assert.*;
@@ -38,28 +41,41 @@
3841

3942
public class CWLServiceTest {
4043

44+
/**
45+
* Used for expected IOExceptions for filesize limits
46+
*/
47+
@Rule
48+
public ExpectedException thrown = ExpectedException.none();
49+
50+
/**
51+
* Test main parsing of a the Hello World example (packed CWL version v1.0)
52+
*/
53+
@Test
54+
public void parsePackedHellov1Workflow() throws Exception {
55+
56+
// Test cwl service
57+
GitHubService mockGithubService = getMockGithubService("workflows/hello/",
58+
"src/test/resources/cwl/hello/");
59+
CWLService cwlService = new CWLService(mockGithubService, 5242880);
60+
61+
// Get workflow from community repo by commit ID so it will not change
62+
GithubDetails helloDetails = new GithubDetails("common-workflow-language",
63+
"workflows", null, "workflows/hello/hello.cwl");
64+
Workflow hello = cwlService.parseWorkflow(helloDetails, "920c6be45f08e979e715a0018f22c532b024074f");
65+
66+
assertNotNull(hello);
67+
68+
}
69+
4170
/**
4271
* Test main parsing of a the LobSTR workflow CWL version draft-3
4372
*/
4473
@Test
4574
public void parseLobSTRDraft3Workflow() throws Exception {
4675

47-
// Mock githubService class to redirect downloads to resources folder
48-
GitHubService mockGithubService = Mockito.mock(GitHubService.class);
49-
Answer fileAnswer = new Answer<String>() {
50-
@Override
51-
public String answer(InvocationOnMock invocation) throws Throwable {
52-
Object[] args = invocation.getArguments();
53-
GithubDetails details = (GithubDetails) args[0];
54-
File workflowFile = new File("src/test/resources/cwl/lobstr-draft3/"
55-
+ details.getPath().replace("workflows/lobSTR/", ""));
56-
return FileUtils.readFileToString(workflowFile);
57-
}
58-
};
59-
when(mockGithubService.downloadFile(anyObject())).thenAnswer(fileAnswer);
60-
when(mockGithubService.downloadFile(anyObject(), anyObject())).thenAnswer(fileAnswer);
61-
6276
// Test cwl service
77+
GitHubService mockGithubService = getMockGithubService("workflows/lobSTR/",
78+
"src/test/resources/cwl/lobstr-draft3/");
6379
CWLService cwlService = new CWLService(mockGithubService, 5242880);
6480

6581
// Get workflow from community repo by commit ID so it will not change
@@ -79,22 +95,9 @@ public String answer(InvocationOnMock invocation) throws Throwable {
7995
@Test
8096
public void parseLobSTRv1Workflow() throws Exception {
8197

82-
// Mock githubService class to redirect downloads to resources folder
83-
GitHubService mockGithubService = Mockito.mock(GitHubService.class);
84-
Answer fileAnswer = new Answer<String>() {
85-
@Override
86-
public String answer(InvocationOnMock invocation) throws Throwable {
87-
Object[] args = invocation.getArguments();
88-
GithubDetails details = (GithubDetails) args[0];
89-
File workflowFile = new File("src/test/resources/cwl/lobstr-v1/"
90-
+ details.getPath().replace("workflows/lobSTR/", ""));
91-
return FileUtils.readFileToString(workflowFile);
92-
}
93-
};
94-
when(mockGithubService.downloadFile(anyObject())).thenAnswer(fileAnswer);
95-
when(mockGithubService.downloadFile(anyObject(), anyObject())).thenAnswer(fileAnswer);
96-
9798
// Test cwl service
99+
GitHubService mockGithubService = getMockGithubService("workflows/lobSTR/",
100+
"src/test/resources/cwl/lobstr-v1/");
98101
CWLService cwlService = new CWLService(mockGithubService, 5242880);
99102

100103
// Get workflow from community repo by commit ID so it will not change
@@ -109,6 +112,103 @@ public String answer(InvocationOnMock invocation) throws Throwable {
109112

110113
}
111114

115+
/**
116+
* Test IOException is thrown when files are over limit with parseWorkflow
117+
*/
118+
@Test
119+
public void parseWorkflowOverSingleFileSizeLimitThrowsIOException() throws Exception {
120+
121+
// Test cwl service
122+
GitHubService mockGithubService = getMockGithubService("workflows/hello/",
123+
"src/test/resources/cwl/hello/");
124+
CWLService cwlService = new CWLService(mockGithubService, 0);
125+
126+
// Get workflow from community repo by commit ID so it will not change
127+
GithubDetails helloDetails = new GithubDetails("common-workflow-language",
128+
"workflows", null, "workflows/hello/hello.cwl");
129+
130+
thrown.expect(IOException.class);
131+
thrown.expectMessage("File 'workflows/hello/hello.cwl' is over singleFileSizeLimit - 672 bytes/0 bytes");
132+
cwlService.parseWorkflow(helloDetails, "920c6be45f08e979e715a0018f22c532b024074f");
133+
134+
}
135+
136+
/**
137+
* Test retrieval of a workflow overview for hello world example in cwl
138+
*/
139+
@Test
140+
public void getHelloWorkflowOverview() throws Exception {
141+
142+
// Mock githubService class
143+
GitHubService mockGithubService = Mockito.mock(GitHubService.class);
144+
File workflowFile = new File("src/test/resources/cwl/hello/hello.cwl");
145+
when(mockGithubService.downloadFile(anyObject()))
146+
.thenReturn(FileUtils.readFileToString(workflowFile));
147+
148+
// Test cwl service
149+
CWLService cwlService = new CWLService(mockGithubService, 5242880);
150+
151+
// Run workflow overview
152+
GithubDetails helloDetails = new GithubDetails("common-workflow-language",
153+
"workflows", "8296e92d358bb5da4dc3c6e7aabefa89726e3409", "workflows/hello/hello.cwl");
154+
WorkflowOverview hello = cwlService.getWorkflowOverview(helloDetails);
155+
assertNotNull(hello);
156+
157+
// No docs for this workflow
158+
assertEquals("Hello World", hello.getLabel());
159+
assertEquals("Puts a message into a file using echo", hello.getDoc());
160+
assertEquals("hello.cwl", hello.getFileName());
161+
162+
}
163+
164+
/**
165+
* Test IOException is thrown when files are over limit with getWorkflowOverview
166+
*/
167+
@Test
168+
public void workflowOverviewOverSingleFileSizeLimitThrowsIOException() throws Exception {
169+
170+
// Mock githubService class
171+
GitHubService mockGithubService = Mockito.mock(GitHubService.class);
172+
File workflowFile = new File("src/test/resources/cwl/hello/hello.cwl");
173+
when(mockGithubService.downloadFile(anyObject()))
174+
.thenReturn(FileUtils.readFileToString(workflowFile));
175+
176+
// Test cwl service with 0 filesize limit
177+
CWLService cwlService = new CWLService(mockGithubService, 0);
178+
179+
// Run workflow overview
180+
GithubDetails helloDetails = new GithubDetails("common-workflow-language",
181+
"workflows", "8296e92d358bb5da4dc3c6e7aabefa89726e3409", "workflows/hello/hello.cwl");
182+
183+
// Should throw IOException due to oversized files
184+
thrown.expect(IOException.class);
185+
thrown.expectMessage("File 'workflows/hello/hello.cwl' is over singleFileSizeLimit - 672 bytes/0 bytes");
186+
cwlService.getWorkflowOverview(helloDetails);
187+
188+
}
189+
190+
/**
191+
* Get a mock GithubService which redirects downloads to the filesystem
192+
*/
193+
private GitHubService getMockGithubService(String originalFolder,
194+
String resourcesFolder) throws IOException {
195+
GitHubService mockGithubService = Mockito.mock(GitHubService.class);
196+
Answer fileAnswer = new Answer<String>() {
197+
@Override
198+
public String answer(InvocationOnMock invocation) throws Throwable {
199+
Object[] args = invocation.getArguments();
200+
GithubDetails details = (GithubDetails) args[0];
201+
File workflowFile = new File(resourcesFolder
202+
+ details.getPath().replace(originalFolder, ""));
203+
return FileUtils.readFileToString(workflowFile);
204+
}
205+
};
206+
when(mockGithubService.downloadFile(anyObject())).thenAnswer(fileAnswer);
207+
when(mockGithubService.downloadFile(anyObject(), anyObject())).thenAnswer(fileAnswer);
208+
209+
return mockGithubService;
210+
}
211+
112212
/**
113213
* Validate a LobSTR workflow
114214
* See: https://github.com/common-workflow-language/workflows/tree/master/workflows/lobSTR
@@ -149,32 +249,4 @@ private void testLobSTRWorkflow(Workflow lobSTR) throws Exception {
149249

150250
}
151251

152-
/**
153-
* Test retrieval of a workflow overview for hello world example in cwl
154-
*/
155-
@Test
156-
public void getHelloWorkflowOverview() throws Exception {
157-
158-
// Mock githubService class
159-
GitHubService mockGithubService = Mockito.mock(GitHubService.class);
160-
File workflowFile = new File("src/test/resources/cwl/hello/hello.cwl");
161-
when(mockGithubService.downloadFile(anyObject()))
162-
.thenReturn(FileUtils.readFileToString(workflowFile));
163-
164-
// Test cwl service
165-
CWLService cwlService = new CWLService(mockGithubService, 5242880);
166-
167-
// Get workflow from community repo by commit ID so it will not change
168-
GithubDetails helloDetails = new GithubDetails("common-workflow-language",
169-
"workflows", "8296e92d358bb5da4dc3c6e7aabefa89726e3409", "workflows/hello/hello.cwl");
170-
WorkflowOverview hello = cwlService.getWorkflowOverview(helloDetails);
171-
assertNotNull(hello);
172-
173-
// No docs for this workflow
174-
assertEquals("Hello World", hello.getLabel());
175-
assertEquals("Puts a message into a file using echo", hello.getDoc());
176-
assertEquals("hello.cwl", hello.getFileName());
177-
178-
}
179-
180252
}

src/test/java/org/commonwl/view/researchobject/ROBundleServiceTest.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,6 @@ public List<RepositoryContents> answer(InvocationOnMock invocation) throws Throw
192192
Answer commitsAnswer = new Answer<List<RepositoryCommit>>() {
193193
@Override
194194
public List<RepositoryCommit> answer(InvocationOnMock invocation) throws Throwable {
195-
Object[] args = invocation.getArguments();
196-
GithubDetails details = (GithubDetails) args[0];
197-
198195
// Make up a commit for the file and return it
199196
List<RepositoryCommit> commitList = new ArrayList<>();
200197
RepositoryCommit commit = new RepositoryCommit();

src/test/resources/cwl/lobstr-v1/lobSTR-workflow.cwl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ outputs:
5555
type: File
5656
outputSource: allelotype/vcf_stats
5757

58-
hints:
58+
requirements:
5959
DockerRequirement:
6060
dockerPull: rabix/lobstr
6161

@@ -74,8 +74,10 @@ steps:
7474
samsort:
7575
run: samtools-sort.cwl
7676
in:
77-
input: lobSTR/bam
78-
output_name: {default: "aligned.sorted.bam" }
77+
- id: input
78+
source: lobSTR/bam
79+
- id: output_name
80+
default: "aligned.sorted.bam"
7981
out: [output_file]
8082

8183
samindex:

0 commit comments

Comments
 (0)