Skip to content

Commit 3c2490d

Browse files
committed
Fix tests
1 parent 2da2a45 commit 3c2490d

File tree

8 files changed

+112
-168
lines changed

8 files changed

+112
-168
lines changed

spring-boot-3-demo-app/src/test/java/io/github/mfvanek/spring/boot3/test/controllers/TimeControllerRetryTest.java

Lines changed: 0 additions & 52 deletions
This file was deleted.

spring-boot-3-demo-app/src/test/java/io/github/mfvanek/spring/boot3/test/controllers/TimeControllerTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.time.LocalDateTime;
3232
import java.util.Arrays;
3333
import java.util.List;
34+
import java.util.Locale;
3435
import java.util.Map;
3536
import java.util.Objects;
3637
import java.util.UUID;
@@ -128,6 +129,36 @@ void mdcValuesShouldBeReportedInLogs(@Nonnull final CapturedOutput output) throw
128129
.contains("\"tenant.name\":\"ru-a1-private\"");
129130
}
130131

132+
@Test
133+
void spanAndMdcShouldBeReportedWhenRetry(@Nonnull final CapturedOutput output) {
134+
final String zoneName = stubErrorResponse();
135+
136+
final EntityExchangeResult<LocalDateTime> result = webTestClient.get()
137+
.uri(uriBuilder -> uriBuilder.path("current-time")
138+
.build())
139+
.header("traceparent", "00-38c19768104ab8ae64fabbeed65bbbdf-4cac1747d4e1ee10-01")
140+
.exchange()
141+
.expectStatus().isOk()
142+
.expectHeader().exists(TRACE_ID_HEADER_NAME)
143+
.expectBody(LocalDateTime.class)
144+
.returnResult();
145+
final String traceId = result.getResponseHeaders().getFirst(TRACE_ID_HEADER_NAME);
146+
assertThat(traceId)
147+
.isEqualTo("38c19768104ab8ae64fabbeed65bbbdf");
148+
149+
assertThat(output.getAll())
150+
.containsPattern(String.format(Locale.ROOT,
151+
".*\"message\":\"Retrying request to '/%1$s', attempt 1/1 due to error:\"," +
152+
"\"logger\":\"io\\.github\\.mfvanek\\.spring\\.boot3\\.test\\.service\\.PublicApiService\"," +
153+
"\"thread\":\"[^\"]+\",\"level\":\"INFO\",\"stack_trace\":\".+?\"," +
154+
"\"traceId\":\"38c19768104ab8ae64fabbeed65bbbdf\",\"spanId\":\"[a-f0-9]+\",\"instance_timezone\":\"%1$s\",\"applicationName\":\"spring-boot-3-demo-app\"\\}%n", zoneName))
155+
.containsPattern(String.format(Locale.ROOT,
156+
".*\"message\":\"Request to '/%s' failed after 2 attempts.\",\"logger\":\"io\\.github\\.mfvanek\\.spring\\.boot3\\.test\\.service\\.PublicApiService\"," +
157+
"\"thread\":\"webflux-http-nio-2\",\"level\":\"ERROR\"," +
158+
"\"traceId\":\"38c19768104ab8ae64fabbeed65bbbdf\",\"spanId\":\"[a-f0-9]+\",\"applicationName\":\"spring-boot-3-demo-app\"}%n",
159+
zoneName));
160+
}
161+
131162
private void assertThatTraceIdPresentInKafkaHeaders(@Nonnull final ConsumerRecord<UUID, String> received,
132163
@Nonnull final String expectedTraceId) {
133164
assertThat(received.value()).startsWith("Current time = ");

spring-boot-3-demo-app/src/test/java/io/github/mfvanek/spring/boot3/test/service/PublicApiServiceTest.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
package io.github.mfvanek.spring.boot3.test.service;
99

1010
import io.github.mfvanek.spring.boot3.test.service.dto.ParsedDateTime;
11-
import io.github.mfvanek.spring.boot3.test.support.RetryTestBase;
11+
import io.github.mfvanek.spring.boot3.test.support.TestBase;
1212
import io.micrometer.observation.Observation;
1313
import io.micrometer.observation.ObservationRegistry;
1414
import io.micrometer.tracing.Tracer;
@@ -30,7 +30,7 @@
3030
import static org.assertj.core.api.Assertions.assertThat;
3131

3232
@ExtendWith(OutputCaptureExtension.class)
33-
class PublicApiServiceTest extends RetryTestBase {
33+
class PublicApiServiceTest extends TestBase {
3434

3535
@Autowired
3636
private PublicApiService publicApiService;
@@ -71,11 +71,14 @@ void retriesOnceToGetZonedTime(@Nonnull final CapturedOutput output) {
7171

7272
assertThat(output.getAll())
7373
.containsPattern(String.format(Locale.ROOT,
74-
".*\\[%s-[a-fA-F0-9]{16}] i\\.g\\.m\\.s\\.b\\.test\\.service\\.PublicApiService {2}: Retrying request to",
75-
traceId))
74+
".*\"message\":\"Retrying request to '[^']+?', attempt 1/1 due to error:\"," +
75+
"\"logger\":\"io\\.github\\.mfvanek\\.spring\\.boot3\\.test\\.service\\.PublicApiService\"," +
76+
"\"thread\":\"[^\"]+\",\"level\":\"INFO\",\"stack_trace\":\".+?\"," +
77+
"\"traceId\":\"%s\",\"spanId\":\"[a-f0-9]+\",\"instance_timezone\":\"%s\",\"applicationName\":\"spring-boot-3-demo-app\"\\}%n", traceId, zoneName))
7678
.containsPattern(String.format(Locale.ROOT,
77-
".*\\[%s-[a-fA-F0-9]{16}] i\\.g\\.m\\.s\\.b\\.test\\.service\\.PublicApiService {2}: Request to '/%s' failed after 2 attempts",
78-
traceId, zoneName))
79+
".*\"message\":\"Request to '[^']+?' failed after 2 attempts.\"," +
80+
"\"logger\":\"io\\.github\\.mfvanek\\.spring\\.boot3\\.test\\.service\\.PublicApiService\"," +
81+
"\"thread\":\"[^\"]+\",\"level\":\"ERROR\",\"traceId\":\"%s\",\"spanId\":\"[a-f0-9]+\",\"applicationName\":\"spring-boot-3-demo-app\"}%n", traceId))
7982
.doesNotContain("Failed to convert response ");
8083
});
8184

