package com.ankurm.logs; import static org.assertj.core.api.Assertions.assertThat; import java.net.URI; import java.net.URLEncoder; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.web.server.LocalServerPort; import tools.jackson.databind.JsonNode; import tools.jackson.databind.json.JsonMapper; @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) class EndpointTest { @LocalServerPort int port; @TempDir Path tmp; @Test void theEndpointReportsTheIncident() throws Exception { Fixtures f = new Fixtures(); Path base = Files.write(tmp.resolve("baseline.log"), f.baselineLines); Path target = Files.write(tmp.resolve("target.log"), f.targetLines); String url = "http://localhost:%d/api/incidents?baseline=%s&target=%s".formatted(port, URLEncoder.encode(base.toString(), StandardCharsets.UTF_8), URLEncoder.encode(target.toString(), StandardCharsets.UTF_8)); HttpResponse res = HttpClient.newHttpClient().send(HttpRequest.newBuilder(URI.create(url)).GET().build(), HttpResponse.BodyHandlers.ofString()); JsonNode body = JsonMapper.builder().build().readTree(res.body()); Transcript t = new Transcript("11-endpoint.txt", "GET /api/incidents on a running Spring Boot application"); t.line("HTTP %d", res.statusCode()); t.line("baselineLines=%s targetLines=%s baselineTemplates=%s targetTemplates=%s clusters=%s threshold=%.3f", body.get("baselineLines"), body.get("targetLines"), body.get("baselineTemplates"), body.get("targetTemplates"), body.get("clusters"), body.get("threshold").asDouble()); t.line("incidents: %d", body.get("incidents").size()); body.get("incidents").get(0).get("findings").forEach(x -> t.line(" %s %s x%s %s", x.get("kind").asString(), x.get("template").get("level").asString(), x.get("count"), x.get("template").get("text").asString())); t.line("summary: %s", body.get("summaries").get(0).asString()); t.write(); assertThat(res.statusCode()).isEqualTo(200); assertThat(body.get("incidents")).hasSize(1); assertThat(body.get("incidents").get(0).get("findings")).hasSize(5); assertThat(body.get("summaries").get(0).asString()).contains("ERROR incident"); } }