2424import org .commonwl .view .github .GithubDetails ;
2525import org .commonwl .view .workflow .Workflow ;
2626import org .commonwl .view .workflow .WorkflowOverview ;
27+ import org .junit .Rule ;
2728import org .junit .Test ;
29+ import org .junit .rules .ExpectedException ;
2830import org .mockito .Mockito ;
2931import org .mockito .invocation .InvocationOnMock ;
3032import org .mockito .stubbing .Answer ;
3133
3234import java .io .File ;
35+ import java .io .IOException ;
3336import java .util .Map ;
3437
3538import static org .junit .Assert .*;
3841
3942public class CWLServiceTest {
4043
44+ /**
45+ * Used for expected IOExceptions for filesize limits
46+ */
47+ @ Rule
48+ public ExpectedException thrown = ExpectedException .none ();
49+
50+ /**
51+ * Test main parsing of a the Hello World example (packed CWL version v1.0)
52+ */
53+ @ Test
54+ public void parsePackedHellov1Workflow () throws Exception {
55+
56+ // Test cwl service
57+ GitHubService mockGithubService = getMockGithubService ("workflows/hello/" ,
58+ "src/test/resources/cwl/hello/" );
59+ CWLService cwlService = new CWLService (mockGithubService , 5242880 );
60+
61+ // Get workflow from community repo by commit ID so it will not change
62+ GithubDetails helloDetails = new GithubDetails ("common-workflow-language" ,
63+ "workflows" , null , "workflows/hello/hello.cwl" );
64+ Workflow hello = cwlService .parseWorkflow (helloDetails , "920c6be45f08e979e715a0018f22c532b024074f" );
65+
66+ assertNotNull (hello );
67+
68+ }
69+
4170 /**
4271 * Test main parsing of a the LobSTR workflow CWL version draft-3
4372 */
4473 @ Test
4574 public void parseLobSTRDraft3Workflow () throws Exception {
4675
47- // Mock githubService class to redirect downloads to resources folder
48- GitHubService mockGithubService = Mockito .mock (GitHubService .class );
49- Answer fileAnswer = new Answer <String >() {
50- @ Override
51- public String answer (InvocationOnMock invocation ) throws Throwable {
52- Object [] args = invocation .getArguments ();
53- GithubDetails details = (GithubDetails ) args [0 ];
54- File workflowFile = new File ("src/test/resources/cwl/lobstr-draft3/"
55- + details .getPath ().replace ("workflows/lobSTR/" , "" ));
56- return FileUtils .readFileToString (workflowFile );
57- }
58- };
59- when (mockGithubService .downloadFile (anyObject ())).thenAnswer (fileAnswer );
60- when (mockGithubService .downloadFile (anyObject (), anyObject ())).thenAnswer (fileAnswer );
61-
6276 // Test cwl service
77+ GitHubService mockGithubService = getMockGithubService ("workflows/lobSTR/" ,
78+ "src/test/resources/cwl/lobstr-draft3/" );
6379 CWLService cwlService = new CWLService (mockGithubService , 5242880 );
6480
6581 // Get workflow from community repo by commit ID so it will not change
@@ -79,22 +95,9 @@ public String answer(InvocationOnMock invocation) throws Throwable {
7995 @ Test
8096 public void parseLobSTRv1Workflow () throws Exception {
8197
82- // Mock githubService class to redirect downloads to resources folder
83- GitHubService mockGithubService = Mockito .mock (GitHubService .class );
84- Answer fileAnswer = new Answer <String >() {
85- @ Override
86- public String answer (InvocationOnMock invocation ) throws Throwable {
87- Object [] args = invocation .getArguments ();
88- GithubDetails details = (GithubDetails ) args [0 ];
89- File workflowFile = new File ("src/test/resources/cwl/lobstr-v1/"
90- + details .getPath ().replace ("workflows/lobSTR/" , "" ));
91- return FileUtils .readFileToString (workflowFile );
92- }
93- };
94- when (mockGithubService .downloadFile (anyObject ())).thenAnswer (fileAnswer );
95- when (mockGithubService .downloadFile (anyObject (), anyObject ())).thenAnswer (fileAnswer );
96-
9798 // Test cwl service
99+ GitHubService mockGithubService = getMockGithubService ("workflows/lobSTR/" ,
100+ "src/test/resources/cwl/lobstr-v1/" );
98101 CWLService cwlService = new CWLService (mockGithubService , 5242880 );
99102
100103 // Get workflow from community repo by commit ID so it will not change
@@ -109,6 +112,103 @@ public String answer(InvocationOnMock invocation) throws Throwable {
109112
110113 }
111114
115+ /**
116+ * Test IOException is thrown when files are over limit with parseWorkflow
117+ */
118+ @ Test
119+ public void parseWorkflowOverSingleFileSizeLimitThrowsIOException () throws Exception {
120+
121+ // Test cwl service
122+ GitHubService mockGithubService = getMockGithubService ("workflows/hello/" ,
123+ "src/test/resources/cwl/hello/" );
124+ CWLService cwlService = new CWLService (mockGithubService , 0 );
125+
126+ // Get workflow from community repo by commit ID so it will not change
127+ GithubDetails helloDetails = new GithubDetails ("common-workflow-language" ,
128+ "workflows" , null , "workflows/hello/hello.cwl" );
129+
130+ thrown .expect (IOException .class );
131+ thrown .expectMessage ("File 'workflows/hello/hello.cwl' is over singleFileSizeLimit - 672 bytes/0 bytes" );
132+ cwlService .parseWorkflow (helloDetails , "920c6be45f08e979e715a0018f22c532b024074f" );
133+
134+ }
135+
136+ /**
137+ * Test retrieval of a workflow overview for hello world example in cwl
138+ */
139+ @ Test
140+ public void getHelloWorkflowOverview () throws Exception {
141+
142+ // Mock githubService class
143+ GitHubService mockGithubService = Mockito .mock (GitHubService .class );
144+ File workflowFile = new File ("src/test/resources/cwl/hello/hello.cwl" );
145+ when (mockGithubService .downloadFile (anyObject ()))
146+ .thenReturn (FileUtils .readFileToString (workflowFile ));
147+
148+ // Test cwl service
149+ CWLService cwlService = new CWLService (mockGithubService , 5242880 );
150+
151+ // Run workflow overview
152+ GithubDetails helloDetails = new GithubDetails ("common-workflow-language" ,
153+ "workflows" , "8296e92d358bb5da4dc3c6e7aabefa89726e3409" , "workflows/hello/hello.cwl" );
154+ WorkflowOverview hello = cwlService .getWorkflowOverview (helloDetails );
155+ assertNotNull (hello );
156+
157+ // No docs for this workflow
158+ assertEquals ("Hello World" , hello .getLabel ());
159+ assertEquals ("Puts a message into a file using echo" , hello .getDoc ());
160+ assertEquals ("hello.cwl" , hello .getFileName ());
161+
162+ }
163+
164+ /**
165+ * Test IOException is thrown when files are over limit with getWorkflowOverview
166+ */
167+ @ Test
168+ public void workflowOverviewOverSingleFileSizeLimitThrowsIOException () throws Exception {
169+
170+ // Mock githubService class
171+ GitHubService mockGithubService = Mockito .mock (GitHubService .class );
172+ File workflowFile = new File ("src/test/resources/cwl/hello/hello.cwl" );
173+ when (mockGithubService .downloadFile (anyObject ()))
174+ .thenReturn (FileUtils .readFileToString (workflowFile ));
175+
176+ // Test cwl service with 0 filesize limit
177+ CWLService cwlService = new CWLService (mockGithubService , 0 );
178+
179+ // Run workflow overview
180+ GithubDetails helloDetails = new GithubDetails ("common-workflow-language" ,
181+ "workflows" , "8296e92d358bb5da4dc3c6e7aabefa89726e3409" , "workflows/hello/hello.cwl" );
182+
183+ // Should throw IOException due to oversized files
184+ thrown .expect (IOException .class );
185+ thrown .expectMessage ("File 'workflows/hello/hello.cwl' is over singleFileSizeLimit - 672 bytes/0 bytes" );
186+ cwlService .getWorkflowOverview (helloDetails );
187+
188+ }
189+
190+ /**
191+ * Get a mock GithubService which redirects downloads to the filesystem
192+ */
193+ private GitHubService getMockGithubService (String originalFolder ,
194+ String resourcesFolder ) throws IOException {
195+ GitHubService mockGithubService = Mockito .mock (GitHubService .class );
196+ Answer fileAnswer = new Answer <String >() {
197+ @ Override
198+ public String answer (InvocationOnMock invocation ) throws Throwable {
199+ Object [] args = invocation .getArguments ();
200+ GithubDetails details = (GithubDetails ) args [0 ];
201+ File workflowFile = new File (resourcesFolder
202+ + details .getPath ().replace (originalFolder , "" ));
203+ return FileUtils .readFileToString (workflowFile );
204+ }
205+ };
206+ when (mockGithubService .downloadFile (anyObject ())).thenAnswer (fileAnswer );
207+ when (mockGithubService .downloadFile (anyObject (), anyObject ())).thenAnswer (fileAnswer );
208+
209+ return mockGithubService ;
210+ }
211+
112212 /**
113213 * Validate a LobSTR workflow
114214 * See: https://github.com/common-workflow-language/workflows/tree/master/workflows/lobSTR
@@ -149,32 +249,4 @@ private void testLobSTRWorkflow(Workflow lobSTR) throws Exception {
149249
150250 }
151251
152- /**
153- * Test retrieval of a workflow overview for hello world example in cwl
154- */
155- @ Test
156- public void getHelloWorkflowOverview () throws Exception {
157-
158- // Mock githubService class
159- GitHubService mockGithubService = Mockito .mock (GitHubService .class );
160- File workflowFile = new File ("src/test/resources/cwl/hello/hello.cwl" );
161- when (mockGithubService .downloadFile (anyObject ()))
162- .thenReturn (FileUtils .readFileToString (workflowFile ));
163-
164- // Test cwl service
165- CWLService cwlService = new CWLService (mockGithubService , 5242880 );
166-
167- // Get workflow from community repo by commit ID so it will not change
168- GithubDetails helloDetails = new GithubDetails ("common-workflow-language" ,
169- "workflows" , "8296e92d358bb5da4dc3c6e7aabefa89726e3409" , "workflows/hello/hello.cwl" );
170- WorkflowOverview hello = cwlService .getWorkflowOverview (helloDetails );
171- assertNotNull (hello );
172-
173- // No docs for this workflow
174- assertEquals ("Hello World" , hello .getLabel ());
175- assertEquals ("Puts a message into a file using echo" , hello .getDoc ());
176- assertEquals ("hello.cwl" , hello .getFileName ());
177-
178- }
179-
180252}
0 commit comments