mirror of
https://github.com/DSpace/DSpace.git
synced 2025-10-10 19:43:10 +00:00
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:
@@ -55,20 +55,51 @@ public class SendLDNMessageAction implements LDNAction {
|
|||||||
|
|
||||||
log.info("Announcing notification {}", request);
|
log.info("Announcing notification {}", request);
|
||||||
|
|
||||||
// https://notify-inbox.info/inbox/ for test
|
try {
|
||||||
|
|
||||||
ResponseEntity<String> response = restTemplate.postForEntity(
|
ResponseEntity<String> response = restTemplate.postForEntity(
|
||||||
notification.getTarget().getInbox(),
|
notification.getTarget().getInbox(),
|
||||||
request,
|
request,
|
||||||
String.class
|
String.class);
|
||||||
);
|
|
||||||
|
|
||||||
if (response.getStatusCode() == HttpStatus.ACCEPTED ||
|
if (isSuccessful(response.getStatusCode())) {
|
||||||
response.getStatusCode() == HttpStatus.CREATED) {
|
|
||||||
return ActionStatus.CONTINUE;
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
return ActionStatus.ABORT;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@@ -0,0 +1,218 @@
|
|||||||
|
/**
|
||||||
|
* The contents of this file are subject to the license and copyright
|
||||||
|
* detailed in the LICENSE and NOTICE files at the root of the source
|
||||||
|
* tree and available online at
|
||||||
|
*
|
||||||
|
* http://www.dspace.org/license/
|
||||||
|
*/
|
||||||
|
package org.dspace.app.ldn.action;
|
||||||
|
|
||||||
|
import static org.dspace.app.ldn.action.ActionStatus.ABORT;
|
||||||
|
import static org.dspace.app.ldn.action.ActionStatus.CONTINUE;
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import org.apache.commons.collections4.CollectionUtils;
|
||||||
|
import org.dspace.AbstractIntegrationTestWithDatabase;
|
||||||
|
import org.dspace.app.ldn.LDNMessageEntity;
|
||||||
|
import org.dspace.app.ldn.NotifyServiceEntity;
|
||||||
|
import org.dspace.app.ldn.factory.NotifyServiceFactory;
|
||||||
|
import org.dspace.app.ldn.model.Notification;
|
||||||
|
import org.dspace.app.ldn.service.LDNMessageService;
|
||||||
|
import org.dspace.builder.CollectionBuilder;
|
||||||
|
import org.dspace.builder.CommunityBuilder;
|
||||||
|
import org.dspace.builder.EPersonBuilder;
|
||||||
|
import org.dspace.builder.NotifyServiceBuilder;
|
||||||
|
import org.dspace.builder.WorkspaceItemBuilder;
|
||||||
|
import org.dspace.content.Collection;
|
||||||
|
import org.dspace.content.Item;
|
||||||
|
import org.dspace.content.WorkspaceItem;
|
||||||
|
import org.dspace.eperson.EPerson;
|
||||||
|
import org.dspace.workflow.WorkflowItem;
|
||||||
|
import org.dspace.workflow.WorkflowService;
|
||||||
|
import org.dspace.workflow.factory.WorkflowServiceFactory;
|
||||||
|
import org.junit.After;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Integration Tests against {@link SendLDNMessageAction}
|
||||||
|
*
|
||||||
|
* @author Mohamed Eskander (mohamed.eskander at 4science.com)
|
||||||
|
*/
|
||||||
|
public class SendLDNMessageActionIT extends AbstractIntegrationTestWithDatabase {
|
||||||
|
|
||||||
|
private Collection collection;
|
||||||
|
private EPerson submitter;
|
||||||
|
|
||||||
|
private LDNMessageService ldnMessageService = NotifyServiceFactory.getInstance().getLDNMessageService();
|
||||||
|
private WorkflowService workflowService = WorkflowServiceFactory.getInstance().getWorkflowService();
|
||||||
|
private SendLDNMessageAction sendLDNMessageAction;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() throws Exception {
|
||||||
|
super.setUp();
|
||||||
|
sendLDNMessageAction = new SendLDNMessageAction();
|
||||||
|
context.turnOffAuthorisationSystem();
|
||||||
|
//** GIVEN **
|
||||||
|
//1. create a normal user to use as submitter
|
||||||
|
submitter = EPersonBuilder.createEPerson(context)
|
||||||
|
.withEmail("submitter@example.com")
|
||||||
|
.withPassword(password)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
//2. A community with one collection.
|
||||||
|
parentCommunity = CommunityBuilder.createCommunity(context)
|
||||||
|
.withName("Parent Community")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
collection = CollectionBuilder.createCollection(context, parentCommunity)
|
||||||
|
.withName("Collection 1")
|
||||||
|
.withSubmitterGroup(submitter)
|
||||||
|
.build();
|
||||||
|
context.setCurrentUser(submitter);
|
||||||
|
|
||||||
|
context.restoreAuthSystemState();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testLDNMessageConsumerRequestReview() throws Exception {
|
||||||
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
|
|
||||||
|
context.turnOffAuthorisationSystem();
|
||||||
|
|
||||||
|
NotifyServiceEntity notifyService =
|
||||||
|
NotifyServiceBuilder.createNotifyServiceBuilder(context)
|
||||||
|
.withName("service name")
|
||||||
|
.withDescription("service description")
|
||||||
|
.withUrl("https://www.notify-inbox.info/")
|
||||||
|
.withLdnUrl("https://notify-inbox.info/inbox/")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
//3. a workspace item ready to go
|
||||||
|
WorkspaceItem workspaceItem =
|
||||||
|
WorkspaceItemBuilder.createWorkspaceItem(context, collection)
|
||||||
|
.withTitle("Submission Item")
|
||||||
|
.withIssueDate("2023-11-20")
|
||||||
|
.withCOARNotifyService(notifyService, "request-review")
|
||||||
|
.withFulltext("test.txt", "test", InputStream.nullInputStream())
|
||||||
|
.grantLicense()
|
||||||
|
.build();
|
||||||
|
|
||||||
|
WorkflowItem workflowItem = workflowService.start(context, workspaceItem);
|
||||||
|
Item item = workflowItem.getItem();
|
||||||
|
context.dispatchEvents();
|
||||||
|
context.restoreAuthSystemState();
|
||||||
|
|
||||||
|
LDNMessageEntity ldnMessage =
|
||||||
|
ldnMessageService.findAll(context).stream().findFirst().orElse(null);
|
||||||
|
|
||||||
|
ldnMessage.getQueueStatus();
|
||||||
|
|
||||||
|
Notification notification = mapper.readValue(ldnMessage.getMessage(), Notification.class);
|
||||||
|
|
||||||
|
assertEquals(sendLDNMessageAction.execute(context, notification, item), CONTINUE);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testLDNMessageConsumerRequestReviewGotRedirection() throws Exception {
|
||||||
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
|
|
||||||
|
context.turnOffAuthorisationSystem();
|
||||||
|
|
||||||
|
// ldnUrl should be https://notify-inbox.info/inbox/
|
||||||
|
// but used https://notify-inbox.info/inbox for redirection
|
||||||
|
NotifyServiceEntity notifyService =
|
||||||
|
NotifyServiceBuilder.createNotifyServiceBuilder(context)
|
||||||
|
.withName("service name")
|
||||||
|
.withDescription("service description")
|
||||||
|
.withUrl("https://www.notify-inbox.info/")
|
||||||
|
.withLdnUrl("https://notify-inbox.info/inbox")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
//3. a workspace item ready to go
|
||||||
|
WorkspaceItem workspaceItem =
|
||||||
|
WorkspaceItemBuilder.createWorkspaceItem(context, collection)
|
||||||
|
.withTitle("Submission Item")
|
||||||
|
.withIssueDate("2023-11-20")
|
||||||
|
.withCOARNotifyService(notifyService, "request-review")
|
||||||
|
.withFulltext("test.txt", "test", InputStream.nullInputStream())
|
||||||
|
.grantLicense()
|
||||||
|
.build();
|
||||||
|
|
||||||
|
WorkflowItem workflowItem = workflowService.start(context, workspaceItem);
|
||||||
|
Item item = workflowItem.getItem();
|
||||||
|
context.dispatchEvents();
|
||||||
|
context.restoreAuthSystemState();
|
||||||
|
|
||||||
|
LDNMessageEntity ldnMessage =
|
||||||
|
ldnMessageService.findAll(context).stream().findFirst().orElse(null);
|
||||||
|
|
||||||
|
ldnMessage.getQueueStatus();
|
||||||
|
|
||||||
|
Notification notification = mapper.readValue(ldnMessage.getMessage(), Notification.class);
|
||||||
|
|
||||||
|
assertEquals(sendLDNMessageAction.execute(context, notification, item), CONTINUE);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testLDNMessageConsumerRequestReviewWithInvalidLdnUrl() throws Exception {
|
||||||
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
|
|
||||||
|
context.turnOffAuthorisationSystem();
|
||||||
|
|
||||||
|
NotifyServiceEntity notifyService =
|
||||||
|
NotifyServiceBuilder.createNotifyServiceBuilder(context)
|
||||||
|
.withName("service name")
|
||||||
|
.withDescription("service description")
|
||||||
|
.withUrl("https://www.notify-inbox.info/")
|
||||||
|
.withLdnUrl("https://notify-inbox.info/invalidLdnUrl/")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
//3. a workspace item ready to go
|
||||||
|
WorkspaceItem workspaceItem =
|
||||||
|
WorkspaceItemBuilder.createWorkspaceItem(context, collection)
|
||||||
|
.withTitle("Submission Item")
|
||||||
|
.withIssueDate("2023-11-20")
|
||||||
|
.withCOARNotifyService(notifyService, "request-review")
|
||||||
|
.withFulltext("test.txt", "test", InputStream.nullInputStream())
|
||||||
|
.grantLicense()
|
||||||
|
.build();
|
||||||
|
|
||||||
|
WorkflowItem workflowItem = workflowService.start(context, workspaceItem);
|
||||||
|
Item item = workflowItem.getItem();
|
||||||
|
context.dispatchEvents();
|
||||||
|
context.restoreAuthSystemState();
|
||||||
|
|
||||||
|
LDNMessageEntity ldnMessage =
|
||||||
|
ldnMessageService.findAll(context).stream().findFirst().orElse(null);
|
||||||
|
|
||||||
|
ldnMessage.getQueueStatus();
|
||||||
|
|
||||||
|
Notification notification = mapper.readValue(ldnMessage.getMessage(), Notification.class);
|
||||||
|
|
||||||
|
assertEquals(sendLDNMessageAction.execute(context, notification, item), ABORT);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@After
|
||||||
|
public void destroy() throws Exception {
|
||||||
|
List<LDNMessageEntity> ldnMessageEntities = ldnMessageService.findAll(context);
|
||||||
|
if (CollectionUtils.isNotEmpty(ldnMessageEntities)) {
|
||||||
|
ldnMessageEntities.forEach(ldnMessage -> {
|
||||||
|
try {
|
||||||
|
ldnMessageService.delete(context, ldnMessage);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
super.destroy();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Reference in New Issue
Block a user