Skip to content

Commit 957d458

Browse files
authored
DSND-3213: Fix metrics 404 not found on PSC GET list (#168)
1 parent 81c7601 commit 957d458

File tree

3 files changed

+11
-12
lines changed

3 files changed

+11
-12
lines changed

src/main/java/uk/gov/companieshouse/pscdataapi/service/CompanyExemptionsApiService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public Optional<CompanyExemptions> getCompanyExemptions(final String companyNumb
4848
throw new BadGatewayException("URI validation error when calling Company Exemptions API", ex);
4949
}
5050

51-
return response != null ? Optional.of(response.getData()) : Optional.empty();
51+
return Optional.ofNullable(response)
52+
.map(ApiResponse::getData);
5253
}
5354
}

src/main/java/uk/gov/companieshouse/pscdataapi/service/CompanyMetricsApiService.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import java.util.Optional;
66
import java.util.function.Supplier;
77
import org.springframework.beans.factory.annotation.Qualifier;
8-
import org.springframework.http.HttpStatus;
98
import org.springframework.stereotype.Component;
109
import uk.gov.companieshouse.api.InternalApiClient;
1110
import uk.gov.companieshouse.api.error.ApiErrorResponseException;
@@ -15,7 +14,6 @@
1514
import uk.gov.companieshouse.logging.Logger;
1615
import uk.gov.companieshouse.logging.LoggerFactory;
1716
import uk.gov.companieshouse.pscdataapi.exceptions.BadGatewayException;
18-
import uk.gov.companieshouse.pscdataapi.exceptions.ResourceNotFoundException;
1917
import uk.gov.companieshouse.pscdataapi.logging.DataMapHolder;
2018

2119
@Component
@@ -31,7 +29,7 @@ public CompanyMetricsApiService(
3129
}
3230

3331
public Optional<MetricsApi> getCompanyMetrics(final String companyNumber) {
34-
ApiResponse<MetricsApi> response;
32+
ApiResponse<MetricsApi> response = null;
3533
try {
3634
response = metricsApiClientSupplier.get()
3735
.privateCompanyMetricsResourceHandler()
@@ -41,17 +39,15 @@ public Optional<MetricsApi> getCompanyMetrics(final String companyNumber) {
4139
final int statusCode = ex.getStatusCode();
4240
LOGGER.info("Company Metrics API call failed with status code [%s]".formatted(statusCode),
4341
DataMapHolder.getLogMap());
44-
if (statusCode == 404) {
45-
throw new ResourceNotFoundException(HttpStatus.NOT_FOUND,
46-
"Company Metrics API responded with 404 Not Found");
47-
} else {
42+
if (statusCode != 404) {
4843
throw new BadGatewayException("Error calling Company Metrics API endpoint", ex);
4944
}
5045
} catch (URIValidationException ex) {
5146
LOGGER.info("URI validation error when calling Company Metrics API", DataMapHolder.getLogMap());
5247
throw new BadGatewayException("URI validation error when calling Company Metrics API", ex);
5348
}
5449

55-
return response != null ? Optional.of(response.getData()) : Optional.empty();
50+
return Optional.ofNullable(response)
51+
.map(ApiResponse::getData);
5652
}
5753
}

src/test/java/uk/gov/companieshouse/pscdataapi/service/CompanyMetricsApiServiceTest.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package uk.gov.companieshouse.pscdataapi.service;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNull;
45
import static org.junit.jupiter.api.Assertions.assertThrows;
6+
import static org.junit.jupiter.api.Assertions.assertTrue;
57
import static org.mockito.ArgumentMatchers.anyString;
68
import static org.mockito.Mockito.verify;
79
import static org.mockito.Mockito.when;
@@ -86,18 +88,18 @@ void shouldGetCompanyMetricsWithNullResponseAndReturnEmptyOptional() throws Exce
8688
}
8789

8890
@Test
89-
void shouldThrowResourceNotFoundExceptionWhenApiRespondsWith404NotFound() throws Exception {
91+
void shouldContinueProcessingWhenApiRespondsWith404NotFound() throws Exception {
9092
// given
9193
when(supplier.get()).thenReturn(client);
9294
when(client.privateCompanyMetricsResourceHandler()).thenReturn(privateCompanyMetricsResourceHandler);
9395
when(privateCompanyMetricsResourceHandler.getCompanyMetrics(anyString())).thenReturn(privateCompanyMetricsGet);
9496
when(privateCompanyMetricsGet.execute()).thenThrow(buildApiErrorResponseException(404));
9597

9698
// when
97-
Executable executable = () -> service.getCompanyMetrics(COMPANY_NUMBER);
99+
Optional<MetricsApi> actual = service.getCompanyMetrics(COMPANY_NUMBER);
98100

99101
// then
100-
assertThrows(ResourceNotFoundException.class, executable);
102+
assertTrue(actual.isEmpty());
101103
verify(privateCompanyMetricsResourceHandler).getCompanyMetrics(URL);
102104
}
103105

0 commit comments

Comments
 (0)