1818
1919import static org .hamcrest .MatcherAssert .assertThat ;
2020import static org .hamcrest .Matchers .is ;
21+ import static org .junit .jupiter .api .Assertions .assertEquals ;
2122import static org .junit .jupiter .api .Assertions .assertDoesNotThrow ;
2223import static org .junit .jupiter .api .Assertions .assertNull ;
2324import static org .junit .jupiter .api .Assertions .assertThrows ;
25+ import static org .mockito .Mockito .doAnswer ;
2426import static org .mockito .Mockito .mock ;
2527import static org .mockito .Mockito .when ;
2628import static uk .gov .pay .webhooks .app .WebhooksKeys .RESOURCE_IS_LIVE ;
@@ -141,11 +143,14 @@ public void httpsCallbackUrlShouldNotThrowError() {
141143 }
142144
143145 @ Test
144- void should_clear_populated_mdc_context_after_validating_create_webhook_request () {
146+ void should_populate_validator_specific_mdc_keys_during_create_webhook_validation_and_clear_them_afterwards () {
147+ var callbackUrlService = mock (CallbackUrlService .class );
148+ var validatorWithMockedCallbackUrlService = new WebhookRequestValidator (callbackUrlService );
149+
145150 MDC .put (MDC_REQUEST_ID_KEY , "request-id" );
146151 MDC .put (GATEWAY_ACCOUNT_ID , "existing-gateway-account-id" );
147152 MDC .put (SERVICE_EXTERNAL_ID , "existing-service-id" );
148- MDC .put (RESOURCE_IS_LIVE , "false " );
153+ MDC .put (RESOURCE_IS_LIVE , "true " );
149154
150155 var createWebhookRequest = new CreateWebhookRequest (
151156 "new-service-id" ,
@@ -156,7 +161,15 @@ void should_clear_populated_mdc_context_after_validating_create_webhook_request(
156161 List .of ()
157162 );
158163
159- assertDoesNotThrow (() -> webhookRequestValidator .validate (createWebhookRequest ));
164+ doAnswer (_ -> {
165+ assertEquals ("request-id" , MDC .get (MDC_REQUEST_ID_KEY ));
166+ assertEquals ("new-gateway-account-id" , MDC .get (GATEWAY_ACCOUNT_ID ));
167+ assertEquals ("new-service-id" , MDC .get (SERVICE_EXTERNAL_ID ));
168+ assertEquals ("false" , MDC .get (RESOURCE_IS_LIVE ));
169+ return null ;
170+ }).when (callbackUrlService ).validateCallbackUrl ("https://pay.gov.uk" , false );
171+
172+ assertDoesNotThrow (() -> validatorWithMockedCallbackUrlService .validate (createWebhookRequest ));
160173
161174 assertThat (MDC .get (MDC_REQUEST_ID_KEY ), is ("request-id" ));
162175 assertNull (MDC .get (GATEWAY_ACCOUNT_ID ));
0 commit comments