Skip to content

Commit 316a6e4

Browse files
committed
update caseurnmapper to urnmapper
1 parent 05a5341 commit 316a6e4

File tree

7 files changed

+64
-59
lines changed

7 files changed

+64
-59
lines changed

src/integrationTest/java/uk/gov/hmcts/cp/config/TestConfig.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ public CourtScheduleService courtScheduleService() {
2121
@Bean("testCaseUrnMapperService")
2222
public CaseUrnMapperService testCaseUrnMapperService() {
2323
RestTemplate restTemplate = new RestTemplate();
24-
ObjectMapper objectMapper = new ObjectMapper();
25-
return new CaseUrnMapperService(restTemplate, objectMapper) {
24+
return new CaseUrnMapperService(restTemplate) {
2625
@Override
2726
public String getCaseMapperServiceUrl() {
2827
return "http://mock-server/test-mapper";

src/main/java/uk/gov/hmcts/cp/controllers/CourtScheduleController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ public ResponseEntity<CourtScheduleResponse> getCourtScheduleByCaseUrn(final Str
3131
final CourtScheduleResponse courtScheduleResponse;
3232
try {
3333
sanitizedCaseUrn = sanitizeString(caseUrn);
34-
courtScheduleResponse = courtScheduleService.getCourtScheduleByCaseId(
35-
caseUrnMapperService.getCaseId(sanitizedCaseUrn));
34+
String caseId = caseUrnMapperService.getCaseId(sanitizedCaseUrn);
35+
courtScheduleResponse = courtScheduleService.getCourtScheduleByCaseId(caseId);
3636
} catch (ResponseStatusException e) {
3737
LOG.atError().log(e.getMessage());
3838
throw e;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package uk.gov.hmcts.cp.domain;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Builder;
5+
import lombok.Getter;
6+
import lombok.NoArgsConstructor;
7+
8+
import java.io.Serializable;
9+
10+
@Builder
11+
@AllArgsConstructor
12+
@NoArgsConstructor
13+
@Getter
14+
public class CaseMapperResponse implements Serializable {
15+
16+
private static final long serialVersionUID = 1L;
17+
18+
private String caseId;
19+
20+
private String caseUrn;
21+
22+
}
Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,20 @@
11
package uk.gov.hmcts.cp.services;
22

3-
import com.fasterxml.jackson.core.JsonProcessingException;
4-
import com.fasterxml.jackson.databind.JsonNode;
5-
import com.fasterxml.jackson.databind.ObjectMapper;
63
import lombok.RequiredArgsConstructor;
7-
import org.apache.logging.log4j.util.Strings;
84
import org.slf4j.Logger;
95
import org.slf4j.LoggerFactory;
106
import org.springframework.beans.factory.annotation.Value;
117
import org.springframework.http.HttpEntity;
128
import org.springframework.http.HttpHeaders;
139
import org.springframework.http.HttpMethod;
14-
import org.springframework.http.HttpMethod;
10+
import org.springframework.http.HttpStatus;
11+
import org.springframework.http.MediaType;
1512
import org.springframework.http.ResponseEntity;
1613
import org.springframework.stereotype.Service;
1714
import org.springframework.web.client.RestTemplate;
15+
import org.springframework.web.server.ResponseStatusException;
1816
import org.springframework.web.util.UriComponentsBuilder;
19-
20-
import java.net.URI;
21-
22-
import static uk.gov.hmcts.cp.utils.Utils.ignoreCertificates;
17+
import uk.gov.hmcts.cp.domain.CaseMapperResponse;
2318

2419
import static uk.gov.hmcts.cp.utils.Utils.ignoreCertificates;
2520

@@ -28,44 +23,41 @@
2823
public class CaseUrnMapperService {
2924
private static final Logger LOG = LoggerFactory.getLogger(CaseUrnMapperService.class);
3025
private final RestTemplate restTemplate;
31-
private final ObjectMapper objectMapper;
3226

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

3630
public String getCaseId(final String caseUrn) {
3731
try {
3832
ignoreCertificates();
39-
ResponseEntity<String> responseEntity = restTemplate.exchange(
33+
ResponseEntity<CaseMapperResponse> responseEntity = restTemplate.exchange(
4034
getCaseIdUrl(caseUrn),
4135
HttpMethod.GET,
4236
getRequestEntity(),
43-
String.class
37+
CaseMapperResponse.class
4438
);
45-
return getCaseId(responseEntity);
39+
if (responseEntity.getStatusCode().is2xxSuccessful() && responseEntity.getBody() != null) {
40+
CaseMapperResponse body = responseEntity.getBody();
41+
return body.getCaseId();
42+
}
4643
} catch (Exception e) {
4744
LOG.atError().log("Error while getting case id from case urn", e);
4845
}
49-
return null;
46+
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Case not found by urn: " + caseUrn);
5047
}
5148

5249
public String getCaseMapperServiceUrl() {
53-
return this.caseMapperServiceUrl;
54-
}
55-
56-
public String getCaseId(ResponseEntity<String> responseEntity) throws JsonProcessingException {
57-
if (responseEntity != null || responseEntity.hasBody()) {
58-
JsonNode root = objectMapper.readTree(responseEntity.getBody());
59-
return root.get("caseId").asText();
50+
if (this.caseMapperServiceUrl != null) {
51+
return this.caseMapperServiceUrl;
6052
}
61-
return Strings.EMPTY;
53+
LOG.atError().log("caseMapperServiceUrl is empty");
54+
return "https://devcp01.ingress01.dev.nl.cjscp.org.uk/urnmapper";
6255
}
6356

6457
private String getCaseIdUrl(String caseUrn) {
6558
LOG.atDebug().log("Fetching case id for case urn: {}", caseUrn);
66-
//return URI.create(getCaseMapperServiceUrl() + "/" + caseUrn).toString();
6759
return UriComponentsBuilder
68-
.fromUri(URI.create(getCaseMapperServiceUrl()))
60+
.fromUriString(getCaseMapperServiceUrl())
6961
.pathSegment(caseUrn)
7062
.buildAndExpand(caseUrn)
7163
.toUriString();
@@ -74,7 +66,6 @@ private String getCaseIdUrl(String caseUrn) {
7466
private HttpEntity<String> getRequestEntity() {
7567
HttpHeaders headers = new HttpHeaders();
7668
headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
77-
//headers.set("Accept", "application/json, application/*+json");
7869
return new HttpEntity<>(headers);
7970
}
8071
}

src/pactVerificationTest/java/uk/gov/hmcts/cp/pact/provider/CourtScheduleProviderPactTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ void setupTarget(PactVerificationContext context) {
4848

4949
@State("court schedule for case 456789 exists")
5050
public void setupCourtSchedule() throws Exception{
51-
courtScheduleClient.clearAll();
52-
CourtScheduleResponse courtScheduleResponse = JsonFileToObject.readJsonFromResources("courtSchedule.json", CourtScheduleResponse.class);
53-
courtScheduleClient.saveCourtSchedule("456789", courtScheduleResponse);
51+
// courtScheduleClient.clearAll();
52+
// CourtScheduleResponse courtScheduleResponse = JsonFileToObject.readJsonFromResources("courtSchedule.json", CourtScheduleResponse.class);
53+
// courtScheduleClient.saveCourtSchedule("456789", courtScheduleResponse);
5454
}
5555

5656
@TestTemplate

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package uk.gov.hmcts.cp.controllers;
22

3-
import com.fasterxml.jackson.databind.ObjectMapper;
43
import org.junit.jupiter.api.BeforeEach;
54
import org.junit.jupiter.api.Test;
65
import org.slf4j.Logger;
@@ -34,7 +33,7 @@ class CourtScheduleControllerTest {
3433
@BeforeEach
3534
void setUp() {
3635
CourtScheduleService courtScheduleService = new CourtScheduleService(new InMemoryCourtScheduleClientImpl());
37-
CaseUrnMapperService testCaseUrnMapperService = new CaseUrnMapperService(new RestTemplate(), new ObjectMapper()) {
36+
CaseUrnMapperService testCaseUrnMapperService = new CaseUrnMapperService(new RestTemplate()) {
3837
@Override
3938
public String getCaseMapperServiceUrl() {
4039
return "http://mock-server/test-mapper";
Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
11
package uk.gov.hmcts.cp.services;
22

3-
import com.fasterxml.jackson.databind.ObjectMapper;
43
import org.junit.jupiter.api.BeforeEach;
54
import org.junit.jupiter.api.Test;
65
import org.springframework.http.HttpEntity;
76
import org.springframework.http.HttpMethod;
7+
import org.springframework.http.HttpStatus;
88
import org.springframework.http.ResponseEntity;
99
import org.springframework.web.client.RestTemplate;
10+
import org.springframework.web.server.ResponseStatusException;
11+
import uk.gov.hmcts.cp.domain.CaseMapperResponse;
1012

13+
import static org.assertj.core.api.Assertions.assertThat;
1114
import static org.junit.jupiter.api.Assertions.assertEquals;
12-
import static org.junit.jupiter.api.Assertions.assertNull;
15+
import static org.junit.jupiter.api.Assertions.assertThrows;
1316
import static org.mockito.ArgumentMatchers.any;
1417
import static org.mockito.ArgumentMatchers.anyString;
1518
import static org.mockito.ArgumentMatchers.eq;
1619
import static org.mockito.Mockito.mock;
1720
import static org.mockito.Mockito.when;
1821

19-
2022
class CaseUrnMapperServiceTest {
2123
private CaseUrnMapperService caseUrnMapperService;
2224

2325
private RestTemplate restTemplate;
24-
private ObjectMapper objectMapper;
2526

2627
private final String mockUrl = "http://mock-server/mapper";
2728

2829
@BeforeEach
2930
void setUp() {
3031
restTemplate = mock(RestTemplate.class);
31-
objectMapper = new ObjectMapper();
32-
caseUrnMapperService = new CaseUrnMapperService(restTemplate, objectMapper) {
32+
caseUrnMapperService = new CaseUrnMapperService(restTemplate) {
3333
@Override
3434
public String getCaseMapperServiceUrl() {
3535
return mockUrl ;
@@ -42,26 +42,16 @@ void shouldReturnCaseIdWhenResponseIsSuccessful() {
4242
String caseUrn = "test-case-urn";
4343
String caseId = "7a2e94c4-38af-43dd-906b-40d632d159b0";
4444

45-
String json = """
46-
{
47-
"caseId": "7a2e94c4-38af-43dd-906b-40d632d159b0",
48-
"caseUrn": "28DI1953715",
49-
"originalResponse": {
50-
"mappingId": "818284bf-a1ed-464b-bc1a-b0298cf1ead1",
51-
"sourceId": "28DI1953715",
52-
"sourceType": "OU_URN",
53-
"targetId": "7a2e94c4-38af-43dd-906b-40d632d159b0",
54-
"targetType": "CASE_FILE_ID",
55-
"createdAt": "2025-07-31T10:07:46.578Z"
56-
}
57-
}
58-
""";
59-
ResponseEntity<String> responseEntity = ResponseEntity.ok(json);
45+
CaseMapperResponse response = CaseMapperResponse.builder()
46+
.caseId(caseId)
47+
.caseUrn(caseUrn)
48+
.build();
49+
ResponseEntity<CaseMapperResponse> responseEntity = ResponseEntity.ok(response);
6050
when(restTemplate.exchange(
6151
eq(mockUrl + "/" + caseUrn),
6252
eq(HttpMethod.GET),
6353
any(HttpEntity.class),
64-
eq(String.class)
54+
eq(CaseMapperResponse.class)
6555
)).thenReturn(responseEntity);
6656

6757
String result = caseUrnMapperService.getCaseId(caseUrn);
@@ -77,10 +67,14 @@ void shouldReturnNullWhenExceptionOccurs() {
7767
anyString(),
7868
eq(HttpMethod.GET),
7969
any(HttpEntity.class),
80-
eq(String.class)
70+
eq(CaseMapperResponse.class)
8171
)).thenThrow(new RuntimeException("Test exception"));
8272

83-
String result = caseUrnMapperService.getCaseId(caseUrn);
84-
assertNull(result);
73+
ResponseStatusException exception = assertThrows(ResponseStatusException.class, () -> {
74+
caseUrnMapperService.getCaseId(caseUrn);
75+
});
76+
assertThat(exception.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
77+
assertThat(exception.getReason()).isEqualTo("Case not found by urn: test-case-urn");
78+
assertThat(exception.getMessage()).isEqualTo("404 NOT_FOUND \"Case not found by urn: test-case-urn\"");
8579
}
8680
}

0 commit comments

Comments
 (0)