PageRenderTime 30ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/test/egats/ServerTest.java

https://github.com/augie/egats
Java | 137 lines | 77 code | 28 blank | 32 comment | 8 complexity | ed734a12af7abd062a387a8e92b12c99 MD5 | raw file
  1. package egats;
  2. import com.mongodb.util.JSON;
  3. import java.util.LinkedList;
  4. import java.util.List;
  5. /**
  6. *
  7. * @author Augie Hill - augie@umich.edu
  8. */
  9. public class ServerTest extends EGATSTestCase {
  10. public int[] fakeEGATProcess(int[] arg1, boolean[] arg2) throws Exception {
  11. // Check inputs
  12. if (arg1.length != arg2.length) {
  13. throw new Exception("Arguments 1 and 2 are not of equal size.");
  14. }
  15. int[] returnArr = new int[arg1.length];
  16. for (int i = 0; i < arg1.length; i++) {
  17. returnArr[i] = arg1[i];
  18. if (arg2[i]) {
  19. returnArr[i]++;
  20. }
  21. }
  22. return returnArr;
  23. }
  24. public void testRunProcess() throws Exception {
  25. // Create the arguments
  26. int[] arg1 = new int[]{0, 0, 0, 0};
  27. boolean[] arg2 = new boolean[]{true, false, true, false};
  28. // Expected output
  29. int[] expectedOutput = new int[]{1, 0, 1, 0};
  30. // Create the first object
  31. EGATSObject arg1Obj = new EGATSObject();
  32. arg1Obj.setClassPath(arg1.getClass().getName());
  33. arg1Obj.setObject(Data.GSON.toJson(arg1));
  34. arg1Obj = EGATSObject.create(Data.GSON.toJson(arg1Obj));
  35. // Create the second object
  36. EGATSObject arg2Obj = new EGATSObject();
  37. arg2Obj.setClassPath(arg2.getClass().getName());
  38. arg2Obj.setObject(Data.GSON.toJson(arg2));
  39. arg2Obj = EGATSObject.create(Data.GSON.toJson(arg2Obj));
  40. // Create an process request
  41. EGATSProcess request = new EGATSProcess();
  42. request.setMethodPath("egats.ServerTest.fakeEGATProcess");
  43. request.setArgs(new String[]{arg1Obj.getID(), arg2Obj.getID()});
  44. // Put into a list
  45. List<EGATSProcess> oList = new LinkedList<EGATSProcess>();
  46. oList.add(request);
  47. // Send the process request
  48. Response response = API.send(API.getURL(API.PROCESSES_FOLDER), JSON.serialize(oList));
  49. // Check response code
  50. assertEquals(Response.STATUS_CODE_OK, response.getStatusCode());
  51. // Read the response
  52. List<String> IDs = (List<String>) JSON.parse(response.getBody());
  53. assertEquals(1, IDs.size());
  54. // Get the process that was created
  55. EGATSProcess p = API.getProcess(IDs.get(0));
  56. // Wait a little for the process to run
  57. while (p.getFinishTime() == null) {
  58. Thread.sleep(25);
  59. }
  60. // Should have not been an error
  61. assertNull(p.getExceptionMessage());
  62. // Get the output object
  63. EGATSObject o = API.getObject(p.getOutputID());
  64. // Is the output from the process as expected?
  65. assertEquals(Data.GSON.toJson(expectedOutput), o.getObject());
  66. }
  67. public void testRunWorkflow() throws Exception {
  68. // Create the arguments
  69. int[] arg1 = new int[]{0, 0, 0, 0};
  70. boolean[] arg2 = new boolean[]{true, false, true, false};
  71. // Expected output
  72. int[] expectedOutput = new int[]{2, 0, 2, 0};
  73. // Create the first object
  74. EGATSObject arg1Obj = new EGATSObject();
  75. arg1Obj.setClassPath(arg1.getClass().getName());
  76. arg1Obj.setObject(Data.GSON.toJson(arg1));
  77. arg1Obj = EGATSObject.create(Data.GSON.toJson(arg1Obj));
  78. // Create the second object
  79. EGATSObject arg2Obj = new EGATSObject();
  80. arg2Obj.setClassPath(arg2.getClass().getName());
  81. arg2Obj.setObject(Data.GSON.toJson(arg2));
  82. arg2Obj = EGATSObject.create(Data.GSON.toJson(arg2Obj));
  83. // Create an process request
  84. EGATSWorkflow request = new EGATSWorkflow();
  85. request.setClassPath("egats.BasicWorkflow");
  86. request.setArgs(new String[]{arg1Obj.getID(), arg2Obj.getID()});
  87. request.setName("My workflow");
  88. // Put into a list
  89. List<EGATSWorkflow> oList = new LinkedList<EGATSWorkflow>();
  90. oList.add(request);
  91. // Send the request
  92. Response response = API.send(API.getURL(API.WORKFLOWS_FOLDER), JSON.serialize(oList));
  93. // Check response code
  94. assertEquals(Response.STATUS_CODE_OK, response.getStatusCode());
  95. // Read the response
  96. List<String> IDs = (List<String>) JSON.parse(response.getBody());
  97. assertEquals(1, IDs.size());
  98. // Get the process that was created
  99. EGATSWorkflow w = EGATSWorkflow.CACHE.get(IDs.get(0));
  100. // Wait a little for the process to run
  101. while (w.getFinishTime() == null) {
  102. Thread.sleep(25);
  103. }
  104. // Should have not been an error
  105. assertNull(w.getExceptionMessage());
  106. // There should be two processes
  107. assertEquals(2, w.getProcessCount());
  108. }
  109. }