Skip to content

Commit

Permalink
Merge pull request #42 from companieshouse/feature/retry-integration-…
Browse files Browse the repository at this point in the history
…tests

Throw correct error on api status response error
  • Loading branch information
amartin7663 authored May 26, 2022
2 parents 7f3af07 + 5269fcb commit 279eb72
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 117 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Feature: Retries and Errors
Scenario: Process message when the data api returns 400
Given the application is running
When the consumer receives a message but the data api returns a 400
Then the message should be moved to topic disqualified-officers-delta-error
Then the message should be moved to topic disqualified-officers-delta-invalid

Scenario: Process message when the data api returns 503
Given the application is running
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.util.Map;
import java.util.Objects;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.kafka.support.KafkaHeaders;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
Expand All @@ -20,7 +19,6 @@
import uk.gov.companieshouse.delta.ChsDelta;
import uk.gov.companieshouse.disqualifiedofficers.delta.exception.NonRetryableErrorException;
import uk.gov.companieshouse.disqualifiedofficers.delta.exception.RetryableErrorException;
import uk.gov.companieshouse.disqualifiedofficers.delta.handler.ApiResponseHandler;
import uk.gov.companieshouse.disqualifiedofficers.delta.service.api.ApiClientService;
import uk.gov.companieshouse.disqualifiedofficers.delta.transformer.DisqualifiedOfficersApiTransformer;
import uk.gov.companieshouse.logging.Logger;
Expand Down Expand Up @@ -109,14 +107,13 @@ private void invokeDisqualificationsDataApi(final String logContext,
logContext,
String.format("Process disqualification for officer with id [%s]",
disqualification.getOfficerId()),
null);
logMap);
final ApiResponse<Void> response =
apiClientService.putDisqualification(logContext,
internalDisqualificationApi.getInternalData().getOfficerId(),
internalDisqualificationApi);
ApiResponseHandler apiResponseHandler = new ApiResponseHandler();
apiResponseHandler.handleResponse(HttpStatus.valueOf(response.getStatusCode()),
logContext, logMap, logger);
logger.debugContext(logContext,
"Response received from disqualified-officers-data-api", logMap);
}

private void invokeDisqualificationsDataApi(final String logContext,
Expand All @@ -127,13 +124,12 @@ private void invokeDisqualificationsDataApi(final String logContext,
logContext,
String.format("Process disqualification for officer with id [%s]",
disqualification.getOfficerId()),
null);
logMap);
final ApiResponse<Void> response =
apiClientService.putDisqualification(logContext,
internalDisqualificationApi.getInternalData().getOfficerId(),
internalDisqualificationApi);
ApiResponseHandler apiResponseHandler = new ApiResponseHandler();
apiResponseHandler.handleResponse(HttpStatus.valueOf(response.getStatusCode()),
logContext, logMap, logger);
logger.debugContext(logContext,
"Response received from disqualified-officers-data-api", logMap);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import uk.gov.companieshouse.api.handler.Executor;
import uk.gov.companieshouse.api.handler.exception.URIValidationException;
import uk.gov.companieshouse.api.model.ApiResponse;
import uk.gov.companieshouse.disqualifiedofficers.delta.exception.NonRetryableErrorException;
import uk.gov.companieshouse.disqualifiedofficers.delta.exception.RetryableErrorException;
import uk.gov.companieshouse.logging.Logger;

