Merged in coar-notify-7_CST-10632_IT (pull request #1324)

[CST-10632] added ITs against SendLDNMessageAction

Approved-by: Stefano Maffei
This commit is contained in:
Mohamed Saber Eskander
2023-11-21 13:45:32 +00:00
committed by Stefano Maffei
2 changed files with 260 additions and 11 deletions

View File

@@ -55,20 +55,51 @@ public class SendLDNMessageAction implements LDNAction {
log.info("Announcing notification {}", request);
// https://notify-inbox.info/inbox/ for test
try {
ResponseEntity<String> response = restTemplate.postForEntity(
notification.getTarget().getInbox(),
request,
String.class);
ResponseEntity<String> response = restTemplate.postForEntity(
notification.getTarget().getInbox(),
request,
String.class
);
if (isSuccessful(response.getStatusCode())) {
return ActionStatus.CONTINUE;
} else if (isRedirect(response.getStatusCode())) {
return handleRedirect(response, request);
} else {
return ActionStatus.ABORT;
}
} catch (Exception e) {
log.error(e);
return ActionStatus.ABORT;
}
}
if (response.getStatusCode() == HttpStatus.ACCEPTED ||
response.getStatusCode() == HttpStatus.CREATED) {
return ActionStatus.CONTINUE;
private boolean isSuccessful(HttpStatus statusCode) {
return statusCode == HttpStatus.ACCEPTED ||
statusCode == HttpStatus.CREATED;
}
private boolean isRedirect(HttpStatus statusCode) {
return statusCode == HttpStatus.PERMANENT_REDIRECT ||
statusCode == HttpStatus.TEMPORARY_REDIRECT;
}
private ActionStatus handleRedirect(ResponseEntity<String> response,
HttpEntity<Notification> request) {
String url = response.getHeaders().getFirst(HttpHeaders.LOCATION);
try {
ResponseEntity<String> responseEntity =
restTemplate.postForEntity(url, request, String.class);
if (isSuccessful(responseEntity.getStatusCode())) {
return ActionStatus.CONTINUE;
}
} catch (Exception e) {
log.error("Error following redirect:", e);
}
return ActionStatus.ABORT;
}
}
}