Skip to content

Commit e306480

Browse files
author
Mark Robinson
committed
Add test for WorkflowFormValidator
1 parent ee03b11 commit e306480

File tree

4 files changed

+136
-6
lines changed

4 files changed

+136
-6
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@ public class GitHubService {
5454
private final CommitService commitService;
5555

5656
// URL validation for directory links
57-
private final String GITHUB_DIR_REGEX = "^https?:\\/\\/github\\.com\\/([A-Za-z0-9_.-]+)\\/([A-Za-z0-9_.-]+)\\/?(?:(?:tree|blob)\\/([^/]+)\\/?(.*)?)?$";
58-
private final Pattern githubDirPattern = Pattern.compile(GITHUB_DIR_REGEX);
57+
private static final String GITHUB_DIR_REGEX = "^https?:\\/\\/github\\.com\\/([A-Za-z0-9_.-]+)\\/([A-Za-z0-9_.-]+)\\/?(?:(?:tree|blob)\\/([^/]+)\\/?(.*)?)?$";
58+
private static final Pattern githubDirPattern = Pattern.compile(GITHUB_DIR_REGEX);
5959

6060
// URL validation for cwl files
61-
private final String GITHUB_CWL_REGEX = "^https?:\\/\\/github\\.com\\/([A-Za-z0-9_.-]+)\\/([A-Za-z0-9_.-]+)\\/?(?:tree|blob)\\/([^/]+)(?:\\/(.+\\.cwl))$";
62-
private final Pattern githubCwlPattern = Pattern.compile(GITHUB_CWL_REGEX);
61+
private static final String GITHUB_CWL_REGEX = "^https?:\\/\\/github\\.com\\/([A-Za-z0-9_.-]+)\\/([A-Za-z0-9_.-]+)\\/?(?:tree|blob)\\/([^/]+)(?:\\/(.+\\.cwl))$";
62+
private static final Pattern githubCwlPattern = Pattern.compile(GITHUB_CWL_REGEX);
6363

6464
private final boolean downloadWithAPI;
6565

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

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

2828
private String githubURL;
2929

30-
public WorkflowForm() {}
31-
3230
public WorkflowForm(String githubURL) {
3331
if (githubURL != null) {
3432
this.githubURL = trimTrailingSlashes(githubURL);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ public String answer(InvocationOnMock invocation) throws Throwable {
8888
when(mockGithubService.downloadFile(anyObject())).thenAnswer(fileAnswer);
8989
when(mockGithubService.downloadFile(anyObject(), anyObject())).thenAnswer(fileAnswer);
9090

91+
// TODO: Also add mock for directories and account for their path
9192
Answer contentsAnswer = new Answer<List<RepositoryContents>>() {
9293
@Override
9394
public List<RepositoryContents> answer(InvocationOnMock invocation) throws Throwable {
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package org.commonwl.view.workflow;
2+
3+
import org.commonwl.view.github.GitHubService;
4+
import org.commonwl.view.github.GithubDetails;
5+
import org.junit.Before;
6+
import org.junit.Test;
7+
import org.mockito.Mockito;
8+
import org.springframework.validation.BeanPropertyBindingResult;
9+
import org.springframework.validation.Errors;
10+
11+
import java.io.IOException;
12+
13+
import static org.junit.Assert.*;
14+
import static org.mockito.Matchers.anyObject;
15+
import static org.mockito.Matchers.anyString;
16+
import static org.mockito.Mockito.when;
17+
18+
/**
19+
* Tests the validator. Parsing is already checked in GithubServiceTest
20+
*/
21+
public class WorkflowFormValidatorTest {
22+
23+
/**
24+
* Workflow form validator to test
25+
*/
26+
private WorkflowFormValidator workflowFormValidator;
27+
28+
/**
29+
* Set up new validator with mock Github service
30+
*/
31+
@Before
32+
public void setUp() throws Exception {
33+
// Mock Github service which always returns non-null for downloads
34+
GitHubService mockGithubService = Mockito.mock(GitHubService.class);
35+
when(mockGithubService.downloadFile(anyObject())).thenReturn("");
36+
when(mockGithubService.detailsFromDirURL(anyString())).thenCallRealMethod();
37+
when(mockGithubService.detailsFromFileURL(anyString())).thenCallRealMethod();
38+
39+
workflowFormValidator = new WorkflowFormValidator(mockGithubService);
40+
}
41+
42+
/**
43+
* Github Directory URL
44+
*/
45+
@Test
46+
public void directoryURL() throws Exception {
47+
48+
WorkflowForm dirURL = new WorkflowForm("https://github.com/common-workflow-language/cwltool/tree/master/cwltool/schemas");
49+
50+
Errors errors = new BeanPropertyBindingResult(dirURL, "workflowForm");
51+
GithubDetails details = workflowFormValidator.validateAndParse(dirURL, errors);
52+
53+
assertNotNull(details);
54+
assertFalse(errors.hasErrors());
55+
56+
}
57+
58+
/**
59+
* Github File URL
60+
*/
61+
@Test
62+
public void fileURL() throws Exception {
63+
64+
WorkflowForm fileURL = new WorkflowForm("https://github.com/nlesc-sherlock/deeplearning/blob/master/CWLworkflow/pipeline.cwl");
65+
66+
Errors errors = new BeanPropertyBindingResult(fileURL, "workflowForm");
67+
GithubDetails details = workflowFormValidator.validateAndParse(fileURL, errors);
68+
69+
assertNotNull(details);
70+
assertFalse(errors.hasErrors());
71+
72+
}
73+
74+
/**
75+
* Empty URL
76+
*/
77+
@Test
78+
public void emptyURL() throws Exception {
79+
80+
WorkflowForm emptyURL = new WorkflowForm("");
81+
82+
Errors errors = new BeanPropertyBindingResult(emptyURL, "workflowForm");
83+
GithubDetails willBeNull = workflowFormValidator.validateAndParse(emptyURL, errors);
84+
85+
assertNull(willBeNull);
86+
assertTrue(errors.hasErrors());
87+
88+
}
89+
90+
/**
91+
* Invalid URL
92+
*/
93+
@Test
94+
public void invalidURL() throws Exception {
95+
96+
WorkflowForm invalidURL = new WorkflowForm("https://google.com/clearly/not/github/url");
97+
98+
Errors errors = new BeanPropertyBindingResult(invalidURL, "workflowForm");
99+
GithubDetails details = workflowFormValidator.validateAndParse(invalidURL, errors);
100+
101+
assertNull(details);
102+
assertTrue(errors.hasErrors());
103+
104+
}
105+
106+
/**
107+
* Invalid URL
108+
*/
109+
@Test
110+
public void cannotDownloadFile() throws Exception {
111+
112+
// Mock Github service which always throws an exception for downloads
113+
GitHubService mockGithubService = Mockito.mock(GitHubService.class);
114+
when(mockGithubService.downloadFile(anyObject())).thenThrow(new IOException("404 Error"));
115+
when(mockGithubService.detailsFromDirURL(anyString())).thenCallRealMethod();
116+
when(mockGithubService.detailsFromFileURL(anyString())).thenCallRealMethod();
117+
118+
WorkflowFormValidator validator = new WorkflowFormValidator(mockGithubService);
119+
120+
WorkflowForm validFileURL = new WorkflowForm("https://github.com/nlesc-sherlock/deeplearning/blob/master/CWLworkflow/pipeline.cwl");
121+
122+
Errors errors = new BeanPropertyBindingResult(validFileURL, "workflowForm");
123+
GithubDetails details = validator.validateAndParse(validFileURL, errors);
124+
125+
assertNull(details);
126+
assertTrue(errors.hasErrors());
127+
128+
}
129+
130+
131+
}

0 commit comments

Comments
 (0)