Skip to content

Commit

Permalink
Merge pull request #59 from hmcts/HMIS-1208-Add-Exception-App-Insight
Browse files Browse the repository at this point in the history
HMIS-1208 Add exception to app insight
  • Loading branch information
joshblackmoor committed Jun 13, 2023
2 parents 5d21fd1 + 2d6deef commit 1e7b453
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 20 deletions.
13 changes: 13 additions & 0 deletions src/main/java/uk/gov/hmcts/reform/hmi/models/ApiResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package uk.gov.hmcts.reform.hmi.models;

import lombok.AllArgsConstructor;
import lombok.Data;

@Data
@AllArgsConstructor
public class ApiResponse {

private Integer statusCode;

private String message;
}
14 changes: 9 additions & 5 deletions src/main/java/uk/gov/hmcts/reform/hmi/runner/Runner.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.xml.sax.SAXException;
import uk.gov.hmcts.reform.hmi.models.ApiResponse;
import uk.gov.hmcts.reform.hmi.service.AzureBlobService;
import uk.gov.hmcts.reform.hmi.service.DistributionService;
import uk.gov.hmcts.reform.hmi.service.ProcessingService;
Expand Down Expand Up @@ -54,20 +56,22 @@ public void run(String... args) throws IOException, SAXException {

//Process the selected blob
processingService.processFile(blob).forEach((key, value) -> {
Future<String> response = distributionService.sendProcessedJson(value);
String responseStatus = null;
Future<ApiResponse> response = distributionService.sendProcessedJson(value);
Integer responseStatus = null;
String responseMessage = null;
try {
responseStatus = response.get();
responseStatus = response.get().getStatusCode();
responseMessage = response.get().getMessage();
} catch (InterruptedException | ExecutionException | NullPointerException e) {
log.error("Async issue");
formatErrorResponse(responseErrors, key, "Async issue while process this request");
Thread.currentThread().interrupt();
}

if (responseStatus != null
&& !responseStatus.contains("received successfully")) {
&& !responseStatus.equals(HttpStatus.OK.value())) {
log.info("Blob failed");
formatErrorResponse(responseErrors, key, responseStatus);
formatErrorResponse(responseErrors, key, responseMessage);
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.WebClientResponseException;
import uk.gov.hmcts.reform.hmi.models.ApiResponse;

import java.text.SimpleDateFormat;
import java.time.Duration;
Expand All @@ -23,6 +25,7 @@

@Slf4j
@Service
@SuppressWarnings("PMD.LawOfDemeter")
public class DistributionService {

private final WebClient webClient;
Expand All @@ -48,9 +51,9 @@ public DistributionService(@Autowired WebClient webClient, @Value("${service-to
}

@Async
public Future<String> sendProcessedJson(String jsonData) {
public Future<ApiResponse> sendProcessedJson(String jsonData) {
try {
Future<String> apiResponse = webClient.post().uri(url + "/schedules")
String apiResponse = webClient.post().uri(url + "/schedules")
.attributes(clientRegistrationId("hmiApim"))
.header("Source-System", "CRIME")
.header("Destination-System", destinationSystem)
Expand All @@ -59,17 +62,16 @@ public Future<String> sendProcessedJson(String jsonData) {
.header("Accept", "application/json")
.body(BodyInserters.fromValue(jsonData))
.retrieve()
.onStatus(
HttpStatus.BAD_REQUEST::equals,
response -> response.bodyToMono(String.class).map(Exception::new))
.bodyToMono(String.class)
.transformDeferred(RateLimiterOperator.of(rateLimiter))
.toFuture();
log.info(String.format("Json data has been sent with response: %s", apiResponse.get()));
return apiResponse;
} catch (Exception ex) { //NOSONAR
log.error("Error response from HMI APIM:", ex.getMessage());
return CompletableFuture.completedFuture(ex.getMessage());
.block();
log.info(String.format("Json data has been sent with response: %s", apiResponse));
return CompletableFuture.completedFuture(new ApiResponse(HttpStatus.OK.value(), apiResponse));
} catch (WebClientResponseException ex) {
log.error(String.format("Error response from HMI APIM: %s Status code: %s", ex.getResponseBodyAsString(),
ex.getStatusCode().value()));
return CompletableFuture.completedFuture(new ApiResponse(ex.getStatusCode().value(),
ex.getResponseBodyAsString()));
}
}
}
9 changes: 7 additions & 2 deletions src/test/java/uk/gov/hmcts/reform/hmi/runner/RunnerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.http.HttpStatus;
import org.xml.sax.SAXException;
import uk.gov.hmcts.reform.hmi.models.ApiResponse;
import uk.gov.hmcts.reform.hmi.service.AzureBlobService;
import uk.gov.hmcts.reform.hmi.service.DistributionService;
import uk.gov.hmcts.reform.hmi.service.ProcessingService;
Expand All @@ -27,6 +29,7 @@
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
@SuppressWarnings("PMD.LawOfDemeter")
class RunnerTest {

@Mock
Expand Down Expand Up @@ -85,7 +88,8 @@ void testRunnerWithEligibleBlobToProcess() throws IOException, SAXException {

when(processingService.processFile(blobItem)).thenReturn(testMap);
when(distributionService.sendProcessedJson(any()))
.thenReturn(CompletableFuture.completedFuture("received successfully"));
.thenReturn(CompletableFuture.completedFuture(
new ApiResponse(HttpStatus.OK.value(),"received successfully")));
when(azureBlobService.deleteProcessingBlob(TEST)).thenReturn("fileDeleted");

runner.run();
Expand Down Expand Up @@ -126,7 +130,8 @@ void testRunnerWithEligibleBlobFailedRequest() throws IOException, SAXException

when(processingService.processFile(blobItem)).thenReturn(testMap);
when(distributionService.sendProcessedJson(any()))
.thenReturn(CompletableFuture.completedFuture("source header is missing"));
.thenReturn(CompletableFuture.completedFuture(
new ApiResponse(HttpStatus.BAD_REQUEST.value(), "source header is missing")));
when(serviceNowService.createServiceNowRequest(any(), any())).thenReturn(true);
when(azureBlobService.deleteProcessingBlob(TEST)).thenReturn("fileDeleted");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.springframework.http.HttpStatus;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.web.reactive.function.client.WebClient;
import uk.gov.hmcts.reform.hmi.models.ApiResponse;

import java.io.IOException;
import java.util.concurrent.ExecutionException;
Expand Down Expand Up @@ -68,8 +69,8 @@ void testSendProcessedJsonFailed() {
void testSendProcessedJsonHmiFailed() throws ExecutionException, InterruptedException {
mockWebServerEndpoint.enqueue(new MockResponse().setResponseCode(HttpStatus.BAD_REQUEST.value()));

Future<String> response = distributionService.sendProcessedJson(TEST_DATA);
assertEquals(null, response.get(),
Future<ApiResponse> response = distributionService.sendProcessedJson(TEST_DATA);
assertEquals(HttpStatus.BAD_REQUEST.value(), response.get().getStatusCode(),
"Error logs did not contain message");
}
}

0 comments on commit 1e7b453

Please sign in to comment.