]> source.dussan.org Git - sonarqube.git/commitdiff
NO-JIRA Fix test sending data to ES
authorantoine.vinot <antoine.vinot@sonarsource.com>
Wed, 6 Sep 2023 08:49:08 +0000 (10:49 +0200)
committersonartech <sonartech@sonarsource.com>
Wed, 6 Sep 2023 20:02:48 +0000 (20:02 +0000)
test-monitoring/src/main/java/org/sonarqube/monitoring/test/aspect/TestFailureAspect.java
test-monitoring/src/test/java/org/sonarqube/monitoring/test/aspect/TestFailureAspectTest.java

index 2cb05c78b160c218e9964e6f96e1d9d150255548..52d6d583cdb1f678176644559dff6a0ff1f10485 100644 (file)
@@ -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
     }
index 1b01be0e0955f17587ae7d16f01f0aafb185cb39..a5c7990fc0138b7b708de5e69dcb59dbcd594d62 100644 (file)
@@ -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);
   }
 
 }