From: antoine.vinot Date: Wed, 6 Sep 2023 08:49:08 +0000 (+0200) Subject: NO-JIRA Fix test sending data to ES X-Git-Tag: 10.3.0.82913~480 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=ea52b9c85ca6035493481049a91f75085e32ec84;p=sonarqube.git NO-JIRA Fix test sending data to ES --- diff --git a/test-monitoring/src/main/java/org/sonarqube/monitoring/test/aspect/TestFailureAspect.java b/test-monitoring/src/main/java/org/sonarqube/monitoring/test/aspect/TestFailureAspect.java index 2cb05c78b16..52d6d583cdb 100644 --- a/test-monitoring/src/main/java/org/sonarqube/monitoring/test/aspect/TestFailureAspect.java +++ b/test-monitoring/src/main/java/org/sonarqube/monitoring/test/aspect/TestFailureAspect.java @@ -47,15 +47,24 @@ public class TestFailureAspect { public static final String BUILD_NUMBER = System.getenv("BUILD_NUMBER"); public static final String QA_CATEGORY = System.getenv("QA_CATEGORY"); - private static final Path PATH = Paths.get("/tmp/test-monitoring.log"); + private final Path path; private static final Gson GSON = new Gson(); - static { + public TestFailureAspect() { + this(Paths.get("/tmp/test-monitoring.log")); + } + + public TestFailureAspect(Path path) { + this.path = path; + createEmptyLogFile(); + } + + private void createEmptyLogFile() { try { - if (!Files.exists(PATH, LinkOption.NOFOLLOW_LINKS)) { - Files.createFile(PATH); + if (!Files.exists(path, LinkOption.NOFOLLOW_LINKS)) { + Files.createFile(path); } - Files.write(PATH, "".getBytes(UTF_8)); + Files.write(path, "".getBytes(UTF_8)); } catch (IOException e) { // Ignore } @@ -90,10 +99,10 @@ public class TestFailureAspect { .build(); } - public static void persistMeasure(Measure measure) { + private void persistMeasure(Measure measure) { try { - Files.write(PATH, GSON.toJson(measure).getBytes(UTF_8), StandardOpenOption.APPEND); - Files.write(PATH, "\n".getBytes(UTF_8), StandardOpenOption.APPEND); + Files.write(path, GSON.toJson(measure).getBytes(UTF_8), StandardOpenOption.APPEND); + Files.write(path, "\n".getBytes(UTF_8), StandardOpenOption.APPEND); } catch (IOException e) { // Ignore } diff --git a/test-monitoring/src/test/java/org/sonarqube/monitoring/test/aspect/TestFailureAspectTest.java b/test-monitoring/src/test/java/org/sonarqube/monitoring/test/aspect/TestFailureAspectTest.java index 1b01be0e095..a5c7990fc01 100644 --- a/test-monitoring/src/test/java/org/sonarqube/monitoring/test/aspect/TestFailureAspectTest.java +++ b/test-monitoring/src/test/java/org/sonarqube/monitoring/test/aspect/TestFailureAspectTest.java @@ -21,19 +21,15 @@ package org.sonarqube.monitoring.test.aspect; import java.io.IOException; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.nio.file.Path; -import java.nio.file.Paths; import org.aspectj.lang.JoinPoint; import org.junit.Before; -import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.notification.Failure; -import static java.nio.file.Files.createDirectory; -import static java.nio.file.Files.exists; import static java.nio.file.Files.readAllBytes; import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.fail; import static org.junit.runner.Description.createTestDescription; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -42,22 +38,17 @@ public class TestFailureAspectTest { private TestFailureAspect testFailureAspect; - private static final Path TMP_PATH = Path.of("/tmp"); - - @BeforeClass - public static void createTmpFolder() throws IOException { - if (!exists(TMP_PATH)) { - createDirectory(TMP_PATH); - } - } + private Path fakeLogPath; @Before - public void setup() { - testFailureAspect = new TestFailureAspect(); + public void setup() throws IOException { + Path tempDir = Files.createTempDirectory("TestFailureAspectTest"); + fakeLogPath = tempDir.resolve("fake-test-monitoring.log"); + testFailureAspect = new TestFailureAspect(fakeLogPath); } @Test - public void afterFireTestFailure_shouldPersistMeasure() { + public void afterFireTestFailure_shouldPersistMeasure() throws IOException { JoinPoint joinPoint = mock(JoinPoint.class); Failure failure = new Failure( createTestDescription("testClass", "testMethod"), @@ -66,8 +57,7 @@ public class TestFailureAspectTest { testFailureAspect.afterFireTestFailure(joinPoint); - String fileContent = getFileContent(Paths.get("/tmp/test-monitoring.log")); - assertThat(fileContent) + assertThat(getFileContent()) .contains("\"timestamp\":\"" ) .contains("\"testClass\":\"testClass\"") .contains("\"testMethod\":\"testMethod\"") @@ -76,14 +66,9 @@ public class TestFailureAspectTest { .contains("\"exceptionLogs\":\"java.lang.IllegalStateException: some exception"); } - private String getFileContent(Path path) { - try { - byte[] bytes = readAllBytes(path); - return new String(bytes, StandardCharsets.UTF_8); - } catch (IOException e) { - fail("Unable to read file " + path, e); - } - return null; + private String getFileContent() throws IOException { + byte[] bytes = readAllBytes(fakeLogPath); + return new String(bytes, StandardCharsets.UTF_8); } }