Skip to content

Commit

Permalink
Merge branch 'master' into PO-438-s2s-auth
Browse files Browse the repository at this point in the history
  • Loading branch information
TomReed committed Aug 21, 2024
2 parents 8f80282 + ab070ae commit 1df6a83
Show file tree
Hide file tree
Showing 20 changed files with 40,287 additions and 50 deletions.
16 changes: 9 additions & 7 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -218,11 +218,11 @@ dependencyManagement {
}

imports {
mavenBom "org.springframework.integration:spring-integration-bom:6.3.2"
mavenBom "org.springframework.integration:spring-integration-bom:6.3.3"
}

dependencies {
dependency group: 'com.google.guava', name: 'guava', version: '33.2.1-jre'
dependency group: 'com.google.guava', name: 'guava', version: '33.3.0-jre'
}
}

Expand All @@ -238,7 +238,7 @@ repositories {

ext {
log4JVersion = "2.23.1"
serenityVersion = "4.1.20"
serenityVersion = "4.2.0"
}

ext['snakeyaml.version'] = '2.0'
Expand All @@ -263,7 +263,7 @@ dependencies {
implementation group: 'org.springframework.integration', name: 'spring-integration-file'


implementation group: 'org.springframework.security', name: 'spring-security-oauth2-authorization-server', version: '1.3.1'
implementation group: 'org.springframework.security', name: 'spring-security-oauth2-authorization-server', version: '1.3.2'

implementation group: 'org.springframework', name: 'spring-aspects'

Expand All @@ -275,10 +275,12 @@ dependencies {

implementation group: 'org.apache.logging.log4j', name: 'log4j-api', version: log4JVersion
implementation group: 'org.apache.logging.log4j', name: 'log4j-to-slf4j', version: log4JVersion
implementation group: 'com.google.guava', name: 'guava', version: '33.2.1-jre'
implementation group: 'com.google.guava', name: 'guava', version: '33.3.0-jre'

implementation group: 'com.launchdarkly', name: 'launchdarkly-java-server-sdk', version: '7.5.0'

implementation(group: 'com.networknt', name: 'json-schema-validator', version: '1.5.1');

implementation group: 'io.rest-assured', name: 'rest-assured'
implementation 'org.flywaydb:flyway-core'
runtimeOnly 'org.flywaydb:flyway-database-postgresql:10.17.1'
Expand Down Expand Up @@ -307,13 +309,13 @@ dependencies {
testImplementation 'org.springframework.boot:spring-boot-testcontainers'
testImplementation 'org.testcontainers:junit-jupiter:1.20.1'
testImplementation 'org.testcontainers:postgresql'
testImplementation(platform('org.junit:junit-bom:5.10.3'))
testImplementation(platform('org.junit:junit-bom:5.11.0'))
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
testImplementation group: 'org.springframework.boot', name: 'spring-boot-starter-test', {
exclude group: 'junit', module: 'junit'
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
testImplementation 'org.junit.platform:junit-platform-suite:1.10.3'
testImplementation 'org.junit.platform:junit-platform-suite:1.11.0'
testImplementation 'io.cucumber:cucumber-junit-platform-engine:7.18.1'

testImplementation "net.serenity-bdd:serenity-core:${serenityVersion}"
Expand Down
2 changes: 1 addition & 1 deletion infrastructure/.terraform-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.9.4
1.9.5
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,25 @@
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.http.MediaType;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import uk.gov.hmcts.opal.dto.ToJsonString;
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.service.opal.DraftAccountService;
import uk.gov.hmcts.opal.service.opal.JsonSchemaValidationService;
import uk.gov.hmcts.opal.service.opal.UserStateService;

import java.time.LocalDate;
import java.util.logging.Logger;

import static java.util.Collections.singletonList;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
Expand All @@ -31,6 +37,8 @@
@ActiveProfiles({"integration"})
class DraftAccountControllerIntegrationTest {

private static final Logger logger = Logger.getLogger(DraftAccountControllerIntegrationTest.class.getSimpleName());

@Autowired
MockMvc mockMvc;

Expand All @@ -41,21 +49,31 @@ class DraftAccountControllerIntegrationTest {
@MockBean
UserStateService userStateService;

@SpyBean
private JsonSchemaValidationService jsonSchemaValidationService;

@Test
void testGetDraftAccountById() throws Exception {
DraftAccountEntity draftAccountEntity = createDraftAccountEntity();

when(draftAccountService.getDraftAccount(1L)).thenReturn(draftAccountEntity);

mockMvc.perform(get("/api/draft-account/1")
MvcResult result = mockMvc.perform(get("/api/draft-account/1")
.header("authorization", "Bearer some_value"))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$.draftAccountId").value(1))
.andExpect(jsonPath("$.businessUnit.businessUnitId").value(7))
.andExpect(jsonPath("$.accountType").value("DRAFT"))
.andExpect(jsonPath("$.createdBy").value("Tony"))
.andExpect(jsonPath("$.accountStatus").value("CREATED"));
.andExpect(jsonPath("$.draft_account_id").value(1))
.andExpect(jsonPath("$.business_unit_id").value(7))
.andExpect(jsonPath("$.account_type").value("DRAFT"))
.andExpect(jsonPath("$.submitted_by").value("Tony"))
.andExpect(jsonPath("$.account_status").value("CREATED"))
.andReturn();

String body = result.getResponse().getContentAsString();

logger.info(":testGetDraftAccountById: Response body:\n" + ToJsonString.toPrettyJson(body));

assertTrue(jsonSchemaValidationService.isValid(body, "getDraftAccountResponse.json"));
}


Expand Down Expand Up @@ -83,7 +101,7 @@ void testPostDraftAccountsSearch() throws Exception {
.andExpect(jsonPath("$[0].draftAccountId").value(1))
.andExpect(jsonPath("$[0].businessUnit.businessUnitId").value(7))
.andExpect(jsonPath("$[0].accountType").value("DRAFT"))
.andExpect(jsonPath("$[0].createdBy").value("Tony"))
.andExpect(jsonPath("$[0].submittedBy").value("Tony"))
.andExpect(jsonPath("$[0].accountStatus").value("CREATED"));
}

Expand All @@ -100,10 +118,13 @@ private DraftAccountEntity createDraftAccountEntity() {
return DraftAccountEntity.builder()
.draftAccountId(1L)
.businessUnit(BusinessUnitEntity.builder().businessUnitId((short)007).build())
.createdDate(LocalDate.of(2023, 1, 2))
.createdBy("Tony")
.createdDate(LocalDate.of(2023, 1, 2).atStartOfDay())
.submittedBy("Tony")
.accountType("DRAFT")
.accountStatus("CREATED")
.account("{}")
.accountSnapshot("{}")
.timelineData("{}")
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import uk.gov.hmcts.opal.dto.GetDraftAccountResponseDto;
import uk.gov.hmcts.opal.dto.search.DraftAccountSearchDto;
import uk.gov.hmcts.opal.entity.DraftAccountEntity;
import uk.gov.hmcts.opal.service.opal.DraftAccountService;
import uk.gov.hmcts.opal.service.opal.JsonSchemaValidationService;
import uk.gov.hmcts.opal.service.opal.UserStateService;

import java.util.List;
import java.util.Optional;

import static uk.gov.hmcts.opal.util.DateTimeUtils.toOffsetDateTime;
import static uk.gov.hmcts.opal.util.HttpUtil.buildResponse;


Expand All @@ -28,27 +32,32 @@
@Tag(name = "DraftAccount Controller")
public class DraftAccountController {

private final DraftAccountService opalDraftAccountService;
private final DraftAccountService draftAccountService;

private final UserStateService userStateService;

public DraftAccountController(UserStateService userStateService, DraftAccountService opalDraftAccountService) {
this.opalDraftAccountService = opalDraftAccountService;
private final JsonSchemaValidationService jsonSchemaValidationService;

public DraftAccountController(UserStateService userStateService, DraftAccountService draftAccountService,
JsonSchemaValidationService jsonSchemaValidationService) {
this.draftAccountService = draftAccountService;
this.userStateService = userStateService;
this.jsonSchemaValidationService = jsonSchemaValidationService;
}

@GetMapping(value = "/{draftAccountId}")
@Operation(summary = "Returns the Draft Account for the given draftAccountId.")
public ResponseEntity<DraftAccountEntity> getDraftAccountById(@PathVariable Long draftAccountId,
@RequestHeader(value = "Authorization", required = false) String authHeaderValue) {
public ResponseEntity<GetDraftAccountResponseDto> getDraftAccountById(
@PathVariable Long draftAccountId,
@RequestHeader(value = "Authorization", required = false) String authHeaderValue) {

log.info(":GET:getDraftAccountById: draftAccountId: {}", draftAccountId);

userStateService.checkForAuthorisedUser(authHeaderValue);

DraftAccountEntity response = opalDraftAccountService.getDraftAccount(draftAccountId);
DraftAccountEntity response = draftAccountService.getDraftAccount(draftAccountId);

return buildResponse(response);
return buildResponse(Optional.ofNullable(response).map(this::toGetResponseDto).orElse(null));
}

@PostMapping(value = "/search", consumes = MediaType.APPLICATION_JSON_VALUE)
Expand All @@ -59,7 +68,7 @@ public ResponseEntity<List<DraftAccountEntity>> postDraftAccountsSearch(@Request

userStateService.checkForAuthorisedUser(authHeaderValue);

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

return buildResponse(response);
}
Expand All @@ -72,8 +81,26 @@ public ResponseEntity<DraftAccountEntity> postDraftAccount(@RequestBody DraftAcc

userStateService.checkForAuthorisedUser(authHeaderValue);

DraftAccountEntity response = opalDraftAccountService.saveDraftAccount(entity);
DraftAccountEntity response = draftAccountService.saveDraftAccount(entity);

return buildResponse(response);
}

GetDraftAccountResponseDto toGetResponseDto(DraftAccountEntity entity) {
return GetDraftAccountResponseDto.builder()
.draftAccountId(entity.getDraftAccountId())
.businessUnitId(entity.getBusinessUnit().getBusinessUnitId())
.createdDate(toOffsetDateTime(entity.getCreatedDate()))
.submittedBy(entity.getSubmittedBy())
.validatedDate(toOffsetDateTime(entity.getValidatedDate()))
.validatedBy(entity.getValidatedBy())
.account(entity.getAccount())
.accountSnapshot(entity.getAccountSnapshot())
.accountType(entity.getAccountType())
.accountStatus(entity.getAccountStatus())
.timelineData(entity.getTimelineData())
.accountNumber(entity.getAccountNumber())
.accountId(entity.getAccountId())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package uk.gov.hmcts.opal.dto;

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 java.time.OffsetDateTime;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@JsonInclude(JsonInclude.Include.NON_NULL)
public class GetDraftAccountResponseDto 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")
private String account;

@JsonProperty("account_snapshot")
private String accountSnapshot;

@JsonProperty("account_type")
private String accountType;

@JsonProperty("account_status")
private String accountStatus;

@JsonProperty("timeline_data")
private String timelineData;

@JsonProperty("account_number")
private String accountNumber;

@JsonProperty("account_id")
private Long accountId;
}
20 changes: 18 additions & 2 deletions src/main/java/uk/gov/hmcts/opal/dto/ToJsonString.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ default String toJson() {
}

default String toPrettyJsonString() throws JsonProcessingException {
return OBJECT_MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(this);
return toPrettyJsonString(this);
}

static String toPrettyJsonString(Object original) throws JsonProcessingException {
return OBJECT_MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(original);
}

default String toPrettyJson() {
Expand All @@ -34,8 +38,20 @@ default String toPrettyJson() {
}
}

static String toPrettyJson(String json) {
try {
return toPrettyJsonString(toJsonNode(json));
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}

default JsonNode toJsonNode() throws JsonProcessingException {
return OBJECT_MAPPER.readTree(this.toJsonString());
return toJsonNode(this.toJsonString());
}

static JsonNode toJsonNode(String json) throws JsonProcessingException {
return OBJECT_MAPPER.readTree(json);
}

static ObjectMapper getObjectMapper() {
Expand Down
Loading

0 comments on commit 1df6a83

Please sign in to comment.