Skip to content

Commit 8db81ba

Browse files
author
Mark Robinson
committed
Add WorkflowService tests
1 parent f75a2b6 commit 8db81ba

File tree

4 files changed

+208
-14
lines changed

4 files changed

+208
-14
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,29 +52,31 @@ public class ROBundleFactory {
5252
private final int singleFileSizeLimit;
5353
private final Path storageLocation;
5454
private final WorkflowRepository workflowRepository;
55+
private final GitHubService githubService;
5556

5657
@Autowired
5758
public ROBundleFactory(@Value("${applicationName}") String applicationName,
5859
@Value("${applicationURL}") String applicationURL,
5960
@Value("${graphvizStorage}") Path graphvizStorage,
6061
@Value("${singleFileSizeLimit}") int singleFileSizeLimit,
62+
GitHubService gitHubService,
6163
WorkflowRepository workflowRepository) {
6264
this.applicationName = applicationName;
6365
this.applicationURL = applicationURL;
6466
this.storageLocation = graphvizStorage;
6567
this.workflowRepository = workflowRepository;
68+
this.githubService = gitHubService;
6669
this.singleFileSizeLimit = singleFileSizeLimit;
6770
}
6871

6972
/**
7073
* Creates a new Workflow Research Object Bundle from a Github URL
7174
* and saves it to a file
72-
* @param githubService The service for Github API functionality
7375
* @param githubInfo Details of the Github repository
7476
* @throws IOException Any API errors which may have occurred
7577
*/
7678
@Async
77-
public void workflowROFromGithub(GitHubService githubService, GithubDetails githubInfo)
79+
public void workflowROFromGithub(GithubDetails githubInfo)
7880
throws IOException, InterruptedException {
7981
logger.info("Creating Research Object Bundle");
8082

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ public Workflow createWorkflow(GithubDetails githubInfo) {
215215
*/
216216
private void generateROBundle(Workflow workflow) {
217217
try {
218-
ROBundleFactory.workflowROFromGithub(githubService, workflow.getRetrievedFrom());
218+
ROBundleFactory.workflowROFromGithub(workflow.getRetrievedFrom());
219219
} catch (Exception ex) {
220220
logger.error("Error creating RO Bundle", ex);
221221
}
Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,57 @@
11
package org.commonwl.view.docker;
22

3+
import org.junit.Before;
34
import org.junit.Test;
45

56
import static org.junit.Assert.assertEquals;
67
import static org.junit.Assert.assertNull;
78

89
public class DockerServiceTest {
910

11+
private DockerService dockerService;
12+
13+
/**
14+
* New instance of DockerService
15+
*/
16+
@Before
17+
public void setUp() throws Exception {
18+
dockerService = new DockerService();
19+
}
20+
1021
/**
1122
* Test conversion from docker pull tag to dockerhub URL
1223
*/
1324
@Test
14-
public void getDockerHubURL() throws Exception {
15-
16-
String test1 = DockerService.getDockerHubURL("stain/cwlviewer");
17-
assertEquals("https://hub.docker.com/r/stain/cwlviewer", test1);
18-
19-
String test2 = DockerService.getDockerHubURL("rabix/lobSTR");
20-
assertEquals("https://hub.docker.com/r/rabix/lobSTR", test2);
25+
public void getStandardDockerHubURL() throws Exception {
26+
String test = DockerService.getDockerHubURL("stain/cwlviewer");
27+
assertEquals("https://hub.docker.com/r/stain/cwlviewer", test);
28+
}
2129

22-
String test3 = DockerService.getDockerHubURL("ubuntu");
23-
assertEquals("https://hub.docker.com/r/_/ubuntu", test3);
30+
/**
31+
* Second valid example
32+
*/
33+
@Test
34+
public void getStandardDockerHubURL2() throws Exception {
35+
String test = DockerService.getDockerHubURL("rabix/lobSTR");
36+
assertEquals("https://hub.docker.com/r/rabix/lobSTR", test);
37+
}
2438

25-
String test4 = DockerService.getDockerHubURL("clearly/not/a/valid/tag");
26-
assertNull(test4);
39+
/**
40+
* Example from the official repository
41+
*/
42+
@Test
43+
public void getOfficialRepoDockerHubURL() throws Exception {
44+
String test = DockerService.getDockerHubURL("ubuntu");
45+
assertEquals("https://hub.docker.com/r/_/ubuntu", test);
46+
}
2747

48+
/**
49+
* Invalid tag should fail to get URL
50+
*/
51+
@Test
52+
public void getInvalidDockerHubURL() throws Exception {
53+
String test = DockerService.getDockerHubURL("clearly/not/a/valid/tag");
54+
assertNull(test);
2855
}
2956

3057
}
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
package org.commonwl.view.workflow;
2+
3+
import org.commonwl.view.cwl.CWLService;
4+
import org.commonwl.view.github.GitHubService;
5+
import org.commonwl.view.github.GithubDetails;
6+
import org.commonwl.view.graphviz.GraphVizService;
7+
import org.commonwl.view.researchobject.ROBundleFactory;
8+
import org.eclipse.egit.github.core.RepositoryContents;
9+
import org.junit.Rule;
10+
import org.junit.Test;
11+
import org.junit.rules.TemporaryFolder;
12+
import org.mockito.Mockito;
13+
import org.mockito.invocation.InvocationOnMock;
14+
import org.mockito.stubbing.Answer;
15+
16+
import java.io.File;
17+
import java.util.ArrayList;
18+
import java.util.Date;
19+
import java.util.HashMap;
20+
import java.util.List;
21+
22+
import static org.junit.Assert.assertEquals;
23+
import static org.junit.Assert.assertTrue;
24+
import static org.mockito.Matchers.anyObject;
25+
import static org.mockito.Matchers.anyString;
26+
import static org.mockito.Mockito.when;
27+
28+
public class WorkflowServiceTest {
29+
30+
/**
31+
* Folder for test research object bundles
32+
*/
33+
@Rule
34+
public TemporaryFolder roBundleFolder = new TemporaryFolder();
35+
36+
/**
37+
* Getting a list of workflow overviews from a directory
38+
*/
39+
@Test
40+
public void getWorkflowsFromDirectory() throws Exception {
41+
42+
// Mock Github service redirecting content query to the filesystem
43+
GitHubService mockGithubService = Mockito.mock(GitHubService.class);
44+
Answer contentsAnswer = new Answer<List<RepositoryContents>>() {
45+
@Override
46+
public List<RepositoryContents> answer(InvocationOnMock invocation) throws Throwable {
47+
List<RepositoryContents> returnList = new ArrayList<>();
48+
49+
// Add all files from lobstr-v1 directory
50+
File[] fileList = new File("src/test/resources/cwl/lobstr-v1/").listFiles();
51+
for (File thisFile : fileList) {
52+
RepositoryContents contentsEntry = new RepositoryContents();
53+
if (thisFile.isFile()) {
54+
contentsEntry.setType(GitHubService.TYPE_FILE);
55+
contentsEntry.setSize(100);
56+
contentsEntry.setName(thisFile.getName());
57+
contentsEntry.setPath("workflows/lobSTR/" + thisFile.getName());
58+
returnList.add(contentsEntry);
59+
}
60+
}
61+
62+
return returnList;
63+
}
64+
};
65+
when(mockGithubService.getContents(anyObject())).thenAnswer(contentsAnswer);
66+
67+
// Mock CWL service which returns simple overview once simulating 1 workflow found
68+
CWLService mockCWLService = Mockito.mock(CWLService.class);
69+
when(mockCWLService.getWorkflowOverview(anyObject()))
70+
.thenReturn(new WorkflowOverview("workflow.cwl", "label", "doc"))
71+
.thenReturn(new WorkflowOverview("workflow2.cwl", "label2", "doc2"))
72+
.thenReturn(null);
73+
74+
// Create service under test
75+
WorkflowService testWorkflowService = new WorkflowService(
76+
mockGithubService, mockCWLService,
77+
Mockito.mock(WorkflowRepository.class), Mockito.mock(ROBundleFactory.class),
78+
Mockito.mock(GraphVizService.class), 1);
79+
80+
// Get a list of workflows from the directory
81+
List<WorkflowOverview> list = testWorkflowService.getWorkflowsFromDirectory(
82+
Mockito.mock(GithubDetails.class));
83+
84+
// 1 workflow should be found
85+
assertTrue(list.size() == 2);
86+
assertEquals("workflow.cwl", list.get(0).getFileName());
87+
assertEquals("label", list.get(0).getLabel());
88+
assertEquals("doc", list.get(0).getDoc());
89+
90+
assertEquals("workflow2.cwl", list.get(1).getFileName());
91+
assertEquals("label2", list.get(1).getLabel());
92+
assertEquals("doc2", list.get(1).getDoc());
93+
94+
}
95+
96+
/**
97+
* Getting a workflow when cache has expired
98+
* And a new workflow needs to be created
99+
*/
100+
@Test
101+
public void getWorkflowCacheHasExpired() throws Exception {
102+
103+
Workflow oldWorkflow = new Workflow("old", "This is the expired workflow",
104+
new HashMap<>(), new HashMap<>(), new HashMap<>(), null);
105+
oldWorkflow.setId("theworkflowid");
106+
oldWorkflow.setRetrievedOn(new Date());
107+
oldWorkflow.setRetrievedFrom(Mockito.mock(GithubDetails.class));
108+
oldWorkflow.setLastCommit("d46ce365f1a10c4c4d6b0caed51c6f64b84c2f63");
109+
oldWorkflow.setRoBundle(roBundleFolder.newFile("robundle.zip").getAbsolutePath());
110+
111+
Workflow updatedWorkflow = new Workflow("new", "This is the updated workflow",
112+
new HashMap<>(), new HashMap<>(), new HashMap<>(), null);
113+
updatedWorkflow.setId("newworkflowid");
114+
115+
WorkflowRepository mockWorkflowRepo = Mockito.mock(WorkflowRepository.class);
116+
when(mockWorkflowRepo.findByRetrievedFrom(anyObject())).thenReturn(oldWorkflow);
117+
118+
CWLService mockCWLService = Mockito.mock(CWLService.class);
119+
when(mockCWLService.parseWorkflow(anyObject(), anyString())).thenReturn(updatedWorkflow);
120+
121+
// Create service under test with negative cache time (always create new workflow)
122+
WorkflowService testWorkflowService = new WorkflowService(
123+
Mockito.mock(GitHubService.class), mockCWLService,
124+
mockWorkflowRepo, Mockito.mock(ROBundleFactory.class),
125+
Mockito.mock(GraphVizService.class), -1);
126+
127+
// Will use check cache algorithm, find expired,
128+
// check github and find commit IDs do not match,
129+
// and thus create a new workflow + matching RO bundle
130+
Workflow workflow = testWorkflowService.getWorkflow(Mockito.mock(GithubDetails.class));
131+
132+
// Check the new workflow was returned
133+
assertEquals("newworkflowid", workflow.getID());
134+
assertEquals("new", workflow.getLabel());
135+
assertEquals("This is the updated workflow", workflow.getDoc());
136+
137+
}
138+
139+
/**
140+
* Get the research object bundle associated with a workflow
141+
* TODO: Test retry for generation within this method
142+
*/
143+
@Test
144+
public void getROBundle() throws Exception {
145+
146+
Workflow workflow = new Workflow("Label", "Doc for the workflow",
147+
new HashMap<>(), new HashMap<>(), new HashMap<>(), null);
148+
String roBundlePath = roBundleFolder.newFile("bundle.zip").getAbsolutePath();
149+
workflow.setRoBundle(roBundlePath);
150+
151+
WorkflowRepository mockWorkflowRepo = Mockito.mock(WorkflowRepository.class);
152+
when(mockWorkflowRepo.findOne(anyString())).thenReturn(workflow);
153+
154+
// Create service under test
155+
WorkflowService testWorkflowService = new WorkflowService(
156+
Mockito.mock(GitHubService.class), Mockito.mock(CWLService.class),
157+
mockWorkflowRepo, Mockito.mock(ROBundleFactory.class),
158+
Mockito.mock(GraphVizService.class), -1);
159+
160+
File fetchedBundle = testWorkflowService.getROBundle("workflowid");
161+
assertEquals(roBundlePath, fetchedBundle.getAbsolutePath());
162+
163+
}
164+
165+
}

0 commit comments

Comments
 (0)