From 371ccac83b8fa395053288b3c320f9a4348d7e81 Mon Sep 17 00:00:00 2001 From: "sonarqube-agent[bot]" <210722872+sonarqube-agent[bot]@users.noreply.github.com> Date: Sat, 9 May 2026 09:17:29 +0000 Subject: [PATCH] fix: Address 3 SonarQube issues Fixed issues: - AZZjJ27ISTHyJcTACtx2 for java:S1155 rule - AZZjJ2_gSTHyJcTACtyl for java:S100 rule - AZZjJ2_gSTHyJcTACtym for java:S100 rule Generated by SonarQube Agent (task: c91726b4-fa52-4ef5-9e26-a7365404cb88) --- .../controller/ExtractionController.java | 4 +- .../service/TransformationService.java | 19 ++++---- .../service/utils/FileNamesUtil.java | 2 +- .../pscextract/ExtractionControllerTests.java | 46 +++++-------------- 4 files changed, 24 insertions(+), 47 deletions(-) diff --git a/src/main/java/fr/ans/psc/pscextract/controller/ExtractionController.java b/src/main/java/fr/ans/psc/pscextract/controller/ExtractionController.java index bad7cc0..4dacf8c 100644 --- a/src/main/java/fr/ans/psc/pscextract/controller/ExtractionController.java +++ b/src/main/java/fr/ans/psc/pscextract/controller/ExtractionController.java @@ -230,11 +230,11 @@ public boolean checkControllerIsBusy() { return busy; } - public String getZIP_EXTENSION() { + public String getZipExtension() { return ".zip"; } - public String getTXT_EXTENSION() { + public String getTxtExtension() { return ".txt"; } diff --git a/src/main/java/fr/ans/psc/pscextract/service/TransformationService.java b/src/main/java/fr/ans/psc/pscextract/service/TransformationService.java index 51419bd..19482f9 100644 --- a/src/main/java/fr/ans/psc/pscextract/service/TransformationService.java +++ b/src/main/java/fr/ans/psc/pscextract/service/TransformationService.java @@ -382,7 +382,7 @@ public File extractToCsv(ExtractionController extractionController) throws IOExc new FileOutputStream( FileNamesUtil.getFilePath( extractionController.getWorkingDirectory(), - getFileNameWithExtension(extractionController.getZIP_EXTENSION()) + getFileNameWithExtension(extractionController.getZipExtension()) ) ) ); @@ -409,18 +409,19 @@ public File extractToCsv(ExtractionController extractionController) throws IOExc Files.move( Path.of(FileNamesUtil.getFilePath( extractionController.getWorkingDirectory(), - getFileNameWithExtension(extractionController.getZIP_EXTENSION()))), + getFileNameWithExtension(extractionController.getZipExtension()))), Path.of(FileNamesUtil.getFilePath( extractionController.getFilesDirectory(), - getFileNameWithExtension(extractionController.getZIP_EXTENSION())))); + getFileNameWithExtension(extractionController.getZipExtension())))); - log.info("File at " + FileNamesUtil.getFilePath(extractionController.getWorkingDirectory(), - getFileNameWithExtension(extractionController.getZIP_EXTENSION())) - + " moved to " + FileNamesUtil.getFilePath(extractionController.getFilesDirectory(), - getFileNameWithExtension(extractionController.getZIP_EXTENSION()))); + log.info("File at {} moved to {}", + FileNamesUtil.getFilePath(extractionController.getWorkingDirectory(), + getFileNameWithExtension(extractionController.getZipExtension())), + FileNamesUtil.getFilePath(extractionController.getFilesDirectory(), + getFileNameWithExtension(extractionController.getZipExtension()))); return FileNamesUtil.getLatestExtract(extractionController.getFilesDirectory(), - getFileNameWithExtension(extractionController.getZIP_EXTENSION())); + getFileNameWithExtension(extractionController.getZipExtension())); } private void writeDigestEntry(final ZipOutputStream zos, MessageDigest extractEntryDigester) throws IOException { @@ -434,7 +435,7 @@ private void writeDigestEntry(final ZipOutputStream zos, MessageDigest extractEn } private MessageDigest writeExtractEntry(ExtractionController extractionController, final ZipOutputStream zos, final InputStream fileContent) throws IOException, NoSuchAlgorithmException { - ZipEntry zipEntry = new ZipEntry(getFileNameWithExtension(extractionController.getTXT_EXTENSION())); + ZipEntry zipEntry = new ZipEntry(getFileNameWithExtension(extractionController.getTxtExtension())); zipEntry.setTime(System.currentTimeMillis()); zos.putNextEntry(zipEntry); byte[] buffer=new byte[4096]; diff --git a/src/main/java/fr/ans/psc/pscextract/service/utils/FileNamesUtil.java b/src/main/java/fr/ans/psc/pscextract/service/utils/FileNamesUtil.java index 2400ac4..f45023a 100644 --- a/src/main/java/fr/ans/psc/pscextract/service/utils/FileNamesUtil.java +++ b/src/main/java/fr/ans/psc/pscextract/service/utils/FileNamesUtil.java @@ -63,7 +63,7 @@ public static void cleanup(String filesDirectory, String exceptFile) { listOfFiles.removeIf(file -> file.getName().contains(exceptFile)); listOfFiles.sort(FileNamesUtil::compare); - if (listOfFiles.size() > 0) { + if (!listOfFiles.isEmpty()) { listOfFiles.remove(listOfFiles.size() - 1); } diff --git a/src/test/java/fr/ans/psc/pscextract/ExtractionControllerTests.java b/src/test/java/fr/ans/psc/pscextract/ExtractionControllerTests.java index aa7507f..c0a559f 100644 --- a/src/test/java/fr/ans/psc/pscextract/ExtractionControllerTests.java +++ b/src/test/java/fr/ans/psc/pscextract/ExtractionControllerTests.java @@ -30,6 +30,8 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; @@ -107,10 +109,15 @@ private void Clean() { await().until(controllerIsReady(controller)); } - @Test - void singlePageExtractionAndResultConformityTest() throws Exception { + @ParameterizedTest + @CsvSource({ + "multiple-work-situations.json, multiple-work-situations-result", + "empty-ps.json, empty-ps-result", + "very-empty-ps.json, very-empty-ps-result" + }) + void singlePageExtractionAndResultConformityTest(String inputFile, String expectedResultFile) throws Exception { - httpMockServer.stubFor(get("/v2/ps?page=0&size=1").willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/json").withBodyFile("multiple-work-situations.json"))); + httpMockServer.stubFor(get("/v2/ps?page=0&size=1").willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/json").withBodyFile(inputFile))); httpMockServer.stubFor(get("/v2/ps?page=1&size=1").willReturn(aResponse().withStatus(410))); controller.generateExtract(null); @@ -118,7 +125,7 @@ void singlePageExtractionAndResultConformityTest() throws Exception { ResponseEntity response = controller.getFile(); - String expected = getContentAsString("multiple-work-situations-result"); + String expected = getContentAsString(expectedResultFile); String actual = getDataEntryAsString(response); Assertions.assertEquals(expected, actual); } @@ -153,38 +160,7 @@ void noPagesExtractionTest() { Assertions.assertThrows(NullPointerException.class, () -> System.out.println(Objects.requireNonNull(response.getBody()))); } - @Test -// @Disabled //FIXME please tell why !!! Disabled tests hsould disappear or get fixed, prefably the latter. - void emptyPsExtractionTest() throws Exception { - - httpMockServer.stubFor(get("/v2/ps?page=0&size=1").willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/json").withBodyFile("empty-ps.json"))); - httpMockServer.stubFor(get("/v2/ps?page=1&size=1").willReturn(aResponse().withStatus(410))); - - controller.generateExtract(null); - await().until(controllerIsReady(controller)); - - ResponseEntity response = controller.getFile(); - - String expected = getContentAsString("empty-ps-result"); - String actual = getDataEntryAsString(response); - Assertions.assertEquals(expected, actual); - } - - @Test - void fullyEmptyPsExtractionTest() throws Exception { - httpMockServer.stubFor(get("/v2/ps?page=0&size=1").willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/json").withBodyFile("very-empty-ps.json"))); - httpMockServer.stubFor(get("/v2/ps?page=1&size=1").willReturn(aResponse().withStatus(410))); - - controller.generateExtract(null); - await().until(controllerIsReady(controller)); - - ResponseEntity response = controller.getFile(); - - String expected = getContentAsString("very-empty-ps-result"); - String actual = getDataEntryAsString(response); - Assertions.assertEquals(expected, actual); - } @Test void testUploadDemoExtractFile() throws Exception {