Skip to content

Commit 9e5aaea

Browse files
committed
TestLink import feature improvements.
1 parent 0c5caa9 commit 9e5aaea

File tree

5 files changed

+162
-294
lines changed

5 files changed

+162
-294
lines changed

source/src/main/java/org/cerberus/core/servlet/crud/test/testcase/ImportTestCaseFromTestLink.java

+14-262
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@
2323
import java.io.File;
2424
import java.io.IOException;
2525
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;
2927
import java.util.ArrayList;
3028
import java.util.HashMap;
3129
import java.util.List;
@@ -49,8 +47,6 @@
4947
import org.cerberus.core.crud.entity.CountryEnvironmentParameters;
5048
import org.cerberus.core.crud.entity.Invariant;
5149
import org.cerberus.core.crud.entity.TestCase;
52-
import org.cerberus.core.crud.entity.TestCaseStepAction;
53-
import org.cerberus.core.crud.entity.TestCaseStepActionControl;
5450
import org.cerberus.core.crud.factory.IFactoryTestCase;
5551
import org.cerberus.core.crud.factory.IFactoryTestCaseCountry;
5652
import org.cerberus.core.crud.factory.IFactoryTestCaseStep;
@@ -62,10 +58,8 @@
6258
import org.cerberus.core.crud.service.ITestCaseService;
6359
import org.cerberus.core.engine.entity.MessageEvent;
6460
import org.cerberus.core.enums.MessageEventEnum;
65-
import org.cerberus.core.util.StringUtil;
6661
import org.cerberus.core.util.answer.Answer;
6762
import org.cerberus.core.util.answer.AnswerUtil;
68-
import org.json.JSONArray;
6963
import org.json.JSONException;
7064
import org.json.JSONObject;
7165
import org.springframework.context.ApplicationContext;
@@ -153,78 +147,26 @@ protected void processRequest(HttpServletRequest httpServletRequest, HttpServlet
153147

154148
SAXParserFactory factory = SAXParserFactory.newInstance();
155149

156-
try (InputStream is = new ByteArrayInputStream(val.getBytes(StandardCharsets.UTF_8))) {
150+
try (InputStream is = new ByteArrayInputStream(val.getBytes())) {
157151

158152
SAXParser saxParser = factory.newSAXParser();
159153

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);
162156

163157
saxParser.parse(is, handler);
164158

165159
// print all
166160
List<TestCase> result = handler.getResult();
167-
result.forEach(System.out::println);
161+
162+
for (TestCase testCase : result) {
163+
testcaseService.createTestcaseWithDependenciesAPI(testCase);
164+
}
168165

169166
} catch (ParserConfigurationException | SAXException | IOException e) {
170167
LOG.error(e, e);
171168
}
172169

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-
228170
}
229171
}
230172

@@ -245,147 +187,6 @@ protected void processRequest(HttpServletRequest httpServletRequest, HttpServlet
245187
httpServletResponse.getWriter().print(jsonResponse.toString());
246188
}
247189

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-
389190
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
390191
/**
391192
* Handles the HTTP <code>GET</code> method.
@@ -425,40 +226,6 @@ public String getServletInfo() {
425226
return "Short description";
426227
}// </editor-fold>
427228

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-
462229
private HashMap<String, String> getParams(HttpServletRequest httpServletRequest) {
463230
HashMap<String, String> result = new HashMap<>();
464231

@@ -482,36 +249,21 @@ private HashMap<String, String> getParams(HttpServletRequest httpServletRequest)
482249
if (item.isFormField()) {
483250
result.put(item.getFieldName(), item.getString());
484251
} 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+
}
487257
}
488258
}
489259
}
490260
}
491261
}
492262
} catch (FileUploadException ex) {
493-
LOG.error(ex,ex);
263+
LOG.error(ex, ex);
494264
}
495265
LOG.debug("result Param : " + result.size());
496266
return result;
497267
}
498268

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-
517269
}

0 commit comments

Comments
 (0)