@@ -30,24 +30,23 @@ public class RemoteApiServlet extends RequestHandler {
30
30
private static final Object COMMAND_START = "start" ;
31
31
private static final Object COMMAND_STATUS = "status" ;
32
32
private static final String PARAM_PASSWORD = "password" ;
33
-
33
+
34
34
private final String password ;
35
35
private boolean detectedUnitTests = false ;
36
-
36
+
37
37
private final static ILogNode LOG = TestManager .LOG ;
38
38
private volatile TestSuiteRunner testSuiteRunner ;
39
-
39
+
40
40
public RemoteApiServlet (String password ) {
41
41
this .password = password ;
42
42
}
43
43
44
44
@ Override
45
- protected void processRequest (IMxRuntimeRequest req ,
46
- IMxRuntimeResponse resp , String path ) throws Exception {
47
-
45
+ protected void processRequest (IMxRuntimeRequest req , IMxRuntimeResponse resp , String path ) throws Exception {
46
+
48
47
HttpServletRequest request = req .getHttpServletRequest ();
49
48
HttpServletResponse response = resp .getHttpServletResponse ();
50
-
49
+
51
50
try {
52
51
if (!"POST" .equals (request .getMethod ()))
53
52
response .setStatus (HttpServletResponse .SC_METHOD_NOT_ALLOWED );
@@ -57,12 +56,10 @@ else if (COMMAND_STATUS.equals(path))
57
56
serveRunStatus (request , response , path );
58
57
else
59
58
response .setStatus (HttpServletResponse .SC_NOT_FOUND );
60
- }
61
- catch (IllegalArgumentException e ) {
59
+ } catch (IllegalArgumentException e ) {
62
60
response .setStatus (HttpServletResponse .SC_BAD_REQUEST );
63
61
write (response , e .getMessage ());
64
- }
65
- catch (InvalidCredentialsException e ) {
62
+ } catch (InvalidCredentialsException e ) {
66
63
response .setStatus (HttpServletResponse .SC_UNAUTHORIZED );
67
64
write (response , "Invalid password provided" );
68
65
}
@@ -74,60 +71,59 @@ private void write(HttpServletResponse response, String data) {
74
71
} catch (Exception e ) {
75
72
throw new RuntimeException (e );
76
73
}
77
-
74
+
78
75
}
79
76
80
- private synchronized void serveRunStatus (HttpServletRequest request ,
81
- HttpServletResponse response , String path ) throws Exception {
77
+ private synchronized void serveRunStatus (HttpServletRequest request , HttpServletResponse response , String path )
78
+ throws Exception {
82
79
JSONObject input = parseInput (request );
83
80
verifyPassword (input );
84
-
81
+
85
82
if (testSuiteRunner == null ) {
86
83
throw new IllegalArgumentException ("No testrun was started yet" );
87
84
}
88
-
85
+
89
86
response .setStatus (HttpServletResponse .SC_OK );
90
87
response .setHeader ("Content-Type" , "application/json" );
91
88
write (response , testSuiteRunner .getStatus ().toString (4 ));
92
89
}
93
90
94
- private synchronized void serveRunStart (HttpServletRequest request ,
95
- HttpServletResponse response , String path ) throws IOException , CoreException , InvalidCredentialsException {
91
+ private synchronized void serveRunStart (HttpServletRequest request , HttpServletResponse response , String path )
92
+ throws IOException , CoreException , InvalidCredentialsException {
96
93
JSONObject input = parseInput (request );
97
94
verifyPassword (input );
98
-
95
+
99
96
IContext context = Core .createSystemContext ();
100
97
if (!detectedUnitTests ) {
101
98
TestManager .instance ().findAllTests (context );
102
99
detectedUnitTests = true ;
103
100
}
104
-
101
+
105
102
if (testSuiteRunner != null && !testSuiteRunner .isFinished ()) {
106
103
throw new IllegalArgumentException ("Cannot start a test run while another test run is still running" );
107
104
}
108
-
105
+
109
106
LOG .info ("[remote api] starting new test run" );
110
107
testSuiteRunner = new TestSuiteRunner ();
111
-
108
+
112
109
Thread t = new Thread () {
113
110
@ Override
114
111
public void run () {
115
112
testSuiteRunner .run ();
116
113
}
117
114
};
118
-
115
+
119
116
t .start ();
120
- response .setStatus (HttpServletResponse .SC_NO_CONTENT );
117
+ response .setStatus (HttpServletResponse .SC_NO_CONTENT );
121
118
}
122
-
123
-
124
119
125
120
private void verifyPassword (JSONObject input ) throws InvalidCredentialsException {
126
121
if (!input .has (PARAM_PASSWORD )) {
127
122
LOG .warn ("[remote api] Missing password" );
128
- throw new IllegalArgumentException ("No '" + PARAM_PASSWORD + "' attribute found in the JSON body. Please provide a password" );
123
+ throw new IllegalArgumentException (
124
+ "No '" + PARAM_PASSWORD + "' attribute found in the JSON body. Please provide a password" );
129
125
}
130
-
126
+
131
127
if (!password .equals (input .getString (PARAM_PASSWORD ))) {
132
128
LOG .warn ("[remote api] Invalid password" );
133
129
throw new InvalidCredentialsException ();
@@ -143,7 +139,7 @@ private class TestSuiteRunner {
143
139
boolean finished = false ;
144
140
long startTime = System .currentTimeMillis ();
145
141
long totalTime = -1 ;
146
-
142
+
147
143
public void run () {
148
144
try {
149
145
TestManager .instance ().runTestSuites ();
@@ -153,9 +149,9 @@ public void run() {
153
149
totalTime = System .currentTimeMillis () - startTime ;
154
150
finished = true ;
155
151
LOG .info ("[remote api] finished test run" );
156
- }
152
+ }
157
153
}
158
-
154
+
159
155
public synchronized boolean isFinished () {
160
156
return finished ;
161
157
}
@@ -164,46 +160,46 @@ public synchronized JSONObject getStatus() throws CoreException {
164
160
JSONObject result = new JSONObject ();
165
161
result .put ("completed" , this .finished );
166
162
result .put ("runtime" , totalTime );
167
-
163
+
168
164
IContext context = Core .createSystemContext ();
169
165
long count = 0l ;
170
166
long failures = 0l ;
171
-
172
- List <IMendixObject > testSuites = Core .retrieveXPathQuery (context , String .format ("//%s" , TestSuite .entityName ));
167
+
168
+ List <IMendixObject > testSuites = Core .retrieveXPathQuery (context ,
169
+ String .format ("//%s" , TestSuite .entityName ));
173
170
174
171
for (IMendixObject mxObject : testSuites ) {
175
172
TestSuite testSuite = TestSuite .initialize (context , mxObject );
176
173
count += testSuite .getTestCount ();
177
174
failures += testSuite .getTestFailedCount ();
178
175
}
179
-
176
+
180
177
result .put ("tests" , count );
181
178
result .put ("failures" , failures );
182
-
179
+
183
180
JSONArray failedTests = new JSONArray ();
184
181
result .put ("failed_tests" , failedTests );
185
-
182
+
186
183
StringBuilder query = new StringBuilder ();
187
184
query .append (String .format ("//%s" , UnitTest .entityName ));
188
185
// Failed tests
189
186
query .append ("[" + UnitTest .MemberNames .Result + "=\" " + UnitTestResult ._2_Failed + "\" ]" );
190
187
// In test suites that are not running anymore
191
188
query .append ("[" + UnitTest .MemberNames .UnitTest_TestSuite + "/" + TestSuite .entityName + "/"
192
189
+ TestSuite .MemberNames .Result + "=\" " + UnitTestResult ._2_Failed + "\" ]" );
193
-
190
+
194
191
List <IMendixObject > unitTests = Core .retrieveXPathQuery (context , query .toString ());
195
192
196
- for (IMendixObject mxObject : unitTests )
197
- {
193
+ for (IMendixObject mxObject : unitTests ) {
198
194
UnitTest test = UnitTest .initialize (context , mxObject );
199
195
JSONObject i = new JSONObject ();
200
196
i .put ("name" , test .getName ());
201
197
i .put ("error" , test .getResultMessage ());
202
198
i .put ("step" , test .getLastStep ());
203
199
failedTests .put (i );
204
200
}
205
-
201
+
206
202
return result ;
207
- }
203
+ }
208
204
}
209
205
}
0 commit comments