Skip to content

Commit cc0ea09

Browse files
committed
RO Bundle generation tests from local test resources
1 parent f9beb5c commit cc0ea09

File tree

7 files changed

+79
-41
lines changed

7 files changed

+79
-41
lines changed

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@
2020
package org.commonwl.view.git;
2121

2222
import org.apache.commons.codec.digest.DigestUtils;
23+
import org.commonwl.view.researchobject.HashableAgent;
2324
import org.eclipse.jgit.api.Git;
2425
import org.eclipse.jgit.api.errors.GitAPIException;
26+
import org.eclipse.jgit.lib.PersonIdent;
27+
import org.eclipse.jgit.revwalk.RevCommit;
2528
import org.slf4j.Logger;
2629
import org.slf4j.LoggerFactory;
2730
import org.springframework.beans.factory.annotation.Autowired;
@@ -30,7 +33,11 @@
3033

3134
import java.io.File;
3235
import java.io.IOException;
36+
import java.net.URI;
37+
import java.net.URISyntaxException;
3338
import java.nio.file.Path;
39+
import java.util.HashSet;
40+
import java.util.Set;
3441

3542
/**
3643
* Handles Git related functionality
@@ -111,5 +118,38 @@ public String getCurrentCommitID(Git repo) throws IOException {
111118
return repo.getRepository().findRef("HEAD").getObjectId().getName();
112119
}
113120

121+
/**
122+
* Gets a set of authors for a path in a given repository
123+
* @param repo The git repository
124+
* @param path The path to get commits for
125+
* @return An iterable of commits
126+
* @throws GitAPIException Any API errors which may occur
127+
* @throws URISyntaxException Error constructing mailto link
128+
*/
129+
public Set<HashableAgent> getAuthors(Git repo, String path) throws GitAPIException, URISyntaxException {
130+
Iterable<RevCommit> logs = repo.log().addPath(path).call();
131+
Set<HashableAgent> fileAuthors = new HashSet<>();
132+
for (RevCommit rev : logs) {
133+
// Use author first with backup of committer
134+
PersonIdent author = rev.getAuthorIdent();
135+
if (author == null) {
136+
author = rev.getCommitterIdent();
137+
}
138+
// Create a new agent and add as much detail as possible
139+
if (author != null) {
140+
HashableAgent newAgent = new HashableAgent();
141+
String name = author.getName();
142+
if (name != null && name.length() > 0) {
143+
newAgent.setName(author.getName());
144+
}
145+
String email = author.getEmailAddress();
146+
if (email != null && email.length() > 0) {
147+
newAgent.setUri(new URI("mailto:" + author.getEmailAddress()));
148+
}
149+
fileAuthors.add(newAgent);
150+
}
151+
}
152+
return fileAuthors;
153+
}
114154

115155
}

