|
6 | 6 | import org.junit.jupiter.api.BeforeEach; |
7 | 7 | import org.junit.jupiter.api.Test; |
8 | 8 | import org.slf4j.MDC; |
| 9 | +import org.springframework.beans.factory.annotation.Autowired; |
9 | 10 | import org.springframework.beans.factory.annotation.Value; |
10 | 11 | import org.springframework.test.web.servlet.MvcResult; |
11 | 12 | import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; |
12 | 13 | import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; |
| 14 | +import uk.gov.hmcts.cp.entities.ExampleEntity; |
| 15 | +import uk.gov.hmcts.cp.repositories.ExampleRepository; |
13 | 16 |
|
14 | 17 | import java.io.ByteArrayOutputStream; |
15 | 18 | import java.io.PrintStream; |
@@ -37,12 +40,21 @@ class TracingIntegrationTest extends IntegrationTestBase { |
37 | 40 |
|
38 | 41 | private final PrintStream originalStdOut = System.out; |
39 | 42 |
|
| 43 | + @Autowired |
| 44 | + ExampleRepository exampleRepository; |
| 45 | + |
| 46 | + private ExampleEntity entity; |
| 47 | + |
40 | 48 | @BeforeEach |
41 | | - public void setUp() { |
42 | | - // Manually populate MDC with trace information similar to TracingFilter |
| 49 | + void setup() { |
43 | 50 | MDC.put(TRACE_ID_FIELD, TEST_TRACE_ID_1); |
44 | 51 | MDC.put(SPAN_ID_FIELD, TEST_SPAN_ID_1); |
45 | 52 | MDC.put("applicationName", springApplicationName); |
| 53 | + entity = exampleRepository.save( |
| 54 | + ExampleEntity.builder() |
| 55 | + .exampleText("Welcome to service-hmcts-springboot-template") |
| 56 | + .build() |
| 57 | + ); |
46 | 58 | } |
47 | 59 |
|
48 | 60 | @AfterEach |
@@ -86,7 +98,7 @@ private ByteArrayOutputStream captureStdOut() { |
86 | 98 | private MvcResultHelper performRequestAndCaptureLogs(final String path, final String traceId, final String spanId) throws Exception { |
87 | 99 | final ByteArrayOutputStream capturedStdOut = captureStdOut(); |
88 | 100 |
|
89 | | - MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.get(path, 1); |
| 101 | + MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.get(path, entity.getId()); |
90 | 102 | if (traceId != null) { |
91 | 103 | requestBuilder = requestBuilder.header(TRACE_ID_FIELD, traceId); |
92 | 104 | } |
@@ -130,7 +142,7 @@ private void assertTracingFields(final Map<String, Object> log, final String exp |
130 | 142 |
|
131 | 143 | private void assertCommonLogFields(final Map<String, Object> log) { |
132 | 144 | assertThat(log.get("logger_name")).isEqualTo("uk.gov.hmcts.cp.controllers.ExampleController"); |
133 | | - assertThat(log.get("message")).isEqualTo("getExampleByExampleId example for 1"); |
| 145 | + assertThat(log.get("message")).isEqualTo("getExampleByExampleId example for "+entity.getId()); |
134 | 146 | } |
135 | 147 |
|
136 | 148 | private void assertResponseHeaders(final MvcResult result, final String expectedTraceId, final String expectedSpanId) { |
|
0 commit comments