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

[Breaking] Uses ItemIdValue instead of String for badges #369

Merged
merged 1 commit into from May 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -532,7 +532,7 @@ public static StatementGroup makeStatementGroup(List<Statement> statements) {
* @return a {@link SiteLink} corresponding to the input
*/
public static SiteLink makeSiteLink(String title, String siteKey,
List<String> badges) {
List<ItemIdValue> badges) {
return factory.getSiteLink(title, siteKey, badges);
}

Expand Down
Expand Up @@ -119,7 +119,7 @@ public ItemDocumentBuilder withSiteLink(SiteLink siteLink) {
* one or more badges
*/
public ItemDocumentBuilder withSiteLink(String title, String siteKey,
String... badges) {
ItemIdValue... badges) {
withSiteLink(factory.getSiteLink(title, siteKey, Arrays.asList(badges)));
return this;
}
Expand Down
Expand Up @@ -191,7 +191,7 @@ public StatementGroup getStatementGroup(List<Statement> statements) {

@Override
public SiteLink getSiteLink(String title, String siteKey,
List<String> badges) {
List<ItemIdValue> badges) {
return new SiteLinkImpl(title, siteKey, badges);
}

Expand Down
@@ -1,7 +1,5 @@
package org.wikidata.wdtk.datamodel.implementation;

import java.util.Collections;

/*
* #%L
* Wikidata Toolkit Data Model
Expand All @@ -22,30 +20,33 @@
* #L%
*/

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import com.fasterxml.jackson.annotation.*;
import org.apache.commons.lang3.Validate;
import org.wikidata.wdtk.datamodel.helpers.Equality;
import org.wikidata.wdtk.datamodel.helpers.Hash;
import org.wikidata.wdtk.datamodel.helpers.ToString;
import org.wikidata.wdtk.datamodel.interfaces.EntityIdValue;
import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue;
import org.wikidata.wdtk.datamodel.interfaces.SiteLink;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;

/**
* Jackson implementation of {@link SiteLink}.
*
* @author Fredo Erxleben
* @author Antonin Delpeuch
* @author Thomas Pellissier Tanon
*/
@JsonIgnoreProperties(ignoreUnknown = true)
public class SiteLinkImpl implements SiteLink {

private final String title;
private final String site;
private final List<String> badges;
private final List<ItemIdValue> badges;

/**
* Constructor.
Expand All @@ -58,20 +59,46 @@ public class SiteLinkImpl implements SiteLink {
* the list of badge identifiers worn by this site link.
* Can be null.
*/
@JsonCreator
public SiteLinkImpl(
String title,
String site,
List<ItemIdValue> badges) {
Validate.notNull(title);
this.title = title;
Validate.notNull(site);
this.site = site;
this.badges = (badges == null) ? Collections.emptyList() : badges;
this.badges.sort(Comparator.comparing(EntityIdValue::getId));
}

/**
* Constructor.
*
* @param title
* the title of the page on the target site
* @param site
* the identifier of the target site (such as "dewiki")
* @param badges
* the list of badge identifiers worn by this site link.
* Can be null.
*/
@JsonCreator
SiteLinkImpl(
@JsonProperty("title") String title,
@JsonProperty("site") String site,
@JsonProperty("badges") List<String> badges) {
@JsonProperty("badges") List<String> badges,
@JacksonInject("siteIri") String siteIri
) {
Validate.notNull(title);
this.title = title;
Validate.notNull(site);
this.site = site;
if (badges != null) {
this.badges = badges;
} else {
this.badges = Collections.emptyList();
}
this.badges = (badges == null || badges.isEmpty())
? Collections.emptyList()
: badges.stream()
.sorted()
.map(id -> new ItemIdValueImpl(id, siteIri))
.collect(Collectors.toList());
}

@JsonProperty("title")
Expand All @@ -86,11 +113,22 @@ public String getSiteKey() {
return this.site;
}

@JsonIgnore
@Override
public List<String> getBadges() {
public List<ItemIdValue> getBadges() {
return this.badges;
}

@JsonProperty("badges")
List<String> getBadgesString() {
if (badges.isEmpty()) {
return Collections.emptyList();
}
return badges.stream()
.map(EntityIdValue::getId)
.collect(Collectors.toList());
}

@Override
public int hashCode() {
return Hash.hashCode(this);
Expand Down
Expand Up @@ -358,7 +358,7 @@ Statement getStatement(Claim claim, List<Reference> references,
* the list of badges of the linked article
* @return a {@link SiteLink} corresponding to the input
*/
SiteLink getSiteLink(String title, String siteKey, List<String> badges);
SiteLink getSiteLink(String title, String siteKey, List<ItemIdValue> badges);

/**
* Creates a {@link PropertyDocument}. It might be more convenient to use
Expand Down
Expand Up @@ -50,10 +50,7 @@ public interface SiteLink {

/**
* Get the list of badges of the linked article.
*
* TODO This feature is not used yet in Wikibase. This interface might
* change when more details are known.
*/
List<String> getBadges();
List<ItemIdValue> getBadges();

}
Expand Up @@ -75,7 +75,7 @@ public void testComplexItemDocumentBuild() {
MonolingualTextValue mtv = Datamodel.makeMonolingualTextValue("Test",
"de");
SiteLink sl = Datamodel.makeSiteLink("Test", "frwiki",
Collections.singletonList("Badge"));
Collections.singletonList(Datamodel.makeWikidataItemIdValue("Q42")));

ItemDocument id1 = Datamodel.makeItemDocument(i,
Collections.singletonList(mtv), Collections.singletonList(mtv),
Expand All @@ -85,7 +85,7 @@ public void testComplexItemDocumentBuild() {
ItemDocument id2 = ItemDocumentBuilder.forItemId(i)
.withLabel("Test", "de").withDescription("Test", "de")
.withAlias("Test", "de")
.withSiteLink("Test", "frwiki", "Badge").withStatement(s1)
.withSiteLink("Test", "frwiki", Datamodel.makeWikidataItemIdValue("Q42")).withStatement(s1)
.withStatement(s2).withRevisionId(1234).build();

assertEquals(id1, id2);
Expand All @@ -99,7 +99,7 @@ public void testModifyingBuild() {
"fr");
MonolingualTextValue alias2 = Datamodel.makeMonolingualTextValue("atoca", "fr");
SiteLink sl = Datamodel.makeSiteLink("Canneberge", "frwiki",
Collections.singletonList("Badge"));
Collections.singletonList(Datamodel.makeWikidataItemIdValue("Q42")));

ItemDocument initial = Datamodel.makeItemDocument(i,
Collections.singletonList(label),
Expand Down
Expand Up @@ -23,36 +23,42 @@
import static org.junit.Assert.*;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
import org.wikidata.wdtk.datamodel.helpers.DatamodelMapper;
import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue;
import org.wikidata.wdtk.datamodel.interfaces.SiteLink;

public class SiteLinkImplTest {

private final ObjectMapper mapper = new ObjectMapper();
private final ObjectMapper mapper = new DatamodelMapper("http://example.com/entity/");

private final SiteLink s1 = new SiteLinkImpl("Dresden", "enwiki", Collections.emptyList());
private final SiteLink s2 = new SiteLinkImpl("Dresden", "enwiki", Collections.emptyList());
private final String JSON_SITE_LINK = "{\"site\":\"enwiki\", \"title\":\"Dresden\", \"badges\":[]}";
private final List<ItemIdValue> badges = Arrays.asList(
new ItemIdValueImpl("Q43", "http://example.com/entity/"),
new ItemIdValueImpl("Q42", "http://example.com/entity/")
);
private final SiteLink s1 = new SiteLinkImpl("Dresden", "enwiki", badges);
private final SiteLink s2 = new SiteLinkImpl("Dresden", "enwiki", badges);
private final String JSON_SITE_LINK = "{\"site\":\"enwiki\", \"title\":\"Dresden\", \"badges\":[\"Q42\",\"Q43\"]}";

@Test
public void fieldsIsCorrect() {
assertEquals(s1.getPageTitle(), "Dresden");
assertEquals(s1.getSiteKey(), "enwiki");
assertEquals(s1.getBadges(), Collections.emptyList());
assertEquals(s1.getBadges(), badges);
}

@Test
public void equalityBasedOnContent() {
SiteLink sDiffTitle = new SiteLinkImpl("Berlin", "enwiki",
Collections.emptyList());
SiteLink sDiffSiteKey = new SiteLinkImpl("Dresden", "dewiki",
Collections.emptyList());
SiteLink sDiffTitle = new SiteLinkImpl("Berlin", "enwiki", badges);
SiteLink sDiffSiteKey = new SiteLinkImpl("Dresden", "dewiki", badges);
SiteLink sDiffBadges = new SiteLinkImpl("Dresden", "enwiki",
Collections.singletonList("some badge?"));
Collections.emptyList());

assertEquals(s1, s1);
assertEquals(s1, s2);
Expand Down Expand Up @@ -93,4 +99,4 @@ public void testToJson() throws JsonProcessingException {
public void testToJava() throws IOException {
assertEquals(s1, mapper.readValue(JSON_SITE_LINK, SiteLinkImpl.class));
}
};
}
Expand Up @@ -74,7 +74,7 @@ public void getSiteInformation() throws IOException {

DataObjectFactory factory = new DataObjectFactoryImpl();
SiteLink siteLink = factory.getSiteLink("Douglas Adams", "dewiki",
Collections.<String> emptyList());
Collections.emptyList());

Sites sites = this.dpc.getSitesInformation();

Expand Down
Expand Up @@ -329,9 +329,9 @@ public List<MonolingualTextValue> createDescriptions() {
public Map<String, SiteLink> createSiteLinks() {
Map<String, SiteLink> result = new HashMap<String, SiteLink>();
result.put("enwiki", factory.getSiteLink("title_en", "enwiki",
new LinkedList<String>()));
Collections.emptyList()));
result.put("dewiki", factory.getSiteLink("title_de", "dewiki",
new LinkedList<String>()));
Collections.emptyList()));
return result;
}

Expand Down