src/main/java/org/commonwl/view/researchobject/HashableAgent.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ public class HashableAgent extends Agent {
3333
private URI orcid;
3434
private URI uri;
3535

36+
public HashableAgent() {}
37+
38+
public HashableAgent(String name, URI orcid, URI uri) {
39+
this.name = name;
40+
this.orcid = orcid;
41+
this.uri = uri;
42+
}
43+
3644
@Override
3745
public String getName() {
3846
return name;

src/main/java/org/commonwl/view/researchobject/ROBundleService.java

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@
3333
import org.commonwl.view.workflow.Workflow;
3434
import org.eclipse.jgit.api.Git;
3535
import org.eclipse.jgit.api.errors.GitAPIException;
36-
import org.eclipse.jgit.lib.PersonIdent;
37-
import org.eclipse.jgit.revwalk.RevCommit;
3836
import org.slf4j.Logger;
3937
import org.slf4j.LoggerFactory;
4038
import org.springframework.beans.factory.annotation.Autowired;
@@ -288,30 +286,8 @@ private void addFilesToBundle(GitDetails gitDetails, Bundle bundle, Path bundleP
288286

289287
// Add authors from git commits to the file
290288
try {
291-
Set<HashableAgent> fileAuthors = new HashSet<>();
292-
Iterable<RevCommit> logs = gitRepo.log()
293-
.addPath(Paths.get(gitDetails.getPath()).resolve(file.getName()).toString())
294-
.call();
295-
for (RevCommit rev : logs) {
296-
// Use author first with backup of committer
297-
PersonIdent author = rev.getAuthorIdent();
298-
if (author == null) {
299-
author = rev.getCommitterIdent();
300-
}
301-
// Create a new agent and add as much detail as possible
302-
if (author != null) {
303-
HashableAgent newAgent = new HashableAgent();
304-
String name = author.getName();
305-
if (name != null && name.length() > 0) {
306-
newAgent.setName(author.getName());
307-
}
308-
String email = author.getEmailAddress();
309-
if (email != null && email.length() > 0) {
310-
newAgent.setUri(new URI("mailto:" + author.getEmailAddress()));
311-
}
312-
fileAuthors.add(newAgent);
313-
}
314-
}
289+
Set<HashableAgent> fileAuthors = gitService.getAuthors(gitRepo,
290+
Paths.get(gitDetails.getPath()).resolve(file.getName()).toString());
315291
authors.addAll(fileAuthors);
316292
aggregation.setAuthoredBy(new ArrayList<>(fileAuthors));
317293
} catch (GitAPIException ex) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public String searchWorkflows(Model model,
108108
@PostMapping("/workflows")
109109
public ModelAndView newWorkflowFromGithubURL(@Valid WorkflowForm workflowForm, BindingResult bindingResult) {
110110

111-
// Run validator which checks the github URL is valid
111+
// Run validator which checks the git URL is valid
112112
GitDetails gitInfo = workflowFormValidator.validateAndParse(workflowForm, bindingResult);
113113

114114
if (bindingResult.hasErrors() || gitInfo == null) {

src/test/java/org/commonwl/view/github/GitDetailsTest.java renamed to src/test/java/org/commonwl/view/git/GitDetailsTest.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@
1717
* under the License.
1818
*/
1919

20-
package org.commonwl.view.github;
20+
package org.commonwl.view.git;
2121

22-
import org.commonwl.view.git.GitDetails;
23-
import org.commonwl.view.git.GitType;
2422
import org.junit.Test;
2523

2624
import static org.commonwl.view.git.GitDetails.normaliseUrl;

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

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.commonwl.view.graphviz.GraphVizService;
3131
import org.commonwl.view.workflow.Workflow;
3232
import org.eclipse.jgit.api.Git;
33+
import org.eclipse.jgit.lib.Repository;
3334
import org.junit.Before;
3435
import org.junit.Rule;
3536
import org.junit.Test;
@@ -39,7 +40,9 @@
3940
import java.io.File;
4041
import java.net.URI;
4142
import java.nio.file.Path;
43+
import java.util.HashSet;
4244
import java.util.List;
45+
import java.util.Set;
4346

4447
import static org.junit.Assert.*;
4548
import static org.mockito.Matchers.anyObject;
@@ -52,7 +55,11 @@ public class ROBundleServiceTest {
5255

5356
@Before
5457
public void setUp() throws Exception {
55-
gitRepo = Git.open(new File("src/test/resources/lobstr-draft3"));
58+
Repository mockRepo = Mockito.mock(Repository.class);
59+
when(mockRepo.getWorkTree()).thenReturn(new File("src/test/resources/cwl/"));
60+
61+
gitRepo = Mockito.mock(Git.class);
62+
when(gitRepo.getRepository()).thenReturn(mockRepo);
5663
}
5764

5865
/**
@@ -62,7 +69,7 @@ public void setUp() throws Exception {
6269
public TemporaryFolder roBundleFolder = new TemporaryFolder();
6370

6471
/**
65-
* Generate a Research Object bundle from lobstr-v1 and check it
72+
* Generate a Research Object bundle from lobstr and check it
6673
*/
6774
@Test
6875
public void generateAndSaveROBundle() throws Exception {
@@ -71,6 +78,11 @@ public void generateAndSaveROBundle() throws Exception {
7178
GitService mockGitService = Mockito.mock(GitService.class);
7279
when(mockGitService.getRepository(anyObject())).thenReturn(gitRepo);
7380

81+
Set<HashableAgent> authors = new HashSet<>();
82+
authors.add(new HashableAgent("Mark Robinson", null, new URI("mailto:[email protected]")));
83+
when(mockGitService.getAuthors(anyObject(), anyObject()))
84+
.thenReturn(authors);
85+
7486
// Mock Graphviz service
7587
GraphVizService mockGraphvizService = Mockito.mock(GraphVizService.class);
7688
when(mockGraphvizService.getGraph(anyString(), anyString(), anyString()))
@@ -93,7 +105,7 @@ public void generateAndSaveROBundle() throws Exception {
93105

94106
// RO details
95107
GitDetails lobSTRv1RODetails = new GitDetails("https://github.com/common-workflow-language/workflows.git",
96-
"933bf2a1a1cce32d88f88f136275535da9df0954", "workflows/lobSTR");
108+
"933bf2a1a1cce32d88f88f136275535da9df0954", "lobstr-draft3/");
97109

98110
// Create new RO bundle
99111
ROBundleService bundleService = new ROBundleService(roBundleFolder.getRoot().toPath(),
@@ -115,12 +127,13 @@ public void generateAndSaveROBundle() throws Exception {
115127
// Check cwl aggregation information
116128
PathMetadata cwlAggregate = manifest.getAggregation(
117129
bundleRoot.resolve("lobSTR-workflow.cwl"));
118-
assertEquals("https://raw.githubusercontent.com/common-workflow-language/workflows/933bf2a1a1cce32d88f88f136275535da9df0954/workflows/lobSTR/lobSTR-workflow.cwl",
130+
assertEquals("https://raw.githubusercontent.com/common-workflow-language/workflows/933bf2a1a1cce32d88f88f136275535da9df0954/lobstr-draft3/lobSTR-workflow.cwl",
119131
cwlAggregate.getRetrievedFrom().toString());
120132
assertEquals("Mark Robinson", cwlAggregate.getAuthoredBy().get(0).getName());
133+
assertEquals("mailto:[email protected]", cwlAggregate.getAuthoredBy().get(0).getUri().toString());
121134
assertNull(cwlAggregate.getAuthoredBy().get(0).getOrcid());
122135
assertEquals("text/x-yaml", cwlAggregate.getMediatype());
123-
assertEquals("https://w3id.org/cwl/v1.0", cwlAggregate.getConformsTo().toString());
136+
assertEquals("https://w3id.org/cwl/draft-3", cwlAggregate.getConformsTo().toString());
124137

125138
// Check visualisations exist as aggregates
126139
PathMetadata pngAggregate = manifest.getAggregation(bundleRoot.resolve("visualisation.png"));
@@ -137,7 +150,7 @@ public void generateAndSaveROBundle() throws Exception {
137150
// Check git2prov link is in the history
138151
List<Path> history = manifest.getHistory();
139152
assertEquals(1, history.size());
140-
assertEquals("http:/git2prov.org/git2prov?giturl=https:/github.com/common-workflow-language/workflows&serialization=PROV-JSON",
153+
assertEquals("http:/git2prov.org/git2prov?giturl=https:/github.com/common-workflow-language/workflows.git&serialization=PROV-JSON",
141154
history.get(0).toString());
142155

143156
// Save and check it exists in the temporary folder
@@ -162,6 +175,11 @@ public void filesOverLimit() throws Exception {
162175
GitService mockGitService = Mockito.mock(GitService.class);
163176
when(mockGitService.getRepository(anyObject())).thenReturn(gitRepo);
164177

178+
Set<HashableAgent> authors = new HashSet<>();
179+
authors.add(new HashableAgent("Mark Robinson", null, new URI("mailto:[email protected]")));
180+
when(mockGitService.getAuthors(anyObject(), anyObject()))
181+
.thenReturn(authors);
182+
165183
// Mock Graphviz service
166184
GraphVizService mockGraphvizService = Mockito.mock(GraphVizService.class);
167185
when(mockGraphvizService.getGraph(anyString(), anyString(), anyString()))
@@ -184,7 +202,7 @@ public void filesOverLimit() throws Exception {
184202

185203
// RO details
186204
GitDetails lobSTRv1RODetails = new GitDetails("https://github.com/common-workflow-language/workflows.git",
187-
"933bf2a1a1cce32d88f88f136275535da9df0954", "workflows/lobSTR");
205+
"933bf2a1a1cce32d88f88f136275535da9df0954", "lobstr-draft3/lobSTR-workflow.cwl");
188206

189207
// Create new RO bundle
190208
ROBundleService bundleService = new ROBundleService(roBundleFolder.getRoot().toPath(),
@@ -198,9 +216,7 @@ public void filesOverLimit() throws Exception {
198216
assertEquals(14, manifest.getAggregates().size());
199217

200218
PathMetadata urlAggregate = manifest.getAggregation(
201-
new URI("https://raw.githubusercontent.com/common-workflow-language/workflows/" +
202-
"933bf2a1a1cce32d88f88f136275535da9df0954/workflows/lobSTR/models/" +
203-
"illumina_v3.pcrfree.stepmodel"));
219+
new URI("https://raw.githubusercontent.com/common-workflow-language/workflows/933bf2a1a1cce32d88f88f136275535da9df0954/lobstr-draft3/lobSTR-workflow.cwl/models/illumina_v3.pcrfree.stepmodel"));
204220
assertEquals("Mark Robinson", urlAggregate.getAuthoredBy().get(0).getName());
205221

206222
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public void getWorkflowCacheHasExpired() throws Exception {
9494
Mockito.mock(CWLToolRunner.class), -1);
9595

9696
// Will use check cache algorithm, find expired,
97-
// check github and find commit IDs do not match,
97+
// check git and find commit IDs do not match,
9898
// and thus create a new workflow + matching RO bundle
9999
Workflow workflow = testWorkflowService.getWorkflow(githubInfo);
100100

0 commit comments

Comments
 (0)