Skip to content

Commit

Permalink
CSV file can now be downloaded from test data lib screen.
Browse files Browse the repository at this point in the history
  • Loading branch information
vertigo17 committed Jan 18, 2024
1 parent 90cac71 commit fc1dae2
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,40 +19,32 @@
*/
package org.cerberus.core.apiprivate;

import org.cerberus.core.util.datatable.DataTableInformation;
import com.google.gson.Gson;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.net.MalformedURLException;
import javax.servlet.http.HttpServletRequest;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.cerberus.core.crud.entity.LogEvent;
import org.cerberus.core.crud.entity.Test;
import org.cerberus.core.crud.factory.IFactoryLogEvent;
import org.cerberus.core.crud.factory.IFactoryTest;
import org.cerberus.core.api.exceptions.EntityNotFoundException;
import org.cerberus.core.crud.entity.TestDataLib;
import org.cerberus.core.crud.service.ILogEventService;
import org.cerberus.core.crud.service.IParameterService;
import org.cerberus.core.crud.service.ITestService;
import org.cerberus.core.crud.service.impl.TestCaseExecutionService;
import org.cerberus.core.engine.entity.MessageEvent;
import org.cerberus.core.enums.MessageEventEnum;
import org.cerberus.core.util.ParameterParserUtil;
import org.cerberus.core.util.answer.Answer;
import org.cerberus.core.crud.service.ITestDataLibService;
import org.cerberus.core.util.StringUtil;
import org.cerberus.core.util.answer.AnswerItem;
import org.cerberus.core.util.answer.AnswerList;
import org.cerberus.core.util.servlet.ServletUtil;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.owasp.html.PolicyFactory;
import org.owasp.html.Sanitizers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

