Skip to content

Commit

Permalink
fix sonar
Browse files Browse the repository at this point in the history
  • Loading branch information
nashtech-longlevanquoc1 committed Oct 18, 2024
1 parent d8418f6 commit 1e33ddb
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package com.yas.commonlibrary.csv;

import com.opencsv.CSVWriter;
import com.opencsv.ICSVWriter;
import com.yas.commonlibrary.csv.anotation.CsvColumn;
import com.yas.commonlibrary.csv.anotation.CsvName;
import com.yas.commonlibrary.utils.DateTimeUtils;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.List;
Expand All @@ -20,15 +23,18 @@
@Slf4j
public class CsvExporter {

public static final String COMMA = ",";
private CsvExporter() {
}

private static final String GET_PREFIX = "get";

public static <T> byte[] exportToCsv(List<BaseCsv> dataList, Class<T> clazz) throws IOException {
try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(byteArrayOutputStream,
StandardCharsets.UTF_8);
CSVWriter csvWriter = new CSVWriter(outputStreamWriter, CSVWriter.DEFAULT_SEPARATOR,
CSVWriter.NO_QUOTE_CHARACTER,
CSVWriter.DEFAULT_ESCAPE_CHARACTER, CSVWriter.DEFAULT_LINE_END)) {
CSVWriter csvWriter = new CSVWriter(outputStreamWriter, ICSVWriter.DEFAULT_SEPARATOR,
ICSVWriter.NO_QUOTE_CHARACTER,
ICSVWriter.DEFAULT_ESCAPE_CHARACTER, ICSVWriter.DEFAULT_LINE_END)) {

// Write CSV header
writeCsvHeader(csvWriter, clazz);
Expand Down Expand Up @@ -77,16 +83,20 @@ private static String[] getFieldValues(Field[] fields, Object data) {

private static String getFieldValueAsString(Field field, Object data) {
try {
field.setAccessible(true);
Object value = field.get(data);
String getterName = GET_PREFIX + StringUtils.capitalize(field.getName());
Method getter = data.getClass().getMethod(getterName);
Object value = getter.invoke(data);

if (!Objects.isNull(value) && value instanceof List) {
return ("[" + String.join("|", (List<String>) value) + "]");
}

return value != null ? value.toString() : StringUtils.EMPTY;
} catch (IllegalAccessException e) {
log.warn("Get value field err ");
e.printStackTrace();
log.warn("Get value field err {}", e.getMessage());
return StringUtils.EMPTY;
} catch (InvocationTargetException | NoSuchMethodException e) {
log.warn("Get value err {}", e.getMessage());
return StringUtils.EMPTY;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

public class DateTimeUtils {

private DateTimeUtils() {
}

private static final String DEFAULT_PATTERN = "dd-MM-yyyy_HH-mm-ss";

public static String format(LocalDateTime dateTime) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.yas.commonlibrary.csv.anotation.CsvColumn;
import com.yas.commonlibrary.csv.anotation.CsvName;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.SuperBuilder;
import org.junit.jupiter.api.Test;

Expand All @@ -16,6 +18,8 @@ class CsvExporterTest {

@SuperBuilder
@CsvName(fileName = "TestFile")
@Getter
@Setter
static class TestData extends BaseCsv {

@CsvColumn(columnName = "Name")
Expand All @@ -27,29 +31,31 @@ static class TestData extends BaseCsv {

@Test
void testExportToCsv_withValidData_shouldReturnCorrectCsvContent() throws IOException {
// Given
List<BaseCsv> dataList = Arrays.asList(
TestData.builder()
.id(1L)
.name("Alice")
.tags(Arrays.asList("tag1", "tag2"))
.build(),
TestData.builder()
.id(2L)
.name("Bob")
.tags(Arrays.asList("tag3", "tag4"))
.build()
);
// When
byte[] csvBytes = CsvExporter.exportToCsv(dataList, TestData.class);
String csvContent = new String(csvBytes);

// Then
String expectedCsv = "Id,Name,Tags\n" +
"1,Alice,[tag1|tag2]\n" +
"2,Bob,[tag3|tag4]\n";

assertEquals(expectedCsv, csvContent);
// Given
List<BaseCsv> dataList = Arrays.asList(
TestData.builder()
.id(1L)
.name("Alice")
.tags(Arrays.asList("tag1", "tag2"))
.build(),
TestData.builder()
.id(2L)
.name("Bob")
.tags(Arrays.asList("tag3", "tag4"))
.build()
);
// When
byte[] csvBytes = CsvExporter.exportToCsv(dataList, TestData.class);
String csvContent = new String(csvBytes);

// Then
String expectedCsv = """
Id,Name,Tags
1,Alice,[tag1|tag2]
2,Bob,[tag3|tag4]
""";

assertEquals(expectedCsv, csvContent);
}

@Test
Expand Down
4 changes: 4 additions & 0 deletions order/src/main/java/com/yas/order/model/csv/OrderItemCsv.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@
import com.yas.order.model.enumeration.PaymentStatus;
import java.math.BigDecimal;
import java.time.ZonedDateTime;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.SuperBuilder;

@CsvName(fileName = "Orders")
@SuperBuilder
@Getter
@Setter
public class OrderItemCsv extends BaseCsv {

@CsvColumn(columnName = "Order status")
Expand Down

0 comments on commit 1e33ddb

Please sign in to comment.