feedback updates

This commit is contained in:
technoddy 2024-03-20 23:22:41 -04:00
parent 329a01ec93
commit 12f1cdc1a3

View File

@ -1,8 +1,7 @@
package com.baeldung.loki; package com.baeldung.loki;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.verify;
import java.net.URI; import java.net.URI;
import java.time.LocalDateTime; import java.time.LocalDateTime;
@ -31,13 +30,17 @@ import com.fasterxml.jackson.databind.ObjectMapper;
@SpringBootTest(classes = DemoService.class) @SpringBootTest(classes = DemoService.class)
public class DemoServiceLiveTest { public class DemoServiceLiveTest {
/**
* This test assumes that loki service is already up.
* For more details please check section #2 Running Loki and Grafana Service
* Which spin up Loki server using docker-compose
*/
@Test @Test
public void givenLokiContainerRunning_whenDemoServiceInvoked_thenLokiAppenderCollectLogs() throws JsonProcessingException, InterruptedException { public void givenLokiContainerRunning_whenDemoServiceInvoked_thenLokiAppenderCollectLogs() throws JsonProcessingException, InterruptedException {
DemoService service = new DemoService(); DemoService service = new DemoService();
service.log(); service.log();
Thread.sleep(2000);
String baseUrl = "http://localhost:3100/loki/api/v1/query_range";
String baseUrl = "http://localhost:3100/loki/api/v1/query_range";
// Set up query parameters // Set up query parameters
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON); headers.setContentType(MediaType.APPLICATION_JSON);
@ -45,43 +48,38 @@ public class DemoServiceLiveTest {
// Get current time in UTC // Get current time in UTC
LocalDateTime currentDateTime = LocalDateTime.now(ZoneOffset.UTC); LocalDateTime currentDateTime = LocalDateTime.now(ZoneOffset.UTC);
String current_time_est = currentDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'")); String currentTimeUtc = currentDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'"));
LocalDateTime tenMinsAgo = currentDateTime.minusMinutes(10); LocalDateTime tenMinsAgo = currentDateTime.minusMinutes(10);
String start_time_est = tenMinsAgo.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'")); String startTimeUtc = tenMinsAgo.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'"));
URI uri = UriComponentsBuilder.fromUriString(baseUrl) URI uri = UriComponentsBuilder.fromUriString(baseUrl)
.queryParam("query", query) .queryParam("query", query)
.queryParam("start", start_time_est) .queryParam("start", startTimeUtc)
.queryParam("end", current_time_est) .queryParam("end", currentTimeUtc)
.build() .build()
.toUri(); .toUri();
RestTemplate restTemplate = new RestTemplate(); RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> response = restTemplate.exchange( ResponseEntity<String> response = restTemplate.exchange(uri, HttpMethod.GET, new HttpEntity<>(headers), String.class);
uri,
HttpMethod.GET,
new HttpEntity<>(headers),
String.class
);
List<String> messages = new ArrayList<>(); List<String> messages = new ArrayList<>();
ObjectMapper objectMapper = new ObjectMapper(); ObjectMapper objectMapper = new ObjectMapper();
if (response.getStatusCode() == HttpStatus.OK) { assertEquals(response.getStatusCode(), HttpStatus.OK);
String responseBody = response.getBody();
JsonNode jsonNode = objectMapper.readTree(responseBody); String responseBody = response.getBody();
JsonNode result = jsonNode.get("data").get("result").get(0).get("values"); JsonNode jsonNode = objectMapper.readTree(responseBody);
result.iterator().forEachRemaining(e-> { JsonNode result = jsonNode.get("data")
.get("result")
.get(0)
.get("values");
result.iterator()
.forEachRemaining(e -> {
Iterator<JsonNode> elements = e.elements(); Iterator<JsonNode> elements = e.elements();
elements.forEachRemaining(f -> elements.forEachRemaining(f -> messages.add(f.toString()));
messages.add(f.toString())
);
}); });
} else {
System.out.println("Error: " + response.getStatusCodeValue());
}
String expected = "DemoService.log invoked"; String expected = "DemoService.log invoked";
assertTrue(messages.stream().anyMatch(e -> e.contains(expected))); assertThat(messages).anyMatch(e -> e.contains(expected));
} }
} }