Skip to content

Commit

Permalink
ADM-940 [frontend][backend]: fix the report file bug (#1437)
Browse files Browse the repository at this point in the history
* ADM-940 [frontend][backend]: fix the bug about deleting expired file

* ADM-940 [frontend][backend]: fix the bug about deleting expired file

* ADM-940 [frontend][backend]: modify test
  • Loading branch information
zhou-yinyuan authored May 10, 2024
1 parent 081a690 commit 2eb02b0
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ private void deleteOldFiles(FIleType fIleType, long currentTimeStamp, File direc
for (File file : files) {
String fileName = file.getName();
String[] splitResult = fileName.split(FILENAME_SPLIT_PATTERN);
String timeStamp = splitResult[1];
String timeStamp = splitResult[3];
if (validateExpire(currentTimeStamp, Long.parseLong(timeStamp)) && !file.delete() && file.exists()) {
log.error("Failed to deleted expired fIleType: {} file, file name: {}", fIleType.getType(),
fileName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ private void deleteOldCSV(long currentTimeStamp, File directory) {
for (File file : files) {
String fileName = file.getName();
String[] splitResult = fileName.split("[-.]");
String timeStamp = splitResult[1];
String timeStamp = splitResult[3];
if (validateExpire(currentTimeStamp, Long.parseLong(timeStamp)) && !file.delete() && file.exists()) {
log.error("Failed to deleted expired CSV file, file name: {}", fileName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ class AsyncExceptionHandlerTest {

public static final String APP_OUTPUT_ERROR = "./app/output/error";

public static final String START_TIME = "20240417";

public static final String END_TIME = "20240418";

@InjectMocks
AsyncExceptionHandler asyncExceptionHandler;

Expand All @@ -56,8 +60,8 @@ void shouldDeleteAsyncException() {
long fileId = System.currentTimeMillis();
String currentTime = Long.toString(fileId);
String expireTime = Long.toString(fileId - 1900000L);
String unExpireFile = IdUtil.getBoardReportFileId(currentTime);
String expireFile = IdUtil.getBoardReportFileId(expireTime);
String unExpireFile = getBoardReportFileId(currentTime);
String expireFile = getBoardReportFileId(expireTime);
asyncExceptionHandler.put(unExpireFile, new UnauthorizedException(""));
asyncExceptionHandler.put(expireFile, new UnauthorizedException(""));

Expand All @@ -74,8 +78,8 @@ void shouldDeleteAsyncExceptionTmpFile() {
long fileId = System.currentTimeMillis();
String currentTime = Long.toString(fileId);
String expireTime = Long.toString(fileId - 1900000L);
String unExpireFile = IdUtil.getBoardReportFileId(currentTime) + ".tmp";
String expireFile = IdUtil.getBoardReportFileId(expireTime) + ".tmp";
String unExpireFile = getBoardReportFileId(currentTime) + ".tmp";
String expireFile = getBoardReportFileId(expireTime) + ".tmp";
asyncExceptionHandler.put(unExpireFile, new UnauthorizedException(""));
asyncExceptionHandler.put(expireFile, new UnauthorizedException(""));

Expand All @@ -92,8 +96,8 @@ void shouldSafeDeleteAsyncExceptionWhenHaveManyThordToDeleteFile() throws Interr
long fileId = System.currentTimeMillis();
String currentTime = Long.toString(fileId);
String expireTime = Long.toString(fileId - 1900000L);
String unExpireFile = IdUtil.getBoardReportFileId(currentTime);
String expireFile = IdUtil.getBoardReportFileId(expireTime);
String unExpireFile = getBoardReportFileId(currentTime);
String expireFile = getBoardReportFileId(expireTime);
asyncExceptionHandler.put(unExpireFile, new UnauthorizedException(""));
asyncExceptionHandler.put(expireFile, new UnauthorizedException(""));
CyclicBarrier barrier = new CyclicBarrier(3);
Expand Down Expand Up @@ -125,7 +129,7 @@ void shouldSafeDeleteAsyncExceptionWhenHaveManyThordToDeleteFile() throws Interr
void shouldPutAndGetAsyncException() {
long currentTimeMillis = System.currentTimeMillis();
String currentTime = Long.toString(currentTimeMillis);
String boardReportId = IdUtil.getBoardReportFileId(currentTime);
String boardReportId = getBoardReportFileId(currentTime);
asyncExceptionHandler.put(boardReportId, new UnauthorizedException("test"));

var baseException = asyncExceptionHandler.get(boardReportId);
Expand All @@ -147,7 +151,7 @@ void shouldThrowExceptionGivenCantWriteFileWhenPutFile() {
@Test
void shouldThrowExceptionGivenCannotReadFileWhenGetFile() throws IOException {
new File("./app/output/error/").mkdirs();
String boardReportId = IdUtil.getBoardReportFileId(Long.toString(System.currentTimeMillis()));
String boardReportId = getBoardReportFileId(Long.toString(System.currentTimeMillis()));
Path filePath = Paths.get("./app/output/error/" + boardReportId);
Files.createFile(filePath);
Files.write(filePath, "test".getBytes());
Expand All @@ -163,7 +167,7 @@ void shouldCreateTargetDirWhenPutAsyncException() {
boolean mkdirs = new File(APP_OUTPUT_ERROR).mkdirs();
long currentTimeMillis = System.currentTimeMillis();
String currentTime = Long.toString(currentTimeMillis);
String boardReportId = IdUtil.getBoardReportFileId(currentTime);
String boardReportId = getBoardReportFileId(currentTime);

asyncExceptionHandler.put(boardReportId, new UnauthorizedException("test"));

Expand All @@ -177,7 +181,7 @@ void shouldCreateTargetDirWhenPutAsyncException() {
void shouldPutAndRemoveAsyncException() {
long currentTimeMillis = System.currentTimeMillis();
String currentTime = Long.toString(currentTimeMillis);
String boardReportId = IdUtil.getBoardReportFileId(currentTime);
String boardReportId = getBoardReportFileId(currentTime);
asyncExceptionHandler.put(boardReportId, new UnauthorizedException("test"));

AsyncExceptionDTO baseException = asyncExceptionHandler.remove(boardReportId);
Expand All @@ -190,7 +194,7 @@ void shouldPutAndRemoveAsyncException() {
@Test
void shouldReturnExceptionGivenWrongFileWhenReadAndRemoveAsyncException() throws IOException {
new File("./app/output/error/").mkdirs();
String boardReportId = IdUtil.getBoardReportFileId(Long.toString(System.currentTimeMillis()));
String boardReportId = getBoardReportFileId(Long.toString(System.currentTimeMillis()));
Path filePath = Paths.get("./app/output/error/" + boardReportId);
Files.createFile(filePath);
Files.write(filePath, "test".getBytes());
Expand All @@ -204,7 +208,7 @@ void shouldReturnExceptionGivenWrongFileWhenReadAndRemoveAsyncException() throws
@Test
void shouldThrowExceptionWhenDeleteFile() {
File mockFile = mock(File.class);
when(mockFile.getName()).thenReturn("board-1683734399999");
when(mockFile.getName()).thenReturn("board-20240417-20240418-1683734399999");
when(mockFile.delete()).thenThrow(new RuntimeException("test"));
File[] mockFiles = new File[] { mockFile };
File directory = mock(File.class);
Expand All @@ -217,7 +221,7 @@ void shouldThrowExceptionWhenDeleteFile() {
@Test
void shouldDeleteFailWhenDeleteFile() {
File mockFile = mock(File.class);
when(mockFile.getName()).thenReturn("board-1683734399999");
when(mockFile.getName()).thenReturn("board-20240417-20240418-1683734399999");
when(mockFile.delete()).thenReturn(false);
when(mockFile.exists()).thenReturn(true);
File[] mockFiles = new File[] { mockFile };
Expand All @@ -237,4 +241,8 @@ private void deleteTestFile(String reportId) {
asyncExceptionHandler.remove(reportId);
}

private String getBoardReportFileId(String timestamp) {
return "board-" + START_TIME + "-" + END_TIME + "-" + timestamp;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class DeleteExpireMetricsDataCompletedFile {
@Test
void shouldDeleteMetricsDataReadyWhenMetricsFileIsExpire() throws IOException {
long currentTimeMillis = System.currentTimeMillis();
String prefix = "prefix-";
String prefix = "prefix-20240417-20240418-";
String currentTimeFileId = prefix + currentTimeMillis;
String expireTimeFileId = prefix + (currentTimeMillis - 1900000L);
String expireTimeLockFileId = prefix + (currentTimeMillis - 1900000L) + ".lock";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ class AsyncReportRequestHandlerTest {

public static final String APP_OUTPUT_REPORT = "./app/output/report";

public static final String START_TIME = "20240417";

public static final String END_TIME = "20240418";

@InjectMocks
AsyncReportRequestHandler asyncReportRequestHandler;

Expand All @@ -47,8 +51,8 @@ void shouldDeleteReportWhenReportIsExpire() throws IOException {
long currentTimeMillis = System.currentTimeMillis();
String currentTime = Long.toString(currentTimeMillis);
String expireTime = Long.toString(currentTimeMillis - 1900000L);
String unExpireFile = IdUtil.getBoardReportFileId(currentTime);
String expireFile = IdUtil.getBoardReportFileId(expireTime);
String unExpireFile = getBoardReportFileId(currentTime);
String expireFile = getBoardReportFileId(expireTime);
asyncReportRequestHandler.putReport(unExpireFile, ReportResponse.builder().build());
asyncReportRequestHandler.putReport(expireFile, ReportResponse.builder().build());

Expand Down Expand Up @@ -79,4 +83,8 @@ void shouldThrowGenerateReportExceptionGivenFileNameInvalidWhenHandlerPutData()
() -> asyncReportRequestHandler.putReport("../", ReportResponse.builder().build()));
}

private String getBoardReportFileId(String timestamp) {
return "board-" + START_TIME + "-" + END_TIME + "-" + timestamp;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -957,7 +957,7 @@ void shouldNotDeleteOldCsvWhenExportCsvWithoutOldCsvInsideThirtyMinutes() throws
@Test
void shouldDeleteFailWhenDeleteCSV() {
File mockFile = mock(File.class);
when(mockFile.getName()).thenReturn("file1-1683734399999.CSV");
when(mockFile.getName()).thenReturn("file1-20240417-20240418-1683734399999.CSV");
when(mockFile.delete()).thenReturn(false);
File[] mockFiles = new File[] { mockFile };
File directory = mock(File.class);
Expand Down Expand Up @@ -985,7 +985,7 @@ void shouldThrowExceptionWhenDeleteCSV() {
@Test
void shouldDeleteFailWhenDeleteFile() {
File mockFile = mock(File.class);
when(mockFile.getName()).thenReturn("board-1683734399999");
when(mockFile.getName()).thenReturn("board-20240417-20240418-1683734399999");
when(mockFile.delete()).thenReturn(false);
when(mockFile.exists()).thenReturn(true);
File[] mockFiles = new File[] { mockFile };
Expand All @@ -1000,7 +1000,7 @@ void shouldDeleteFailWhenDeleteFile() {
@Test
void shouldDeleteTempFailWhenDeleteFile() {
File mockFile = mock(File.class);
when(mockFile.getName()).thenReturn("board-1683734399999.tmp");
when(mockFile.getName()).thenReturn("board-20240417-20240418-1683734399999.tmp");
when(mockFile.delete()).thenReturn(true);
when(mockFile.exists()).thenReturn(false);
File[] mockFiles = new File[] { mockFile };
Expand Down

0 comments on commit 2eb02b0

Please sign in to comment.