Test unitarios para el siguiente código @Slf4j @RequiredArgsConstructor public class BulkThread extends Thread { private final CrudTemplatesConfiguration templatesConfiguration; private final CrudTemplateFeignClient client; private final EmailService emailService; private final InputStream file; private final String templateId; private final MethodType method; private final Collection<String> emails; private final UUID uuid; private final String user; private String globalStatus = STATUS_OK; @Override public void run() { StopWatch timeWatcher = new StopWatch(); timeWatcher.start(); try { // Get configuration template TemplateProperties templateProperties = templatesConfiguration.getTemplates().get(templateId); if (templateProperties == null) { throw new TemplateNotFoundException(String.format("Template %s not found", templateId)); } // Extract request, send and save the result byte[] resultFile = parseToJson(file, templateProperties, method); timeWatcher.stop(); // Send emails if (emails != null) { emailService.send(new ArrayList<>(emails), EmailData.builder() .file(resultFile) .fileId(uuid.toString()) .startTime(timeWatcher.getStartTime()) .endTime(timeWatcher.getStartTime() + timeWatcher.getTime()) .status(globalStatus) .user(user) .build()); } } catch (IOException ioException) { log.error("Error executing asynchronous loading", ioException); } } /** * Invoke service * @param jsonNode request * @param properties properties * @return status of the invocation */ private String invokeClient(JSONObject jsonNode, TemplateProperties properties, MethodType method) { log.info("Trying to send {}", jsonNode); try (Response response = MethodType.POST.equals(method) ? client.post(URI.create(properties.getUrl()), jsonNode.toString()) : client.patch(URI.create(properties.getUrl()), jsonNode.get(CELL_ID_NAME).toString(), jsonNode.toString())) { if (response != null && HttpStatus.OK.ordinal() != response.status()) { log.error("Error sending {}, status code is {}", jsonNode, response.status()); globalStatus = STATUS_ERROR; return String.valueOf(response.status()); } } catch (Exception e) { log.error("Error sending {}", jsonNode, e); globalStatus = STATUS_ERROR; return e.getMessage(); } return STATUS_OK; } /** * Parse Excel file, create requests and invoke service. * @param file Excel file. * @param templateProperties template properties * @return Excel file result. */ private byte[] parseToJson(InputStream file, TemplateProperties templateProperties, MethodType method) throws IOException { // Open file Workbook workbook = new XSSFWorkbook(file); // Get excel page with data and create page with data result Sheet sheet = workbook.getSheet(PAGE_NAME); Sheet resultSheet = workbook.createSheet(PAGE_RESULT_NAME); // Create list of fields to calculate requests List<String> fields = new ArrayList<>(); boolean fieldExtracted = false; // Get row iterator Iterator<Row> rowIterator = sheet.iterator(); int i = 0; while (rowIterator.hasNext()) { // Result objects JSONObject jsonObject = new JSONObject(); Row resultRow = resultSheet.createRow(i); // Get cell iterator Iterator<Cell> it = rowIterator.next().cellIterator(); int j = 0; while (it.hasNext()) { // Get current cell and prepare result cell Cell cell = it.next(); setCellResult(cell, resultRow.createCell(j)); // Extract values (first row) or prepare request if (!fieldExtracted) { fields.add(cell.getStringCellValue()); } else { setJSONValue(fields.get(j), jsonObject, cell); } j++; } if (!fieldExtracted) { // Create extra cells to status log.info("Extracted fields are {}", fields); Cell resultCell = resultRow.createCell(j); resultCell.setCellValue(CELL_STATUS_NAME); resultCell = resultRow.createCell(++j); resultCell.setCellValue(CELL_STATUS_DESC_NAME); } else { // Invoke endpoint and save status String status = invokeClient(jsonObject, templateProperties, method); Cell resultCell = resultRow.createCell(j); if (STATUS_OK.equals(status)) { resultCell.setCellValue(status); } else { resultCell.setCellValue(STATUS_ERROR); resultCell = resultRow.createCell(++j); resultCell.setCellValue(status); } } fieldExtracted = true; i++; } // Save new file file.close(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); workbook.write(outputStream); workbook.close(); return outputStream.toByteArray(); } private void setCellResult(Cell cell, Cell resultCell) { switch (cell.getCellType()) { case NUMERIC: resultCell.setCellValue(cell.getNumericCellValue()); break; case STRING: resultCell.setCellValue(cell.getStringCellValue()); break; case BOOLEAN: resultCell.setCellValue(cell.getBooleanCellValue()); break; default: throw new ParseExcelException(String.format("Type %s not found", cell.getCellType())); } } private Object extractType(Cell cell) { switch (cell.getCellType()) { case NUMERIC: return cell.getNumericCellValue(); case STRING: return cell.getStringCellValue().isEmpty() ? null : cell.getStringCellValue(); case BOOLEAN: return cell.getBooleanCellValue(); default: throw new ParseExcelException(String.format("Type %s not found", cell.getCellType())); } } private void setJSONValue(String field, JSONObject node, Cell cell) { if(field.contains(".")) { String[] fieldNames = field.split("\\."); for (int i = 0; i < fieldNames.length - 1; i++) { String fieldName = fieldNames[i]; if (!node.has(fieldName)) { node.put(fieldName, new JSONObject()); } node = node.getJSONObject(fieldName); } node.put(fieldNames[fieldNames.length - 1], extractType(cell)); } else { node.put(field, extractType(cell)); } } }
def add(a, b): return a + b add(1, 2)