Skip to content
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

PO-606: Pre-work before GET multiple Draft Account summaries #540

Merged
merged 1 commit into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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


Original file line number Diff line number Diff line change
Expand Up @@ -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


Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -74,15 +74,17 @@ public ResponseEntity<DraftAccountResponseDto> getDraftAccountById(

@PostMapping(value = "/search", consumes = MediaType.APPLICATION_JSON_VALUE)
@Operation(summary = "Searches Draft Accounts based upon criteria in request body")
public ResponseEntity<List<DraftAccountEntity>> postDraftAccountsSearch(@RequestBody DraftAccountSearchDto criteria,
@RequestHeader(value = "Authorization", required = false) String authHeaderValue) {
public ResponseEntity<List<DraftAccountResponseDto>> postDraftAccountsSearch(
@RequestBody DraftAccountSearchDto criteria,
@RequestHeader(value = "Authorization", required = false) String authHeaderValue) {

log.info(":POST:postDraftAccountsSearch: query: \n{}", criteria);

userStateService.checkForAuthorisedUser(authHeaderValue);

List<DraftAccountEntity> response = draftAccountService.searchDraftAccounts(criteria);

return buildResponse(response);
return buildResponse(response.stream().map(this::toGetResponseDto).toList());
}

@PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
Expand Down Expand Up @@ -121,16 +123,16 @@ public ResponseEntity<String> 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()))
.validatedBy(entity.getValidatedBy())
.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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import uk.gov.hmcts.opal.entity.DraftAccountStatus;

import java.time.OffsetDateTime;

Expand Down Expand Up @@ -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
Expand Down
54 changes: 54 additions & 0 deletions src/main/java/uk/gov/hmcts/opal/dto/DraftAccountSummaryDto.java
Original file line number Diff line number Diff line change
@@ -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("created_at")
private OffsetDateTime createdDate;

@JsonProperty("submitted_by")
private String submittedBy;

@JsonProperty("business_unit_id")
private Short businessUnitId;

@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;
}
31 changes: 31 additions & 0 deletions src/main/java/uk/gov/hmcts/opal/dto/DraftAccountsResponseDto.java
Original file line number Diff line number Diff line change
@@ -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<DraftAccountSummaryDto> summaries;

public static class DraftAccountsResponseDtoBuilder {
public DraftAccountsResponseDto.DraftAccountsResponseDtoBuilder summaries(
List<DraftAccountSummaryDto> 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;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package uk.gov.hmcts.opal.dto;
package uk.gov.hmcts.opal.entity;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import uk.gov.hmcts.opal.dto.ToJsonString;

import java.time.OffsetDateTime;
import java.util.List;
Expand All @@ -14,9 +15,9 @@
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class DraftAccountSnapshotsDto implements ToJsonString {
public class DraftAccountSnapshots implements ToJsonString {

@JsonProperty("AccountSnapshot")
@JsonProperty("account_snapshot")
private List<Snapshot> accountSnapshot;

@Data
Expand All @@ -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;
}
}
12 changes: 11 additions & 1 deletion src/main/java/uk/gov/hmcts/opal/entity/DraftAccountStatus.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
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;

DraftAccountStatus(String label) {
this.label = label;
}

@JsonValue
public String getLabel() {
return label;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand All @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,11 @@ void testSearchDraftAccounts_Success() {

// Act
DraftAccountSearchDto searchDto = DraftAccountSearchDto.builder().build();
ResponseEntity<List<DraftAccountEntity>> response = draftAccountController.postDraftAccountsSearch(
ResponseEntity<List<DraftAccountResponseDto>> response = draftAccountController.postDraftAccountsSearch(
searchDto, BEARER_TOKEN);

// Assert
assertEquals(HttpStatus.OK, response.getStatusCode());
assertEquals(draftAccountList, response.getBody());
verify(draftAccountService, times(1)).searchDraftAccounts(any());
}

Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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())
Expand Down