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
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package uk.gov.hmcts.cp.http;

import org.junit.jupiter.api.Test;
import org.springframework.http.*;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import static org.assertj.core.api.Assertions.assertThat;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package uk.gov.hmcts.cp.entities;

import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
Expand All @@ -11,9 +9,7 @@
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;
import uk.gov.hmcts.cp.model.EventType;
import uk.gov.hmcts.cp.model.EntityEventType;

import java.time.OffsetDateTime;
import java.util.List;
Expand All @@ -32,10 +28,7 @@ public class ClientSubscriptionEntity {
private UUID id;

private String notificationEndpoint;
@Enumerated(EnumType.STRING)
private List<EventType> eventTypes;
@CreationTimestamp
private List<EntityEventType> eventTypes;
private OffsetDateTime createdAt;
@UpdateTimestamp
private OffsetDateTime updatedAt;
}
16 changes: 15 additions & 1 deletion src/main/java/uk/gov/hmcts/cp/mappers/SubscriptionMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,35 @@

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;
import org.mapstruct.ReportingPolicy;
import uk.gov.hmcts.cp.entities.ClientSubscriptionEntity;
import uk.gov.hmcts.cp.model.EntityEventType;
import uk.gov.hmcts.cp.openapi.model.ClientSubscription;
import uk.gov.hmcts.cp.openapi.model.ClientSubscriptionRequest;
import uk.gov.hmcts.cp.openapi.model.EventType;
import uk.gov.hmcts.cp.openapi.model.NotificationEndpoint;

import java.net.URI;
import java.util.List;

import static java.util.stream.Collectors.toList;

@Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface SubscriptionMapper {

@Mapping(source = "request.eventTypes", target = "eventTypes", qualifiedByName = "sortedEventTypes")
@Mapping(target = "createdAt", expression = "java(java.time.OffsetDateTime.now())")
ClientSubscriptionEntity mapRequestToEntity(ClientSubscriptionRequest request);

@Mapping(source = "id", target = "clientSubscriptionId")
ClientSubscription mapEntityToResponse(ClientSubscriptionEntity subscription);
ClientSubscription mapEntityToResponse(ClientSubscriptionEntity entity);

@Named("sortedEventTypes")
static List<EntityEventType> sortedEventTypes(final List<EventType> events) {
final List<String> sorted = events.stream().map(e -> e.name()).sorted().collect(toList());
return sorted.stream().map(e -> EntityEventType.valueOf(e)).toList();
}

static String mapFromNotificationEndpoint(final NotificationEndpoint notificationEndpoint) {
return notificationEndpoint.getWebhookUrl().toString();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package uk.gov.hmcts.cp.model;

public enum EventType {
public enum EntityEventType {
PCR,
CUSTODIAL_RESULT
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
package uk.gov.hmcts.cp.integration;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.transaction.annotation.Transactional;
import tools.jackson.databind.ObjectMapper;
import uk.gov.hmcts.cp.entities.ClientSubscriptionEntity;
import uk.gov.hmcts.cp.model.EntityEventType;
import uk.gov.hmcts.cp.openapi.model.ClientSubscriptionRequest;
import uk.gov.hmcts.cp.openapi.model.NotificationEndpoint;
import uk.gov.hmcts.cp.repositories.SubscriptionRepository;

import java.net.URI;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static uk.gov.hmcts.cp.openapi.model.EventType.CUSTODIAL_RESULT;
import static uk.gov.hmcts.cp.openapi.model.EventType.PCR;

class SubscriptionControllerIntegrationTest extends IntegrationTestBase {

@Autowired
SubscriptionRepository subscriptionRepository;

NotificationEndpoint notificationEndpoint = NotificationEndpoint.builder()
.webhookUrl(URI.create("https://my-callback-url"))
.build();
Expand All @@ -33,6 +43,19 @@ void save_client_subscription_should_save_subscription() throws Exception {
.contentType(MediaType.APPLICATION_JSON)
.header("client-id-todo", "1234")
.content(body))
.andExpect(status().isOk());
.andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("$.clientSubscriptionId").exists())
.andExpect(jsonPath("$.eventTypes.[0]").value("CUSTODIAL_RESULT"))
.andExpect(jsonPath("$.eventTypes.[1]").value("PCR"))
.andExpect(jsonPath("$.createdAt").exists());
assertThatEventTypesAreSortedInDatabase();
}

private void assertThatEventTypesAreSortedInDatabase() {
List<ClientSubscriptionEntity> entities = subscriptionRepository.findAll();
assertThat(entities).hasSize(1);
assertThat(entities.get(0).getEventTypes().get(0)).isEqualTo(EntityEventType.CUSTODIAL_RESULT);
assertThat(entities.get(0).getEventTypes().get(1)).isEqualTo(EntityEventType.PCR);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.junit.jupiter.api.Test;
import uk.gov.hmcts.cp.entities.ClientSubscriptionEntity;
import uk.gov.hmcts.cp.model.EntityEventType;
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 All @@ -12,7 +13,7 @@
import java.util.UUID;

import static org.assertj.core.api.Assertions.assertThat;
import static uk.gov.hmcts.cp.model.EventType.CUSTODIAL_RESULT;
import static uk.gov.hmcts.cp.openapi.model.EventType.CUSTODIAL_RESULT;
import static uk.gov.hmcts.cp.openapi.model.EventType.PCR;

class SubscriptionMapperTest {
Expand All @@ -27,18 +28,18 @@ class SubscriptionMapperTest {
OffsetDateTime updatedAt = OffsetDateTime.now().minusHours(2);

@Test
void request_should_map_to_entity() {
void request_should_map_to_entity_with_sorted_types() {
ClientSubscriptionRequest request = ClientSubscriptionRequest.builder()
.notificationEndpoint(notificationEndpoint)
.eventTypes(List.of(PCR))
.eventTypes(List.of(PCR, CUSTODIAL_RESULT))
.build();

ClientSubscriptionEntity entity = mapper.mapRequestToEntity(request);

assertThat(entity.getId()).isNull();
assertThat(entity.getNotificationEndpoint()).isEqualTo("https://example.com");
assertThat(entity.getEventTypes().toString()).isEqualTo("[PCR]");
assertThat(entity.getCreatedAt()).isNull();
assertThat(entity.getEventTypes().toString()).isEqualTo("[CUSTODIAL_RESULT, PCR]");
assertThat(entity.getCreatedAt()).isNotNull();
assertThat(entity.getUpdatedAt()).isNull();
}

Expand All @@ -47,7 +48,7 @@ void entity_should_map_to_response() {
ClientSubscriptionEntity clientSubscriptionEntity = ClientSubscriptionEntity.builder()
.id(clientNotificationId)
.notificationEndpoint(notificationEndpoint.getWebhookUrl().toString())
.eventTypes(List.of(CUSTODIAL_RESULT))
.eventTypes(List.of(EntityEventType.CUSTODIAL_RESULT, EntityEventType.PCR))
.createdAt(createdAt)
.updatedAt(updatedAt)
.build();
Expand All @@ -56,7 +57,7 @@ void entity_should_map_to_response() {

assertThat(subscription.getClientSubscriptionId()).isEqualTo(clientNotificationId);
assertThat(subscription.getNotificationEndpoint()).isEqualTo(notificationEndpoint);
assertThat(subscription.getEventTypes().toString()).isEqualTo("[CUSTODIAL_RESULT]");
assertThat(subscription.getEventTypes().toString()).isEqualTo("[CUSTODIAL_RESULT, PCR]");
assertThat(subscription.getCreatedAt()).isEqualTo(createdAt);
assertThat(subscription.getUpdatedAt()).isEqualTo(updatedAt);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
import org.springframework.test.context.ContextConfiguration;
import uk.gov.hmcts.cp.config.TestContainersInitialise;
import uk.gov.hmcts.cp.entities.ClientSubscriptionEntity;
import uk.gov.hmcts.cp.model.EventType;
import uk.gov.hmcts.cp.model.EntityEventType;

import java.time.OffsetDateTime;
import java.util.List;

@SpringBootTest
Expand All @@ -25,10 +26,11 @@ protected void clearAllTables() {
subscriptionRepository.deleteAll();
}

protected ClientSubscriptionEntity insertSubscription(String notificationUri, List<EventType> eventTypes) {
protected ClientSubscriptionEntity insertSubscription(String notificationUri, List<EntityEventType> entityEventTypes) {
ClientSubscriptionEntity subscription = ClientSubscriptionEntity.builder()
.eventTypes(eventTypes)
.eventTypes(entityEventTypes)
.notificationEndpoint(notificationUri)
.createdAt(OffsetDateTime.now())
.build();
ClientSubscriptionEntity saved = subscriptionRepository.save(subscription);
log.info("Inserted subscription:{}", saved.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static uk.gov.hmcts.cp.model.EventType.PCR;
import static uk.gov.hmcts.cp.model.EntityEventType.CUSTODIAL_RESULT;
import static uk.gov.hmcts.cp.model.EntityEventType.PCR;

class SubscriptionRepositoryTest extends RepositoryTestBase {

Expand All @@ -20,11 +21,11 @@ void beforeEach() {
@Transactional
@Test
void subscription_should_save_and_read_ok() {
insertSubscription("https://example.com/notify", List.of(PCR));
insertSubscription("https://example.com/notify", List.of(CUSTODIAL_RESULT, PCR));
List<ClientSubscriptionEntity> subscriptions = subscriptionRepository.findAll();
assertThat(subscriptions).hasSize(1);
assertThat(subscriptions.get(0).getId()).isNotNull();
assertThat(subscriptions.get(0).getEventTypes()).isEqualTo(List.of(PCR));
assertThat(subscriptions.get(0).getEventTypes()).isEqualTo(List.of(CUSTODIAL_RESULT, PCR));
assertThat(subscriptions.get(0).getNotificationEndpoint()).isEqualTo("https://example.com/notify");
assertThat(subscriptions.get(0).getCreatedAt()).isNotNull();
}
Expand Down
Loading