Skip to content

Commit

Permalink
PO-559: Updates to 'broken' Draft Accounts POST endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
RustyHMCTS committed Sep 10, 2024
1 parent 081e179 commit 7202a86
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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;
Expand Down Expand Up @@ -35,9 +36,11 @@ public class DraftAccountResponseDto implements ToJsonString {
private String validatedBy;

@JsonProperty("account")
@JsonRawValue
private String account;

@JsonProperty("account_snapshot")
@JsonRawValue
private String accountSnapshot;

@JsonProperty("account_type")
Expand All @@ -47,6 +50,7 @@ public class DraftAccountResponseDto implements ToJsonString {
private String accountStatus;

@JsonProperty("timeline_data")
@JsonRawValue
private String timelineData;

@JsonProperty("account_number")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonRawValue;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
Expand All @@ -20,7 +19,7 @@
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import uk.gov.hmcts.opal.util.KeepAsJsonDeserializer;
import org.hibernate.annotations.ColumnTransformer;

import java.time.LocalDateTime;

Expand Down Expand Up @@ -58,7 +57,7 @@ public class DraftAccountEntity {
private String validatedBy;

@Column(name = "account", columnDefinition = "json", nullable = false)
@JsonDeserialize(using = KeepAsJsonDeserializer.class)
@ColumnTransformer(write = "?::jsonb")
@JsonRawValue
private String account;

Expand All @@ -69,7 +68,7 @@ public class DraftAccountEntity {
private Long accountId;

@Column(name = "account_snapshot", columnDefinition = "json", nullable = false)
@JsonDeserialize(using = KeepAsJsonDeserializer.class)
@ColumnTransformer(write = "?::jsonb")
@JsonRawValue
private String accountSnapshot;

Expand All @@ -78,7 +77,7 @@ public class DraftAccountEntity {
private DraftAccountStatus accountStatus;

@Column(name = "timeline_data", columnDefinition = "json")
@JsonDeserialize(using = KeepAsJsonDeserializer.class)
@ColumnTransformer(write = "?::jsonb")
@JsonRawValue
private String timelineData;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package uk.gov.hmcts.opal.service.opal;


import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.Option;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
Expand All @@ -20,12 +16,15 @@
import uk.gov.hmcts.opal.repository.BusinessUnitRepository;
import uk.gov.hmcts.opal.repository.DraftAccountRepository;
import uk.gov.hmcts.opal.repository.jpa.DraftAccountSpecs;
import uk.gov.hmcts.opal.util.JsonPathUtil;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.List;
import java.util.Optional;

import static uk.gov.hmcts.opal.util.JsonPathUtil.createDocContext;

@Service
@Slf4j(topic = "DraftAccountService")
@RequiredArgsConstructor
Expand Down Expand Up @@ -80,8 +79,7 @@ private String createInitialSnapshot(AddDraftAccountRequestDto dto, LocalDateTim
private DraftAccountSnapshotsDto.Snapshot buildInitialSnapshot(String document, LocalDateTime created,
BusinessUnitEntity businessUnit, String userName) {

Configuration config = Configuration.defaultConfiguration().addOptions(Option.DEFAULT_PATH_LEAF_TO_NULL);
DocumentContext docContext = JsonPath.parse(document, config);
JsonPathUtil.DocContext docContext = createDocContext(document);

String companyName = docContext.read("$.accountCreateRequest.Defendant.CompanyName");

Expand Down
36 changes: 36 additions & 0 deletions src/main/java/uk/gov/hmcts/opal/util/JsonPathUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package uk.gov.hmcts.opal.util;

import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.Option;
import com.jayway.jsonpath.PathNotFoundException;
import lombok.extern.slf4j.Slf4j;

@Slf4j(topic = "JsonPathUtil")
public class JsonPathUtil {

public static DocContext createDocContext(String document) {
Configuration config = Configuration.defaultConfiguration().addOptions(Option.DEFAULT_PATH_LEAF_TO_NULL);
return new DocContext(JsonPath.parse(document, config), document);
}

public static class DocContext {
private final DocumentContext documentContext;
private final String originalDocument;

public DocContext(DocumentContext documentContext, String originalDocument) {
this.documentContext = documentContext;
this.originalDocument = originalDocument;
}

public <T> T read(String path) {
try {
return documentContext.read(path);
} catch (PathNotFoundException pnfe) {
// All this is required because the PathNotFoundException suppresses the Stack Trace.
throw new RuntimeException(pnfe.getMessage());
}
}
}
}
6 changes: 3 additions & 3 deletions src/main/resources/jsonSchemas/getDraftAccountResponse.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@
"description": "[Optional] ID of the User that validated the Draft Account"
},
"account": {
"type": "string",
"type": "object",
"description": "The structured Account data (JSON)"
},
"account_snapshot": {
"type": "string",
"type": "object",
"description": "Summary business data to identify the Account - System generated (BE) from Account data (JSON)"
},
"account_type": {
Expand All @@ -47,7 +47,7 @@
"description": "Status of the Draft Account - one of Submitted, Resubmitted, Rejected, Approved, Deleted"
},
"timeline_data": {
"type": "string",
"type": "object",
"description": "Status changes to the Draft Account in chronological order (JSON Array) - System generated (UI)"
},
"account_number": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,8 @@ void testSearchDraftAccounts() {

}

@SuppressWarnings("unchecked")
@Test
void testSubmitDraftAccounts() {
void testSubmitDraftAccounts_success() {
// Arrange
DraftAccountEntity draftAccountEntity = DraftAccountEntity.builder().build();
AddDraftAccountRequestDto addDraftAccountDto = AddDraftAccountRequestDto.builder()
Expand All @@ -99,6 +98,26 @@ void testSubmitDraftAccounts() {
assertEquals(draftAccountEntity, result);
}

@Test
void testSubmitDraftAccounts_fail() {
// Arrange
AddDraftAccountRequestDto addDraftAccountDto = AddDraftAccountRequestDto.builder()
.account("{}")
.build();
BusinessUnitEntity businessUnit = BusinessUnitEntity.builder()
.businessUnitName("Old Bailey")
.build();

when(businessUnitRepository.findById(any())).thenReturn(Optional.of(businessUnit));

// Act
RuntimeException re = assertThrows(RuntimeException.class, () ->
draftAccountService.submitDraftAccount(addDraftAccountDto, "Charles"));

// Assert
assertEquals("Missing property in path $['accountCreateRequest']", re.getMessage());
}

@Test
void testDeleteDraftAccount_success() {
// Arrange
Expand Down

0 comments on commit 7202a86

Please sign in to comment.