Skip to content
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,7 +15,9 @@

import java.util.UUID;

import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.swagger.v3.oas.models.OpenAPI;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class OpenAPIConfiguration {
Expand All @@ -13,4 +14,9 @@ public class OpenAPIConfiguration {
public OpenAPI openAPI() {
return openAPIConfigLoader.openAPI();
}

@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
Original file line number Diff line number Diff line change
@@ -1,33 +1,38 @@
package uk.gov.hmcts.cp.controllers;

import org.apache.commons.text.StringEscapeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;
import uk.gov.hmcts.cp.openapi.api.CourtScheduleApi;
import uk.gov.hmcts.cp.openapi.model.CourtScheduleResponse;
import uk.gov.hmcts.cp.services.CaseUrnMapperService;
import uk.gov.hmcts.cp.services.CourtScheduleService;

import static uk.gov.hmcts.cp.utils.Utils.sanitizeString;

@RestController
public class CourtScheduleController implements CourtScheduleApi {
private static final Logger LOG = LoggerFactory.getLogger(CourtScheduleController.class);
private final CourtScheduleService courtScheduleService;
private final CaseUrnMapperService caseUrnMapperService;

public CourtScheduleController(final CourtScheduleService courtScheduleService) {
public CourtScheduleController(final CourtScheduleService courtScheduleService,
final CaseUrnMapperService caseUrnMapperService) {
this.courtScheduleService = courtScheduleService;
this.caseUrnMapperService = caseUrnMapperService;
}

@Override
public ResponseEntity<CourtScheduleResponse> getCourtScheduleByCaseUrn(final String caseUrn) {
final String sanitizedCaseUrn;
final CourtScheduleResponse courtScheduleResponse;
try {
sanitizedCaseUrn = sanitizeCaseUrn(caseUrn);
courtScheduleResponse = courtScheduleService.getCourtScheduleByCaseUrn(sanitizedCaseUrn);
sanitizedCaseUrn = sanitizeString(caseUrn);
courtScheduleResponse = courtScheduleService.getCourtScheduleByCaseId(
caseUrnMapperService.getCaseId(sanitizedCaseUrn));
} catch (ResponseStatusException e) {
LOG.atError().log(e.getMessage());
throw e;
Expand All @@ -37,11 +42,4 @@ public ResponseEntity<CourtScheduleResponse> getCourtScheduleByCaseUrn(final Str
.contentType(MediaType.APPLICATION_JSON)
.body(courtScheduleResponse);
}

private String sanitizeCaseUrn(final String urn) {
if (urn == null) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "caseUrn is required");
}
return StringEscapeUtils.escapeHtml4(urn);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
@Repository
public interface CourtScheduleRepository {

CourtScheduleResponse getCourtScheduleByCaseUrn(String caseUrn);
CourtScheduleResponse getCourtScheduleByCaseId(String caseUrn);
void saveCourtSchedule(String caseUrn, CourtScheduleResponse courtScheduleResponse);
void clearAll();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void saveCourtSchedule(final String caseUrn, final CourtScheduleResponse
courtScheduleResponseMap.put(caseUrn, courtScheduleResponse);
}

public CourtScheduleResponse getCourtScheduleByCaseUrn(final String caseUrn) {
public CourtScheduleResponse getCourtScheduleByCaseId(final String caseUrn) {
if (!courtScheduleResponseMap.containsKey(caseUrn)) {
saveCourtSchedule(caseUrn, createCourtScheduleResponse());
}
Expand Down
68 changes: 68 additions & 0 deletions src/main/java/uk/gov/hmcts/cp/services/CaseUrnMapperService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package uk.gov.hmcts.cp.services;

import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import java.net.URI;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

@Service
@RequiredArgsConstructor
public class CaseUrnMapperService {

private static final Logger LOG = LoggerFactory.getLogger(CaseUrnMapperService.class);

private final RestTemplate restTemplate;

@Value("${service.case-mapper-service.url}")
private String caseMapperServiceUrl;

private static final String CASEURN_ID = "caseurn/{caseurn}";

private final Map<String, String> caseUrnToCaseIdMap = new ConcurrentHashMap<>();



public String getCaseId(final String caseUrn) {
return UUID.randomUUID().toString();
// This below code is commented out as it is will be used when the case mapper service is available.

/* try {
ResponseEntity<String> responseEntity = restTemplate.exchange(
getCaseIdUrl(caseUrn),
HttpMethod.GET,
getRequestEntity(),
String.class
);
return responseEntity.hasBody() ? responseEntity.getBody(): Strings.EMPTY;
} catch (Exception e) {
LOG.atError().log("Error while getting case id from case urn", e);
}
return null;*/
}

private String getCaseIdUrl(String caseUrn) {

Check warning on line 55 in src/main/java/uk/gov/hmcts/cp/services/CaseUrnMapperService.java

View workflow job for this annotation

GitHub Actions / pmd-analysis

Parameter 'caseUrn' is not assigned and could be declared final

Reports method and constructor parameters that can be made final because they are never reassigned within the body of the method. This rule ignores unused parameters so as not to overlap with the rule {% rule java/bestpractices/UnusedFormalParameter %}. It will also ignore the parameters of abstract methods. MethodArgumentCouldBeFinal (Priority: 3, Ruleset: Code Style) https://docs.pmd-code.org/snapshot/pmd_rules_java_codestyle.html#methodargumentcouldbefinal

Check warning on line 55 in src/main/java/uk/gov/hmcts/cp/services/CaseUrnMapperService.java

View workflow job for this annotation

GitHub Actions / pmd-analysis

Avoid unused private methods such as 'getCaseIdUrl(String)'.

Unused Private Method detects when a private method is declared but is unused. UnusedPrivateMethod (Priority: 3, Ruleset: Best Practices) https://docs.pmd-code.org/snapshot/pmd_rules_java_bestpractices.html#unusedprivatemethod
return UriComponentsBuilder
.fromUri(URI.create(caseMapperServiceUrl))
.pathSegment(caseUrn)
.buildAndExpand(caseUrn)
.toUriString();
}

private HttpEntity<String> getRequestEntity() {

Check warning on line 63 in src/main/java/uk/gov/hmcts/cp/services/CaseUrnMapperService.java

View workflow job for this annotation

GitHub Actions / pmd-analysis

Avoid unused private methods such as 'getRequestEntity()'.

Unused Private Method detects when a private method is declared but is unused. UnusedPrivateMethod (Priority: 3, Ruleset: Best Practices) https://docs.pmd-code.org/snapshot/pmd_rules_java_bestpractices.html#unusedprivatemethod
HttpHeaders headers = new HttpHeaders();

Check warning on line 64 in src/main/java/uk/gov/hmcts/cp/services/CaseUrnMapperService.java

View workflow job for this annotation

GitHub Actions / pmd-analysis

Local variable 'headers' could be declared final

A local variable assigned only once can be declared final. LocalVariableCouldBeFinal (Priority: 3, Ruleset: Code Style) https://docs.pmd-code.org/snapshot/pmd_rules_java_codestyle.html#localvariablecouldbefinal
headers.setAccept(List.of(MediaType.APPLICATION_JSON));
return new HttpEntity<>(headers);
}
}
14 changes: 8 additions & 6 deletions src/main/java/uk/gov/hmcts/cp/services/CourtScheduleService.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import uk.gov.hmcts.cp.openapi.model.CourtScheduleResponse;
import uk.gov.hmcts.cp.repositories.CourtScheduleRepository;

import static uk.gov.hmcts.cp.utils.Utils.sanitizeString;

@Service
@RequiredArgsConstructor
public class CourtScheduleService {
Expand All @@ -18,13 +20,13 @@ public class CourtScheduleService {

private final CourtScheduleRepository courtScheduleRepository;

public CourtScheduleResponse getCourtScheduleByCaseUrn(final String caseUrn) throws ResponseStatusException {
if (StringUtils.isEmpty(caseUrn)) {
LOG.atWarn().log("No case urn provided");
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "caseUrn is required");
public CourtScheduleResponse getCourtScheduleByCaseId(final String caseId) throws ResponseStatusException {
if (StringUtils.isEmpty(caseId)) {
LOG.atWarn().log("No case Id provided");
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "caseId is required");
}
LOG.atWarn().log("NOTE: System configured to return stubbed Court Schedule details. Ignoring provided caseUrn : {}", caseUrn);
final CourtScheduleResponse stubbedCourtScheduleResponse = courtScheduleRepository.getCourtScheduleByCaseUrn(caseUrn);
LOG.atWarn().log("NOTE: System configured to return stubbed Court Schedule details. Ignoring provided caseId : {}", sanitizeString(caseId));
final CourtScheduleResponse stubbedCourtScheduleResponse = courtScheduleRepository.getCourtScheduleByCaseId(caseId);
LOG.atDebug().log("Court Schedule response: {}", stubbedCourtScheduleResponse);
return stubbedCourtScheduleResponse;
}
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/uk/gov/hmcts/cp/utils/Utils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package uk.gov.hmcts.cp.utils;

import org.apache.commons.text.StringEscapeUtils;
import org.springframework.http.HttpStatus;
import org.springframework.web.server.ResponseStatusException;

public class Utils {

public static String sanitizeString(final String urn) {
if (urn == null) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "caseUrn is required");
}
return StringEscapeUtils.escapeHtml4(urn);
}

}
4 changes: 4 additions & 0 deletions src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ spring:
# key: ${COSMOSDB_KEY}
# database: ${COSMOSDB_DATABASE}

service:
case-mapper-service:
url: https://virtserver.swaggerhub.com/HMCTS-DTS/api-cp-refdata-case-mapper

azure:
application-insights:
instrumentation-key: ${rpe.AppInsightsInstrumentationKey:00000000-0000-0000-0000-000000000000}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.server.ResponseStatusException;
import uk.gov.hmcts.cp.openapi.model.CourtSchedule;
import uk.gov.hmcts.cp.openapi.model.CourtScheduleResponse;

import uk.gov.hmcts.cp.openapi.model.CourtSitting;
import uk.gov.hmcts.cp.openapi.model.Hearing;
import uk.gov.hmcts.cp.repositories.CourtScheduleRepository;
import uk.gov.hmcts.cp.repositories.InMemoryCourtScheduleRepositoryImpl;
import uk.gov.hmcts.cp.services.CaseUrnMapperService;
import uk.gov.hmcts.cp.services.CourtScheduleService;

import java.util.UUID;
Expand All @@ -33,7 +34,8 @@ class CourtScheduleControllerTest {
@BeforeEach
void setUp() {
CourtScheduleService courtScheduleService = new CourtScheduleService(new InMemoryCourtScheduleRepositoryImpl());
courtScheduleController = new CourtScheduleController(courtScheduleService);
CaseUrnMapperService caseUrnMapperService = new CaseUrnMapperService(new RestTemplate());
courtScheduleController = new CourtScheduleController(courtScheduleService, caseUrnMapperService);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ void setUp() {
@Test
void getCourtScheduleByCaseUrn_shouldReturnCourtScheduleResponse() {
UUID caseUrn = UUID.randomUUID();
CourtScheduleResponse response = courtScheduleRepository.getCourtScheduleByCaseUrn(caseUrn.toString());
CourtScheduleResponse response = courtScheduleRepository.getCourtScheduleByCaseId(caseUrn.toString());

assertNotNull(response.getCourtSchedule());
assertEquals(1, response.getCourtSchedule().size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ void shouldReturnStubbedCourtScheduleResponse_whenValidCaseUrnProvided() {
String validCaseUrn = "123-ABC-456";

// Act
CourtScheduleResponse response = courtScheduleService.getCourtScheduleByCaseUrn(validCaseUrn);
CourtScheduleResponse response = courtScheduleService.getCourtScheduleByCaseId(validCaseUrn);

// Assert
assertThat(response).isNotNull();
Expand All @@ -37,10 +37,10 @@ void shouldThrowBadRequestException_whenCaseUrnIsNull() {
String nullCaseUrn = null;

// Act & Assert
assertThatThrownBy(() -> courtScheduleService.getCourtScheduleByCaseUrn(nullCaseUrn))
assertThatThrownBy(() -> courtScheduleService.getCourtScheduleByCaseId(nullCaseUrn))
.isInstanceOf(ResponseStatusException.class)
.hasMessageContaining("400 BAD_REQUEST")
.hasMessageContaining("caseUrn is required");
.hasMessageContaining("caseId is required");
}

@Test
Expand All @@ -49,9 +49,9 @@ void shouldThrowBadRequestException_whenCaseUrnIsEmpty() {
String emptyCaseUrn = "";

// Act & Assert
assertThatThrownBy(() -> courtScheduleService.getCourtScheduleByCaseUrn(emptyCaseUrn))
assertThatThrownBy(() -> courtScheduleService.getCourtScheduleByCaseId(emptyCaseUrn))
.isInstanceOf(ResponseStatusException.class)
.hasMessageContaining("400 BAD_REQUEST")
.hasMessageContaining("caseUrn is required");
.hasMessageContaining("caseId is required");
}
}
Loading