Expand All @@ -68,16 +60,10 @@ public class TestDataLibController {
private final PolicyFactory policy = Sanitizers.FORMATTING.and(Sanitizers.LINKS);

@Autowired
TestCaseExecutionService testCaseExecutionService;
@Autowired
ITestService testService;
@Autowired
IFactoryTest factoryTest;
ITestDataLibService testDataLibService;
@Autowired
ILogEventService logEventService;
@Autowired
IFactoryLogEvent factoryLogEvent;
@Autowired
IParameterService parameterService;

/**
Expand All @@ -87,26 +73,48 @@ public class TestDataLibController {
* @param request
* @return
*/
@GetMapping(path = "/csv", produces = MediaType.TEXT_PLAIN_VALUE)
public String readCsv(
// @PathVariable("testdatalibid") String testdatalibid,
@GetMapping(path = "{testdatalibid}/csv", produces = MediaType.TEXT_PLAIN_VALUE)
public ResponseEntity<Resource> downloadCsv(
@PathVariable("testdatalibid") int testdatalibid,
HttpServletRequest request) {

JSONObject object = new JSONObject();
boolean userHasPermissions = request.isUserInRole("TestAdmin");

Resource resource;
String filename = "";
try {
// Calling Servlet Transversal Util.

ServletUtil.servletStart(request);

// testdatalibid = policy.sanitize(testdatalibid);
// AnswerItem<Test> answerTest = testService.readByKey(test);
object.put("hasPermissions", userHasPermissions);
AnswerItem<TestDataLib> answerTest = testDataLibService.readByKey(testdatalibid);
TestDataLib res = answerTest.getItem();
if ((res == null) || !(TestDataLib.TYPE_CSV.equals(res.getType())) || (StringUtil.isEmpty(res.getCsvUrl()))) {
throw new EntityNotFoundException(TestDataLib.class, "Data Library", testdatalibid);
}

} catch (JSONException ex) {
LOG.warn(ex, ex);
filename = res.getName();
String servicePathCsv = res.getCsvUrl();
if (!StringUtil.isURL(servicePathCsv)) {
// Url is still not valid. We try to add the path from csv parameter.
String csv_path = parameterService.getParameterStringByKey("cerberus_testdatalibcsv_path", "", "");
csv_path = StringUtil.addSuffixIfNotAlready(csv_path, File.separator);
servicePathCsv = csv_path + servicePathCsv;
resource = new InputStreamResource(new FileInputStream(servicePathCsv));

} else {

resource = new UrlResource(servicePathCsv);

}

} catch (MalformedURLException e) {
LOG.error(e, e);
return null;
} catch (FileNotFoundException ex) {
LOG.error(ex, ex);
return null;
}
return "toto";

return ResponseEntity.ok().contentType(MediaType.TEXT_PLAIN)
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + filename + "\"")
.body(resource);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
package org.cerberus.core.apiprivate.error;

import java.nio.file.AccessDeniedException;
import org.cerberus.core.api.controllers.wrappers.ResponseWrapper;
import org.cerberus.core.api.exceptions.EntityNotFoundException;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.InvalidDataAccessApiUsageException;
Expand All @@ -31,6 +33,7 @@
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

Expand Down Expand Up @@ -67,7 +70,7 @@ protected ResponseEntity<Object> handleMethodArgumentNotValid(final MethodArgume
}

@Override
protected ResponseEntity<Object> handleMissingServletRequestParameter(final MissingServletRequestParameterException ex, final HttpHeaders headers, final HttpStatus status, final WebRequest request) {
protected ResponseEntity<Object> handleMissingServletRequestParameter(final MissingServletRequestParameterException ex, final HttpHeaders headers, final HttpStatus status, final WebRequest request) {
String name = ex.getParameterName();
final String bodyOfResponse = name + " parameter is missing";
return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.EXPECTATION_FAILED, request);
Expand All @@ -85,6 +88,13 @@ protected ResponseEntity<Object> handleConflict(final RuntimeException ex, final
return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.CONFLICT, request);
}

@ExceptionHandler(EntityNotFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
protected ResponseEntity<Object> handleEntityNotFound(final EntityNotFoundException ex, final WebRequest request) {
final String bodyOfResponse = "Object not found !!";
return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.NOT_FOUND, request);
}

// 500
@ExceptionHandler({NullPointerException.class, IllegalArgumentException.class, IllegalStateException.class})
public ResponseEntity<Object> handleInternal(final RuntimeException ex, final WebRequest request) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public interface ICsvFileService {
* columns will not be included in the answer
* @param defaultNoMatchColumnValue the default value to set to any non-matched
* column if necessary
* @param execution
* @return
*/
AnswerList<HashMap<String, String>> parseCSVFile(String urlToCSVFile, String separator, HashMap<String, String> columnsToGet,List<String> columnsToHide, boolean ignoreNoMatchColumns, String defaultNoMatchColumnValue, TestCaseExecution execution);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ public static void servletStart(HttpServletRequest request) {
case "/ReadExecutionStat":
timeToWait = 30;
break;
case "/ReadTestCase":
timeToWait = 30;
break;
default:
}
LOG.debug("Servlet " + request.getServletPath() + " - Waiting : " + timeToWait);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* Edit Robot button is available directly from execution page.
* MANUAL Proxy configuration is also supported (on top of NETWORKTRAFFIC).
* Copy to Clipboard button on APIKey User screen.
* CSV file can now be downloaded from test data lib screen.

*Warning to be considered before applying the version (deprecated features)*
[square]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,16 @@ <h4 class="modal-title" id="editTestDataLibModalLabel"><span id="editTestDataLib
</div>
<div class="form-group col-sm-6">
<label name="lbl_csvUrl" for="csvUrl">CSVUrl</label>
<input id="csvUrl" name="csvUrl" class="form-control">
<div class="input-group">
<input id="csvUrl" name="csvUrl" class="form-control">
<span class="input-group-btn">
<a id="buttonDownloadCsvFile" class="pull-right" href="#">
<button class="btn btn-default" type="button" id="downloadCSV">
<span class="glyphicon glyphicon-download-alt"></span>
</button>
</a>
</span>
</div>
</div>
<div class="form-group col-sm-3">
<label name="lbl_separator" for="separator">Separator</label>
Expand Down
2 changes: 1 addition & 1 deletion source/src/main/webapp/js/transversalobject/TestDataLib.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ function initModalDataLib() {
addNewSubDataRow("SubdataTable_edit");
});


displayInvariantList("system", "SYSTEM", false, "", "");
displayInvariantList("environment", "ENVIRONMENT", false, "", "");
displayInvariantList("country", "COUNTRY", false, "", "");
Expand Down Expand Up @@ -493,6 +492,7 @@ function feedDataLibModalData(testDataLib, modalId, mode, hasPermissionsUpdate)
var obj = testDataLib;
$('#editTestDataLibModal #testdatalibid').val(obj.testDataLibID);
$('#editTestDataLibModal #name').prop("value", obj.name);
$("#buttonDownloadCsvFile").attr("href", "./api/testdatalib/" + encodeURI(obj.testDataLibID) + "/csv/");

$('#editTestDataLibModal #types').prop("value", obj.type);
collapseOrExpandTypes();
Expand Down

0 comments on commit fc1dae2

Please sign in to comment.