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