diff --git a/v2/src/main/java/io/keploy/Keploy.java b/v2/src/main/java/io/keploy/Keploy.java index 5aae90c..5a43a5c 100644 --- a/v2/src/main/java/io/keploy/Keploy.java +++ b/v2/src/main/java/io/keploy/Keploy.java @@ -221,17 +221,16 @@ public static void FindCoverage(String testSet) throws IOException, InterruptedE String dest = "target/" + testSet + ".exec"; String jacocoCliPath = getJacococliPath(); List command = Arrays.asList( - "java", - "-jar", - jacocoCliPath, - "dump", - "--address", - "localhost", - "--port", - "36320", - "--destfile", - dest - ); + "java", + "-jar", + jacocoCliPath, + "dump", + "--address", + "localhost", + "--port", + "36320", + "--destfile", + dest); // Start the command using ProcessBuilder ProcessBuilder processBuilder = new ProcessBuilder(command); @@ -552,6 +551,11 @@ public static void runTests(String jarPath, RunOptions runOptions) { if (appErr != null) { throw new AssertionError("error stopping user application: " + appErr); } + + Boolean updateReportResult = udpateReportWithCoverage(testRunId, testSet); + if (!updateReportResult) { + throw new AssertionError("error updating report with coverage data"); + } } // unload the ebpf hooks from the kernel // delete jacoco files @@ -566,7 +570,7 @@ public static void startKeploy(String runCmd, int delay, boolean debug, int port Runnable task = () -> runKeploy(runCmd, delay, debug, port); Thread thread = new Thread(task); thread.start(); - return ; + return; } public static void runKeploy(String runCmd, int delay, boolean debug, int port) { @@ -805,6 +809,50 @@ public static RunTestSetResult runTestSet(String testRunId, String testSetId, St } } + public static Boolean udpateReportWithCoverage(String testRunId, String testSetId) { + HttpURLConnection conn = setHttpClient(); + if (conn == null) { + logger.error("Could not initialize HTTP connection."); + return false; + } + + String payload = String.format( + "{\"query\": \"mutation updateReportWithCov{ updateReportWithCov(testRunId: \\\"%s\\\", testSetId: \\\"%s\\\", language: \\\"java\\\") }\"}", + testRunId, testSetId); + + try { + conn.setDoOutput(true); + try (OutputStream os = conn.getOutputStream()) { + os.write(payload.getBytes()); + os.flush(); + } + + int responseCode = conn.getResponseCode(); + logger.debug("Status code received: " + responseCode); + + if (responseCode >= 200 && responseCode < 300) { + String resBody = getResponseBody(conn); + logger.debug("Response body received: " + resBody); + + // Parse the response body using Gson + Gson gson = new Gson(); + GraphQLResponse response = gson.fromJson(resBody, GraphQLResponse.class); + + if (response.data == null) { + return false; + } + + return true; + } else { + logger.error("Failed to update report with coverage data. Status code: " + responseCode); + return false; + } + } catch (Exception e) { + logger.error("Error updating report with coverage data: " + e.getMessage(), e); + return false; + } + } + public static class StopHooksResult { Boolean success; String error;