Improvements to the code
[jabaws.git] / testsrc / compbio / runner / disorder / IUPredTester.java
1 /* Copyright (c) 2011 Peter Troshin\r
2  *  \r
3  *  JAva Bioinformatics Analysis Web Services (JABAWS) @version: 2.0     \r
4  * \r
5  *  This library is free software; you can redistribute it and/or modify it under the terms of the\r
6  *  Apache License version 2 as published by the Apache Software Foundation\r
7  * \r
8  *  This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without\r
9  *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache \r
10  *  License for more details.\r
11  * \r
12  *  A copy of the license is in apache_license.txt. It is also available here:\r
13  * @see: http://www.apache.org/licenses/LICENSE-2.0.txt\r
14  * \r
15  * Any republication or derived work distributed in source code form\r
16  * must include this copyright and license notice.\r
17  */\r
18 \r
19 package compbio.runner.disorder;\r
20 \r
21 import static org.testng.Assert.assertEquals;\r
22 import static org.testng.Assert.assertFalse;\r
23 import static org.testng.Assert.assertNotNull;\r
24 import static org.testng.Assert.assertNull;\r
25 import static org.testng.Assert.assertTrue;\r
26 import static org.testng.Assert.fail;\r
27 \r
28 import java.io.File;\r
29 import java.io.FileInputStream;\r
30 import java.io.FileNotFoundException;\r
31 import java.io.IOException;\r
32 import java.text.ParseException;\r
33 \r
34 import javax.xml.bind.ValidationException;\r
35 \r
36 import org.ggf.drmaa.DrmaaException;\r
37 import org.ggf.drmaa.JobInfo;\r
38 import org.testng.annotations.BeforeMethod;\r
39 import org.testng.annotations.Test;\r
40 \r
41 import compbio.data.sequence.ScoreManager;\r
42 import compbio.engine.AsyncExecutor;\r
43 import compbio.engine.Configurator;\r
44 import compbio.engine.SyncExecutor;\r
45 import compbio.engine.client.ConfExecutable;\r
46 import compbio.engine.client.ConfiguredExecutable;\r
47 import compbio.engine.client.Executable;\r
48 import compbio.engine.client.RunConfiguration;\r
49 import compbio.engine.cluster.drmaa.ClusterUtil;\r
50 import compbio.engine.cluster.drmaa.JobRunner;\r
51 import compbio.engine.cluster.drmaa.StatisticManager;\r
52 import compbio.engine.local.LocalRunner;\r
53 import compbio.metadata.AllTestSuit;\r
54 import compbio.metadata.JobExecutionException;\r
55 import compbio.metadata.JobStatus;\r
56 import compbio.metadata.JobSubmissionException;\r
57 import compbio.metadata.LimitsManager;\r
58 import compbio.metadata.PresetManager;\r
59 import compbio.metadata.ResultNotAvailableException;\r
60 import compbio.metadata.RunnerConfig;\r
61 import compbio.util.SysPrefs;\r
62 \r
63 public class IUPredTester {\r
64 \r
65         public static String test_outfile = "output";\r
66 \r
67         private IUPred iupred;\r
68 \r
69         @BeforeMethod(alwaysRun = true)\r
70         void init() {\r
71                 iupred = new IUPred();\r
72                 iupred.setInput(AllTestSuit.test_input).setOutput(test_outfile);\r
73         }\r
74 \r
75         @Test(groups = {AllTestSuit.test_group_cluster,\r
76                         AllTestSuit.test_group_runner})\r
77         public void testRunOnCluster() {\r
78                 assertFalse(SysPrefs.isWindows,\r
79                                 "Cluster execution can only be in unix environment");\r
80                 try {\r
81                         ConfiguredExecutable<IUPred> confIUPred = Configurator\r
82                                         .configureExecutable(iupred,\r
83                                                         Executable.ExecProvider.Cluster);\r
84                         JobRunner runner = JobRunner.getInstance(confIUPred);\r
85 \r
86                         assertNotNull(runner, "Runner is NULL");\r
87                         runner.executeJob();\r
88                         // assertNotNull("JobId is null", jobId1);\r
89                         JobStatus status = runner.getJobStatus();\r
90                         assertTrue(status == JobStatus.PENDING\r
91                                         || status == JobStatus.RUNNING,\r
92                                         "Status of the process is wrong!");\r
93                         JobInfo info = runner.getJobInfo();\r
94                         assertNotNull(info, "JobInfo is null");\r
95                         StatisticManager sm = new StatisticManager(info);\r
96                         assertNotNull(sm, "Statictic manager is null");\r
97                         try {\r
98 \r
99                                 String exits = sm.getExitStatus();\r
100                                 assertNotNull("Exit status is null", exits);\r
101                                 // cut 4 trailing zeros from the number\r
102                                 int exitsInt = ClusterUtil.CLUSTER_STAT_IN_SEC.parse(exits)\r
103                                                 .intValue();\r
104                                 assertEquals(0, exitsInt);\r
105                                 System.out.println(sm.getAllStats());\r
106 \r
107                         } catch (ParseException e) {\r
108                                 e.printStackTrace();\r
109                                 fail("Parse Exception: " + e.getMessage());\r
110                         }\r
111                         // assertFalse(runner.cleanup());\r
112                         assertTrue(sm.hasExited());\r
113                         assertFalse(sm.wasAborted());\r
114                         assertFalse(sm.hasDump());\r
115                         assertFalse(sm.hasSignaled());\r
116 \r
117                 } catch (JobSubmissionException e) {\r
118                         e.printStackTrace();\r
119                         fail("DrmaaException caught:" + e.getMessage());\r
120                 } catch (JobExecutionException e) {\r
121                         e.printStackTrace();\r
122                         fail("DrmaaException caught:" + e.getMessage());\r
123                 } catch (DrmaaException e) {\r
124                         e.printStackTrace();\r
125                         fail("DrmaaException caught:" + e.getMessage());\r
126                 }\r
127         }\r
128 \r
129         /**\r
130          * This tests fails from time to time depending on the cluster load or some\r
131          * other factors. Any client code has to adjust for this issue\r
132          */\r
133         @Test(groups = {AllTestSuit.test_group_cluster,\r
134                         AllTestSuit.test_group_runner})\r
135         public void testRunOnClusterAsync() {\r
136                 assertFalse(SysPrefs.isWindows,\r
137                                 "Cluster execution can only be in unix environment");\r
138                 try {\r
139                         ConfiguredExecutable<IUPred> confIUPred = Configurator\r
140                                         .configureExecutable(iupred,\r
141                                                         Executable.ExecProvider.Cluster);\r
142                         AsyncExecutor aengine = Configurator.getAsyncEngine(confIUPred);\r
143                         String jobId = aengine.submitJob(confIUPred);\r
144                         assertNotNull(jobId, "Runner is NULL");\r
145                         // let drmaa to start\r
146                         Thread.sleep(500);\r
147                         JobStatus status = aengine.getJobStatus(jobId);\r
148                         while (status != JobStatus.FINISHED) {\r
149                                 System.out.println("Job Status: " + status);\r
150                                 Thread.sleep(1000);\r
151                                 status = aengine.getJobStatus(jobId);\r
152                                 ConfiguredExecutable<IUPred> result = (ConfiguredExecutable<IUPred>) aengine\r
153                                                 .getResults(jobId);\r
154                                 assertNotNull(result);\r
155                                 System.out.println("RES:" + result);\r
156                                 // Some times the job could be removed from the cluster\r
157                                 // accounting\r
158                                 // before it has been reported to finish. Make sure\r
159                                 // to stop waiting in such case\r
160                                 if (status == JobStatus.UNDEFINED) {\r
161                                         break;\r
162                                 }\r
163                         }\r
164                 } catch (JobSubmissionException e) {\r
165                         e.printStackTrace();\r
166                         fail("DrmaaException caught:" + e.getMessage());\r
167                 } catch (InterruptedException e) {\r
168                         e.printStackTrace();\r
169                         fail(e.getMessage());\r
170                 } catch (ResultNotAvailableException e) {\r
171                         e.printStackTrace();\r
172                         fail(e.getMessage());\r
173                 }\r
174         }\r
175 \r
176         @Test(groups = {AllTestSuit.test_group_runner})\r
177         public void testRunLocally() {\r
178                 try {\r
179                         ConfiguredExecutable<IUPred> confIUPred = Configurator\r
180                                         .configureExecutable(iupred, Executable.ExecProvider.Local);\r
181 \r
182                         // For local execution use relative\r
183                         LocalRunner lr = new LocalRunner(confIUPred);\r
184                         lr.executeJob();\r
185                         ConfiguredExecutable<?> al1 = lr.waitForResult();\r
186                         assertNotNull(al1.getResults());\r
187                         ScoreManager al2 = confIUPred.getResults();\r
188                         assertNotNull(al2);\r
189                         assertEquals(al2.asMap().size(), 3);\r
190                         assertEquals(al1.getResults(), al2);\r
191                 } catch (JobSubmissionException e) {\r
192                         e.printStackTrace();\r
193                         fail(e.getLocalizedMessage());\r
194                 } catch (ResultNotAvailableException e) {\r
195                         e.printStackTrace();\r
196                         fail(e.getLocalizedMessage());\r
197                 } catch (JobExecutionException e) {\r
198                         e.printStackTrace();\r
199                         fail(e.getLocalizedMessage());\r
200                 }\r
201         }\r
202 \r
203         @Test(groups = {AllTestSuit.test_group_runner})\r
204         public void readStatistics() {\r
205                 // No execution statistics is available!\r
206         }\r
207 \r
208         @Test(groups = {AllTestSuit.test_group_runner})\r
209         public void testPersistance() {\r
210                 try {\r
211                         IUPred disembl = new IUPred();\r
212                         disembl.setError("errrr.txt").setInput(AllTestSuit.test_input)\r
213                                         .setOutput("outtt.txt");\r
214                         assertEquals(disembl.getInput(), AllTestSuit.test_input);\r
215                         assertEquals(disembl.getError(), "errrr.txt");\r
216                         assertEquals(disembl.getOutput(), "outtt.txt");\r
217                         ConfiguredExecutable<IUPred> cIUPred = Configurator\r
218                                         .configureExecutable(disembl, Executable.ExecProvider.Local);\r
219 \r
220                         SyncExecutor sexec = Configurator.getSyncEngine(cIUPred);\r
221                         sexec.executeJob();\r
222                         ConfiguredExecutable<?> al = sexec.waitForResult();\r
223                         assertNotNull(al.getResults());\r
224                         // Save run configuration\r
225                         assertTrue(cIUPred.saveRunConfiguration());\r
226 \r
227                         // See if loaded configuration is the same as saved\r
228                         RunConfiguration loadedRun = RunConfiguration\r
229                                         .load(new FileInputStream(new File(cIUPred\r
230                                                         .getWorkDirectory(), RunConfiguration.rconfigFile)));\r
231                         assertEquals(\r
232                                         ((ConfExecutable<IUPred>) cIUPred).getRunConfiguration(),\r
233                                         loadedRun);\r
234                         // Load run configuration as ConfExecutable\r
235                         ConfiguredExecutable<IUPred> resurrectedCIUPred = (ConfiguredExecutable<IUPred>) cIUPred\r
236                                         .loadRunConfiguration(new FileInputStream(new File(cIUPred\r
237                                                         .getWorkDirectory(), RunConfiguration.rconfigFile)));\r
238                         assertNotNull(resurrectedCIUPred);\r
239                         assertEquals(resurrectedCIUPred.getExecutable().getInput(),\r
240                                         AllTestSuit.test_input);\r
241                         assertEquals(resurrectedCIUPred.getExecutable().getError(),\r
242                                         "errrr.txt");\r
243                         assertEquals(resurrectedCIUPred.getExecutable().getOutput(),\r
244                                         "outtt.txt");\r
245                         // See in details whether executables are the same\r
246                         assertEquals(resurrectedCIUPred.getExecutable(), disembl);\r
247 \r
248                         ConfiguredExecutable<IUPred> resIUPred = Configurator\r
249                                         .configureExecutable(resurrectedCIUPred.getExecutable(),\r
250                                                         Executable.ExecProvider.Local);\r
251 \r
252                         sexec = Configurator.getSyncEngine(resIUPred,\r
253                                         Executable.ExecProvider.Local);\r
254                         sexec.executeJob();\r
255                         al = sexec.waitForResult();\r
256                         assertNotNull(al);\r
257 \r
258                 } catch (JobSubmissionException e) {\r
259                         e.printStackTrace();\r
260                         fail(e.getMessage());\r
261                 } catch (JobExecutionException e) {\r
262                         e.printStackTrace();\r
263                         fail(e.getMessage());\r
264                 } catch (FileNotFoundException e) {\r
265                         e.printStackTrace();\r
266                         fail(e.getMessage());\r
267                 } catch (IOException e) {\r
268                         e.printStackTrace();\r
269                         fail(e.getMessage());\r
270                 } catch (ResultNotAvailableException e) {\r
271                         e.printStackTrace();\r
272                         fail(e.getMessage());\r
273                 }\r
274         }\r
275 \r
276         @Test(groups = {AllTestSuit.test_group_runner})\r
277         public void testConfigurationLoading() {\r
278                 try {\r
279                         RunnerConfig<IUPred> disemblConfig = ConfExecutable\r
280                                         .getRunnerOptions(IUPred.class);\r
281                         // There is no disembl parameters\r
282                         assertNull(disemblConfig);\r
283 \r
284                         // If there were a IUPredParameter.xml file and reference to it,\r
285                         // it would be like that\r
286                         // assertTrue(disemblConfig.getArguments().size() == 0);\r
287 \r
288                         PresetManager<IUPred> disemblPresets = ConfExecutable\r
289                                         .getRunnerPresets(IUPred.class);\r
290                         assertNull(disemblPresets); // there is no presets\r
291 \r
292                         LimitsManager<IUPred> disemblLimits = ConfExecutable\r
293                                         .getRunnerLimits(IUPred.class);\r
294                         assertNotNull(disemblLimits);\r
295                         assertTrue(disemblLimits.getLimits().size() > 0);\r
296                         disemblLimits.validate(disemblPresets);\r
297 \r
298                 } catch (FileNotFoundException e) {\r
299                         e.printStackTrace();\r
300                         fail(e.getLocalizedMessage());\r
301                 } catch (IOException e) {\r
302                         e.printStackTrace();\r
303                         fail(e.getLocalizedMessage());\r
304                 } catch (ValidationException e) {\r
305                         e.printStackTrace();\r
306                         fail(e.getLocalizedMessage());\r
307                 }\r
308         }\r
309 \r
310 }\r