From 5f41a294ef66ca27fdb203323e31895871c6f647 Mon Sep 17 00:00:00 2001 From: Russell Dodd Date: Thu, 19 Sep 2024 10:23:08 +0100 Subject: [PATCH] PO-606: Pre-work before GET multiple Draft Account summaries --- .../PO-559_getDraftAccount.feature | 18 +++---- .../PO-591_createDraftAccount.feature | 18 +++---- ...DraftAccountControllerIntegrationTest.java | 10 ++-- .../controllers/DraftAccountController.java | 16 +++--- .../opal/dto/DraftAccountResponseDto.java | 3 +- .../opal/dto/DraftAccountSummaryDto.java | 54 +++++++++++++++++++ .../opal/dto/DraftAccountsResponseDto.java | 31 +++++++++++ .../DraftAccountSnapshots.java} | 21 ++++---- .../hmcts/opal/entity/DraftAccountStatus.java | 12 ++++- .../service/opal/DraftAccountService.java | 8 +-- .../DraftAccountControllerTest.java | 7 ++- 11 files changed, 146 insertions(+), 52 deletions(-) create mode 100644 src/main/java/uk/gov/hmcts/opal/dto/DraftAccountSummaryDto.java create mode 100644 src/main/java/uk/gov/hmcts/opal/dto/DraftAccountsResponseDto.java rename src/main/java/uk/gov/hmcts/opal/{dto/DraftAccountSnapshotsDto.java => entity/DraftAccountSnapshots.java} (65%) diff --git a/src/functionalTest/resources/features/opalMode/manualAccountCreation/draftAccounts/PO-559_getDraftAccount.feature b/src/functionalTest/resources/features/opalMode/manualAccountCreation/draftAccounts/PO-559_getDraftAccount.feature index d2309e276..0b6d83c36 100644 --- a/src/functionalTest/resources/features/opalMode/manualAccountCreation/draftAccounts/PO-559_getDraftAccount.feature +++ b/src/functionalTest/resources/features/opalMode/manualAccountCreation/draftAccounts/PO-559_getDraftAccount.feature @@ -15,15 +15,13 @@ Feature: PO-559 get draft account And I store the created draft account ID Then I get the single created draft account and the response contains - | business_unit_id | 73 | - | account_type | Fine | - | account_status | Submitted | - | account_snapshot.DefendantName | LNAME, FNAME | - | account_snapshot.DateOfBirth | 01/01/2000 | - | account_snapshot.AccountType | Fine | - | account_snapshot.SubmittedBy | opal-test@HMCTS.NET | - | account_snapshot.BusinessUnitName | West London | + | business_unit_id | 73 | + | account_type | Fine | + | account_status | Submitted | + | account_snapshot.defendant_name | LNAME, FNAME | + | account_snapshot.date_of_birth | 01/01/2000 | + | account_snapshot.account_type | Fine | + | account_snapshot.submitted_by | opal-test@HMCTS.NET | + | account_snapshot.business_unit_name | West London | Then I delete the created draft accounts - - diff --git a/src/functionalTest/resources/features/opalMode/manualAccountCreation/draftAccounts/PO-591_createDraftAccount.feature b/src/functionalTest/resources/features/opalMode/manualAccountCreation/draftAccounts/PO-591_createDraftAccount.feature index 9e7712391..f7d9aea20 100644 --- a/src/functionalTest/resources/features/opalMode/manualAccountCreation/draftAccounts/PO-591_createDraftAccount.feature +++ b/src/functionalTest/resources/features/opalMode/manualAccountCreation/draftAccounts/PO-591_createDraftAccount.feature @@ -15,15 +15,13 @@ Feature: PO-591 create draft account And I store the created draft account ID And The draft account response contains the following data - | business_unit_id | 73 | - | account_type | Fine | - | account_status | Submitted | - | account_snapshot.DefendantName | LNAME, FNAME | - | account_snapshot.DateOfBirth | 01/01/2000 | - | account_snapshot.AccountType | Fine | - | account_snapshot.SubmittedBy | opal-test@HMCTS.NET | - | account_snapshot.BusinessUnitName | West London | + | business_unit_id | 73 | + | account_type | Fine | + | account_status | Submitted | + | account_snapshot.defendant_name | LNAME, FNAME | + | account_snapshot.date_of_birth | 01/01/2000 | + | account_snapshot.account_type | Fine | + | account_snapshot.submitted_by | opal-test@HMCTS.NET | + | account_snapshot.business_unit_name | West London | Then I delete the created draft accounts - - diff --git a/src/integrationTest/java/uk/gov/hmcts/opal/controllers/DraftAccountControllerIntegrationTest.java b/src/integrationTest/java/uk/gov/hmcts/opal/controllers/DraftAccountControllerIntegrationTest.java index 0bf08a5b2..ac872dac8 100644 --- a/src/integrationTest/java/uk/gov/hmcts/opal/controllers/DraftAccountControllerIntegrationTest.java +++ b/src/integrationTest/java/uk/gov/hmcts/opal/controllers/DraftAccountControllerIntegrationTest.java @@ -113,11 +113,11 @@ void testPostDraftAccountsSearch() throws Exception { .content("{\"criteria\":\"value\"}")) .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.APPLICATION_JSON)) - .andExpect(jsonPath("$[0].draftAccountId").value(1)) - .andExpect(jsonPath("$[0].businessUnit.businessUnitId").value(7)) - .andExpect(jsonPath("$[0].accountType").value("DRAFT")) - .andExpect(jsonPath("$[0].submittedBy").value("Tony")) - .andExpect(jsonPath("$[0].accountStatus").value("SUBMITTED")); + .andExpect(jsonPath("$[0].draft_account_id").value(1)) + .andExpect(jsonPath("$[0].business_unit_id").value(7)) + .andExpect(jsonPath("$[0].account_type").value("DRAFT")) + .andExpect(jsonPath("$[0].submitted_by").value("Tony")) + .andExpect(jsonPath("$[0].account_status").value("Submitted")); } @Test diff --git a/src/main/java/uk/gov/hmcts/opal/controllers/DraftAccountController.java b/src/main/java/uk/gov/hmcts/opal/controllers/DraftAccountController.java index 80f1d5e50..f25bfe550 100644 --- a/src/main/java/uk/gov/hmcts/opal/controllers/DraftAccountController.java +++ b/src/main/java/uk/gov/hmcts/opal/controllers/DraftAccountController.java @@ -20,8 +20,8 @@ import uk.gov.hmcts.opal.dto.AddDraftAccountRequestDto; import uk.gov.hmcts.opal.dto.DraftAccountResponseDto; import uk.gov.hmcts.opal.dto.search.DraftAccountSearchDto; +import uk.gov.hmcts.opal.entity.BusinessUnitEntity; import uk.gov.hmcts.opal.entity.DraftAccountEntity; -import uk.gov.hmcts.opal.entity.DraftAccountStatus; import uk.gov.hmcts.opal.service.opal.DraftAccountService; import uk.gov.hmcts.opal.service.opal.JsonSchemaValidationService; import uk.gov.hmcts.opal.service.opal.UserStateService; @@ -74,15 +74,17 @@ public ResponseEntity getDraftAccountById( @PostMapping(value = "/search", consumes = MediaType.APPLICATION_JSON_VALUE) @Operation(summary = "Searches Draft Accounts based upon criteria in request body") - public ResponseEntity> postDraftAccountsSearch(@RequestBody DraftAccountSearchDto criteria, - @RequestHeader(value = "Authorization", required = false) String authHeaderValue) { + public ResponseEntity> postDraftAccountsSearch( + @RequestBody DraftAccountSearchDto criteria, + @RequestHeader(value = "Authorization", required = false) String authHeaderValue) { + log.info(":POST:postDraftAccountsSearch: query: \n{}", criteria); userStateService.checkForAuthorisedUser(authHeaderValue); List response = draftAccountService.searchDraftAccounts(criteria); - return buildResponse(response); + return buildResponse(response.stream().map(this::toGetResponseDto).toList()); } @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE) @@ -121,7 +123,8 @@ public ResponseEntity deleteDraftAccountById( DraftAccountResponseDto toGetResponseDto(DraftAccountEntity entity) { return DraftAccountResponseDto.builder() .draftAccountId(entity.getDraftAccountId()) - .businessUnitId(entity.getBusinessUnit().getBusinessUnitId()) + .businessUnitId(Optional.ofNullable(entity.getBusinessUnit()) + .map(BusinessUnitEntity::getBusinessUnitId).orElse(null)) .createdDate(toOffsetDateTime(entity.getCreatedDate())) .submittedBy(entity.getSubmittedBy()) .validatedDate(toOffsetDateTime(entity.getValidatedDate())) @@ -129,8 +132,7 @@ DraftAccountResponseDto toGetResponseDto(DraftAccountEntity entity) { .account(entity.getAccount()) .accountSnapshot(entity.getAccountSnapshot()) .accountType(entity.getAccountType()) - .accountStatus(Optional.ofNullable(entity.getAccountStatus()) - .map(DraftAccountStatus::getLabel).orElse(null)) + .accountStatus(entity.getAccountStatus()) .timelineData(entity.getTimelineData()) .accountNumber(entity.getAccountNumber()) .accountId(entity.getAccountId()) diff --git a/src/main/java/uk/gov/hmcts/opal/dto/DraftAccountResponseDto.java b/src/main/java/uk/gov/hmcts/opal/dto/DraftAccountResponseDto.java index ee85e797a..3b579ed80 100644 --- a/src/main/java/uk/gov/hmcts/opal/dto/DraftAccountResponseDto.java +++ b/src/main/java/uk/gov/hmcts/opal/dto/DraftAccountResponseDto.java @@ -7,6 +7,7 @@ import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; +import uk.gov.hmcts.opal.entity.DraftAccountStatus; import java.time.OffsetDateTime; @@ -47,7 +48,7 @@ public class DraftAccountResponseDto implements ToJsonString { private String accountType; @JsonProperty("account_status") - private String accountStatus; + private DraftAccountStatus accountStatus; @JsonProperty("timeline_data") @JsonRawValue diff --git a/src/main/java/uk/gov/hmcts/opal/dto/DraftAccountSummaryDto.java b/src/main/java/uk/gov/hmcts/opal/dto/DraftAccountSummaryDto.java new file mode 100644 index 000000000..7c6daf05f --- /dev/null +++ b/src/main/java/uk/gov/hmcts/opal/dto/DraftAccountSummaryDto.java @@ -0,0 +1,54 @@ +package uk.gov.hmcts.opal.dto; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonRawValue; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; +import uk.gov.hmcts.opal.entity.DraftAccountStatus; + +import java.time.OffsetDateTime; + +@Data +@NoArgsConstructor +@AllArgsConstructor +@Builder +@JsonInclude(JsonInclude.Include.NON_NULL) +public class DraftAccountSummaryDto implements ToJsonString { + + @JsonProperty("draft_account_id") + private Long draftAccountId; + + @JsonProperty("business_unit_id") + private Short businessUnitId; + + @JsonProperty("created_at") + private OffsetDateTime createdDate; + + @JsonProperty("submitted_by") + private String submittedBy; + + @JsonProperty("validated_at") + private OffsetDateTime validatedDate; + + @JsonProperty("validated_by") + private String validatedBy; + + @JsonProperty("account_snapshot") + @JsonRawValue + private String accountSnapshot; + + @JsonProperty("account_type") + private String accountType; + + @JsonProperty("account_status") + private DraftAccountStatus accountStatus; + + @JsonProperty("account_number") + private String accountNumber; + + @JsonProperty("account_id") + private Long accountId; +} diff --git a/src/main/java/uk/gov/hmcts/opal/dto/DraftAccountsResponseDto.java b/src/main/java/uk/gov/hmcts/opal/dto/DraftAccountsResponseDto.java new file mode 100644 index 000000000..82618efbb --- /dev/null +++ b/src/main/java/uk/gov/hmcts/opal/dto/DraftAccountsResponseDto.java @@ -0,0 +1,31 @@ +package uk.gov.hmcts.opal.dto; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import java.util.List; +import java.util.Optional; + +@Builder +@Getter +@NoArgsConstructor +@AllArgsConstructor +public class DraftAccountsResponseDto { + private Integer count; + private List summaries; + + public static class DraftAccountsResponseDtoBuilder { + public DraftAccountsResponseDto.DraftAccountsResponseDtoBuilder summaries( + List summaries) { + this.summaries = summaries; + return this.count(Optional.ofNullable(summaries).map(List::size).orElse(0)); + } + + private DraftAccountsResponseDto.DraftAccountsResponseDtoBuilder count(Integer count) { + this.count = count; + return this; + } + } +} diff --git a/src/main/java/uk/gov/hmcts/opal/dto/DraftAccountSnapshotsDto.java b/src/main/java/uk/gov/hmcts/opal/entity/DraftAccountSnapshots.java similarity index 65% rename from src/main/java/uk/gov/hmcts/opal/dto/DraftAccountSnapshotsDto.java rename to src/main/java/uk/gov/hmcts/opal/entity/DraftAccountSnapshots.java index fd3570049..a4649b963 100644 --- a/src/main/java/uk/gov/hmcts/opal/dto/DraftAccountSnapshotsDto.java +++ b/src/main/java/uk/gov/hmcts/opal/entity/DraftAccountSnapshots.java @@ -1,4 +1,4 @@ -package uk.gov.hmcts.opal.dto; +package uk.gov.hmcts.opal.entity; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonProperty; @@ -6,6 +6,7 @@ import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; +import uk.gov.hmcts.opal.dto.ToJsonString; import java.time.OffsetDateTime; import java.util.List; @@ -14,9 +15,9 @@ @NoArgsConstructor @AllArgsConstructor @Builder -public class DraftAccountSnapshotsDto implements ToJsonString { +public class DraftAccountSnapshots implements ToJsonString { - @JsonProperty("AccountSnapshot") + @JsonProperty("account_snapshot") private List accountSnapshot; @Data @@ -25,25 +26,25 @@ public class DraftAccountSnapshotsDto implements ToJsonString { @Builder @JsonInclude(JsonInclude.Include.NON_NULL) public static class Snapshot implements ToJsonString { - @JsonProperty("DefendantName") + @JsonProperty("defendant_name") private String defendantName; - @JsonProperty("DateOfBirth") + @JsonProperty("date_of_birth") private String dateOfBirth; - @JsonProperty("CreatedDate") + @JsonProperty("created_date") private OffsetDateTime createdDate; - @JsonProperty("AccountType") + @JsonProperty("account_type") private String accountType; - @JsonProperty("SubmittedBy") + @JsonProperty("submitted_by") private String submittedBy; - @JsonProperty("ApprovedDate") + @JsonProperty("approved_date") private OffsetDateTime approvedDate; - @JsonProperty("BusinessUnitName") + @JsonProperty("business_unit_name") private String businessUnitName; } } diff --git a/src/main/java/uk/gov/hmcts/opal/entity/DraftAccountStatus.java b/src/main/java/uk/gov/hmcts/opal/entity/DraftAccountStatus.java index c40d44add..e1bf511cf 100644 --- a/src/main/java/uk/gov/hmcts/opal/entity/DraftAccountStatus.java +++ b/src/main/java/uk/gov/hmcts/opal/entity/DraftAccountStatus.java @@ -1,7 +1,16 @@ package uk.gov.hmcts.opal.entity; +import com.fasterxml.jackson.annotation.JsonValue; + public enum DraftAccountStatus { - SUBMITTED("Submitted"); + + SUBMITTED("Submitted"), + REJECTED("Rejected"), + DELETED("Deleted"), + APPROVED("Approved"), + RESUBMITTED("Resubmitted"), + PENDING("Pending"), + ERROR_IN_PUBLISHING("Error in publishing"); private final String label; @@ -9,6 +18,7 @@ public enum DraftAccountStatus { this.label = label; } + @JsonValue public String getLabel() { return label; } diff --git a/src/main/java/uk/gov/hmcts/opal/service/opal/DraftAccountService.java b/src/main/java/uk/gov/hmcts/opal/service/opal/DraftAccountService.java index 805743ac9..46a32867c 100644 --- a/src/main/java/uk/gov/hmcts/opal/service/opal/DraftAccountService.java +++ b/src/main/java/uk/gov/hmcts/opal/service/opal/DraftAccountService.java @@ -8,7 +8,7 @@ import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import uk.gov.hmcts.opal.dto.AddDraftAccountRequestDto; -import uk.gov.hmcts.opal.dto.DraftAccountSnapshotsDto; +import uk.gov.hmcts.opal.entity.DraftAccountSnapshots; import uk.gov.hmcts.opal.dto.search.DraftAccountSearchDto; import uk.gov.hmcts.opal.entity.BusinessUnitEntity; import uk.gov.hmcts.opal.entity.DraftAccountEntity; @@ -76,8 +76,8 @@ private String createInitialSnapshot(AddDraftAccountRequestDto dto, LocalDateTim return buildInitialSnapshot(dto.getAccount(), created, businessUnit, userName).toPrettyJson(); } - private DraftAccountSnapshotsDto.Snapshot buildInitialSnapshot(String document, LocalDateTime created, - BusinessUnitEntity businessUnit, String userName) { + private DraftAccountSnapshots.Snapshot buildInitialSnapshot(String document, LocalDateTime created, + BusinessUnitEntity businessUnit, String userName) { JsonPathUtil.DocContext docContext = createDocContext(document); @@ -95,7 +95,7 @@ private DraftAccountSnapshotsDto.Snapshot buildInitialSnapshot(String document : null; String accType = docContext.read("$.accountCreateRequest.Account.AccountType"); - return DraftAccountSnapshotsDto.Snapshot.builder() + return DraftAccountSnapshots.Snapshot.builder() .defendantName(defendantName) .dateOfBirth(dob) .createdDate(created.atOffset(ZoneOffset.UTC)) diff --git a/src/test/java/uk/gov/hmcts/opal/controllers/DraftAccountControllerTest.java b/src/test/java/uk/gov/hmcts/opal/controllers/DraftAccountControllerTest.java index 946552e82..ccbe887ae 100644 --- a/src/test/java/uk/gov/hmcts/opal/controllers/DraftAccountControllerTest.java +++ b/src/test/java/uk/gov/hmcts/opal/controllers/DraftAccountControllerTest.java @@ -75,12 +75,11 @@ void testSearchDraftAccounts_Success() { // Act DraftAccountSearchDto searchDto = DraftAccountSearchDto.builder().build(); - ResponseEntity> response = draftAccountController.postDraftAccountsSearch( + ResponseEntity> response = draftAccountController.postDraftAccountsSearch( searchDto, BEARER_TOKEN); // Assert assertEquals(HttpStatus.OK, response.getStatusCode()); - assertEquals(draftAccountList, response.getBody()); verify(draftAccountService, times(1)).searchDraftAccounts(any()); } @@ -114,7 +113,7 @@ void testSaveDraftAccounts_Success() { assertEquals(HttpStatus.CREATED, response.getStatusCode()); DraftAccountResponseDto responseEntity = response.getBody(); assertEquals("Large", responseEntity.getAccountType()); - assertEquals("Submitted", responseEntity.getAccountStatus()); + assertEquals("Submitted", responseEntity.getAccountStatus().getLabel()); assertEquals("{\"acc\": \"1\"}", responseEntity.getAccount()); assertEquals("Charles", responseEntity.getSubmittedBy()); assertEquals("{\"dat\": \"2\"}", responseEntity.getTimelineData()); @@ -145,7 +144,7 @@ DraftAccountResponseDto toGetDto(DraftAccountEntity entity) { .account(entity.getAccount()) .accountSnapshot(entity.getAccountSnapshot()) .accountType(entity.getAccountType()) - .accountStatus(Optional.ofNullable(entity.getAccountStatus()).map(r -> r.getLabel()).orElse(null)) + .accountStatus(entity.getAccountStatus()) .timelineData(entity.getTimelineData()) .accountNumber(entity.getAccountNumber()) .accountId(entity.getAccountId())