[CST-12108] fix failed tests

This commit is contained in:
Mykhaylo Boychuk
2023-12-05 15:45:47 +01:00
parent af0a521eeb
commit 67f7148eee
4 changed files with 145 additions and 173 deletions

View File

@@ -31,7 +31,8 @@ public class QATopicConverter implements DSpaceConverter<QATopic, QATopicRest> {
public QATopicRest convert(QATopic modelObject, Projection projection) {
QATopicRest rest = new QATopicRest();
rest.setProjection(projection);
rest.setId(modelObject.getSource() + ":" + modelObject.getKey().replace("/", "!"));
rest.setId(modelObject.getSource() + ":" + modelObject.getKey().replace("/", "!") +
(modelObject.getFocus() != null ? ":" + modelObject.getFocus().toString() : ""));
rest.setName(modelObject.getKey());
rest.setLastEvent(modelObject.getLastEvent());
rest.setTotalEvents(modelObject.getTotalEvents());

View File

@@ -43,11 +43,15 @@ public class QATopicRestRepository extends DSpaceRestRepository<QATopicRest, Str
@Override
@PreAuthorize("hasPermission(#id, 'QUALITYASSURANCETOPIC', 'READ')")
public QATopicRest findOne(Context context, String id) {
QATopic topic = qaEventService.findTopicByTopicId(id);
if (topic == null) {
String[] topicIdSplitted = id.split(":", 3);
if (topicIdSplitted.length < 2) {
return null;
}
return converter.toRest(topic, utils.obtainProjection());
String sourceName = topicIdSplitted[0];
String topicName = topicIdSplitted[1].replaceAll("!", "/");
UUID target = topicIdSplitted.length == 3 ? UUID.fromString(topicIdSplitted[2]) : null;
QATopic topic = qaEventService.findTopicBySourceAndNameAndTarget(context, sourceName, topicName, target);
return (topic != null) ? converter.toRest(topic, utils.obtainProjection()) : null;
}
@SearchRestMethod(name = "bySource")

View File

@@ -20,6 +20,7 @@ import org.dspace.builder.CollectionBuilder;
import org.dspace.builder.CommunityBuilder;
import org.dspace.builder.QAEventBuilder;
import org.dspace.content.Collection;
import org.dspace.qaevent.QANotifyPatterns;
import org.dspace.services.ConfigurationService;
import org.hamcrest.Matchers;
import org.junit.Test;
@@ -38,114 +39,58 @@ public class QATopicRestRepositoryIT extends AbstractControllerIntegrationTest {
@Test
public void findAllTest() throws Exception {
context.turnOffAuthorisationSystem();
parentCommunity = CommunityBuilder.createCommunity(context)
.withName("Parent Community")
.build();
Collection col1 = CollectionBuilder.createCollection(context, parentCommunity).withName("Collection 1").build();
QAEventBuilder.createTarget(context, col1, "Science and Freedom")
.withTopic("ENRICH/MISSING/PID")
.withMessage("{\"pids[0].type\":\"doi\",\"pids[0].value\":\"10.2307/2144300\"}").build();
QAEventBuilder.createTarget(context, col1, "Science and Freedom 2")
.withTopic("ENRICH/MISSING/PID")
.withMessage("{\"pids[0].type\":\"doi\",\"pids[0].value\":\"10.2307/2144301\"}").build();
QAEventBuilder.createTarget(context, col1, "Science and Freedom 3")
.withTopic("ENRICH/MORE/PID")
.withMessage("{\"pids[0].type\":\"pmid\",\"pids[0].value\":\"10.2307/2144302\"}").build();
QAEventBuilder.createTarget(context, col1, "Science and Freedom 4")
.withTopic("ENRICH/MISSING/ABSTRACT")
.withMessage(
"{\"abstracts[0]\": \"Descrizione delle caratteristiche...\"}")
.build();
context.restoreAuthSystemState();
String authToken = getAuthToken(admin.getEmail(), password);
getClient(authToken).perform(get("/api/integration/qualityassurancetopics")).andExpect(status().isOk())
.andExpect(content().contentType(contentType))
.andExpect(jsonPath("$._embedded.qualityassurancetopics",
Matchers.containsInAnyOrder(QATopicMatcher.matchQATopicEntry("ENRICH/MISSING/PID", 2),
QATopicMatcher.matchQATopicEntry("ENRICH/MISSING/ABSTRACT", 1),
QATopicMatcher.matchQATopicEntry("ENRICH/MORE/PID", 1))))
.andExpect(jsonPath("$.page.size", is(20))).andExpect(jsonPath("$.page.totalElements", is(3)));
}
@Test
public void findAllUnauthorizedTest() throws Exception {
getClient().perform(get("/api/integration/qualityassurancetopics")).andExpect(status().isUnauthorized());
}
@Test
public void findAllForbiddenTest() throws Exception {
String authToken = getAuthToken(eperson.getEmail(), password);
getClient(authToken).perform(get("/api/integration/qualityassurancetopics")).andExpect(status().isForbidden());
}
@Test
public void findAllPaginationTest() throws Exception {
context.turnOffAuthorisationSystem();
parentCommunity = CommunityBuilder.createCommunity(context)
.withName("Parent Community")
.build();
//create collection
Collection col1 = CollectionBuilder.createCollection(context, parentCommunity).withName("Collection 1").build();
QAEventBuilder.createTarget(context, col1, "Science and Freedom")
.withTopic("ENRICH/MISSING/PID")
.withMessage("{\"pids[0].type\":\"doi\",\"pids[0].value\":\"10.2307/2144300\"}").build();
QAEventBuilder.createTarget(context, col1, "Science and Freedom 2")
.withTopic("ENRICH/MISSING/PID")
.withMessage("{\"pids[0].type\":\"doi\",\"pids[0].value\":\"10.2307/2144301\"}").build();
QAEventBuilder.createTarget(context, col1, "Science and Freedom 3")
.withTopic("ENRICH/MORE/PID")
.withMessage("{\"pids[0].type\":\"pmid\",\"pids[0].value\":\"10.2307/2144302\"}").build();
QAEventBuilder.createTarget(context, col1, "Science and Freedom 4")
.withTopic("ENRICH/MISSING/ABSTRACT")
.withMessage(
"{\"abstracts[0]\": \"Descrizione delle caratteristiche...\"}")
.build();
context.restoreAuthSystemState();
String authToken = getAuthToken(admin.getEmail(), password);
getClient(authToken).perform(get("/api/integration/qualityassurancetopics").param("size", "2"))
.andExpect(status().isOk())
.andExpect(content().contentType(contentType))
.andExpect(jsonPath("$._embedded.qualityassurancetopics", Matchers.hasSize(2)))
.andExpect(jsonPath("$.page.size", is(2))).andExpect(jsonPath("$.page.totalElements", is(3)));
getClient(authToken)
.perform(get("/api/integration/qualityassurancetopics")
.param("size", "2")
.param("page", "1"))
.andExpect(status().isOk())
.andExpect(content().contentType(contentType))
.andExpect(jsonPath("$._embedded.qualityassurancetopics", Matchers.hasSize(1)))
.andExpect(jsonPath("$.page.size", is(2))).andExpect(jsonPath("$.page.totalElements", is(3)));
String adminToken = getAuthToken(admin.getEmail(), password);
getClient(adminToken).perform(get("/api/integration/qualityassurancetopics"))
.andExpect(status().isMethodNotAllowed());
}
@Test
public void findOneTest() throws Exception {
context.turnOffAuthorisationSystem();
configurationService.setProperty("qaevent.sources", new String[] { "openaire", "test-source" });
parentCommunity = CommunityBuilder.createCommunity(context)
.withName("Parent Community")
.build();
Collection col1 = CollectionBuilder.createCollection(context, parentCommunity).withName("Collection 1").build();
.withName("Parent Community")
.build();
Collection col1 = CollectionBuilder.createCollection(context, parentCommunity)
.withName("Collection 1")
.build();
QAEventBuilder.createTarget(context, col1, "Science and Freedom")
.withTopic("ENRICH/MISSING/PID")
.withMessage("{\"pids[0].type\":\"doi\",\"pids[0].value\":\"10.2307/2144300\"}").build();
.withTopic(QANotifyPatterns.TOPIC_ENRICH_MISSING_PID)
.withMessage("{\"pids[0].type\":\"doi\",\"pids[0].value\":\"10.2307/2144300\"}")
.build();
QAEventBuilder.createTarget(context, col1, "Science and Freedom 2")
.withTopic("ENRICH/MISSING/PID")
.withMessage("{\"pids[0].type\":\"doi\",\"pids[0].value\":\"10.2307/2144301\"}").build();
.withTopic(QANotifyPatterns.TOPIC_ENRICH_MISSING_PID)
.withMessage("{\"pids[0].type\":\"doi\",\"pids[0].value\":\"10.2307/2144301\"}")
.build();
QAEventBuilder.createTarget(context, col1, "Science and Freedom 3")
.withTopic("ENRICH/MORE/PID")
.withMessage("{\"pids[0].type\":\"pmid\",\"pids[0].value\":\"10.2307/2144302\"}").build();
.withTopic(QANotifyPatterns.TOPIC_ENRICH_MORE_PID)
.withMessage("{\"pids[0].type\":\"pmid\",\"pids[0].value\":\"10.2307/2144302\"}")
.build();
QAEventBuilder.createTarget(context, col1, "Science and Freedom 4")
.withTopic("ENRICH/MISSING/ABSTRACT")
.withMessage(
"{\"abstracts[0]\": \"Descrizione delle caratteristiche...\"}")
.build();
.withTopic(org.dspace.qaevent.QANotifyPatterns.TOPIC_ENRICH_MISSING_ABSTRACT)
.withMessage("{\"test\": \"Test...\"}")
.build();
QAEventBuilder.createTarget(context, col1, "Science and Freedom 4")
.withSource("test-source")
.withTopic("TOPIC/TEST")
.withMessage("{\"abstracts[0]\": \"Descrizione delle caratteristiche...\"}")
.build();
context.restoreAuthSystemState();
String authToken = getAuthToken(admin.getEmail(), password);
getClient(authToken).perform(get("/api/integration/qualityassurancetopics/ENRICH!MISSING!PID"))
.andExpect(jsonPath("$", QATopicMatcher.matchQATopicEntry("ENRICH/MISSING/PID", 2)));
getClient(authToken).perform(get("/api/integration/qualityassurancetopics/ENRICH!MISSING!ABSTRACT"))
.andExpect(jsonPath("$", QATopicMatcher.matchQATopicEntry("ENRICH/MISSING/ABSTRACT", 1)));
String adminToken = getAuthToken(admin.getEmail(), password);
getClient(adminToken).perform(get("/api/integration/qualityassurancetopics/openaire:ENRICH!MISSING!PID"))
.andExpect(status().isOk())
.andExpect(jsonPath("$", QATopicMatcher.matchQATopicEntry(
QANotifyPatterns.TOPIC_ENRICH_MISSING_PID, 2)));
getClient(adminToken).perform(get("/api/integration/qualityassurancetopics/openaire:ENRICH!MISSING!ABSTRACT"))
.andExpect(status().isOk())
.andExpect(jsonPath("$", QATopicMatcher.matchQATopicEntry(
QANotifyPatterns.TOPIC_ENRICH_MISSING_ABSTRACT, 1)));
getClient(adminToken).perform(get("/api/integration/qualityassurancetopics/test-source:TOPIC!TEST"))
.andExpect(status().isOk())
.andExpect(jsonPath("$",QATopicMatcher.matchQATopicEntry("test-source", "TOPIC/TEST", 1)));
}
@Test
@@ -184,94 +129,96 @@ public class QATopicRestRepositoryIT extends AbstractControllerIntegrationTest {
@Test
public void findBySourceTest() throws Exception {
context.turnOffAuthorisationSystem();
configurationService.setProperty("qaevent.sources",
new String[] { "openaire", "test-source", "test-source-2" });
configurationService.setProperty("qaevent.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();
.withName("Parent Community")
.build();
Collection col1 = CollectionBuilder.createCollection(context, parentCommunity)
.withName("Collection 1")
.build();
QAEventBuilder.createTarget(context, col1, "Science and Freedom")
.withTopic("ENRICH/MISSING/PID")
.withMessage("{\"pids[0].type\":\"doi\",\"pids[0].value\":\"10.2307/2144300\"}").build();
.withTopic(QANotifyPatterns.TOPIC_ENRICH_MISSING_PID)
.withMessage("{\"pids[0].type\":\"doi\",\"pids[0].value\":\"10.2307/2144300\"}")
.build();
QAEventBuilder.createTarget(context, col1, "Science and Freedom 2")
.withTopic("ENRICH/MISSING/PID")
.withMessage("{\"pids[0].type\":\"doi\",\"pids[0].value\":\"10.2307/2144301\"}").build();
.withTopic(QANotifyPatterns.TOPIC_ENRICH_MISSING_PID)
.withMessage("{\"pids[0].type\":\"doi\",\"pids[0].value\":\"10.2307/2144301\"}")
.build();
QAEventBuilder.createTarget(context, col1, "Science and Freedom 3")
.withTopic("ENRICH/MORE/PID")
.withMessage("{\"pids[0].type\":\"pmid\",\"pids[0].value\":\"10.2307/2144302\"}").build();
.withTopic(QANotifyPatterns.TOPIC_ENRICH_MORE_PID)
.withMessage("{\"pids[0].type\":\"pmid\",\"pids[0].value\":\"10.2307/2144302\"}")
.build();
QAEventBuilder.createTarget(context, col1, "Science and Freedom 4")
.withTopic("ENRICH/MISSING/ABSTRACT")
.withMessage(
"{\"abstracts[0]\": \"Descrizione delle caratteristiche...\"}")
.build();
.withTopic(org.dspace.qaevent.QANotifyPatterns.TOPIC_ENRICH_MISSING_ABSTRACT)
.withMessage("{\"abstracts[0]\": \"Descrizione delle caratteristiche...\"}")
.build();
QAEventBuilder.createTarget(context, col1, "Science and Freedom 5")
.withTopic("TEST/TOPIC")
.withSource("test-source")
.build();
.withTopic("TEST/TOPIC")
.withSource("test-source")
.build();
QAEventBuilder.createTarget(context, col1, "Science and Freedom 6")
.withTopic("TEST/TOPIC")
.withSource("test-source")
.build();
.withTopic("TEST/TOPIC")
.withSource("test-source")
.build();
QAEventBuilder.createTarget(context, col1, "Science and Freedom 7")
.withTopic("TEST/TOPIC/2")
.withSource("test-source")
.build();
.withTopic("TEST/TOPIC/2")
.withSource("test-source")
.build();
context.restoreAuthSystemState();
String authToken = getAuthToken(admin.getEmail(), password);
getClient(authToken).perform(get("/api/integration/qualityassurancetopics/search/bySource")
.param("source", "openaire"))
.andExpect(status().isOk())
.andExpect(content().contentType(contentType))
.andExpect(jsonPath("$._embedded.qualityassurancetopics",
Matchers.containsInAnyOrder(QATopicMatcher.matchQATopicEntry("ENRICH/MISSING/PID", 2),
QATopicMatcher.matchQATopicEntry("ENRICH/MISSING/ABSTRACT", 1),
QATopicMatcher.matchQATopicEntry("ENRICH/MORE/PID", 1))))
.andExpect(jsonPath("$.page.size", is(20))).andExpect(jsonPath("$.page.totalElements", is(3)));
.param("source", "openaire"))
.andExpect(status().isOk())
.andExpect(content().contentType(contentType))
.andExpect(jsonPath("$._embedded.qualityassurancetopics", Matchers.containsInAnyOrder(
QATopicMatcher.matchQATopicEntry(QANotifyPatterns.TOPIC_ENRICH_MISSING_PID, 2),
QATopicMatcher.matchQATopicEntry(QANotifyPatterns.TOPIC_ENRICH_MISSING_ABSTRACT, 1),
QATopicMatcher.matchQATopicEntry(QANotifyPatterns.TOPIC_ENRICH_MORE_PID, 1)
)))
.andExpect(jsonPath("$.page.size", is(20)))
.andExpect(jsonPath("$.page.totalElements", is(3)));
getClient(authToken).perform(get("/api/integration/qualityassurancetopics/search/bySource")
.param("source", "test-source"))
.andExpect(status().isOk())
.andExpect(content().contentType(contentType))
.andExpect(jsonPath("$._embedded.qualityassurancetopics",
Matchers.containsInAnyOrder(QATopicMatcher.matchQATopicEntry("TEST/TOPIC/2", 1),
QATopicMatcher.matchQATopicEntry("TEST/TOPIC", 2))))
.andExpect(jsonPath("$.page.size", is(20))).andExpect(jsonPath("$.page.totalElements", is(2)));
.param("source", "test-source"))
.andExpect(status().isOk())
.andExpect(content().contentType(contentType))
.andExpect(jsonPath("$._embedded.qualityassurancetopics", Matchers.containsInAnyOrder(
QATopicMatcher.matchQATopicEntry("test-source", "TEST/TOPIC/2", 1),
QATopicMatcher.matchQATopicEntry("test-source", "TEST/TOPIC", 2)
)))
.andExpect(jsonPath("$.page.size", is(20)))
.andExpect(jsonPath("$.page.totalElements", is(2)));
getClient(authToken).perform(get("/api/integration/qualityassurancetopics/search/bySource")
.param("source", "test-source-2"))
.andExpect(status().isOk())
.andExpect(content().contentType(contentType))
.andExpect(jsonPath("$._embedded.qualityassurancetopics").doesNotExist())
.andExpect(jsonPath("$.page.size", is(20))).andExpect(jsonPath("$.page.totalElements", is(0)));
.param("source", "test-source-2"))
.andExpect(status().isOk())
.andExpect(content().contentType(contentType))
.andExpect(jsonPath("$._embedded.qualityassurancetopics").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();
.withName("Parent Community")
.build();
Collection col1 = CollectionBuilder.createCollection(context, parentCommunity)
.withName("Collection 1")
.build();
QAEventBuilder.createTarget(context, col1, "Science and Freedom")
.withTopic("ENRICH/MISSING/PID").build();
.withTopic("ENRICH/MISSING/PID")
.build();
context.restoreAuthSystemState();
getClient().perform(get("/api/integration/qualityassurancetopics/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();
QAEventBuilder.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/qualityassurancetopics/search/bySource")
.param("source", "openaire"))
.andExpect(status().isForbidden());
getClient().perform(get("/api/integration/qualityassurancetopics/search/bySource")
.param("source", "openaire"))
.andExpect(status().isUnauthorized());
}
}

View File

@@ -12,6 +12,7 @@ import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.is;
import org.dspace.app.rest.model.hateoas.QATopicResource;
import org.dspace.content.QAEvent;
import org.hamcrest.Matcher;
/**
@@ -24,22 +25,41 @@ public class QATopicMatcher {
private QATopicMatcher() { }
public static Matcher<? super Object> matchQATopicEntry(String key, int totalEvents) {
public static Matcher<? super Object> matchQATopicEntry(String topicName, int totalEvents) {
return matchQATopicEntry(QAEvent.OPENAIRE_SOURCE, topicName, totalEvents);
}
public static Matcher<? super Object> matchQATopicEntry(String topicName) {
return matchQATopicEntry(QAEvent.OPENAIRE_SOURCE, topicName);
}
public static Matcher<? super Object> matchQATopicEntry(String source, String topicName, int totalEvents) {
return allOf(
hasJsonPath("$.type", is("qualityassurancetopic")),
hasJsonPath("$.name", is(key)),
hasJsonPath("$.id", is(key.replace("/", "!"))),
hasJsonPath("$.name", is(topicName)),
hasJsonPath("$.id", is(source + ":" + topicName.replace("/", "!"))),
hasJsonPath("$.totalEvents", is(totalEvents))
);
}
public static Matcher<? super Object> matchQATopicEntry(String key) {
public static Matcher<? super Object> matchQATopicEntry(String source, String topicName) {
return allOf(
hasJsonPath("$.type", is("qualityassurancetopic")),
hasJsonPath("$.name", is(key)),
hasJsonPath("$.id", is(key.replace("/", "/")))
hasJsonPath("$.name", is(topicName)),
hasJsonPath("$.id", is(source + ":" + topicName.replace("/", "!")))
);
}
public static Matcher<? super Object> matchQATopicEntry(String source, String topicName, String itemUuid,
int totalEvents) {
return allOf(
hasJsonPath("$.type", is("qualityassurancetopic")),
hasJsonPath("$.name", is(topicName)),
hasJsonPath("$.id", is(source + ":" + topicName.replace("/", "!") + ":" + itemUuid)),
hasJsonPath("$.totalEvents", is(totalEvents))
);
}
}