23
23
import java .io .File ;
24
24
import java .io .IOException ;
25
25
import java .io .InputStream ;
26
- import java .nio .charset .StandardCharsets ;
27
- import java .nio .file .Files ;
28
- import java .nio .file .Paths ;
26
+ import java .io .UnsupportedEncodingException ;
29
27
import java .util .ArrayList ;
30
28
import java .util .HashMap ;
31
29
import java .util .List ;
49
47
import org .cerberus .core .crud .entity .CountryEnvironmentParameters ;
50
48
import org .cerberus .core .crud .entity .Invariant ;
51
49
import org .cerberus .core .crud .entity .TestCase ;
52
- import org .cerberus .core .crud .entity .TestCaseStepAction ;
53
- import org .cerberus .core .crud .entity .TestCaseStepActionControl ;
54
50
import org .cerberus .core .crud .factory .IFactoryTestCase ;
55
51
import org .cerberus .core .crud .factory .IFactoryTestCaseCountry ;
56
52
import org .cerberus .core .crud .factory .IFactoryTestCaseStep ;
62
58
import org .cerberus .core .crud .service .ITestCaseService ;
63
59
import org .cerberus .core .engine .entity .MessageEvent ;
64
60
import org .cerberus .core .enums .MessageEventEnum ;
65
- import org .cerberus .core .util .StringUtil ;
66
61
import org .cerberus .core .util .answer .Answer ;
67
62
import org .cerberus .core .util .answer .AnswerUtil ;
68
- import org .json .JSONArray ;
69
63
import org .json .JSONException ;
70
64
import org .json .JSONObject ;
71
65
import org .springframework .context .ApplicationContext ;
@@ -153,78 +147,26 @@ protected void processRequest(HttpServletRequest httpServletRequest, HttpServlet
153
147
154
148
SAXParserFactory factory = SAXParserFactory .newInstance ();
155
149
156
- try (InputStream is = new ByteArrayInputStream (val .getBytes (StandardCharsets . UTF_8 ))) {
150
+ try (InputStream is = new ByteArrayInputStream (val .getBytes ())) {
157
151
158
152
SAXParser saxParser = factory .newSAXParser ();
159
153
160
- // parse XML and map to object, it works, but not recommend, try JAXB
161
- MapTestLinkObjectHandlerSax handler = new MapTestLinkObjectHandlerSax ();
154
+ // parse XML and map to object,
155
+ MapTestLinkObjectHandlerSax handler = new MapTestLinkObjectHandlerSax (targetFolder , targetApplication , userCreated , testcaseService );
162
156
163
157
saxParser .parse (is , handler );
164
158
165
159
// print all
166
160
List <TestCase > result = handler .getResult ();
167
- result .forEach (System .out ::println );
161
+
162
+ for (TestCase testCase : result ) {
163
+ testcaseService .createTestcaseWithDependenciesAPI (testCase );
164
+ }
168
165
169
166
} catch (ParserConfigurationException | SAXException | IOException e ) {
170
167
LOG .error (e , e );
171
168
}
172
169
173
- // JSONObject json = new JSONObject(val);
174
- // if (isCompatible(json)) {
175
- // String masterSIDEURL = json.getString("url");
176
- // JSONArray testList = new JSONArray();
177
- // testList = json.getJSONArray("tests");
178
-
179
-
180
-
181
- // for (int i = 0; i < testList.length(); i++) {
182
- //
183
- // JSONObject test = new JSONObject();
184
- // test = testList.getJSONObject(i);
185
- // LOG.debug("importing :" + i + " : " + test.toString());
186
- //
187
- // // Dynamically get a new testcase ID.
188
- // String targetTestcase = testcaseService.getNextAvailableTestcaseId(targetFolder);
189
- // TestCase newTC = testcaseFactory.create(targetFolder, targetTestcase, test.getString("name"));
190
- // newTC.setComment("Imported from Selenium IDE. Test ID : " + test.getString("id"));
191
- // newTC.setApplication(targetApplication);
192
- // newTC.setType(TestCase.TESTCASE_TYPE_AUTOMATED);
193
- // newTC.setConditionOperator("always");
194
- // newTC.setOrigine(TestCase.TESTCASE_ORIGIN_TESTLINK);
195
- // newTC.setRefOrigine(test.getString("id"));
196
- // newTC.setStatus("WORKING");
197
- // newTC.setUsrCreated(userCreated);
198
- //
199
- // countries.forEach(country -> {
200
- // newTC.appendTestCaseCountries(testcaseCountryFactory.create(targetFolder, targetTestcase, country.getValue()));
201
- // });
202
- // // Step
203
- // TestCaseStep newStep = testcaseStepFactory.create(targetFolder, targetTestcase, 1, 1, TestCaseStep.LOOP_ONCEIFCONDITIONTRUE, "always", "", "", "", new JSONArray(), "", false, null, null, 0, false, false, userCreated, null, null, null);
204
- //
205
- // // Action
206
- // for (int j = 0; j < test.getJSONArray("commands").length(); j++) {
207
- // JSONObject command = test.getJSONArray("commands").getJSONObject(j);
208
- // TestCaseStepAction newA = getActionFromSIDE(command, (j + 1), masterSIDEURL, urls, targetFolder, targetTestcase);
209
- // if (newA != null) {
210
- // newStep.appendActions(newA);
211
- // }
212
- // }
213
- //
214
- // newTC.appendSteps(newStep);
215
- //
216
- // testcaseService.createTestcaseWithDependencies(newTC);
217
-
218
- // }
219
- // } else {
220
- // msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
221
- // msg.setDescription(msg.getDescription().replace("%ITEM%", "TestCase ")
222
- // .replace("%OPERATION%", "Import")
223
- // .replace("%REASON%", "The file you're trying to import is not supported or is not in a compatible version format."));
224
- // ans.setResultMessage(msg);
225
- // finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, ans);
226
- // }
227
-
228
170
}
229
171
}
230
172
@@ -245,147 +187,6 @@ protected void processRequest(HttpServletRequest httpServletRequest, HttpServlet
245
187
httpServletResponse .getWriter ().print (jsonResponse .toString ());
246
188
}
247
189
248
- private TestCaseStepAction getActionFromSIDE (JSONObject command , Integer i , String masterSIDEURL , List <String > applicationURLs , String targetFolder , String targetTestcase ) {
249
- TestCaseStepAction newAction = null ;
250
- TestCaseStepActionControl newControl = null ;
251
- try {
252
- String action = null ;
253
- String action_value1 = "" ;
254
- String action_value2 = "" ;
255
- String control = null ;
256
- String control_value1 = "" ;
257
- String description = command .getString ("comment" );
258
- String cond = TestCaseStepAction .CONDITIONOPERATOR_ALWAYS ;
259
- String commandS = command .getString ("command" );
260
- if (commandS .startsWith ("//" )) {
261
- cond = TestCaseStepAction .CONDITIONOPERATOR_NEVER ;
262
- commandS = commandS .substring (2 );
263
- }
264
-
265
- switch (commandS ) {
266
- case "setWindowSize" :
267
- case "mouseOut" :
268
- // Those commands are ignored.
269
- break ;
270
- case "open" :
271
- LOG .debug (masterSIDEURL );
272
- LOG .debug (applicationURLs );
273
- action_value1 = masterSIDEURL + command .getString ("target" );
274
- if (!isURLInApplication (action_value1 , applicationURLs )) {
275
- action = TestCaseStepAction .ACTION_OPENURL ;
276
- } else {
277
- action = TestCaseStepAction .ACTION_OPENURLWITHBASE ;
278
- action_value1 = command .getString ("target" );
279
- }
280
- break ;
281
- case "type" :
282
- action = TestCaseStepAction .ACTION_TYPE ;
283
- action_value1 = convertElement (command );
284
- action_value2 = command .getString ("value" );
285
- break ;
286
- case "click" :
287
- action = TestCaseStepAction .ACTION_CLICK ;
288
- action_value1 = convertElement (command );
289
- break ;
290
- case "mouseDown" :
291
- action = TestCaseStepAction .ACTION_MOUSELEFTBUTTONPRESS ;
292
- action_value1 = convertElement (command );
293
- break ;
294
- case "sendKeys" :
295
- action = TestCaseStepAction .ACTION_KEYPRESS ;
296
- action_value1 = convertElement (command );
297
- action_value2 = mappKey (command .getString ("value" ));
298
- break ;
299
- case "mouseUp" :
300
- action = TestCaseStepAction .ACTION_MOUSELEFTBUTTONRELEASE ;
301
- action_value1 = convertElement (command );
302
- break ;
303
- case "mouseOver" :
304
- action = TestCaseStepAction .ACTION_MOUSEOVER ;
305
- action_value1 = convertElement (command );
306
- break ;
307
- case "waitForElementVisible" :
308
- action = TestCaseStepAction .ACTION_WAIT ;
309
- action_value1 = convertElement (command );
310
- break ;
311
- case "verifyText" :
312
- action = TestCaseStepAction .ACTION_DONOTHING ;
313
- control = TestCaseStepActionControl .CONTROL_VERIFYELEMENTPRESENT ;
314
- control_value1 = convertElement (command );
315
- break ;
316
- default :
317
- action = TestCaseStepAction .ACTION_DONOTHING ;
318
- description = "Unknow Selenium IDE command '" + commandS + "'" ;
319
- if (!StringUtil .isEmptyOrNull (command .getString ("target" ))) {
320
- description += " on target '" + convertElement (command ) + "'" ;
321
- }
322
- if (!StringUtil .isEmptyOrNull (command .getString ("value" ))) {
323
- description += " with value '" + command .getString ("value" ) + "'" ;
324
- }
325
- if (!StringUtil .isEmptyOrNull (command .getString ("comment" ))) {
326
- description += " - " + command .getString ("comment" );
327
- }
328
- }
329
- if (action != null ) {
330
- newAction = testcaseStepActionFactory .create (targetFolder , targetTestcase , 1 , i , i , TestCaseStepAction .CONDITIONOPERATOR_ALWAYS , "" , "" , "" , new JSONArray (), action , action_value1 , action_value2 , "" ,
331
- new JSONArray (), false , description , null ,
332
- false , false , 0 , 0 );
333
- if (control != null ) {
334
- newControl = testcaseStepActionControlFactory .create (targetFolder , targetTestcase , 1 , i , 1 , 1 , TestCaseStepAction .CONDITIONOPERATOR_ALWAYS , "" , "" , "" , new JSONArray (), control , control_value1 , "" , "" , new JSONArray (), true , description , null , false , false , 0 , 0 );
335
- List <TestCaseStepActionControl > controlList = new ArrayList <>();
336
- controlList .add (newControl );
337
- newAction .setControls (controlList );
338
- }
339
-
340
- }
341
- } catch (JSONException ex ) {
342
- LOG .error (ex , ex );
343
- }
344
- return newAction ;
345
-
346
- }
347
-
348
- private static String convertElement (JSONObject command ) throws JSONException {
349
- String target = command .getString ("target" );
350
- if (target .startsWith ("name=" ) || target .startsWith ("xpath=" ) || target .startsWith ("id=" )) {
351
- return target ;
352
- }
353
- JSONArray targets = command .getJSONArray ("targets" );
354
- for (int i = 0 ; i < targets .length (); i ++) {
355
- if (targets .getJSONArray (i ).getString (0 ).startsWith ("xpath=" )) {
356
- return targets .getJSONArray (i ).getString (0 );
357
- }
358
- }
359
- return target ;
360
-
361
- }
362
-
363
- private static String mappKey (String key ) throws JSONException {
364
- switch (key ) {
365
- case "${KEY_ENTER}" :
366
- return "ENTER" ;
367
- default :
368
- return key ;
369
- }
370
- }
371
-
372
- private static boolean isURLInApplication (String url , List <String > appURLs ) throws JSONException {
373
- String cleanedUrl = StringUtil .addSuffixIfNotAlready (StringUtil .removeProtocolFromHostURL (url ), "/" );
374
- for (String appURL : appURLs ) {
375
- appURL = StringUtil .addSuffixIfNotAlready (StringUtil .removeProtocolFromHostURL (appURL ), "/" );
376
- LOG .debug (appURL + " - " + cleanedUrl );
377
- if (appURL .equalsIgnoreCase (cleanedUrl )) {
378
- return true ;
379
- }
380
- }
381
- return false ;
382
- }
383
-
384
- public static JSONObject parseJSONFile (String filename ) throws JSONException , IOException {
385
- String content = new String (Files .readAllBytes (Paths .get (filename )));
386
- return new JSONObject (content );
387
- }
388
-
389
190
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
390
191
/**
391
192
* Handles the HTTP <code>GET</code> method.
@@ -425,40 +226,6 @@ public String getServletInfo() {
425
226
return "Short description" ;
426
227
}// </editor-fold>
427
228
428
- private List <String > getFiles (HttpServletRequest httpServletRequest ) {
429
- List <String > result = new ArrayList <>();
430
-
431
- try {
432
- if (ServletFileUpload .isMultipartContent (httpServletRequest )) {
433
- DiskFileItemFactory factory = new DiskFileItemFactory ();
434
-
435
- ServletContext servletContext = this .getServletConfig ().getServletContext ();
436
- File repository = (File ) servletContext .getAttribute ("javax.servlet.context.tempdir" );
437
- factory .setRepository (repository );
438
-
439
- ServletFileUpload upload = new ServletFileUpload (factory );
440
-
441
- List <FileItem > formItems = upload .parseRequest (httpServletRequest );
442
- if (formItems != null ) {
443
- LOG .debug ("Nb of Files to import : " + formItems .size ());
444
- if (formItems .size () > 0 ) {
445
- int i = 1 ;
446
- for (FileItem item : formItems ) {
447
- LOG .debug ("File to import (" + i ++ + ") : " + item .toString () + " FieldName : " + item .getFieldName () + " ContentType : " + item .getContentType ());
448
- if (!item .isFormField ()) {
449
- result .add (item .getString ());
450
- }
451
- }
452
- }
453
- }
454
- }
455
- } catch (FileUploadException ex ) {
456
- LOG .error (ex ,ex );
457
- }
458
- LOG .debug ("result : " + result .size ());
459
- return result ;
460
- }
461
-
462
229
private HashMap <String , String > getParams (HttpServletRequest httpServletRequest ) {
463
230
HashMap <String , String > result = new HashMap <>();
464
231
@@ -482,36 +249,21 @@ private HashMap<String, String> getParams(HttpServletRequest httpServletRequest)
482
249
if (item .isFormField ()) {
483
250
result .put (item .getFieldName (), item .getString ());
484
251
} else {
485
- result .put (item .getFieldName () + i , item .getString ());
486
-
252
+ try {
253
+ result .put (item .getFieldName () + i , item .getString ("utf-8" ));
254
+ } catch (UnsupportedEncodingException ex ) {
255
+ LOG .warn (ex , ex );
256
+ }
487
257
}
488
258
}
489
259
}
490
260
}
491
261
}
492
262
} catch (FileUploadException ex ) {
493
- LOG .error (ex ,ex );
263
+ LOG .error (ex , ex );
494
264
}
495
265
LOG .debug ("result Param : " + result .size ());
496
266
return result ;
497
267
}
498
268
499
- private boolean isCompatible (JSONObject json ) {
500
-
501
- try {
502
- if (!json .has ("version" )) {
503
- return false ;
504
- }
505
- String fileVersion = json .getString ("version" );
506
- LOG .debug ("Version from import file : " + fileVersion );
507
-
508
- return true ;
509
-
510
- //Compatibility Matrix. To update if testcase (including dependencies) model change.
511
- } catch (JSONException ex ) {
512
- LOG .warn (ex );
513
- return false ;
514
- }
515
- }
516
-
517
269
}
0 commit comments