Skip to content

Commit b51e453

Browse files
authored
Dev/mapper service addition (#56)
* addition of case urn mapper service * addition of case urn mapper service * refactor the case mapper urn service
1 parent 992e957 commit b51e453

File tree

12 files changed

+127
-29
lines changed

12 files changed

+127
-29
lines changed

src/integrationTest/java/uk/gov/hmcts/cp/controllers/CourtScheduleControllerIT.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515

1616
import java.util.UUID;
1717

18-
import static org.junit.jupiter.api.Assertions.*;
18+
import static org.junit.jupiter.api.Assertions.assertEquals;
19+
import static org.junit.jupiter.api.Assertions.assertNotNull;
20+
import static org.junit.jupiter.api.Assertions.assertTrue;
1921
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
2022
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
2123

src/main/java/uk/gov/hmcts/cp/config/OpenAPIConfiguration.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import io.swagger.v3.oas.models.OpenAPI;
44
import org.springframework.context.annotation.Bean;
55
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.web.client.RestTemplate;
67

78
@Configuration
89
public class OpenAPIConfiguration {
@@ -13,4 +14,9 @@ public class OpenAPIConfiguration {
1314
public OpenAPI openAPI() {
1415
return openAPIConfigLoader.openAPI();
1516
}
17+
18+
@Bean
19+
public RestTemplate restTemplate() {
20+
return new RestTemplate();
21+
}
1622
}
Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,38 @@
11
package uk.gov.hmcts.cp.controllers;
22

3-
import org.apache.commons.text.StringEscapeUtils;
43
import org.slf4j.Logger;
54
import org.slf4j.LoggerFactory;
6-
import org.springframework.http.HttpStatus;
75
import org.springframework.http.MediaType;
86
import org.springframework.http.ResponseEntity;
97
import org.springframework.web.bind.annotation.RestController;
108
import org.springframework.web.server.ResponseStatusException;
119
import uk.gov.hmcts.cp.openapi.api.CourtScheduleApi;
1210
import uk.gov.hmcts.cp.openapi.model.CourtScheduleResponse;
11+
import uk.gov.hmcts.cp.services.CaseUrnMapperService;
1312
import uk.gov.hmcts.cp.services.CourtScheduleService;
1413

14+
import static uk.gov.hmcts.cp.utils.Utils.sanitizeString;
15+
1516
@RestController
1617
public class CourtScheduleController implements CourtScheduleApi {
1718
private static final Logger LOG = LoggerFactory.getLogger(CourtScheduleController.class);
1819
private final CourtScheduleService courtScheduleService;
20+
private final CaseUrnMapperService caseUrnMapperService;
1921

20-
public CourtScheduleController(final CourtScheduleService courtScheduleService) {
22+
public CourtScheduleController(final CourtScheduleService courtScheduleService,
23+
final CaseUrnMapperService caseUrnMapperService) {
2124
this.courtScheduleService = courtScheduleService;
25+
this.caseUrnMapperService = caseUrnMapperService;
2226
}
2327

2428
@Override
2529
public ResponseEntity<CourtScheduleResponse> getCourtScheduleByCaseUrn(final String caseUrn) {
2630
final String sanitizedCaseUrn;
2731
final CourtScheduleResponse courtScheduleResponse;
2832
try {
29-
sanitizedCaseUrn = sanitizeCaseUrn(caseUrn);
30-
courtScheduleResponse = courtScheduleService.getCourtScheduleByCaseUrn(sanitizedCaseUrn);
33+
sanitizedCaseUrn = sanitizeString(caseUrn);
34+
courtScheduleResponse = courtScheduleService.getCourtScheduleByCaseId(
35+
caseUrnMapperService.getCaseId(sanitizedCaseUrn));
3136
} catch (ResponseStatusException e) {
3237
LOG.atError().log(e.getMessage());
3338
throw e;
@@ -37,11 +42,4 @@ public ResponseEntity<CourtScheduleResponse> getCourtScheduleByCaseUrn(final Str
3742
.contentType(MediaType.APPLICATION_JSON)
3843
.body(courtScheduleResponse);
3944
}
40-
41-
private String sanitizeCaseUrn(final String urn) {
42-
if (urn == null) {
43-
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "caseUrn is required");
44-
}
45-
return StringEscapeUtils.escapeHtml4(urn);
46-
}
4745
}

src/main/java/uk/gov/hmcts/cp/repositories/CourtScheduleRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
@Repository
77
public interface CourtScheduleRepository {
88

9-
CourtScheduleResponse getCourtScheduleByCaseUrn(String caseUrn);
9+
CourtScheduleResponse getCourtScheduleByCaseId(String caseUrn);
1010
void saveCourtSchedule(String caseUrn, CourtScheduleResponse courtScheduleResponse);
1111
void clearAll();
1212

src/main/java/uk/gov/hmcts/cp/repositories/InMemoryCourtScheduleRepositoryImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public void saveCourtSchedule(final String caseUrn, final CourtScheduleResponse
2323
courtScheduleResponseMap.put(caseUrn, courtScheduleResponse);
2424
}
2525

26-
public CourtScheduleResponse getCourtScheduleByCaseUrn(final String caseUrn) {
26+
public CourtScheduleResponse getCourtScheduleByCaseId(final String caseUrn) {
2727
if (!courtScheduleResponseMap.containsKey(caseUrn)) {
2828
saveCourtSchedule(caseUrn, createCourtScheduleResponse());
2929
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package uk.gov.hmcts.cp.services;
2+
3+
import lombok.RequiredArgsConstructor;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
import org.springframework.beans.factory.annotation.Value;
7+
import org.springframework.http.HttpEntity;
8+
import org.springframework.http.HttpHeaders;
9+
import org.springframework.http.MediaType;
10+
import org.springframework.stereotype.Service;
11+
import org.springframework.web.client.RestTemplate;
12+
import org.springframework.web.util.UriComponentsBuilder;
13+
14+
import java.net.URI;
15+
import java.util.List;
16+
import java.util.Map;
17+
import java.util.UUID;
18+
import java.util.concurrent.ConcurrentHashMap;
19+
20+
@Service
21+
@RequiredArgsConstructor
22+
public class CaseUrnMapperService {
23+
24+
private static final Logger LOG = LoggerFactory.getLogger(CaseUrnMapperService.class);
25+
26+
private final RestTemplate restTemplate;
27+
28+
@Value("${service.case-mapper-service.url}")
29+
private String caseMapperServiceUrl;
30+
31+
private static final String CASEURN_ID = "caseurn/{caseurn}";
32+
33+
private final Map<String, String> caseUrnToCaseIdMap = new ConcurrentHashMap<>();
34+
35+
36+
37+
public String getCaseId(final String caseUrn) {
38+
return UUID.randomUUID().toString();
39+
// This below code is commented out as it is will be used when the case mapper service is available.
40+
41+
/* try {
42+
ResponseEntity<String> responseEntity = restTemplate.exchange(
43+
getCaseIdUrl(caseUrn),
44+
HttpMethod.GET,
45+
getRequestEntity(),
46+
String.class
47+
);
48+
return responseEntity.hasBody() ? responseEntity.getBody(): Strings.EMPTY;
49+
} catch (Exception e) {
50+
LOG.atError().log("Error while getting case id from case urn", e);
51+
}
52+
return null;*/
53+
}
54+
55+
private String getCaseIdUrl(String caseUrn) {
56+
return UriComponentsBuilder
57+
.fromUri(URI.create(caseMapperServiceUrl))
58+
.pathSegment(caseUrn)
59+
.buildAndExpand(caseUrn)
60+
.toUriString();
61+
}
62+
63+
private HttpEntity<String> getRequestEntity() {
64+
HttpHeaders headers = new HttpHeaders();
65+
headers.setAccept(List.of(MediaType.APPLICATION_JSON));
66+
return new HttpEntity<>(headers);
67+
}
68+
}

src/main/java/uk/gov/hmcts/cp/services/CourtScheduleService.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import uk.gov.hmcts.cp.openapi.model.CourtScheduleResponse;
1111
import uk.gov.hmcts.cp.repositories.CourtScheduleRepository;
1212

13+
import static uk.gov.hmcts.cp.utils.Utils.sanitizeString;
14+
1315
@Service
1416
@RequiredArgsConstructor
1517
public class CourtScheduleService {
@@ -18,13 +20,13 @@ public class CourtScheduleService {
1820

1921
private final CourtScheduleRepository courtScheduleRepository;
2022

21-
public CourtScheduleResponse getCourtScheduleByCaseUrn(final String caseUrn) throws ResponseStatusException {
22-
if (StringUtils.isEmpty(caseUrn)) {
23-
LOG.atWarn().log("No case urn provided");
24-
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "caseUrn is required");
23+
public CourtScheduleResponse getCourtScheduleByCaseId(final String caseId) throws ResponseStatusException {
24+
if (StringUtils.isEmpty(caseId)) {
25+
LOG.atWarn().log("No case Id provided");
26+
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "caseId is required");
2527
}
26-
LOG.atWarn().log("NOTE: System configured to return stubbed Court Schedule details. Ignoring provided caseUrn : {}", caseUrn);
27-
final CourtScheduleResponse stubbedCourtScheduleResponse = courtScheduleRepository.getCourtScheduleByCaseUrn(caseUrn);
28+
LOG.atWarn().log("NOTE: System configured to return stubbed Court Schedule details. Ignoring provided caseId : {}", sanitizeString(caseId));
29+
final CourtScheduleResponse stubbedCourtScheduleResponse = courtScheduleRepository.getCourtScheduleByCaseId(caseId);
2830
LOG.atDebug().log("Court Schedule response: {}", stubbedCourtScheduleResponse);
2931
return stubbedCourtScheduleResponse;
3032
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package uk.gov.hmcts.cp.utils;
2+
3+
import org.apache.commons.text.StringEscapeUtils;
4+
import org.springframework.http.HttpStatus;
5+
import org.springframework.web.server.ResponseStatusException;
6+
7+
public class Utils {
8+
9+
public static String sanitizeString(final String urn) {
10+
if (urn == null) {
11+
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "caseUrn is required");
12+
}
13+
return StringEscapeUtils.escapeHtml4(urn);
14+
}
15+
16+
}

src/main/resources/application.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ spring:
3030
# key: ${COSMOSDB_KEY}
3131
# database: ${COSMOSDB_DATABASE}
3232

33+
service:
34+
case-mapper-service:
35+
url: https://virtserver.swaggerhub.com/HMCTS-DTS/api-cp-refdata-case-mapper
36+
3337
azure:
3438
application-insights:
3539
instrumentation-key: ${rpe.AppInsightsInstrumentationKey:00000000-0000-0000-0000-000000000000}

src/test/java/uk/gov/hmcts/cp/controllers/CourtScheduleControllerTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@
66
import org.slf4j.LoggerFactory;
77
import org.springframework.http.HttpStatus;
88
import org.springframework.http.ResponseEntity;
9+
import org.springframework.web.client.RestTemplate;
910
import org.springframework.web.server.ResponseStatusException;
1011
import uk.gov.hmcts.cp.openapi.model.CourtSchedule;
1112
import uk.gov.hmcts.cp.openapi.model.CourtScheduleResponse;
1213

1314
import uk.gov.hmcts.cp.openapi.model.CourtSitting;
1415
import uk.gov.hmcts.cp.openapi.model.Hearing;
15-
import uk.gov.hmcts.cp.repositories.CourtScheduleRepository;
1616
import uk.gov.hmcts.cp.repositories.InMemoryCourtScheduleRepositoryImpl;
17+
import uk.gov.hmcts.cp.services.CaseUrnMapperService;
1718
import uk.gov.hmcts.cp.services.CourtScheduleService;
1819

1920
import java.util.UUID;
@@ -33,7 +34,8 @@ class CourtScheduleControllerTest {
3334
@BeforeEach
3435
void setUp() {
3536
CourtScheduleService courtScheduleService = new CourtScheduleService(new InMemoryCourtScheduleRepositoryImpl());
36-
courtScheduleController = new CourtScheduleController(courtScheduleService);
37+
CaseUrnMapperService caseUrnMapperService = new CaseUrnMapperService(new RestTemplate());
38+
courtScheduleController = new CourtScheduleController(courtScheduleService, caseUrnMapperService);
3739
}
3840

3941
@Test

0 commit comments

Comments
 (0)