Skip to content

Commit 41387a5

Browse files
AMP-167 - addressed review comments.
1 parent 0674193 commit 41387a5

23 files changed

+107
-161
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ apply {
3232
}
3333

3434
dependencies {
35-
implementation(group: 'uk.gov.hmcts.cp', name: 'api-cp-crime-hearing-case-event-subscription', version: '1.0.2')
35+
implementation(group: 'uk.gov.hmcts.cp', name: 'api-cp-crime-hearing-case-event-subscription', version: '1.0.4')
3636

3737
// This is proving to be a real puzzle. This is actually included in the api published pom
3838
// ( though with scope of "runtime" )

docs/debugging.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ export PORT=4550
66
export HOST="localhost:$PORT"
77
curl -XPOST http://$HOST/client-subscriptions \
88
-H "Content-Type: application/json" \
9-
-d '{"eventTypes":["PCR","CUSTODIAL_RESULT"],"notificationEndpoint":{"webhookUrl":"https://my-callback-url"}}'
9+
-d '{"eventTypes":["PRISON_COURT_REGISTER_GENERATED","CUSTODIAL_RESULT"],"notificationEndpoint":{"webhookUrl":"https://my-callback-url"}}'
1010
```
1111

1212
... Creates and returns a subscription with a clientSubscriptionId
1313
ie.
1414
```
1515
{"clientSubscriptionId":"585fde62-44df-4b3d-ac38-5e1f4d7669e3",
1616
"createdAt":"2025-12-19T15:16:38.456506Z",
17-
"eventTypes":["CUSTODIAL_RESULT","PCR"],
17+
"eventTypes":["CUSTODIAL_RESULT","PRISON_COURT_REGISTER_GENERATED"],
1818
"notificationEndpoint":{"webhookUrl":"https://my-callback-url"},"updatedAt":"2025-12-19T15:16:38.456772Z"}
1919
```
2020

src/apiTest/java/uk/gov/hmcts/cp/subscription/http/SubscriptionApiTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ void round_trip_subscription_should_work_ok() throws InterruptedException {
2727
final String postUrl = String.format("%s/client-subscriptions", baseUrl);
2828
final HttpHeaders headers = new HttpHeaders();
2929
headers.setContentType(MediaType.APPLICATION_JSON);
30-
final String body = "{\"eventTypes\":[\"PCR\",\"CUSTODIAL_RESULT\"],\n" +
30+
final String body = "{\"eventTypes\":[\"PRISON_COURT_REGISTER_GENERATED\",\"CUSTODIAL_RESULT\"],\n" +
3131
"\"notificationEndpoint\":{\"webhookUrl\":\"https://my-callback-url\"}}";
3232
final ResponseEntity<String> postResult = http.exchange(
3333
postUrl,

src/main/java/uk/gov/hmcts/cp/subscription/controllers/GlobalExceptionHandler.java

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

3+
import feign.FeignException;
34
import jakarta.persistence.EntityNotFoundException;
45
import lombok.extern.slf4j.Slf4j;
56
import org.springframework.http.HttpStatus;
@@ -37,4 +38,16 @@ public ResponseEntity<String> handleUnknownException(final Exception exception)
3738
.status(HttpStatus.INTERNAL_SERVER_ERROR)
3839
.body(exception.getMessage());
3940
}
41+
42+
@ExceptionHandler(FeignException.class)
43+
public ResponseEntity<String> handleFeignException(FeignException ex) {
44+
log.error("FeignException from downstream service", ex);
45+
HttpStatus status = HttpStatus.resolve(ex.status());
46+
if (status == null) {
47+
status = HttpStatus.BAD_GATEWAY;
48+
}
49+
return ResponseEntity
50+
.status(status)
51+
.body(ex.contentUTF8());
52+
}
4053
}

src/main/java/uk/gov/hmcts/cp/subscription/controllers/NotificationController.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,22 @@
55
import lombok.extern.slf4j.Slf4j;
66
import org.springframework.http.HttpStatus;
77
import org.springframework.http.ResponseEntity;
8-
import org.springframework.web.bind.annotation.PathVariable;
98
import org.springframework.web.bind.annotation.PostMapping;
109
import org.springframework.web.bind.annotation.RequestBody;
1110
import org.springframework.web.bind.annotation.RestController;
1211
import uk.gov.hmcts.cp.openapi.model.PcrEventPayload;
1312
import uk.gov.hmcts.cp.subscription.services.NotificationService;
1413

15-
import java.util.UUID;
16-
1714
@RestController
1815
@RequiredArgsConstructor
1916
@Slf4j
2017
public class NotificationController {
2118

2219
private final NotificationService notificationService;
2320

24-
@PostMapping("/notifications/pcr/{materialId}")
25-
public ResponseEntity<PcrEventPayload> notificationPCR(@PathVariable UUID materialId, @RequestBody @Valid PcrEventPayload pcrEventPayload) {
26-
27-
notificationService.processPcrEvent(materialId);
28-
return new ResponseEntity<>(pcrEventPayload, HttpStatus.CREATED);
21+
@PostMapping("/notifications/pcr")
22+
public ResponseEntity<PcrEventPayload> notificationPCR(@RequestBody @Valid final PcrEventPayload pcrEventPayload) {
23+
notificationService.processPcrEvent(pcrEventPayload.getMaterialId());
24+
return new ResponseEntity<>(HttpStatus.ACCEPTED);
2925
}
3026
}

src/main/java/uk/gov/hmcts/cp/subscription/mappers/SubscriptionMapper.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ public interface SubscriptionMapper {
3434
ClientSubscriptionEntity mapUpdateRequestToEntity(@Context ClockService clockService, ClientSubscriptionEntity existing, ClientSubscriptionRequest request);
3535

3636
@Mapping(source = "id", target = "clientSubscriptionId")
37-
ClientSubscription mapEntityToResponse(ClientSubscriptionEntity entity);
37+
@Mapping(target = "createdAt", expression = "java(clockService.now().toInstant())")
38+
@Mapping(target = "updatedAt", expression = "java(clockService.now().toInstant())")
39+
ClientSubscription mapEntityToResponse(@Context ClockService clockService, ClientSubscriptionEntity entity);
3840

3941
@Named("mapWithSortedEventTypes")
4042
static List<EntityEventType> sortedEventTypes(final List<EventType> events) {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package uk.gov.hmcts.cp.subscription.model;
22

33
public enum EntityEventType {
4-
PCR,
4+
PRISON_COURT_REGISTER_GENERATED,
55
CUSTODIAL_RESULT
66
}
Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package uk.gov.hmcts.cp.subscription.services;
22

3-
import feign.FeignException;
43
import lombok.RequiredArgsConstructor;
54
import lombok.extern.slf4j.Slf4j;
65
import org.springframework.stereotype.Service;
@@ -15,13 +14,7 @@ public class NotificationService {
1514

1615
private final MaterialClient client;
1716

18-
public void processPcrEvent(final UUID materialId) {
19-
try {
20-
client.getContentById(materialId);
21-
log.info("Successfully received document.");
22-
} catch (FeignException ex) {
23-
log.error("Failed to get document.", ex);
24-
throw ex;
25-
}
17+
public byte[] processPcrEvent(final UUID materialId) {
18+
return client.getContentById(materialId);
2619
}
2720
}

src/main/java/uk/gov/hmcts/cp/subscription/services/SubscriptionService.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,18 @@ public class SubscriptionService {
2222

2323
public ClientSubscription saveSubscription(final ClientSubscriptionRequest request) {
2424
final ClientSubscriptionEntity entity = mapper.mapCreateRequestToEntity(clockService, request);
25-
return mapper.mapEntityToResponse(subscriptionRepository.save(entity));
25+
return mapper.mapEntityToResponse(clockService, subscriptionRepository.save(entity));
2626
}
2727

2828
public ClientSubscription updateSubscription(final UUID clientSubscriptionId, final ClientSubscriptionRequest request) {
2929
final ClientSubscriptionEntity existing = subscriptionRepository.getReferenceById(clientSubscriptionId);
3030
final ClientSubscriptionEntity entity = mapper.mapUpdateRequestToEntity(clockService, existing, request);
31-
return mapper.mapEntityToResponse(subscriptionRepository.save(entity));
31+
return mapper.mapEntityToResponse(clockService, subscriptionRepository.save(entity));
3232
}
3333

3434
public ClientSubscription getSubscription(final UUID clientSubscriptionId) {
3535
final ClientSubscriptionEntity entity = subscriptionRepository.getReferenceById(clientSubscriptionId);
36-
return mapper.mapEntityToResponse(entity);
36+
return mapper.mapEntityToResponse(clockService, entity);
3737
}
3838

3939
public void deleteSubscription(final UUID clientSubscriptionId) {

src/test/java/uk/gov/hmcts/cp/subscription/config/TestContainersInitialise.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
@Slf4j
1111
public class TestContainersInitialise implements ApplicationContextInitializer<ConfigurableApplicationContext> {
1212

13-
private static final PostgreSQLContainer postgreSQLContainer = new PostgreSQLContainer(
13+
private final PostgreSQLContainer<?> postgreSQLContainer = new PostgreSQLContainer(
1414
"postgres")
1515
.withDatabaseName("appdb")
1616
.withUsername("postgres")

0 commit comments

Comments
 (0)