From 0686df8dbea59cc66c7ce71e0af8f97ad4b1133c Mon Sep 17 00:00:00 2001 From: mohamed eskander Date: Wed, 3 Jan 2024 12:32:42 +0200 Subject: [PATCH] [CST-13103] added new ITs --- .../service/impl/LDNMessageServiceImpl.java | 5 + .../app/rest/LDNMessageRestControllerIT.java | 152 +++++++++++ .../app/rest/LDNMessageRestRepositoryIT.java | 252 ++++++++++++++++++ 3 files changed, 409 insertions(+) create mode 100644 dspace-server-webapp/src/test/java/org/dspace/app/rest/LDNMessageRestControllerIT.java create mode 100644 dspace-server-webapp/src/test/java/org/dspace/app/rest/LDNMessageRestRepositoryIT.java diff --git a/dspace-api/src/main/java/org/dspace/app/ldn/service/impl/LDNMessageServiceImpl.java b/dspace-api/src/main/java/org/dspace/app/ldn/service/impl/LDNMessageServiceImpl.java index 3bda6f42fa..e6333b4776 100644 --- a/dspace-api/src/main/java/org/dspace/app/ldn/service/impl/LDNMessageServiceImpl.java +++ b/dspace-api/src/main/java/org/dspace/app/ldn/service/impl/LDNMessageServiceImpl.java @@ -75,6 +75,11 @@ public class LDNMessageServiceImpl implements LDNMessageService { @Override public LDNMessageEntity find(Context context, String id) throws SQLException { + + if (id == null) { + return null; + } + id = id.startsWith(LDN_ID_PREFIX) ? id : LDN_ID_PREFIX + id; return ldnMessageDao.findByID(context, LDNMessageEntity.class, id); } diff --git a/dspace-server-webapp/src/test/java/org/dspace/app/rest/LDNMessageRestControllerIT.java b/dspace-server-webapp/src/test/java/org/dspace/app/rest/LDNMessageRestControllerIT.java new file mode 100644 index 0000000000..76356f871d --- /dev/null +++ b/dspace-server-webapp/src/test/java/org/dspace/app/rest/LDNMessageRestControllerIT.java @@ -0,0 +1,152 @@ +/** + * 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.rest; + +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.nullValue; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import java.io.InputStream; +import java.nio.charset.Charset; +import java.sql.SQLException; +import java.util.List; + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.io.IOUtils; +import org.dspace.app.ldn.LDNMessageEntity; +import org.dspace.app.ldn.NotifyServiceEntity; +import org.dspace.app.ldn.model.Notification; +import org.dspace.app.ldn.service.LDNMessageService; +import org.dspace.app.rest.test.AbstractControllerIntegrationTest; +import org.dspace.builder.CollectionBuilder; +import org.dspace.builder.CommunityBuilder; +import org.dspace.builder.ItemBuilder; +import org.dspace.builder.NotifyServiceBuilder; +import org.dspace.content.Collection; +import org.dspace.content.Community; +import org.dspace.content.Item; +import org.dspace.services.ConfigurationService; +import org.junit.After; +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; + +/** + * Integration Tests against {@link LDNMessageRestController} + * + * @author Mohamed Eskander (mohamed.eskander at 4science.com) + */ +public class LDNMessageRestControllerIT extends AbstractControllerIntegrationTest { + + @Autowired + private ConfigurationService configurationService; + + @Autowired + private LDNMessageService ldnMessageService; + + @Test + public void findByItemUnAuthorizedTest() throws Exception { + getClient() + .perform(get("/api/ldn/messages/enqueueretry/urn:uuid:668f26e0-2c8d-4117-a0d2-ee713523bcb4")) + .andExpect(status().isUnauthorized()); + } + + @Test + public void findByItemIsForbiddenTest() throws Exception { + String authToken = getAuthToken(eperson.getEmail(), password); + getClient(authToken) + .perform(get("/api/ldn/messages/enqueueretry/urn:uuid:668f26e0-2c8d-4117-a0d2-ee713523bcb4")) + .andExpect(status().isForbidden()); + } + + @Test + public void findByItemNotFoundTest() throws Exception { + String authToken = getAuthToken(admin.getEmail(), password); + getClient(authToken) + .perform(get("/api/ldn/messages/enqueueretry/urn:uuid:668f26e0-2c8d-4117-a0d2-ee713523bcb4")) + .andExpect(status().isNotFound()); + } + + @Test + public void findByItemTest() throws Exception { + context.turnOffAuthorisationSystem(); + Community community = CommunityBuilder.createCommunity(context) + .withName("community") + .build(); + Collection collection = CollectionBuilder.createCollection(context, community).build(); + + Item item = ItemBuilder.createItem(context, collection) + .withTitle("item title") + .build(); + + String object = configurationService.getProperty("dspace.ui.url") + "/handle/" + item.getHandle(); + + NotifyServiceEntity notifyService = + NotifyServiceBuilder.createNotifyServiceBuilder(context) + .withName("service name") + .withDescription("service description") + .withUrl("service url") + .withLdnUrl("https://overlay-journal.com/inbox/") + .build(); + + context.restoreAuthSystemState(); + + InputStream announceEndorsementStream = getClass().getResourceAsStream("ldn_announce_endorsement.json"); + String announceEndorsement = IOUtils.toString(announceEndorsementStream, Charset.defaultCharset()); + announceEndorsementStream.close(); + String message = announceEndorsement.replaceAll("<>", object); + message = message.replaceAll("<>", object); + + ObjectMapper mapper = new ObjectMapper(); + Notification notification = mapper.readValue(message, Notification.class); + getClient() + .perform(post("/ldn/inbox") + .contentType("application/ld+json") + .content(message)) + .andExpect(status().isAccepted()); + + String authToken = getAuthToken(admin.getEmail(), password); + getClient(authToken) + .perform(get("/api/ldn/messages/enqueueretry/" + notification.getId())) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.id", is(notification.getId()))) + .andExpect(jsonPath("$.notificationId", is(notification.getId()))) + .andExpect(jsonPath("$.queueStatus", is(7))) + .andExpect(jsonPath("$.queueStatusLabel", is("QUEUE_STATUS_QUEUED_FOR_RETRY"))) + .andExpect(jsonPath("$.context", is(item.getID().toString()))) + .andExpect(jsonPath("$.object", is(item.getID().toString()))) + .andExpect(jsonPath("$.target", nullValue())) + .andExpect(jsonPath("$.origin", is(notifyService.getID()))) + .andExpect(jsonPath("$.inReplyTo", nullValue())) + .andExpect(jsonPath("$.activityStreamType", is("Announce"))) + .andExpect(jsonPath("$.coarNotifyType", is("coar-notify:EndorsementAction"))) + .andExpect(jsonPath("$.queueAttempts", is(0))) + .andExpect(jsonPath("$.notificationType", is("Incoming"))); + + } + + @Override + @After + public void destroy() throws Exception { + List 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(); + } +} \ No newline at end of file diff --git a/dspace-server-webapp/src/test/java/org/dspace/app/rest/LDNMessageRestRepositoryIT.java b/dspace-server-webapp/src/test/java/org/dspace/app/rest/LDNMessageRestRepositoryIT.java new file mode 100644 index 0000000000..713634c345 --- /dev/null +++ b/dspace-server-webapp/src/test/java/org/dspace/app/rest/LDNMessageRestRepositoryIT.java @@ -0,0 +1,252 @@ +/** + * 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.rest; + +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.nullValue; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import java.io.InputStream; +import java.nio.charset.Charset; +import java.sql.SQLException; +import java.util.List; + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.io.IOUtils; +import org.dspace.app.ldn.LDNMessageEntity; +import org.dspace.app.ldn.NotifyServiceEntity; +import org.dspace.app.ldn.model.Notification; +import org.dspace.app.ldn.service.LDNMessageService; +import org.dspace.app.rest.model.LDNMessageEntityRest; +import org.dspace.app.rest.repository.LDNMessageRestRepository; +import org.dspace.app.rest.test.AbstractControllerIntegrationTest; +import org.dspace.builder.CollectionBuilder; +import org.dspace.builder.CommunityBuilder; +import org.dspace.builder.ItemBuilder; +import org.dspace.builder.NotifyServiceBuilder; +import org.dspace.content.Collection; +import org.dspace.content.Community; +import org.dspace.content.Item; +import org.dspace.services.ConfigurationService; +import org.junit.After; +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; + +/** + * Integration Tests against {@link LDNMessageRestRepository} + * + * @author Mohamed Eskander (mohamed.eskander at 4science.com) + */ +public class LDNMessageRestRepositoryIT extends AbstractControllerIntegrationTest { + + @Autowired + private ConfigurationService configurationService; + + @Autowired + private LDNMessageService ldnMessageService; + + @Test + public void findOneUnAuthorizedTest() throws Exception { + getClient() + .perform(get("/api/ldn/messages/urn:uuid:668f26e0-2c8d-4117-a0d2-ee713523bcb4")) + .andExpect(status().isUnauthorized()); + } + + @Test + public void findOneIsForbiddenTest() throws Exception { + String authToken = getAuthToken(eperson.getEmail(), password); + getClient(authToken) + .perform(get("/api/ldn/messages/urn:uuid:668f26e0-2c8d-4117-a0d2-ee713523bcb4")) + .andExpect(status().isForbidden()); + } + + @Test + public void findOneIsNotFoundTest() throws Exception { + String authToken = getAuthToken(admin.getEmail(), password); + getClient(authToken) + .perform(get("/api/ldn/messages/urn:uuid:668f26e0-2c8d-4117-a0d2-ee713523bcb4")) + .andExpect(status().isNotFound()); + } + + @Test + public void findOneTest() throws Exception { + context.turnOffAuthorisationSystem(); + Community community = CommunityBuilder.createCommunity(context) + .withName("community") + .build(); + Collection collection = CollectionBuilder.createCollection(context, community).build(); + + Item item = ItemBuilder.createItem(context, collection) + .withTitle("item title") + .build(); + + String object = configurationService.getProperty("dspace.ui.url") + "/handle/" + item.getHandle(); + + NotifyServiceEntity notifyService = + NotifyServiceBuilder.createNotifyServiceBuilder(context) + .withName("service name") + .withDescription("service description") + .withUrl("service url") + .withLdnUrl("https://overlay-journal.com/inbox/") + .build(); + + context.restoreAuthSystemState(); + + InputStream announceEndorsementStream = getClass().getResourceAsStream("ldn_announce_endorsement.json"); + String announceEndorsement = IOUtils.toString(announceEndorsementStream, Charset.defaultCharset()); + announceEndorsementStream.close(); + String message = announceEndorsement.replaceAll("<>", object); + message = message.replaceAll("<>", object); + + ObjectMapper mapper = new ObjectMapper(); + Notification notification = mapper.readValue(message, Notification.class); + getClient() + .perform(post("/ldn/inbox") + .contentType("application/ld+json") + .content(message)) + .andExpect(status().isAccepted()); + + String authToken = getAuthToken(admin.getEmail(), password); + getClient(authToken) + .perform(get("/api/ldn/messages/" + notification.getId())) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.id", is(notification.getId()))) + .andExpect(jsonPath("$.notificationId", is(notification.getId()))) + .andExpect(jsonPath("$.queueStatus", is(1))) + .andExpect(jsonPath("$.queueStatusLabel", is("QUEUE_STATUS_QUEUED"))) + .andExpect(jsonPath("$.context", is(item.getID().toString()))) + .andExpect(jsonPath("$.object", is(item.getID().toString()))) + .andExpect(jsonPath("$.target", nullValue())) + .andExpect(jsonPath("$.origin", is(notifyService.getID()))) + .andExpect(jsonPath("$.inReplyTo", nullValue())) + .andExpect(jsonPath("$.activityStreamType", is("Announce"))) + .andExpect(jsonPath("$.coarNotifyType", is("coar-notify:EndorsementAction"))) + .andExpect(jsonPath("$.queueAttempts", is(0))) + .andExpect(jsonPath("$.notificationType", is("Incoming"))); + } + + @Test + public void findAllUnAuthorizedTest() throws Exception { + getClient().perform(get("/api/ldn/messages")) + .andExpect(status().isUnauthorized()); + } + + @Test + public void findAllIsForbiddenTest() throws Exception { + String authToken = getAuthToken(eperson.getEmail(), password); + getClient(authToken).perform(get("/api/ldn/messages")) + .andExpect(status().isForbidden()); + } + + @Test + public void findAllTest() throws Exception { + context.turnOffAuthorisationSystem(); + Community community = CommunityBuilder.createCommunity(context) + .withName("community") + .build(); + Collection collection = CollectionBuilder.createCollection(context, community).build(); + + Item item = ItemBuilder.createItem(context, collection) + .withTitle("item title") + .build(); + + String object = configurationService.getProperty("dspace.ui.url") + "/handle/" + item.getHandle(); + + NotifyServiceEntity notifyService = + NotifyServiceBuilder.createNotifyServiceBuilder(context) + .withName("service name") + .withDescription("service description") + .withUrl("service url") + .withLdnUrl("https://overlay-journal.com/inbox/") + .build(); + + context.restoreAuthSystemState(); + + InputStream announceEndorsementStream = getClass().getResourceAsStream("ldn_announce_endorsement.json"); + String announceEndorsement = IOUtils.toString(announceEndorsementStream, Charset.defaultCharset()); + announceEndorsementStream.close(); + String message = announceEndorsement.replaceAll("<>", object); + message = message.replaceAll("<>", object); + + ObjectMapper mapper = new ObjectMapper(); + Notification notification = mapper.readValue(message, Notification.class); + getClient() + .perform(post("/ldn/inbox") + .contentType("application/ld+json") + .content(message)) + .andExpect(status().isAccepted()); + + String authToken = getAuthToken(admin.getEmail(), password); + getClient(authToken) + .perform(get("/api/ldn/messages/")) + .andExpect(status().isOk()) + .andExpect(jsonPath("$._embedded.messages[0].id", is(notification.getId()))) + .andExpect(jsonPath("$._embedded.messages[0].notificationId", is(notification.getId()))) + .andExpect(jsonPath("$._embedded.messages[0].queueStatus", is(1))) + .andExpect(jsonPath("$._embedded.messages[0].queueStatusLabel", is("QUEUE_STATUS_QUEUED"))) + .andExpect(jsonPath("$._embedded.messages[0].context", is(item.getID().toString()))) + .andExpect(jsonPath("$._embedded.messages[0].object", is(item.getID().toString()))) + .andExpect(jsonPath("$._embedded.messages[0].target", nullValue())) + .andExpect(jsonPath("$._embedded.messages[0].origin", is(notifyService.getID()))) + .andExpect(jsonPath("$._embedded.messages[0].inReplyTo", nullValue())) + .andExpect(jsonPath("$._embedded.messages[0].activityStreamType", is("Announce"))) + .andExpect(jsonPath("$._embedded.messages[0].coarNotifyType", is("coar-notify:EndorsementAction"))) + .andExpect(jsonPath("$._embedded.messages[0].queueAttempts", is(0))) + .andExpect(jsonPath("$._embedded.messages[0].notificationType", is("Incoming"))); + } + + @Test + public void createLDNMessageTest() throws Exception { + ObjectMapper mapper = new ObjectMapper(); + LDNMessageEntityRest data = new LDNMessageEntityRest(); + String authToken = getAuthToken(admin.getEmail(), password); + + getClient(authToken) + .perform(post("/api/ldn/messages") + .content(mapper.writeValueAsBytes(data)) + .contentType(contentType)) + .andExpect(status().isInternalServerError()); + } + + @Test + public void deleteLDNMessageTest() throws Exception { + String authToken = getAuthToken(admin.getEmail(), password); + getClient(authToken).perform(delete("/api/ldn/messages")) + .andExpect(status().isMethodNotAllowed()); + } + + @Test + public void patchLDNMessageTest() throws Exception { + String authToken = getAuthToken(admin.getEmail(), password); + getClient(authToken).perform(patch("/api/ldn/messages")) + .andExpect(status().isMethodNotAllowed()); + } + + @Override + @After + public void destroy() throws Exception { + List 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(); + } +}