diff --git a/dspace-api/src/main/java/org/dspace/app/nbevent/service/impl/NBEventServiceImpl.java b/dspace-api/src/main/java/org/dspace/app/nbevent/service/impl/NBEventServiceImpl.java index 47c98e1467..667e446b5b 100644 --- a/dspace-api/src/main/java/org/dspace/app/nbevent/service/impl/NBEventServiceImpl.java +++ b/dspace-api/src/main/java/org/dspace/app/nbevent/service/impl/NBEventServiceImpl.java @@ -107,8 +107,7 @@ public class NBEventServiceImpl implements NBEventService { solrQuery.setRows(0); solrQuery.setQuery("*:*"); solrQuery.setFacet(true); - // we would like to get eventually topic that has no longer active nb events - solrQuery.setFacetMinCount(0); + solrQuery.setFacetMinCount(1); solrQuery.addFacetField(TOPIC); QueryResponse response; try { @@ -125,8 +124,7 @@ public class NBEventServiceImpl implements NBEventService { solrQuery.setRows(0); solrQuery.setQuery("*:*"); solrQuery.setFacet(true); - // we would like to get eventually topic that has no longer active nb events - solrQuery.setFacetMinCount(0); + solrQuery.setFacetMinCount(1); solrQuery.addFacetField(TOPIC); solrQuery.addFilterQuery("source:" + source); QueryResponse response; @@ -164,8 +162,7 @@ public class NBEventServiceImpl implements NBEventService { solrQuery.setRows(0); solrQuery.setQuery(TOPIC + ":" + topicId.replaceAll("!", "/")); solrQuery.setFacet(true); - // we would like to get eventually topic that has no longer active nb events - solrQuery.setFacetMinCount(0); + solrQuery.setFacetMinCount(1); solrQuery.addFacetField(TOPIC); QueryResponse response; try { @@ -194,16 +191,20 @@ public class NBEventServiceImpl implements NBEventService { @Override public List findAllTopicsBySource(String source, long offset, long count) { + + if (source != null && isNotSupportedSource(source)) { + return null; + } + SolrQuery solrQuery = new SolrQuery(); solrQuery.setRows(0); solrQuery.setQuery("*:*"); solrQuery.setFacet(true); - // we would like to get eventually topic that has no longer active nb events - solrQuery.setFacetMinCount(0); + solrQuery.setFacetMinCount(1); solrQuery.setFacetLimit((int) (offset + count)); solrQuery.addFacetField(TOPIC); if (source != null) { - solrQuery.addFilterQuery("source:" + source); + solrQuery.addFilterQuery(SOURCE + ":" + source); } QueryResponse response; List nbTopics = null; @@ -236,7 +237,7 @@ public class NBEventServiceImpl implements NBEventService { UpdateRequest updateRequest = new UpdateRequest(); String topic = dto.getTopic(); - if (!ArrayUtils.contains(getSupportedSources(), dto.getSource())) { + if (isNotSupportedSource(dto.getSource())) { throw new IllegalArgumentException("The source of the given event is not supported: " + dto.getSource()); } @@ -374,7 +375,7 @@ public class NBEventServiceImpl implements NBEventService { @Override public NBSource findSource(String sourceName) { - if (!ArrayUtils.contains(getSupportedSources(), sourceName)) { + if (isNotSupportedSource(sourceName)) { return null; } @@ -382,7 +383,7 @@ public class NBEventServiceImpl implements NBEventService { solrQuery.setRows(0); solrQuery.addFilterQuery(SOURCE + ":" + sourceName); solrQuery.setFacet(true); - solrQuery.setFacetMinCount(0); + solrQuery.setFacetMinCount(1); solrQuery.addFacetField(SOURCE); QueryResponse response; @@ -424,6 +425,10 @@ public class NBEventServiceImpl implements NBEventService { return getSupportedSources().length; } + private boolean isNotSupportedSource(String source) { + return !ArrayUtils.contains(getSupportedSources(), source); + } + private String[] getSupportedSources() { return configurationService.getArrayProperty("nbevent.sources", new String[] { NBEvent.OPENAIRE_SOURCE }); } diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/NBTopicRestRepository.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/NBTopicRestRepository.java index 280d7e26d1..53b1a4be6c 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/NBTopicRestRepository.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/NBTopicRestRepository.java @@ -11,6 +11,7 @@ import java.util.List; import org.dspace.app.nbevent.NBTopic; import org.dspace.app.nbevent.service.NBEventService; +import org.dspace.app.rest.Parameter; import org.dspace.app.rest.SearchRestMethod; import org.dspace.app.rest.model.NBTopicRest; import org.dspace.core.Context; @@ -55,7 +56,8 @@ public class NBTopicRestRepository extends DSpaceRestRepository findBySource(Context context, String source, Pageable pageable) { + public Page findBySource(Context context, + @Parameter(value = "source", required = true) String source, Pageable pageable) { List nbTopics = nbEventService.findAllTopicsBySource(source, pageable.getOffset(), pageable.getPageSize()); long count = nbEventService.countTopicsBySource(source); diff --git a/dspace-server-webapp/src/test/java/org/dspace/app/rest/NBTopicRestRepositoryIT.java b/dspace-server-webapp/src/test/java/org/dspace/app/rest/NBTopicRestRepositoryIT.java index dea109219a..7fe9dbc8b2 100644 --- a/dspace-server-webapp/src/test/java/org/dspace/app/rest/NBTopicRestRepositoryIT.java +++ b/dspace-server-webapp/src/test/java/org/dspace/app/rest/NBTopicRestRepositoryIT.java @@ -20,9 +20,10 @@ import org.dspace.builder.CollectionBuilder; import org.dspace.builder.CommunityBuilder; import org.dspace.builder.NBEventBuilder; import org.dspace.content.Collection; -import org.dspace.content.NBEvent; +import org.dspace.services.ConfigurationService; import org.hamcrest.Matchers; import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; /** * Integration tests for {@link NBTopicRestRepository}. @@ -32,6 +33,9 @@ import org.junit.Test; */ public class NBTopicRestRepositoryIT extends AbstractControllerIntegrationTest { + @Autowired + private ConfigurationService configurationService; + @Test public void findAllTest() throws Exception { context.turnOffAuthorisationSystem(); @@ -39,16 +43,16 @@ public class NBTopicRestRepositoryIT extends AbstractControllerIntegrationTest { .withName("Parent Community") .build(); Collection col1 = CollectionBuilder.createCollection(context, parentCommunity).withName("Collection 1").build(); - NBEvent event1 = NBEventBuilder.createTarget(context, col1, "Science and Freedom") + NBEventBuilder.createTarget(context, col1, "Science and Freedom") .withTopic("ENRICH/MISSING/PID") .withMessage("{\"pids[0].type\":\"doi\",\"pids[0].value\":\"10.2307/2144300\"}").build(); - NBEvent event2 = NBEventBuilder.createTarget(context, col1, "Science and Freedom 2") + NBEventBuilder.createTarget(context, col1, "Science and Freedom 2") .withTopic("ENRICH/MISSING/PID") .withMessage("{\"pids[0].type\":\"doi\",\"pids[0].value\":\"10.2307/2144301\"}").build(); - NBEvent event3 = NBEventBuilder.createTarget(context, col1, "Science and Freedom 3") + NBEventBuilder.createTarget(context, col1, "Science and Freedom 3") .withTopic("ENRICH/MORE/PID") .withMessage("{\"pids[0].type\":\"pmid\",\"pids[0].value\":\"10.2307/2144302\"}").build(); - NBEvent event4 = NBEventBuilder.createTarget(context, col1, "Science and Freedom 4") + NBEventBuilder.createTarget(context, col1, "Science and Freedom 4") .withTopic("ENRICH/MISSING/ABSTRACT") .withMessage( "{\"abstracts[0]\": \"Descrizione delle caratteristiche...\"}") @@ -84,16 +88,16 @@ public class NBTopicRestRepositoryIT extends AbstractControllerIntegrationTest { .build(); //create collection Collection col1 = CollectionBuilder.createCollection(context, parentCommunity).withName("Collection 1").build(); - NBEvent event1 = NBEventBuilder.createTarget(context, col1, "Science and Freedom") + NBEventBuilder.createTarget(context, col1, "Science and Freedom") .withTopic("ENRICH/MISSING/PID") .withMessage("{\"pids[0].type\":\"doi\",\"pids[0].value\":\"10.2307/2144300\"}").build(); - NBEvent event2 = NBEventBuilder.createTarget(context, col1, "Science and Freedom 2") + NBEventBuilder.createTarget(context, col1, "Science and Freedom 2") .withTopic("ENRICH/MISSING/PID") .withMessage("{\"pids[0].type\":\"doi\",\"pids[0].value\":\"10.2307/2144301\"}").build(); - NBEvent event3 = NBEventBuilder.createTarget(context, col1, "Science and Freedom 3") + NBEventBuilder.createTarget(context, col1, "Science and Freedom 3") .withTopic("ENRICH/MORE/PID") .withMessage("{\"pids[0].type\":\"pmid\",\"pids[0].value\":\"10.2307/2144302\"}").build(); - NBEvent event4 = NBEventBuilder.createTarget(context, col1, "Science and Freedom 4") + NBEventBuilder.createTarget(context, col1, "Science and Freedom 4") .withTopic("ENRICH/MISSING/ABSTRACT") .withMessage( "{\"abstracts[0]\": \"Descrizione delle caratteristiche...\"}") @@ -119,16 +123,16 @@ public class NBTopicRestRepositoryIT extends AbstractControllerIntegrationTest { .withName("Parent Community") .build(); Collection col1 = CollectionBuilder.createCollection(context, parentCommunity).withName("Collection 1").build(); - NBEvent event1 = NBEventBuilder.createTarget(context, col1, "Science and Freedom") + NBEventBuilder.createTarget(context, col1, "Science and Freedom") .withTopic("ENRICH/MISSING/PID") .withMessage("{\"pids[0].type\":\"doi\",\"pids[0].value\":\"10.2307/2144300\"}").build(); - NBEvent event2 = NBEventBuilder.createTarget(context, col1, "Science and Freedom 2") + NBEventBuilder.createTarget(context, col1, "Science and Freedom 2") .withTopic("ENRICH/MISSING/PID") .withMessage("{\"pids[0].type\":\"doi\",\"pids[0].value\":\"10.2307/2144301\"}").build(); - NBEvent event3 = NBEventBuilder.createTarget(context, col1, "Science and Freedom 3") + NBEventBuilder.createTarget(context, col1, "Science and Freedom 3") .withTopic("ENRICH/MORE/PID") .withMessage("{\"pids[0].type\":\"pmid\",\"pids[0].value\":\"10.2307/2144302\"}").build(); - NBEvent event4 = NBEventBuilder.createTarget(context, col1, "Science and Freedom 4") + NBEventBuilder.createTarget(context, col1, "Science and Freedom 4") .withTopic("ENRICH/MISSING/ABSTRACT") .withMessage( "{\"abstracts[0]\": \"Descrizione delle caratteristiche...\"}") @@ -148,7 +152,7 @@ public class NBTopicRestRepositoryIT extends AbstractControllerIntegrationTest { .withName("Parent Community") .build(); Collection col1 = CollectionBuilder.createCollection(context, parentCommunity).withName("Collection 1").build(); - NBEvent event1 = NBEventBuilder.createTarget(context, col1, "Science and Freedom") + NBEventBuilder.createTarget(context, col1, "Science and Freedom") .withTopic("ENRICH/MISSING/PID").build(); context.restoreAuthSystemState(); getClient().perform(get("/api/integration/nbtopics/ENRICH!MISSING!PID")).andExpect(status().isUnauthorized()); @@ -163,7 +167,7 @@ public class NBTopicRestRepositoryIT extends AbstractControllerIntegrationTest { .withName("Parent Community") .build(); Collection col1 = CollectionBuilder.createCollection(context, parentCommunity).withName("Collection 1").build(); - NBEvent event1 = NBEventBuilder.createTarget(context, col1, "Science and Freedom") + NBEventBuilder.createTarget(context, col1, "Science and Freedom") .withTopic("ENRICH/MISSING/PID").build(); context.restoreAuthSystemState(); String authToken = getAuthToken(eperson.getEmail(), password); @@ -173,4 +177,97 @@ public class NBTopicRestRepositoryIT extends AbstractControllerIntegrationTest { .andExpect(status().isForbidden()); } + @Test + public void findBySourceTest() throws Exception { + context.turnOffAuthorisationSystem(); + configurationService.setProperty("nbevent.sources", + new String[] { "openaire", "test-source", "test-source-2" }); + parentCommunity = CommunityBuilder.createCommunity(context) + .withName("Parent Community") + .build(); + Collection col1 = CollectionBuilder.createCollection(context, parentCommunity).withName("Collection 1").build(); + NBEventBuilder.createTarget(context, col1, "Science and Freedom") + .withTopic("ENRICH/MISSING/PID") + .withMessage("{\"pids[0].type\":\"doi\",\"pids[0].value\":\"10.2307/2144300\"}").build(); + NBEventBuilder.createTarget(context, col1, "Science and Freedom 2") + .withTopic("ENRICH/MISSING/PID") + .withMessage("{\"pids[0].type\":\"doi\",\"pids[0].value\":\"10.2307/2144301\"}").build(); + NBEventBuilder.createTarget(context, col1, "Science and Freedom 3") + .withTopic("ENRICH/MORE/PID") + .withMessage("{\"pids[0].type\":\"pmid\",\"pids[0].value\":\"10.2307/2144302\"}").build(); + NBEventBuilder.createTarget(context, col1, "Science and Freedom 4") + .withTopic("ENRICH/MISSING/ABSTRACT") + .withMessage( + "{\"abstracts[0]\": \"Descrizione delle caratteristiche...\"}") + .build(); + NBEventBuilder.createTarget(context, col1, "Science and Freedom 5") + .withTopic("TEST/TOPIC") + .withSource("test-source") + .build(); + NBEventBuilder.createTarget(context, col1, "Science and Freedom 6") + .withTopic("TEST/TOPIC") + .withSource("test-source") + .build(); + NBEventBuilder.createTarget(context, col1, "Science and Freedom 7") + .withTopic("TEST/TOPIC/2") + .withSource("test-source") + .build(); + context.restoreAuthSystemState(); + String authToken = getAuthToken(admin.getEmail(), password); + getClient(authToken).perform(get("/api/integration/nbtopics/search/bySource") + .param("source", "openaire")) + .andExpect(status().isOk()) + .andExpect(content().contentType(contentType)) + .andExpect(jsonPath("$._embedded.nbtopics", + Matchers.containsInAnyOrder(NBTopicMatcher.matchNBTopicEntry("ENRICH/MISSING/PID", 2), + NBTopicMatcher.matchNBTopicEntry("ENRICH/MISSING/ABSTRACT", 1), + NBTopicMatcher.matchNBTopicEntry("ENRICH/MORE/PID", 1)))) + .andExpect(jsonPath("$.page.size", is(20))).andExpect(jsonPath("$.page.totalElements", is(3))); + getClient(authToken).perform(get("/api/integration/nbtopics/search/bySource") + .param("source", "test-source")) + .andExpect(status().isOk()) + .andExpect(content().contentType(contentType)) + .andExpect(jsonPath("$._embedded.nbtopics", + Matchers.containsInAnyOrder(NBTopicMatcher.matchNBTopicEntry("TEST/TOPIC/2", 1), + NBTopicMatcher.matchNBTopicEntry("TEST/TOPIC", 2)))) + .andExpect(jsonPath("$.page.size", is(20))).andExpect(jsonPath("$.page.totalElements", is(2))); + getClient(authToken).perform(get("/api/integration/nbtopics/search/bySource") + .param("source", "test-source-2")) + .andExpect(status().isOk()) + .andExpect(content().contentType(contentType)) + .andExpect(jsonPath("$._embedded.nbtopics").doesNotExist()) + .andExpect(jsonPath("$.page.size", is(20))).andExpect(jsonPath("$.page.totalElements", is(0))); + } + + @Test + public void findBySourceUnauthorizedTest() throws Exception { + context.turnOffAuthorisationSystem(); + parentCommunity = CommunityBuilder.createCommunity(context) + .withName("Parent Community") + .build(); + Collection col1 = CollectionBuilder.createCollection(context, parentCommunity).withName("Collection 1").build(); + NBEventBuilder.createTarget(context, col1, "Science and Freedom") + .withTopic("ENRICH/MISSING/PID").build(); + context.restoreAuthSystemState(); + getClient().perform(get("/api/integration/nbtopics/search/bySource") + .param("source", "openaire")) + .andExpect(status().isUnauthorized()); + } + + @Test + public void findBySourceForbiddenTest() throws Exception { + context.turnOffAuthorisationSystem(); + parentCommunity = CommunityBuilder.createCommunity(context) + .withName("Parent Community") + .build(); + Collection col1 = CollectionBuilder.createCollection(context, parentCommunity).withName("Collection 1").build(); + NBEventBuilder.createTarget(context, col1, "Science and Freedom") + .withTopic("ENRICH/MISSING/PID").build(); + context.restoreAuthSystemState(); + String authToken = getAuthToken(eperson.getEmail(), password); + getClient(authToken).perform(get("/api/integration/nbtopics/search/bySource") + .param("source", "openaire")) + .andExpect(status().isForbidden()); + } + }