Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ apply {
}

dependencies {
implementation('uk.gov.hmcts.cp:api-cp-crime-hearing-case-event-subscription:1.1.4')
implementation('uk.gov.hmcts.cp:api-cp-crime-hearing-case-event-subscription:1.1.5')

// This is proving to be a real puzzle. This is actually included in the api published pom
// ( though with scope of "runtime" )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ public interface SubscriptionMapper {
@Mapping(source = "id", target = "clientSubscriptionId")
@Mapping(target = "createdAt", expression = "java(entity.getCreatedAt().toInstant())")
@Mapping(target = "updatedAt", expression = "java(entity.getUpdatedAt().toInstant())")
@Mapping(target = "keyId", ignore = true) //TBD
@Mapping(target = "secret", ignore = true) //TBD
@Mapping(target = "hmac", ignore = true)
ClientSubscription mapEntityToResponse(ClientSubscriptionEntity entity);

@Named("mapWithSortedEventTypes")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.server.ResponseStatusException;
import uk.gov.hmcts.cp.hmac.services.HmacKeyService;
import uk.gov.hmcts.cp.hmac.services.HmacKeyStore;
import uk.gov.hmcts.cp.openapi.model.ClientSubscription;
import uk.gov.hmcts.cp.openapi.model.ClientSubscriptionRequest;
import uk.gov.hmcts.cp.openapi.model.HmacCredentials;
import uk.gov.hmcts.cp.subscription.entities.ClientSubscriptionEntity;
import uk.gov.hmcts.cp.subscription.mappers.SubscriptionMapper;
import uk.gov.hmcts.cp.subscription.model.EntityEventType;
Expand All @@ -24,6 +27,7 @@ public class SubscriptionService {
private final ClockService clockService;
private final SubscriptionRepository subscriptionRepository;
private final SubscriptionMapper mapper;
private final HmacKeyStore hmacKeyStore;

@Transactional
public ClientSubscription saveSubscription(final ClientSubscriptionRequest request, final UUID clientId) {
Expand All @@ -33,7 +37,11 @@ public ClientSubscription saveSubscription(final ClientSubscriptionRequest reque
});
ClientSubscriptionEntity entity = mapper.mapCreateRequestToEntity(request, clockService.nowOffsetUTC());
entity = entity.toBuilder().clientId(clientId).build();
return mapper.mapEntityToResponse(subscriptionRepository.save(entity));
entity = subscriptionRepository.save(entity);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we prefer immutable objects ... so think would be better to not have line 40 / 43

  1. pass clientId into the builder rather than adding it in a second operation
  2. and get the saved entity back into a new object like 'saved" rather than overwriting the current object.
  3. pass the hmac into the mapper.mapEntityToResponse(entity) so dont need to do line43 updating the response

final ClientSubscription response = mapper.mapEntityToResponse(entity);
final HmacKeyService.KeyPair keyPair = hmacKeyStore.generateAndStore(entity.getId());
response.setHmac(HmacCredentials.builder().keyId(keyPair.keyId()).secret(keyPair.secret()).build());
return response;
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.http.HttpStatus;
import org.springframework.web.server.ResponseStatusException;
import uk.gov.hmcts.cp.hmac.services.HmacKeyService;
import uk.gov.hmcts.cp.hmac.services.HmacKeyStore;
import uk.gov.hmcts.cp.openapi.model.ClientSubscription;
import uk.gov.hmcts.cp.openapi.model.ClientSubscriptionRequest;
import uk.gov.hmcts.cp.openapi.model.NotificationEndpoint;
Expand Down Expand Up @@ -38,6 +40,8 @@ class SubscriptionServiceTest {
SubscriptionRepository subscriptionRepository;
@Mock
SubscriptionMapper mapper;
@Mock
HmacKeyStore hmacKeyStore;
@InjectMocks
SubscriptionService subscriptionService;

Expand Down Expand Up @@ -69,11 +73,15 @@ void save_request_should_save_new_entity() {
when(clockService.nowOffsetUTC()).thenReturn(now);
when(mapper.mapCreateRequestToEntity(createRequest, now)).thenReturn(requestEntity);
when(subscriptionRepository.save(any(ClientSubscriptionEntity.class))).thenReturn(savedEntity);
when(hmacKeyStore.generateAndStore(subscriptionId))
.thenReturn(new HmacKeyService.KeyPair("kid-1", "secret-1"));
when(mapper.mapEntityToResponse(savedEntity)).thenReturn(response);

ClientSubscription result = subscriptionService.saveSubscription(createRequest, clientId);

assertThat(result).isEqualTo(response);
assertThat(result.getHmac().getKeyId()).isEqualTo("kid-1");
assertThat(result.getHmac().getSecret()).isEqualTo("secret-1");
}

@Test
Expand Down
Loading