11package uk .gov .hmcts .cp .subscription .mappers ;
22
33import org .junit .jupiter .api .Test ;
4+ import org .junit .jupiter .api .extension .ExtendWith ;
5+ import org .mockito .Mock ;
6+ import org .mockito .junit .jupiter .MockitoExtension ;
47import uk .gov .hmcts .cp .openapi .model .ClientSubscription ;
58import uk .gov .hmcts .cp .openapi .model .ClientSubscriptionRequest ;
69import uk .gov .hmcts .cp .openapi .model .NotificationEndpoint ;
710import uk .gov .hmcts .cp .subscription .entities .ClientSubscriptionEntity ;
811import uk .gov .hmcts .cp .subscription .model .EntityEventType ;
12+ import uk .gov .hmcts .cp .subscription .services .ClockService ;
913
1014import java .time .OffsetDateTime ;
15+ import java .time .ZoneOffset ;
1116import java .util .ArrayList ;
1217import java .util .List ;
1318import java .util .UUID ;
1419
1520import static org .assertj .core .api .Assertions .assertThat ;
21+ import static org .mockito .Mockito .when ;
1622import static uk .gov .hmcts .cp .openapi .model .EventType .CUSTODIAL_RESULT ;
1723import static uk .gov .hmcts .cp .openapi .model .EventType .PCR ;
1824
25+ @ ExtendWith (MockitoExtension .class )
1926class SubscriptionMapperTest {
2027
28+ private final static OffsetDateTime MOCKCREATED = OffsetDateTime .of (2025 , 12 , 1 , 11 , 30 , 50 , 582007048 , ZoneOffset .UTC );
29+ private final static OffsetDateTime MOCKUPDATED = OffsetDateTime .of (2025 , 12 , 2 , 12 , 40 , 55 , 234567890 , ZoneOffset .UTC );
30+
31+ @ Mock
32+ ClockService clockService ;
33+
2134 SubscriptionMapper mapper = new SubscriptionMapperImpl ();
2235
2336 UUID clientSubscriptionId = UUID .fromString ("d730c6e1-66ba-4ef0-a3dd-0b9928faa76d" );
2437 NotificationEndpoint notificationEndpoint = NotificationEndpoint .builder ()
2538 .webhookUrl ("https://example.com" )
2639 .build ();
27- OffsetDateTime createdAt = OffsetDateTime .now ().minusDays (2 );
28- OffsetDateTime updatedAt = OffsetDateTime .now ().minusHours (2 );
2940 ClientSubscriptionEntity existing = ClientSubscriptionEntity .builder ()
3041 .id (clientSubscriptionId )
3142 .notificationEndpoint (notificationEndpoint .getWebhookUrl ().toString ())
3243 .eventTypes (mutableLisOfEventTypes ())
33- .createdAt (createdAt )
34- .updatedAt (updatedAt )
44+ .createdAt (MOCKCREATED )
45+ .updatedAt (MOCKCREATED )
3546 .build ();
3647
3748 @ Test
3849 void create_request_should_map_to_entity_with_sorted_types () {
50+ when (clockService .now ()).thenReturn (MOCKCREATED );
3951 ClientSubscriptionRequest request = ClientSubscriptionRequest .builder ()
4052 .notificationEndpoint (notificationEndpoint )
4153 .eventTypes (List .of (PCR , CUSTODIAL_RESULT ))
4254 .build ();
4355
44- ClientSubscriptionEntity entity = mapper .mapCreateRequestToEntity (request );
56+ ClientSubscriptionEntity entity = mapper .mapCreateRequestToEntity (clockService , request );
4557
4658 assertThat (entity .getId ()).isNull ();
4759 assertThat (entity .getNotificationEndpoint ()).isEqualTo ("https://example.com" );
@@ -59,14 +71,14 @@ void update_request_should_map_to_entity_with_sorted_types() {
5971 .notificationEndpoint (updatedEndpoint )
6072 .eventTypes (List .of (CUSTODIAL_RESULT ))
6173 .build ();
62-
63- ClientSubscriptionEntity entity = mapper .mapUpdateRequestToEntity (existing , request );
74+ when ( clockService . now ()). thenReturn ( MOCKUPDATED );
75+ ClientSubscriptionEntity entity = mapper .mapUpdateRequestToEntity (clockService , existing , request );
6476
6577 assertThat (entity .getId ()).isEqualTo (clientSubscriptionId );
6678 assertThat (entity .getNotificationEndpoint ()).isEqualTo ("https://updated.com" );
6779 assertThat (entity .getEventTypes ().toString ()).isEqualTo ("[CUSTODIAL_RESULT]" );
68- assertThat (entity .getCreatedAt ()).isEqualTo (createdAt );
69- assertThat (entity .getUpdatedAt ()).isAfter ( updatedAt );
80+ assertThat (entity .getCreatedAt ()).isEqualTo (MOCKCREATED );
81+ assertThat (entity .getUpdatedAt ()).isEqualTo ( MOCKUPDATED );
7082 }
7183
7284 @ Test
@@ -76,8 +88,8 @@ void entity_should_map_to_response() {
7688 assertThat (subscription .getClientSubscriptionId ()).isEqualTo (clientSubscriptionId );
7789 assertThat (subscription .getNotificationEndpoint ()).isEqualTo (notificationEndpoint );
7890 assertThat (subscription .getEventTypes ().toString ()).isEqualTo ("[CUSTODIAL_RESULT, PCR]" );
79- assertThat (subscription .getCreatedAt ()).isEqualTo (createdAt );
80- assertThat (subscription .getUpdatedAt ()).isEqualTo (updatedAt );
91+ assertThat (subscription .getCreatedAt ()).isEqualTo (MOCKCREATED );
92+ assertThat (subscription .getUpdatedAt ()).isEqualTo (MOCKCREATED );
8193 }
8294
8395 private List <EntityEventType > mutableLisOfEventTypes () {
0 commit comments