Skip to content

Commit f9beb5c

Browse files
committed
Refactor tests for generic git changes
1 parent 1794079 commit f9beb5c

21 files changed

+471
-587
lines changed

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import org.apache.jena.riot.RiotException;
3737
import org.commonwl.view.docker.DockerService;
3838
import org.commonwl.view.git.GitDetails;
39-
import org.commonwl.view.git.GitService;
4039
import org.commonwl.view.graphviz.ModelDotWriter;
4140
import org.commonwl.view.graphviz.RDFDotWriter;
4241
import org.commonwl.view.workflow.Workflow;
@@ -67,7 +66,6 @@ public class CWLService {
6766
private final Logger logger = LoggerFactory.getLogger(this.getClass());
6867

6968
// Autowired properties/services
70-
private final GitService gitService;
7169
private final RDFService rdfService;
7270
private final CWLTool cwlTool;
7371
private final int singleFileSizeLimit;
@@ -98,16 +96,14 @@ public class CWLService {
9896

9997
/**
10098
* Constructor for the Common Workflow Language service
101-
* @param gitService A service for accessing Github functionality
99+
* @param rdfService A service for handling RDF queries
102100
* @param cwlTool Handles cwltool integration
103101
* @param singleFileSizeLimit The file size limit for single files
104102
*/
105103
@Autowired
106-
public CWLService(GitService gitService,
107-
RDFService rdfService,
104+
public CWLService(RDFService rdfService,
108105
CWLTool cwlTool,
109106
@Value("${singleFileSizeLimit}") int singleFileSizeLimit) {
110-
this.gitService = gitService;
111107
this.rdfService = rdfService;
112108
this.cwlTool = cwlTool;
113109
this.singleFileSizeLimit = singleFileSizeLimit;

src/main/java/org/commonwl/view/git/GitService.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,5 +101,15 @@ public Git getRepository(GitDetails gitDetails)
101101
return repo;
102102
}
103103

104+
/**
105+
* Gets the commit ID of the HEAD for the given repository
106+
* @param repo The Git repository
107+
* @return The commit ID of the HEAD for the repository
108+
* @throws IOException If the HEAD is detached
109+
*/
110+
public String getCurrentCommitID(Git repo) throws IOException {
111+
return repo.getRepository().findRef("HEAD").getObjectId().getName();
112+
}
113+
104114

105115
}

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

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,8 @@
2020
package org.commonwl.view.workflow;
2121

2222
import org.commonwl.view.git.GitDetails;
23-
import org.commonwl.view.git.GitService;
2423
import org.slf4j.Logger;
2524
import org.slf4j.LoggerFactory;
26-
import org.springframework.beans.factory.annotation.Autowired;
2725
import org.springframework.stereotype.Component;
2826
import org.springframework.validation.Errors;
2927
import org.springframework.validation.ValidationUtils;
@@ -51,16 +49,6 @@ public class WorkflowFormValidator {
5149
private static final String GIT_REPO_REGEX = "^((git|ssh|http(s)?)|(git@[\\w\\.]+))(:(//)?)([\\w\\.@\\:/\\-~]+)(\\.git)(/)?$";
5250
private static final Pattern gitRepoPattern = Pattern.compile(GIT_REPO_REGEX);
5351

54-
/**
55-
* Github API service
56-
*/
57-
private final GitService githubService;
58-
59-
@Autowired
60-
public WorkflowFormValidator(GitService githubService) {
61-
this.githubService = githubService;
62-
}
63-
6452
/**
6553
* Validates a WorkflowForm to ensure the URL is not empty and links to a cwl file
6654
* @param form The given WorkflowForm
@@ -72,9 +60,6 @@ public GitDetails validateAndParse(WorkflowForm form, Errors e) {
7260
// If not null and isn't just whitespace
7361
if (!e.hasErrors()) {
7462

75-
// Object to be returned if valid Git details are found
76-
GitDetails gitDetails = null;
77-
7863
// Github URL
7964
Matcher m = githubCwlPattern.matcher(form.getUrl());
8065
if (m.find()) {
@@ -92,9 +77,11 @@ public GitDetails validateAndParse(WorkflowForm form, Errors e) {
9277
// General Git details if didn't match the above
9378
ValidationUtils.rejectIfEmptyOrWhitespace(e, "branch", "branch.emptyOrWhitespace");
9479
ValidationUtils.rejectIfEmptyOrWhitespace(e, "path", "path.emptyOrWhitespace");
95-
m = gitRepoPattern.matcher(form.getUrl());
96-
if (m.find()) {
97-
return new GitDetails(form.getUrl(), form.getBranch(), form.getPath());
80+
if (!e.hasErrors()) {
81+
m = gitRepoPattern.matcher(form.getUrl());
82+
if (m.find()) {
83+
return new GitDetails(form.getUrl(), form.getBranch(), form.getPath());
84+
}
9885
}
9986

10087
}

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import org.commonwl.view.cwl.CWLService;
2323
import org.commonwl.view.cwl.CWLToolRunner;
2424
import org.commonwl.view.cwl.CWLToolStatus;
25-
import org.commonwl.view.cwl.CWLValidationException;
2625
import org.commonwl.view.git.GitDetails;
2726
import org.commonwl.view.git.GitService;
2827
import org.commonwl.view.graphviz.GraphVizService;
@@ -217,7 +216,7 @@ public File getROBundle(GitDetails gitDetails) throws ROBundleNotFoundException
217216
* @throws IOException Other file handling exceptions
218217
*/
219218
public QueuedWorkflow createQueuedWorkflow(GitDetails gitInfo)
220-
throws GitAPIException, CWLValidationException, IOException {
219+
throws GitAPIException, WorkflowNotFoundException, IOException {
221220

222221
// Clone repository to temporary folder
223222
Git repo = null;
@@ -235,7 +234,7 @@ public QueuedWorkflow createQueuedWorkflow(GitDetails gitInfo)
235234
}
236235
}
237236
File localPath = repo.getRepository().getWorkTree();
238-
String latestCommit = repo.getRepository().findRef("HEAD").getObjectId().getName();
237+
String latestCommit = gitService.getCurrentCommitID(repo);
239238

240239
Path pathToWorkflowFile = localPath.toPath().resolve(gitInfo.getPath()).normalize().toAbsolutePath();
241240
// Prevent path traversal attacks
@@ -345,7 +344,7 @@ private boolean cacheExpired(Workflow workflow) {
345344
// Check current head of the branch with the cached head
346345
logger.info("Time has expired for caching, checking commits...");
347346
Git repo = gitService.getRepository(workflow.getRetrievedFrom());
348-
String currentHead = repo.getRepository().findRef("HEAD").getObjectId().getName();
347+
String currentHead = gitService.getCurrentCommitID(repo);
349348
logger.info("Current: " + workflow.getLastCommit() + ", HEAD: " + currentHead);
350349

351350
// Reset date in database if there are still no changes

src/main/resources/templates/about.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
<div class="col-md-12" role="main" id="main">
3737
<h1>About</h1>
3838
<div style="position:relative;float:right;height:0;padding-bottom:30%;width:50%;margin-left:20px;">
39-
<iframe src="https://www.youtube.com/embed/_yjhVTmvxLU?ecver=2" style="position:absolute;width:100%;height:100%;left:0" allowfullscreen="true"></iframe>
39+
<iframe src="https://www.youtube-nocookie.com/embed/_yjhVTmvxLU?rel=0?ecver=2" style="position:absolute;width:100%;height:100%;left:0" allowfullscreen="true"></iframe>
4040
</div>
4141

4242
<p>CWL Viewer is a richly featured web visualisation suite for workflows written in the

src/test/java/org/commonwl/view/PageControllerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public void homePageWithURL() throws Exception {
6767
.andExpect(view().name("index"))
6868
.andExpect(model().attributeExists("workflowForm"))
6969
.andExpect(model().attribute("workflowForm",
70-
hasProperty("githubURL", is("https://github.com/test/default/link"))));
70+
hasProperty("url", is("https://github.com/test/default/link"))));
7171
}
7272

7373
/**

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

Lines changed: 25 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,8 @@
2222
import org.apache.jena.query.*;
2323
import org.apache.jena.rdf.model.Model;
2424
import org.apache.jena.rdf.model.ModelFactory;
25-
import org.commonwl.view.github.GitDetails;
26-
import org.commonwl.view.github.GitService;
25+
import org.commonwl.view.git.GitDetails;
2726
import org.commonwl.view.workflow.Workflow;
28-
import org.commonwl.view.workflow.WorkflowOverview;
2927
import org.junit.Before;
3028
import org.junit.Rule;
3129
import org.junit.Test;
@@ -63,7 +61,7 @@ public void setUp() throws Exception {
6361
Model workflowModel = ModelFactory.createDefaultModel();
6462
workflowModel.read(new ByteArrayInputStream(readFileToString(packedWorkflowRdf).getBytes()), null, "TURTLE");
6563
Dataset workflowDataset = DatasetFactory.create();
66-
workflowDataset.addNamedModel("https://cdn.rawgit.com/common-workflow-language/workflows/master/workflows/make-to-cwl/dna.cwl#main", workflowModel);
64+
workflowDataset.addNamedModel("http://localhost:3030/cwlviewer/github.com/common-workflow-language/workflows/blob/549c973ccc01781595ce562dea4cedc6c9540fe0/workflows/make-to-cwl/dna.cwl#main", workflowModel);
6765

6866
Answer queryRdf = new Answer<ResultSet>() {
6967
@Override
@@ -76,7 +74,7 @@ public ResultSet answer(InvocationOnMock invocation) throws Throwable {
7674
}
7775
};
7876

79-
this.rdfService = Mockito.spy(new RDFService("http://madeup.endpoint/"));
77+
this.rdfService = Mockito.spy(new RDFService("http://localhost:3030/cwlviewer/"));
8078
Mockito.doAnswer(queryRdf).when(rdfService).runQuery(anyObject());
8179
Mockito.doReturn(true).when(rdfService).graphExists(anyString());
8280
}
@@ -92,45 +90,21 @@ public ResultSet answer(InvocationOnMock invocation) throws Throwable {
9290
*/
9391
@Test
9492
public void parseLobSTRDraft3WorkflowNative() throws Exception {
95-
96-
// Get mock Github service
97-
GitService mockGithubService = getMockGithubService("workflows/lobSTR/",
98-
"src/test/resources/cwl/lobstr-draft3/");
99-
100-
// Test cwl service
101-
CWLService cwlService = new CWLService(mockGithubService,
102-
rdfService, new CWLTool(), 5242880);
103-
104-
// Get workflow from community repo by commit ID so it will not change
105-
GitDetails lobSTRDraft3Details = new GitDetails("common-workflow-language",
106-
"workflows", null, "workflows/lobSTR/lobSTR-workflow.cwl");
107-
Workflow lobSTRDraft3 = cwlService.parseWorkflowNative(lobSTRDraft3Details, "920c6be45f08e979e715a0018f22c532b024074f");
108-
93+
CWLService cwlService = new CWLService(rdfService, Mockito.mock(CWLTool.class), 5242880);
94+
Workflow lobSTRDraft3 = cwlService.parseWorkflowNative(
95+
new File("src/test/resources/cwl/lobstr-draft3/lobSTR-workflow.cwl"));
10996
testLobSTRWorkflow(lobSTRDraft3, true);
110-
11197
}
11298

11399
/**
114100
* Test native loading parsing of a the LobSTR workflow CWL version 1.0
115101
*/
116102
@Test
117103
public void parseLobSTRv1WorkflowNative() throws Exception {
118-
119-
// Get mock Github service
120-
GitService mockGithubService = getMockGithubService("workflows/lobSTR/",
121-
"src/test/resources/cwl/lobstr-draft3/");
122-
123-
// Test cwl service
124-
CWLService cwlService = new CWLService(mockGithubService,
125-
rdfService, new CWLTool(), 5242880);
126-
127-
// Get workflow from community repo by commit ID so it will not change
128-
GitDetails lobSTRv1Details = new GitDetails("common-workflow-language",
129-
"workflows", null, "workflows/lobSTR/lobSTR-workflow.cwl");
130-
Workflow lobSTRv1 = cwlService.parseWorkflowNative(lobSTRv1Details, "933bf2a1a1cce32d88f88f136275535da9df0954");
131-
104+
CWLService cwlService = new CWLService(rdfService, new CWLTool(), 5242880);
105+
Workflow lobSTRv1 = cwlService.parseWorkflowNative(
106+
new File("src/test/resources/cwl/lobstr-v1/lobSTR-workflow.cwl"));
132107
testLobSTRWorkflow(lobSTRv1, true);
133-
134108
}
135109

136110
/**
@@ -146,15 +120,18 @@ public void parseWorkflowWithCwltool() throws Exception {
146120
.thenReturn(readFileToString(packedWorkflowRdf));
147121

148122
// CWLService to test
149-
CWLService cwlService = new CWLService(Mockito.mock(GitService.class),
150-
rdfService, mockCwlTool, 5242880);
123+
CWLService cwlService = new CWLService(rdfService, mockCwlTool, 5242880);
151124

152-
GitDetails githubInfo = new GitDetails("common-workflow-language",
153-
"workflows", "549c973ccc01781595ce562dea4cedc6c9540fe0",
154-
"workflows/make-to-cwl/dna.cwl");
125+
GitDetails gitInfo = new GitDetails("https://github.com/common-workflow-language/workflows.git",
126+
"549c973ccc01781595ce562dea4cedc6c9540fe0", "workflows/make-to-cwl/dna.cwl");
127+
Workflow basicModel = new Workflow(null, null, null, null, null, null);
128+
basicModel.setRetrievedFrom(gitInfo);
129+
basicModel.setPackedWorkflowID("main");
130+
basicModel.setLastCommit("549c973ccc01781595ce562dea4cedc6c9540fe0");
155131

156132
// Parse the workflow
157-
Workflow workflow = cwlService.parseWorkflowWithCwltool(githubInfo, "master", "main");
133+
Workflow workflow = cwlService.parseWorkflowWithCwltool(basicModel,
134+
new File("src/test/resources/cwl/make_to_cwl/dna.cwl"));
158135

159136
// Check basic information
160137
assertNotNull(workflow);
@@ -167,58 +144,18 @@ public void parseWorkflowWithCwltool() throws Exception {
167144
}
168145

169146
/**
170-
* Test retrieval of a workflow overview for hello world example in cwl
147+
* Test IOException is thrown when files are over limit
171148
*/
172149
@Test
173-
public void getHelloWorkflowOverview() throws Exception {
174-
175-
// Mock githubService class
176-
GitService mockGithubService = Mockito.mock(GitService.class);
177-
File workflowFile = new File("src/test/resources/cwl/hello/hello.cwl");
178-
when(mockGithubService.downloadFile(anyObject()))
179-
.thenReturn(readFileToString(workflowFile));
180-
181-
// Test cwl service
182-
CWLService cwlService = new CWLService(mockGithubService,
183-
rdfService, Mockito.mock(CWLTool.class), 5242880);
184-
185-
// Run workflow overview
186-
GitDetails helloDetails = new GitDetails("common-workflow-language",
187-
"workflows", "8296e92d358bb5da4dc3c6e7aabefa89726e3409", "workflows/hello/hello.cwl");
188-
WorkflowOverview hello = cwlService.getWorkflowOverview(helloDetails);
189-
assertNotNull(hello);
190-
191-
// No docs for this workflow
192-
assertEquals("Hello World", hello.getLabel());
193-
assertEquals("Puts a message into a file using echo", hello.getDoc());
194-
assertEquals("hello.cwl", hello.getFileName());
195-
196-
}
197-
198-
/**
199-
* Test IOException is thrown when files are over limit with getWorkflowOverview
200-
*/
201-
@Test
202-
public void workflowOverviewOverSingleFileSizeLimitThrowsIOException() throws Exception {
203-
204-
// Mock githubService class
205-
GitService mockGithubService = Mockito.mock(GitService.class);
206-
File workflowFile = new File("src/test/resources/cwl/hello/hello.cwl");
207-
when(mockGithubService.downloadFile(anyObject()))
208-
.thenReturn(readFileToString(workflowFile));
209-
210-
// Test cwl service with 0 filesize limit
211-
CWLService cwlService = new CWLService(mockGithubService,
212-
Mockito.mock(RDFService.class), Mockito.mock(CWLTool.class), 0);
213-
214-
// Run workflow overview
215-
GitDetails helloDetails = new GitDetails("common-workflow-language",
216-
"workflows", "8296e92d358bb5da4dc3c6e7aabefa89726e3409", "workflows/hello/hello.cwl");
150+
public void workflowOverSingleFileSizeLimitThrowsIOException() throws Exception {
217151

218152
// Should throw IOException due to oversized files
219153
thrown.expect(IOException.class);
220-
thrown.expectMessage("File 'workflows/hello/hello.cwl' is over singleFileSizeLimit - 672 bytes/0 bytes");
221-
cwlService.getWorkflowOverview(helloDetails);
154+
thrown.expectMessage("File 'lobSTR-workflow.cwl' is over singleFileSizeLimit - 2 KB/0 bytes");
155+
156+
CWLService cwlService = new CWLService(rdfService, Mockito.mock(CWLTool.class), 0);
157+
cwlService.parseWorkflowNative(
158+
new File("src/test/resources/cwl/lobstr-draft3/lobSTR-workflow.cwl"));
222159

223160
}
224161

@@ -265,26 +202,4 @@ private void testLobSTRWorkflow(Workflow lobSTR, boolean nativeParsed) throws Ex
265202
}
266203
}
267204

268-
/**
269-
* Get a mock GithubService which redirects downloads to the filesystem
270-
*/
271-
private GitService getMockGithubService(String originalFolder,
272-
String resourcesFolder) throws IOException {
273-
GitService mockGithubService = Mockito.mock(GitService.class);
274-
Answer fileAnswer = new Answer<String>() {
275-
@Override
276-
public String answer(InvocationOnMock invocation) throws Throwable {
277-
Object[] args = invocation.getArguments();
278-
GitDetails details = (GitDetails) args[0];
279-
File workflowFile = new File(resourcesFolder
280-
+ details.getPath().replace(originalFolder, ""));
281-
return readFileToString(workflowFile);
282-
}
283-
};
284-
when(mockGithubService.downloadFile(anyObject())).thenAnswer(fileAnswer);
285-
when(mockGithubService.downloadFile(anyObject(), anyObject())).thenAnswer(fileAnswer);
286-
287-
return mockGithubService;
288-
}
289-
290205
}

0 commit comments

Comments
 (0)