Skip to content

Commit 55d9a19

Browse files
committed
addition of case urn mapper service
1 parent cdbdfa5 commit 55d9a19

File tree

15 files changed

+114
-75
lines changed

15 files changed

+114
-75
lines changed
Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
11
package uk.gov.hmcts.cp.config;
22

3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
import uk.gov.hmcts.cp.services.CaseUrnMapperService;
6+
import uk.gov.hmcts.cp.services.InMemoryCaseUrnMapper;
7+
8+
@Configuration
39
public class TestConfig {
4-
}
10+
11+
@Bean
12+
public CaseUrnMapperService caseUrnMapperService() {
13+
return new InMemoryCaseUrnMapper();
14+
}
15+
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,20 @@
22

33
import com.fasterxml.jackson.databind.JsonNode;
44
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import org.junit.jupiter.api.BeforeEach;
56
import org.junit.jupiter.api.Test;
67
import org.junit.jupiter.api.extension.ExtendWith;
78
import org.slf4j.Logger;
89
import org.slf4j.LoggerFactory;
910
import org.springframework.beans.factory.annotation.Autowired;
1011
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
1112
import org.springframework.boot.test.context.SpringBootTest;
13+
import org.springframework.context.annotation.Import;
1214
import org.springframework.http.MediaType;
1315
import org.springframework.test.context.junit.jupiter.SpringExtension;
1416
import org.springframework.test.web.servlet.MockMvc;
17+
import uk.gov.hmcts.cp.config.TestConfig;
18+
import uk.gov.hmcts.cp.services.InMemoryCaseUrnMapper;
1519

1620
import java.util.UUID;
1721

@@ -22,12 +26,22 @@
2226
@ExtendWith(SpringExtension.class)
2327
@SpringBootTest
2428
@AutoConfigureMockMvc
29+
@Import(TestConfig.class)
2530
class CourtScheduleControllerIT {
2631
private static final Logger log = LoggerFactory.getLogger(CourtScheduleControllerIT.class);
2732

2833
@Autowired
2934
private MockMvc mockMvc;
3035

36+
@Autowired
37+
private InMemoryCaseUrnMapper inMemoryCaseUrnMapper;
38+
39+
@BeforeEach
40+
void setUp() {
41+
inMemoryCaseUrnMapper.clearAllMappings();
42+
inMemoryCaseUrnMapper.saveCaseUrnMapping("test-case-urn", "test-case-id");
43+
}
44+
3145
@Test
3246
void shouldReturnOkWhenValidUrnIsProvided() throws Exception {
3347
String caseUrn = "test-case-urn";

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
}

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

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -17,45 +17,7 @@
1717
import java.net.URI;
1818
import java.util.List;
1919

20-
@Service
21-
@RequiredArgsConstructor
22-
public class CaseUrnMapperServiceImpl {
2320

24-
private static final Logger LOG = LoggerFactory.getLogger(CaseUrnMapperServiceImpl.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-
public String getCaseId(final String caseUrn) {
34-
try {
35-
ResponseEntity<String> responseEntity = restTemplate.exchange(
36-
getCaseIdUrl(caseUrn),
37-
HttpMethod.GET,
38-
getRequestEntity(),
39-
String.class
40-
);
41-
return responseEntity.hasBody() ? responseEntity.getBody(): Strings.EMPTY;
42-
} catch (Exception e) {
43-
LOG.atError().log("Error while getting case id from case urn", e);
44-
}
45-
return null;
46-
}
47-
48-
private String getCaseIdUrl(String caseUrn) {
49-
return UriComponentsBuilder
50-
.fromUri(URI.create(caseMapperServiceUrl))
51-
.pathSegment(caseUrn)
52-
.buildAndExpand(caseUrn)
53-
.toUriString();
54-
}
55-
56-
private HttpEntity<String> getRequestEntity() {
57-
HttpHeaders headers = new HttpHeaders();
58-
headers.setAccept(List.of(MediaType.APPLICATION_JSON));
59-
return new HttpEntity<>(headers);
60-
}
21+
public interface CaseUrnMapperService {
22+
public String getCaseId(final String caseUrn) ;
6123
}

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919

2020
@Service
2121
@RequiredArgsConstructor
22-
public class CaseUrnMapperService {
22+
public class CaseUrnMapperServiceImpl implements CaseUrnMapperService {
2323

24-
private static final Logger LOG = LoggerFactory.getLogger(CaseUrnMapperService.class);
24+
private static final Logger LOG = LoggerFactory.getLogger(CaseUrnMapperServiceImpl.class);
2525

2626
private final RestTemplate restTemplate;
2727

@@ -30,6 +30,7 @@ public class CaseUrnMapperService {
3030

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

33+
@Override
3334
public String getCaseId(final String caseUrn) {
3435
try {
3536
ResponseEntity<String> responseEntity = restTemplate.exchange(
@@ -45,15 +46,15 @@ public String getCaseId(final String caseUrn) {
4546
return null;
4647
}
4748

48-
protected String getCaseIdUrl(String caseUrn) {
49+
private String getCaseIdUrl(String caseUrn) {
4950
return UriComponentsBuilder
5051
.fromUri(URI.create(caseMapperServiceUrl))
5152
.pathSegment(caseUrn)
5253
.buildAndExpand(caseUrn)
5354
.toUriString();
5455
}
5556

56-
protected HttpEntity<String> getRequestEntity() {
57+
private HttpEntity<String> getRequestEntity() {
5758
HttpHeaders headers = new HttpHeaders();
5859
headers.setAccept(List.of(MediaType.APPLICATION_JSON));
5960
return new HttpEntity<>(headers);

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: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,25 @@
11
package uk.gov.hmcts.cp.services;
22

3-
public class InMemoryCaseUrnMapper {
4-
}
3+
import org.springframework.stereotype.Component;
4+
5+
import java.util.Map;
6+
import java.util.concurrent.ConcurrentHashMap;
7+
8+
@Component
9+
public class InMemoryCaseUrnMapper implements CaseUrnMapperService {
10+
11+
private final Map<String, String> caseUrnToCaseIdMap = new ConcurrentHashMap<>();
12+
13+
public void saveCaseUrnMapping(final String caseUrn, final String caseId) {
14+
caseUrnToCaseIdMap.put(caseUrn, caseId);
15+
}
16+
17+
@Override
18+
public String getCaseId(final String caseUrn) {
19+
return caseUrnToCaseIdMap.getOrDefault(caseUrn, "default-case-id");
20+
}
21+
22+
public void clearAllMappings() {
23+
caseUrnToCaseIdMap.clear();
24+
}
25+
}

0 commit comments

Comments
 (0)