Skip to content

Commit

Permalink
feat(irs-api): allow local urls for irs callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
ds-jhartmann committed Jul 23, 2024
1 parent cfd8475 commit 373c49d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,13 @@
@Service
class CallbackResponderEventListener {

public static final String INVALID_CALLBACK_URL = "Invalid callback url '{}'.";
private final UrlValidator urlValidator;
private final RestTemplate restTemplate;

/* package */ CallbackResponderEventListener(@Qualifier(NO_ERROR_REST_TEMPLATE) final RestTemplate noErrorRestTemplate) {
this.urlValidator = new UrlValidator();
/* package */ CallbackResponderEventListener(
@Qualifier(NO_ERROR_REST_TEMPLATE) final RestTemplate noErrorRestTemplate) {
this.urlValidator = new UrlValidator(UrlValidator.ALLOW_LOCAL_URLS);
this.restTemplate = noErrorRestTemplate;
}

Expand All @@ -83,6 +85,8 @@ public void handleJobProcessingFinishedEvent(final JobProcessingFinishedEvent jo
} catch (final ResourceAccessException resourceAccessException) {
log.warn("Callback url is not reachable - connection timed out, jobId {}", jobProcessingFinishedEvent.jobId());
}
} else {
log.warn(INVALID_CALLBACK_URL, callbackUri);
}
}
}
Expand All @@ -105,6 +109,8 @@ public void handleBatchProcessingFinishedEvent(final BatchProcessingFinishedEven
} catch (final ResourceAccessException resourceAccessException) {
log.warn("Callback url is not reachable - connection timed out, orderId {} batchId {}", batchProcessingFinishedEvent.batchOrderId(), batchProcessingFinishedEvent.batchId());
}
} else {
log.warn(INVALID_CALLBACK_URL, callbackUri);
}
}
}
Expand All @@ -127,6 +133,8 @@ public void handleBatchOrderProcessingFinishedEvent(final BatchOrderProcessingFi
} catch (final ResourceAccessException resourceAccessException) {
log.warn("Callback url is not reachable - connection timed out, jobId {}", batchOrderProcessingFinishedEvent.batchOrderId());
}
} else {
log.warn(INVALID_CALLBACK_URL, callbackUri);
}
}
}
Expand All @@ -136,7 +144,7 @@ private boolean thereIsCallbackUrlRegistered(final String callbackUrl) {
}

@SuppressWarnings("PMD.UseConcurrentHashMap")
private URI buildCallbackUri(final String callbackUrl, final String jobId, final JobState jobState) {
private URI buildCallbackUri(final String callbackUrl, final String jobId, final JobState jobState) {
final Map<String, Object> uriVariables = new HashMap<>();
uriVariables.put("id", jobId);
uriVariables.put("state", jobState);
Expand All @@ -145,7 +153,7 @@ private URI buildCallbackUri(final String callbackUrl, final String jobId, fina
uriComponentsBuilder.uriVariables(uriVariables);
return uriComponentsBuilder.build().toUri();
}

@SuppressWarnings("PMD.UseConcurrentHashMap")
private URI buildCallbackUri(final String callbackUrl, final UUID orderId, final UUID batchId, final ProcessingState orderState, final ProcessingState batchState) {
final Map<String, Object> uriVariables = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,37 @@ void shouldNotCallCallbackUrlIfIsNotValidAndBatchOrderProcessingFinishedEvent()
verifyNoInteractions(this.restTemplate);
}

@Test
void shouldCallCallbackUrlIfIsInternalAndStateCompletedAndJobProcessingFinishedEvent() throws URISyntaxException {
// given
final String callbackUrlTemplate = "https://internal:1234/callback?id={id}&state={state}";
final String jobId = UUID.randomUUID().toString();
final JobState jobState = JobState.COMPLETED;
final JobProcessingFinishedEvent jobProcessingFinishedEvent = new JobProcessingFinishedEvent(jobId,
jobState.name(), callbackUrlTemplate, Optional.empty());
// when
callbackResponderEventListener.handleJobProcessingFinishedEvent(jobProcessingFinishedEvent);

// then
final String expectedCallbackUrl = "https://internal:1234/callback?id=" + jobId + "&state=" + jobState;
verify(this.restTemplate, times(1)).getForEntity(new URI(expectedCallbackUrl), Void.class);
}

@Test
void shouldNotCallCallbackUrlIfTldIsNotValidAndStateCompletedAndJobProcessingFinishedEvent() {
// given
final String callbackUrlTemplateWithInvalidTld = "https://domain.unknown/callback?id={id}&state={state}";
final String jobId = UUID.randomUUID().toString();
final JobState jobState = JobState.COMPLETED;
final JobProcessingFinishedEvent jobProcessingFinishedEvent = new JobProcessingFinishedEvent(jobId,
jobState.name(), callbackUrlTemplateWithInvalidTld, Optional.empty());
// when
callbackResponderEventListener.handleJobProcessingFinishedEvent(jobProcessingFinishedEvent);

// then
verifyNoInteractions(this.restTemplate);
}

@Test
void shouldNotCallCallbackUrlIfCallbackUrlIsMissing() {
// given
Expand Down

0 comments on commit 373c49d

Please sign in to comment.