Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin' into po-149
Browse files Browse the repository at this point in the history
  • Loading branch information
sabahirfan committed Jan 29, 2024
2 parents edd7669 + 4a28588 commit 5c0817d
Show file tree
Hide file tree
Showing 12 changed files with 365 additions and 129 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ dependencies {

testImplementation 'org.springframework.boot:spring-boot-devtools'
testImplementation 'org.springframework.boot:spring-boot-testcontainers'
testImplementation 'org.testcontainers:junit-jupiter:1.19.3'
testImplementation 'org.testcontainers:junit-jupiter:1.19.4'
testImplementation 'org.testcontainers:postgresql'
testImplementation(platform('org.junit:junit-bom:5.10.1'))
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.client.RestClient;

@Configuration
public class RestTemplateConfiguration {
public class RestClientConfiguration {

@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
public RestClient restClient() {
return RestClient.create();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.client.RestClient;
import uk.gov.hmcts.opal.dto.AccountDetailsDto;
import uk.gov.hmcts.opal.dto.AccountEnquiryDto;
import uk.gov.hmcts.opal.dto.AccountSearchDto;
Expand All @@ -20,7 +20,7 @@
import java.util.List;

@Service
@Slf4j
@Slf4j(topic = "LegacyDefendantAccountService")
public class LegacyDefendantAccountService extends LegacyService implements DefendantAccountServiceInterface {

public static final String SEARCH_DEFENDANT_ACCOUNTS = "searchDefendantAccounts";
Expand All @@ -30,8 +30,8 @@ public class LegacyDefendantAccountService extends LegacyService implements Defe

@Autowired
protected LegacyDefendantAccountService(@Value("${legacy-gateway-url}") String gatewayUrl,
RestTemplate restTemplate) {
super(gatewayUrl, restTemplate);
RestClient restClient) {
super(gatewayUrl, restClient);
}

@Override
Expand All @@ -42,7 +42,7 @@ public Logger getLog() {
@Override
public DefendantAccountEntity getDefendantAccount(AccountEnquiryDto request) {
log.info("Get defendant account for {} from {}", request.toJson(), gatewayUrl);
return getFromGateway(GET_DEFENDANT_ACCOUNT, DefendantAccountEntity.class, request);
return postToGateway(GET_DEFENDANT_ACCOUNT, DefendantAccountEntity.class, request);
}

@Override
Expand Down Expand Up @@ -72,9 +72,9 @@ public AccountDetailsDto getAccountDetailsByDefendantAccountId(Long defendantAcc
.defendantAccountId(defendantAccountId)
.build();

LegacyAccountDetailsResponseDto response = getFromGateway(GET_ACCOUNT_DETAILS,
LegacyAccountDetailsResponseDto response = postToGateway(GET_ACCOUNT_DETAILS,
LegacyAccountDetailsResponseDto.class,
defendantAccountId);
request);

return LegacyAccountDetailsResponseDto.toAccountDetailsDto(response);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.client.RestClient;
import uk.gov.hmcts.opal.dto.NoteDto;

@Service
Expand All @@ -13,8 +13,8 @@ public class LegacyNoteService extends LegacyService implements NoteServiceInter

public static final String POST_ACCOUNT_NOTES = "postAccountNotes";

protected LegacyNoteService(@Value("${legacy-gateway-url}") String gatewayUrl, RestTemplate restTemplate) {
super(gatewayUrl, restTemplate);
protected LegacyNoteService(@Value("${legacy-gateway-url}") String gatewayUrl, RestClient restClient) {
super(gatewayUrl, restClient);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.client.RestClient;
import uk.gov.hmcts.opal.dto.AccountSearchDto;
import uk.gov.hmcts.opal.dto.PartyDto;
import uk.gov.hmcts.opal.entity.PartySummary;

import java.util.Collections;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
Expand All @@ -21,8 +22,8 @@ public class LegacyPartyService extends LegacyService implements PartyServiceInt
public static final String POST_PARTY = "postParty";

@Autowired
protected LegacyPartyService(@Value("${legacy-gateway-url}") String gatewayUrl, RestTemplate restTemplate) {
super(gatewayUrl, restTemplate);
protected LegacyPartyService(@Value("${legacy-gateway-url}") String gatewayUrl, RestClient restClient) {
super(gatewayUrl, restClient);
}

@Override
Expand All @@ -33,7 +34,7 @@ public Logger getLog() {
@Override
public PartyDto getParty(long partyId) {
log.info("Get party for id: {}", partyId);
return getFromGateway(GET_PARTY, PartyDto.class, partyId);
return postParamsToGateway(GET_PARTY, PartyDto.class, Map.of("party_id", partyId));
}

@Override
Expand Down
39 changes: 29 additions & 10 deletions src/main/java/uk/gov/hmcts/opal/service/LegacyService.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
package uk.gov.hmcts.opal.service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.client.RestClient;
import org.springframework.web.util.UriComponentsBuilder;
import uk.gov.hmcts.opal.dto.ToJsonString;

import java.util.Map;

public abstract class LegacyService {

public static final String ACTION_TYPE = "actionType";
final String gatewayUrl;

final RestTemplate restTemplate;
final RestClient restClient;

protected LegacyService(String gatewayUrl, RestTemplate restTemplate) {
protected LegacyService(String gatewayUrl, RestClient restTemplate) {
this.gatewayUrl = gatewayUrl;
this.restTemplate = restTemplate;
this.restClient = restTemplate;
}

protected abstract Logger getLog();
Expand All @@ -42,29 +46,44 @@ public <T> T extractResponse(ResponseEntity<String> responseEntity, Class<T> clz
return null;
}

public <T> T getFromGateway(String actionType, Class<T> responseType, Object... uriVariables) {
getLog().info("getFromGateway: GET from Gateway: {}", gatewayUrl);
public <T> T getFromGateway(String actionType, Class<T> responseType) {
getLog().info("getFromGateway: GET from Gateway: {}", gatewayUrl);

// Create a UriComponentsBuilder and add parameters
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString("")
.queryParam(ACTION_TYPE, actionType);

ResponseEntity<String> responseEntity = restTemplate.getForEntity(gatewayUrl
+ builder.toUriString(), String.class, uriVariables);
ResponseEntity<String> responseEntity = restClient.get()
.uri(gatewayUrl + builder.toUriString())
.retrieve()
.toEntity(String.class);

return extractResponse(responseEntity, responseType);

}

public <T> T postParamsToGateway(String actionType, Class<T> responseType, Map<String, Object> requestParams) {
try {
return postToGateway(actionType, responseType,
ToJsonString.getObjectMapper().writeValueAsString(requestParams));
} catch (JsonProcessingException jpe) {
throw new RuntimeException(jpe);
}
}

public <T> T postToGateway(String actionType, Class<T> responseType, Object request) {
getLog().info("postToGateway: POST to Gateway: {}", gatewayUrl);

// Create a UriComponentsBuilder and add parameters
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString("")
.queryParam(ACTION_TYPE, actionType);

ResponseEntity<String> responseEntity = restTemplate.postForEntity(
gatewayUrl + builder.toUriString(), request, String.class);
ResponseEntity<String> responseEntity = restClient.post()
.uri(gatewayUrl + builder.toUriString())
.contentType(MediaType.APPLICATION_JSON)
.body(request)
.retrieve()
.toEntity(String.class);

return extractResponse(responseEntity, responseType);
}
Expand Down
Loading

0 comments on commit 5c0817d

Please sign in to comment.