spring-boot-3-demo-app/src/test/java/io/github/mfvanek/spring/boot3/test/support/AbstractTestBase.java

Lines changed: 0 additions & 83 deletions
This file was deleted.

spring-boot-3-demo-app/src/test/java/io/github/mfvanek/spring/boot3/test/support/RetryTestBase.java

Lines changed: 0 additions & 15 deletions
This file was deleted.

spring-boot-3-demo-app/src/test/java/io/github/mfvanek/spring/boot3/test/support/TestBase.java

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,85 @@
77

88
package io.github.mfvanek.spring.boot3.test.support;
99

10+
import com.fasterxml.jackson.databind.ObjectMapper;
11+
import com.github.tomakehurst.wiremock.client.WireMock;
12+
import io.github.mfvanek.spring.boot3.test.service.dto.CurrentTime;
13+
import io.github.mfvanek.spring.boot3.test.service.dto.ParsedDateTime;
14+
import lombok.SneakyThrows;
15+
import org.junit.jupiter.api.BeforeEach;
1016
import org.springframework.beans.factory.annotation.Autowired;
17+
import org.springframework.boot.test.autoconfigure.actuate.observability.AutoConfigureObservability;
18+
import org.springframework.boot.test.context.SpringBootTest;
19+
import org.springframework.cloud.contract.wiremock.AutoConfigureWireMock;
1120
import org.springframework.jdbc.core.JdbcTemplate;
1221
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
1322
import org.springframework.test.context.ActiveProfiles;
23+
import org.springframework.test.context.ContextConfiguration;
24+
import org.springframework.test.web.reactive.server.WebTestClient;
25+
26+
import java.time.Clock;
27+
import java.util.TimeZone;
28+
import javax.annotation.Nonnull;
29+
30+
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
31+
import static com.github.tomakehurst.wiremock.client.WireMock.get;
32+
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
33+
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching;
1434

1535
@ActiveProfiles("test")
16-
public abstract class TestBase extends AbstractTestBase {
36+
@AutoConfigureObservability
37+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
38+
@ContextConfiguration(initializers = {KafkaInitializer.class, JaegerInitializer.class, PostgresInitializer.class})
39+
@AutoConfigureWireMock(port = 0)
40+
public abstract class TestBase {
1741

42+
@Autowired
43+
protected WebTestClient webTestClient;
44+
@Autowired
45+
protected ObjectMapper objectMapper;
46+
@Autowired
47+
protected Clock clock;
1848
@Autowired
1949
protected JdbcTemplate jdbcTemplate;
2050
@Autowired
2151
protected NamedParameterJdbcTemplate namedParameterJdbcTemplate;
52+
53+
@BeforeEach
54+
void resetExternalMocks() {
55+
WireMock.resetAllRequests();
56+
}
57+
58+
@Nonnull
59+
protected String stubOkResponse(@Nonnull final ParsedDateTime parsedDateTime) {
60+
final String zoneName = TimeZone.getDefault().getID();
61+
stubOkResponse(zoneName, parsedDateTime);
62+
return zoneName;
63+
}
64+
65+
@SneakyThrows
66+
private void stubOkResponse(@Nonnull final String zoneName, @Nonnull final ParsedDateTime parsedDateTime) {
67+
final CurrentTime currentTime = new CurrentTime(parsedDateTime);
68+
stubFor(get(urlPathMatching("/" + zoneName))
69+
.willReturn(aResponse()
70+
.withStatus(200)
71+
.withBody(objectMapper.writeValueAsString(currentTime))
72+
));
73+
}
74+
75+
@Nonnull
76+
protected String stubErrorResponse() {
77+
final String zoneName = TimeZone.getDefault().getID();
78+
final RuntimeException exception = new RuntimeException("Retries exhausted");
79+
stubErrorResponse(zoneName, exception);
80+
return zoneName;
81+
}
82+
83+
@SneakyThrows
84+
private void stubErrorResponse(@Nonnull final String zoneName, @Nonnull final RuntimeException errorForResponse) {
85+
stubFor(get(urlPathMatching("/" + zoneName))
86+
.willReturn(aResponse()
87+
.withStatus(500)
88+
.withBody(objectMapper.writeValueAsString(errorForResponse))
89+
));
90+
}
2291
}

spring-boot-3-demo-app/src/test/resources/application-test-retry.yml

Lines changed: 0 additions & 11 deletions
This file was deleted.

spring-boot-3-demo-app/src/test/resources/application-test.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ app:
33
retries: 1
44

55
logging:
6+
# appender:
7+
# name: CONSOLE
68
level:
79
org.testcontainers: INFO # In order to troubleshoot issues with Testcontainers, increase the logging level to DEBUG
810
com.github.dockerjava: WARN

0 commit comments

Comments
 (0)