Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip processing in EmbeddablePlumXMetricProvider as soon as possible #407

Open
wants to merge 5 commits into
base: dspace-cris-7
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -139,31 +139,31 @@ public void setPublicationListViewEnabled(boolean publicationListViewEnabled) {

@Override
public boolean hasMetric(Context context, Item item, List<CrisMetrics> retrivedStoredMetrics) {
if (! isEnabled()) {
return false;
}

String entityType = getEntityType(item);
if (entityType != null) {
if (entityType.equals("Person")) {
// if it is of type person use orcid
String orcid = getItemService()
.getMetadataFirstValue(item, "person", "identifier", "orcid", Item.ANY);
if (orcid != null) {
return true;
} else {
if (orcid == null) {
return false;
}
return personListViewEnabled || personDetailViewEnabled;
} else {
// if it is of type publication use doi
if (entityType.equals("Publication")) {
String doiIdentifier = getItemService()
.getMetadataFirstValue(item, "dc", "identifier", "doi", Item.ANY);
if (doiIdentifier != null) {
return true;
} else {
if (doiIdentifier == null) {
return false;
}
return publicationListViewEnabled || publicationDetailViewEnabled;
}
}
} else {
return false;
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

import org.dspace.content.Item;
Expand All @@ -19,6 +18,7 @@
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

Expand All @@ -31,7 +31,7 @@
public class EmbeddablePlumXMetricProviderTest {
@Mock
private ItemService itemService;
@Mock
@InjectMocks
private EmbeddablePlumXMetricProvider provider;
@Mock
Context context;
Expand All @@ -40,20 +40,15 @@ public class EmbeddablePlumXMetricProviderTest {

@Before
public void setUp() throws Exception {
when(provider.innerHtml(any(), any())).thenCallRealMethod();
when(provider.getEntityType(any())).thenCallRealMethod();
when(provider.hasMetric(any(), any(), any())).thenCallRealMethod();
when(provider.getItemService()).thenReturn(itemService);

provider.personPlumXScript = "//cdn.plu.mx/widget-person.js";
provider.publicationPlumXScript = "//cdn.plu.mx/widget-popup.js";
provider.publicationHref = "https://plu.mx/plum/a/";
provider.personHref = "https://plu.mx/plum/u/";
provider.dataNumArtifacts = 5;
provider.dataWidth = "350px";
provider.dataPopup = "left";
provider.listDataWidth = "350px";
provider.listDataPopup = "left";
provider.setPersonPlumXScript("//cdn.plu.mx/widget-person.js");
provider.setPublicationPlumXScript("//cdn.plu.mx/widget-popup.js");
provider.setPublicationHref("https://plu.mx/plum/a/");
provider.setPersonHref("https://plu.mx/plum/u/");
provider.setDataNumArtifacts(5);
provider.setDataWidth("350px");
provider.setDataPopup("left");
provider.setListDataWidth("350px");
provider.setListDataPopup("left");
}

@Test
Expand All @@ -64,14 +59,16 @@ public void hasMetricEmptyEntityType() {

@Test
public void hasMetricPublicationWithoutDoi() {
provider.setPublicationListViewEnabled(true);
when(itemService.getMetadataFirstValue(item, "dspace", "entity", "type", Item.ANY)).thenReturn("Publication");
when(itemService.getMetadataFirstValue(item, "dc", "identifier", "doi", Item.ANY)).thenReturn(null);
boolean hasMetric = provider.hasMetric(context, item, null);
assertFalse(hasMetric);
}

@Test
public void hasMetricPublicationWithDoi() {
public void hasMetricPublicationWithDoiEnabled() {
provider.setPublicationListViewEnabled(true);
when(itemService.getMetadataFirstValue(item, "dspace",
"entity", "type", Item.ANY)).thenReturn("Publication");
when(itemService.getMetadataFirstValue(item, "dc",
Expand All @@ -81,7 +78,19 @@ public void hasMetricPublicationWithDoi() {
}

@Test
public void hasMetricPublicationWithOrcid() {
public void hasMetricPublicationWithDoiDisabled() {
provider.setPersonListViewEnabled(true);
when(itemService.getMetadataFirstValue(item, "dspace",
"entity", "type", Item.ANY)).thenReturn("Publication");
when(itemService.getMetadataFirstValue(item, "dc",
"identifier", "doi", Item.ANY)).thenReturn("10.1016/j.gene.2009.04.019");
boolean hasMetric = provider.hasMetric(context, item, null);
assertFalse(hasMetric);
}

@Test
public void hasMetricPersonWithOrcidEnabled() {
provider.setPersonListViewEnabled(true);
when(itemService.getMetadataFirstValue(item, "dspace",
"entity", "type", Item.ANY)).thenReturn("Person");
when(itemService.getMetadataFirstValue(item, "person",
Expand All @@ -91,7 +100,19 @@ public void hasMetricPublicationWithOrcid() {
}

@Test
public void hasMetricPublicationWithoutOrcid() {
public void hasMetricPersonWithOrcidDisabled() {
provider.setPublicationListViewEnabled(true);
when(itemService.getMetadataFirstValue(item, "dspace",
"entity", "type", Item.ANY)).thenReturn("Person");
when(itemService.getMetadataFirstValue(item, "person",
"identifier", "orcid", Item.ANY)).thenReturn("0000-0002-9029-1854");
boolean hasMetric = provider.hasMetric(context, item, null);
assertFalse(hasMetric);
}

@Test
public void hasMetricPersonWithoutOrcid() {
provider.setPersonListViewEnabled(true);
when(itemService.getMetadataFirstValue(item, "dspace", "entity", "type", Item.ANY)).thenReturn("Person");
when(itemService.getMetadataFirstValue(item, "person", "identifier", "orcid", Item.ANY)).thenReturn(null);
boolean hasMetric = provider.hasMetric(context, item, null);
Expand All @@ -100,12 +121,14 @@ public void hasMetricPublicationWithoutOrcid() {

@Test
public void innerHtmlForPersonItem() {
provider.setPersonListViewEnabled(true);
provider.setPersonDetailViewEnabled(true);
when(itemService.getMetadataFirstValue(item, "dspace", "entity", "type", Item.ANY)).thenReturn("Person");
when(itemService.getMetadataFirstValue(item, "person", "identifier", "orcid", Item.ANY))
.thenReturn("0000-0002-9029-1854");
String template = provider.innerHtml(context, item);

assertEquals("{\"data-person-badge-enabled\":false,\"list-data-person-badge-enabled\":false," +
assertEquals("{\"data-person-badge-enabled\":true,\"list-data-person-badge-enabled\":true," +
"\"data-publication-badge-enabled\":false,\"list-data-publication-badge-enabled\":false," +
"\"type\":\"Person\",\"list-type\":\"Person\",\"placeholder\":\"\",\"list-placeholder\":\"\"," +
"\"src\":\"//cdn.plu.mx/widget-person.js\",\"href\":\"https://plu.mx/plum/u/?orcid=0000-0002-9029-1854\"," +
Expand All @@ -126,13 +149,15 @@ public void innerHtmlForPersonItem() {

@Test
public void innerHtmlForPublicationItem() {
provider.setPublicationListViewEnabled(true);
provider.setPublicationDetailViewEnabled(true);
when(itemService.getMetadataFirstValue(item, "dspace", "entity", "type", Item.ANY)).thenReturn("Publication");
when(itemService.getMetadataFirstValue(item, "dc", "identifier", "doi", Item.ANY))
.thenReturn("10.1016/j.gene.2009.04.019");
String template = provider.innerHtml(context, item);

assertEquals("{\"data-person-badge-enabled\":false,\"list-data-person-badge-enabled\":false," +
"\"data-publication-badge-enabled\":false,\"list-data-publication-badge-enabled\":false," +
"\"data-publication-badge-enabled\":true,\"list-data-publication-badge-enabled\":true," +
"\"type\":\"Publication\",\"list-type\":\"Publication\",\"placeholder\":\"\"," +
"\"list-placeholder\":\"\",\"src\":\"//cdn.plu.mx/widget-popup.js\"," +
"\"href\":\"https://plu.mx/plum/a/?doi=10.1016/j.gene.2009.04.019\"," +
Expand All @@ -150,4 +175,29 @@ public void innerHtmlForPublicationItem() {
"\"list-data-hide-socialmedia\":false,\"list-data-hide-citations\":false," +
"\"list-data-pass-hidden-categories\":false,\"list-data-detail-same-page\":false}", template);
}

@Test
public void innerHtmlForOtherItem() {
when(itemService.getMetadataFirstValue(item, "dspace", "entity", "type", Item.ANY)).thenReturn("Other");
String template = provider.innerHtml(context, item);

assertEquals("{\"data-person-badge-enabled\":false,\"list-data-person-badge-enabled\":false," +
"\"data-publication-badge-enabled\":false,\"list-data-publication-badge-enabled\":false," +
"\"type\":\"Other\",\"list-type\":\"Other\",\"placeholder\":\"\",\"list-placeholder\":\"\"," +
"\"src\":\"//cdn.plu.mx/widget-popup.js\",\"href\":\"https://plu.mx/plum/a/?doi=null\"," +
"\"list-src\":\"//cdn.plu.mx/widget-popup.js\"," +
"\"list-href\":\"https://plu.mx/plum/a/?doi=null\",\"data-no-name\":false," +
"\"data-num-artifacts\":5,\"data-width\":\"350px\",\"data-no-description\":false," +
"\"data-no-stats\":false,\"data-no-thumbnail\":false,\"data-no-artifacts\":false," +
"\"data-popup\":\"left\",\"data-hide-when-empty\":false,\"data-hide-usage\":false," +
"\"data-hide-captures\":false,\"data-hide-mentions\":false,\"data-hide-socialmedia\":false," +
"\"data-hide-citations\":false,\"data-pass-hidden-categories\":false,\"data-detail-same-page\":false," +
"\"list-data-no-name\":false,\"list-data-num-artifacts\":0,\"list-data-width\":\"350px\"," +
"\"list-data-no-description\":false,\"list-data-no-stats\":false,\"list-data-no-thumbnail\":false," +
"\"list-data-no-artifacts\":false,\"list-data-popup\":\"left\",\"list-data-hide-when-empty\":false," +
"\"list-data-hide-usage\":false,\"list-data-hide-captures\":false,\"list-data-hide-mentions\":false," +
"\"list-data-hide-socialmedia\":false,\"list-data-hide-citations\":false," +
"\"list-data-pass-hidden-categories\":false,\"list-data-detail-same-page\":false}", template);
}

}
Loading