Skip to content

Commit 7e65109

Browse files
Merge branch 'main' into renovate/node
2 parents 0387951 + 2483aa2 commit 7e65109

2 files changed

Lines changed: 29 additions & 7 deletions

File tree

legacy/src/main/java/ca/bc/gov/app/controller/ClientController.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import lombok.RequiredArgsConstructor;
1010
import lombok.extern.slf4j.Slf4j;
1111
import java.util.List;
12+
import java.util.Optional;
1213
import org.apache.commons.lang3.tuple.Pair;
1314
import org.springframework.data.domain.PageRequest;
1415
import org.springframework.http.HttpStatus;
@@ -54,10 +55,15 @@ public Flux<HistoryLogDto> findHistoryLogsByClientNumber(
5455
clientNumber);
5556
return service
5657
.findHistoryLogsByClientNumber(clientNumber, PageRequest.of(page, size), sources)
57-
.doOnNext(pair -> serverResponse.getHeaders()
58-
.putIfAbsent("x-total-count", List.of(pair.getValue().toString()))
59-
)
60-
.map(Pair::getKey);
58+
.switchOnFirst((signal, flux) -> {
59+
if (signal.hasValue()) {
60+
int total = Optional.ofNullable(signal.get()).map(Pair::getValue).orElse(0);
61+
serverResponse.getHeaders().add("x-total-count", String.valueOf(total));
62+
} else {
63+
serverResponse.getHeaders().add("x-total-count", "0");
64+
}
65+
return flux.map(Pair::getKey);
66+
});
6167
}
6268

6369
@GetMapping("/{clientNumber}/related-clients")

legacy/src/test/java/ca/bc/gov/app/controller/ClientHistoryControllerIntegrationTest.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public class ClientHistoryControllerIntegrationTest extends
2525
void shouldReturnHistoryLogsByClientNumber(
2626
String clientNumber,
2727
String expectedClientNumber,
28+
boolean expectEmptyWithHeader,
2829
Class<RuntimeException> exception
2930
) {
3031

@@ -41,22 +42,37 @@ void shouldReturnHistoryLogsByClientNumber(
4142
if (StringUtils.isNotBlank(expectedClientNumber)) {
4243
response
4344
.expectStatus().isOk()
45+
.expectHeader().exists("x-total-count")
46+
.expectHeader().value("x-total-count",
47+
count -> assertThat(Integer.parseInt(count)).isGreaterThan(0))
4448
.expectBodyList(HistoryLogDto.class)
4549
.value(logs -> assertThat(logs).isNotEmpty());
4650
}
4751

52+
if (expectEmptyWithHeader) {
53+
response
54+
.expectStatus().isOk()
55+
.expectHeader().exists("x-total-count")
56+
.expectHeader().valueEquals("x-total-count", "0")
57+
.expectBodyList(HistoryLogDto.class)
58+
.value(logs -> assertThat(logs).isEmpty());
59+
}
60+
4861
if (exception != null) {
4962
response.expectStatus().is4xxClientError();
5063
}
5164
}
5265

5366
private static Stream<Arguments> byClientNumber() {
5467
return Stream.of(
55-
// Valid case
56-
Arguments.of("00000138", "00000138", null),
68+
// Valid case: client with history logs
69+
Arguments.of("00000138", "00000138", false, null),
70+
71+
// Valid case: client with no history logs — x-total-count must still be "0"
72+
Arguments.of("99999999", null, true, null),
5773

5874
// Invalid case: missing client number
59-
Arguments.of(null, null, MissingRequiredParameterException.class));
75+
Arguments.of(null, null, false, MissingRequiredParameterException.class));
6076
}
6177

6278
}

0 commit comments

Comments
 (0)