Skip to content

Commit f4f0635

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

File tree

9 files changed

+79
-51
lines changed

9 files changed

+79
-51
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
}

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,19 @@
1010
import org.springframework.web.server.ResponseStatusException;
1111
import uk.gov.hmcts.cp.openapi.api.CourtScheduleApi;
1212
import uk.gov.hmcts.cp.openapi.model.CourtScheduleResponse;
13+
import uk.gov.hmcts.cp.services.CaseUrnMapperService;
1314
import uk.gov.hmcts.cp.services.CourtScheduleService;
1415

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
@@ -27,7 +31,8 @@ public ResponseEntity<CourtScheduleResponse> getCourtScheduleByCaseUrn(final Str
2731
final CourtScheduleResponse courtScheduleResponse;
2832
try {
2933
sanitizedCaseUrn = sanitizeCaseUrn(caseUrn);
30-
courtScheduleResponse = courtScheduleService.getCourtScheduleByCaseUrn(sanitizedCaseUrn);
34+
courtScheduleResponse = courtScheduleService.getCourtScheduleByCaseUrn(
35+
caseUrnMapperService.getCaseId(sanitizedCaseUrn));
3136
} catch (ResponseStatusException e) {
3237
LOG.atError().log(e.getMessage());
3338
throw e;

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);
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+
}

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: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,18 @@
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;
18+
import uk.gov.hmcts.cp.services.CaseUrnMapperServiceImpl;
1719
import uk.gov.hmcts.cp.services.CourtScheduleService;
20+
import uk.gov.hmcts.cp.services.InMemoryCaseUrnMapper;
1821

1922
import java.util.UUID;
2023

@@ -33,7 +36,8 @@ class CourtScheduleControllerTest {
3336
@BeforeEach
3437
void setUp() {
3538
CourtScheduleService courtScheduleService = new CourtScheduleService(new InMemoryCourtScheduleRepositoryImpl());
36-
courtScheduleController = new CourtScheduleController(courtScheduleService);
39+
CaseUrnMapperService caseUrnMapperService = new InMemoryCaseUrnMapper();
40+
courtScheduleController = new CourtScheduleController(courtScheduleService, caseUrnMapperService);
3741
}
3842

3943
@Test

0 commit comments

Comments
 (0)