-
Notifications
You must be signed in to change notification settings - Fork 1
update integration test; enhance error handling responses #24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c572f42
update integration test; enhance error handling responses
duncancrawford bdb3b25
refactor integration test names
duncancrawford f148dd7
feat: mocked response moved under InMemoryCourtScheduleRepositoryImpl;
dalgazinhmcts 4acb154
feat: mocked response moved under InMemoryCourtScheduleRepositoryImpl;
dalgazinhmcts File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 0 additions & 39 deletions
39
src/functionalTest/java/uk/gov/hmcts/cp/controllers/SampleFunctionalTest.java
This file was deleted.
Oops, something went wrong.
64 changes: 56 additions & 8 deletions
64
src/integrationTest/java/uk/gov/hmcts/cp/controllers/CourtScheduleControllerITest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,31 +1,79 @@ | ||
| package uk.gov.hmcts.cp.controllers; | ||
|
|
||
| import org.junit.jupiter.api.DisplayName; | ||
| import com.fasterxml.jackson.databind.JsonNode; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.extension.ExtendWith; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; | ||
| import org.springframework.boot.test.context.SpringBootTest; | ||
| import org.springframework.http.MediaType; | ||
| import org.springframework.test.context.bean.override.mockito.MockitoBean; | ||
| import org.springframework.test.context.junit.jupiter.SpringExtension; | ||
| import org.springframework.test.web.servlet.MockMvc; | ||
| import uk.gov.hmcts.cp.openapi.model.CourtScheduleResponse; | ||
| import uk.gov.hmcts.cp.services.CourtScheduleService; | ||
|
|
||
| import java.util.UUID; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
| import static org.mockito.Mockito.when; | ||
| import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
| import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; | ||
| import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
|
||
| @ExtendWith(SpringExtension.class) | ||
| @SpringBootTest | ||
| @AutoConfigureMockMvc | ||
| class CourtScheduleControllerITest { | ||
| private static final Logger log = LoggerFactory.getLogger(CourtScheduleControllerITest.class); | ||
|
|
||
| @Autowired | ||
| private MockMvc mockMvc; | ||
|
|
||
| @DisplayName("Should /case/{case_urn}/courtschedule request with 200 response code") | ||
| @Test | ||
| void shouldCallActuatorAndGet200() throws Exception { | ||
| mockMvc.perform(get("/case/123/courtschedule")) | ||
| .andDo(print()) | ||
| .andExpect(status().isOk()); | ||
| void shouldReturnOkWhenValidUrnIsProvided() throws Exception { | ||
| String caseUrn = "test-case-urn"; | ||
| CourtScheduleResponse expectedResponse = CourtScheduleResponse.builder().build(); | ||
|
|
||
| mockMvc.perform(get("/case/{case_urn}/courtschedule", caseUrn) | ||
| .accept(MediaType.APPLICATION_JSON)) | ||
| .andExpect(status().isOk()) | ||
| .andExpect(result -> { | ||
| // You may need to adjust this depending on the actual fields in CourtScheduleResponse | ||
| String responseBody = result.getResponse().getContentAsString(); | ||
| log.info("Response: {}", responseBody); | ||
| JsonNode jsonBody = new ObjectMapper().readTree(result.getResponse().getContentAsString()); | ||
|
|
||
|
|
||
| assertEquals("courtSchedule", jsonBody.fieldNames().next()); | ||
| JsonNode courtSchedule = jsonBody.get("courtSchedule"); | ||
| assertTrue(courtSchedule.isArray()); | ||
| assertEquals(1, courtSchedule.size()); | ||
|
|
||
| JsonNode hearings = courtSchedule.get(0).get("hearings"); | ||
| assertTrue(hearings.isArray()); | ||
| assertEquals(1, hearings.size()); | ||
|
|
||
| JsonNode hearing = hearings.get(0); | ||
| UUID hearingId = UUID.fromString(hearing.get("hearingId").asText()); | ||
| assertNotNull(hearingId); | ||
| assertEquals("Requires interpreter", hearing.get("listNote").asText()); | ||
| assertEquals("Sentencing for theft case", hearing.get("hearingDescription").asText()); | ||
| assertEquals("Trial", hearing.get("hearingType").asText()); | ||
|
|
||
| JsonNode courtSittings = hearing.get("courtSittings"); | ||
| assertTrue(courtSittings.isArray()); | ||
| assertEquals(1, courtSittings.size()); | ||
|
|
||
| JsonNode sitting = courtSittings.get(0); | ||
| assertEquals("Central Criminal Court", sitting.get("courtHouse").asText()); | ||
| assertNotNull(sitting.get("sittingStart").asText()); | ||
| assertNotNull(sitting.get("sittingEnd").asText()); | ||
| UUID judiciaryId = UUID.fromString(sitting.get("judiciaryId").asText()); | ||
| assertNotNull(sitting.get("judiciaryId").asText()); | ||
| log.info("Response Object: {}", jsonBody); | ||
| }); | ||
| } | ||
| } | ||
| } | ||
49 changes: 0 additions & 49 deletions
49
src/integrationTest/java/uk/gov/hmcts/cp/openapi/OpenAPIPublisherTest.java
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/main/java/uk/gov/hmcts/cp/controllers/GlobalExceptionHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| package uk.gov.hmcts.cp.controllers; | ||
|
|
||
| import io.micrometer.tracing.Tracer; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.ExceptionHandler; | ||
| import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
| import org.springframework.web.server.ResponseStatusException; | ||
| import uk.gov.hmcts.cp.openapi.model.ErrorResponse; | ||
|
|
||
| import java.time.OffsetDateTime; | ||
| import java.time.ZoneOffset; | ||
| import java.util.Objects; | ||
|
|
||
| @RestControllerAdvice | ||
| public class GlobalExceptionHandler { | ||
|
|
||
| private final Tracer tracer; | ||
|
|
||
| public GlobalExceptionHandler(Tracer tracer) { | ||
| this.tracer = tracer; | ||
| } | ||
|
|
||
| @ExceptionHandler(ResponseStatusException.class) | ||
| public ResponseEntity<ErrorResponse> handleResponseStatusException(ResponseStatusException e) { | ||
| ErrorResponse error = ErrorResponse.builder() | ||
| .error(String.valueOf(e.getStatusCode().value())) | ||
| .message(e.getReason() != null ? e.getReason() : e.getMessage()) | ||
| .timestamp(OffsetDateTime.now(ZoneOffset.UTC)) | ||
| .traceId(Objects.requireNonNull(tracer.currentSpan()).context().traceId()) | ||
| .build(); | ||
|
|
||
| return ResponseEntity | ||
| .status(e.getStatusCode()) | ||
| .body(error); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this class name can be CourtScheduleControllerIT