public abstract class BaseApiClientServiceImpl {
Expand Down Expand Up @@ -41,15 +43,25 @@ public <T> ApiResponse<T> executeOp(final String logContext,
return executor.execute();

} catch (URIValidationException ex) {
logger.errorContext(logContext, "SDK exception", ex, logMap);
String msg = "404 NOT_FOUND response received from disqualified-officers-data-api";
logger.errorContext(logContext, msg, ex, logMap);

throw new ResponseStatusException(HttpStatus.NOT_FOUND, ex.getMessage(), ex);
throw new RetryableErrorException(msg, ex);
} catch (ApiErrorResponseException ex) {
logMap.put("status", ex.getStatusCode());
logger.errorContext(logContext, "SDK exception", ex, logMap);

throw new ResponseStatusException(HttpStatus.valueOf(ex.getStatusCode()),
ex.getStatusMessage(), ex);
if (ex.getStatusCode() == HttpStatus.BAD_REQUEST.value()) {
// 400 BAD REQUEST status cannot be retried
String msg =
"400 BAD_REQUEST response received from disqualified-officers-data-api";
logger.errorContext(logContext, msg, ex, logMap);
throw new NonRetryableErrorException(msg, ex);
}

// any other client or server status is retryable
String msg = "Non-Successful response received from disqualified-officers-data-api";
logger.errorContext(logContext, msg + ", retry", ex, logMap);
throw new RetryableErrorException(msg, ex);
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ void When_ApiReturns500_Expect_RetryableError() throws IOException {
Message<ChsDelta> mockChsDeltaMessage = testHelper.createChsDeltaMessage();
InternalNaturalDisqualificationApi apiObject = testHelper.createDisqualificationApi();
final ApiResponse<Void> response = new ApiResponse<>(HttpStatus.INTERNAL_SERVER_ERROR.value(), null, null);
when(apiClientService.putDisqualification(any(),any(), eq(apiObject))).thenReturn(response);
when(apiClientService.putDisqualification(any(),any(), eq(apiObject))).thenThrow(new RetryableErrorException(""));
when(transformer.transformNaturalDisqualification(any())).thenReturn(apiObject);
assertThrows(RetryableErrorException.class, ()->deltaProcessor.processDelta(mockChsDeltaMessage));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package uk.gov.companieshouse.disqualifiedofficers.delta.service.api;

import com.google.api.client.http.HttpHeaders;
import com.google.api.client.http.HttpResponseException.Builder;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import uk.gov.companieshouse.api.error.ApiErrorResponseException;
import uk.gov.companieshouse.api.handler.Executor;
import uk.gov.companieshouse.api.handler.exception.URIValidationException;
import uk.gov.companieshouse.api.model.ApiResponse;
import uk.gov.companieshouse.disqualifiedofficers.delta.exception.NonRetryableErrorException;
import uk.gov.companieshouse.disqualifiedofficers.delta.exception.RetryableErrorException;
import uk.gov.companieshouse.logging.Logger;

import java.util.HashMap;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertThrows;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
public class BaseApiClientServiceImplTest {

private BaseApiClientServiceImpl service;

@Mock
private Logger logger;
@Mock
private Executor executor;

@BeforeEach
void setup() {
service = new BaseApiClientServiceImpl(logger) {};
}

@Test
void returnsApiResponse() throws Exception {
ApiResponse expectedResponse = new ApiResponse(200, new HashMap<>());
when(executor.execute()).thenReturn(expectedResponse);

ApiResponse actualResponse = service.executeOp(null, null, null, executor);

assertThat(actualResponse).isEqualTo(expectedResponse);
}

@Test
void throwsRetryableErrorOn404() throws Exception {
when(executor.execute()).thenThrow(new URIValidationException("Not Found"));

RetryableErrorException thrown = assertThrows(RetryableErrorException.class,
() -> service.executeOp(null, null, null, executor));

assertThat(thrown.getMessage()).isEqualTo("404 NOT_FOUND response received from disqualified-officers-data-api");
}

@Test
void throwsRetryableErrorOn500() throws Exception {
when(executor.execute()).thenThrow(
new ApiErrorResponseException(new Builder(500, "500", new HttpHeaders())));

RetryableErrorException thrown = assertThrows(RetryableErrorException.class,
() -> service.executeOp(null, null, null, executor));

assertThat(thrown.getMessage()).isEqualTo("Non-Successful response received from disqualified-officers-data-api");
}

@Test
void throwsNonRetryableErrorOn400() throws Exception {
when(executor.execute()).thenThrow(
new ApiErrorResponseException(new Builder(400, "400", new HttpHeaders())));

NonRetryableErrorException thrown = assertThrows(NonRetryableErrorException.class,
() -> service.executeOp(null, null, null, executor));

assertThat(thrown.getMessage()).isEqualTo("400 BAD_REQUEST response received from disqualified-officers-data-api");
}
}

0 comments on commit 279eb72

Please sign in to comment.