Skip to content

Commit

Permalink
Merge pull request #467 from m-wessendorf/master
Browse files Browse the repository at this point in the history
wbSearchEntities expose IOException
  • Loading branch information
wetneb committed Dec 5, 2019
2 parents 685664d + 3247ac5 commit 1b6fabe
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 26 deletions.
Expand Up @@ -78,7 +78,7 @@ public WbSearchEntitiesAction(ApiConnection connection, String siteUri) {
}

public List<WbSearchEntitiesResult> wbSearchEntities(WbGetEntitiesSearchData properties)
throws MediaWikiApiErrorException {
throws MediaWikiApiErrorException, IOException {
return wbSearchEntities(properties.search, properties.language,
properties.strictlanguage, properties.type, properties.limit, properties.offset);
}
Expand Down Expand Up @@ -123,7 +123,7 @@ public List<WbSearchEntitiesResult> wbSearchEntities(WbGetEntitiesSearchData pro
*/
public List<WbSearchEntitiesResult> wbSearchEntities(String search, String language,
Boolean strictLanguage, String type, Long limit, Long offset)
throws MediaWikiApiErrorException {
throws MediaWikiApiErrorException, IOException {

Map<String, String> parameters = new HashMap<>();
parameters.put(ApiConnection.PARAM_ACTION, "wbsearchentities");
Expand Down Expand Up @@ -159,22 +159,18 @@ public List<WbSearchEntitiesResult> wbSearchEntities(String search, String langu

List<WbSearchEntitiesResult> results = new ArrayList<>();

try {
JsonNode root = this.connection.sendJsonRequest("POST", parameters);
JsonNode entities = root.path("search");
for (JsonNode entityNode : entities) {
try {
JacksonWbSearchEntitiesResult ed = mapper.treeToValue(entityNode,
JacksonWbSearchEntitiesResult.class);
results.add(ed);
} catch (JsonProcessingException e) {
LOGGER.error("Error when reading JSON for entity "
+ entityNode.path("id").asText("UNKNOWN") + ": "
+ e.toString());
}
JsonNode root = this.connection.sendJsonRequest("POST", parameters);
JsonNode entities = root.path("search");
for (JsonNode entityNode : entities) {
try {
JacksonWbSearchEntitiesResult ed = mapper.treeToValue(entityNode,
JacksonWbSearchEntitiesResult.class);
results.add(ed);
} catch (JsonProcessingException e) {
LOGGER.error("Error when reading JSON for entity "
+ entityNode.path("id").asText("UNKNOWN") + ": "
+ e.toString());
}
} catch (IOException e) {
LOGGER.error("Could not retrive data: " + e.toString());
}

return results;
Expand Down
Expand Up @@ -306,23 +306,23 @@ Map<String, EntityDocument> getEntityDocumentMap(int numOfEntities,
}

public List<WbSearchEntitiesResult> searchEntities(String search)
throws MediaWikiApiErrorException {
throws MediaWikiApiErrorException, IOException {
WbGetEntitiesSearchData properties = new WbGetEntitiesSearchData();
properties.search = search;
properties.language = "en";
return searchEntities(properties);
}

public List<WbSearchEntitiesResult> searchEntities(String search, String language)
throws MediaWikiApiErrorException {
throws MediaWikiApiErrorException, IOException {
WbGetEntitiesSearchData properties = new WbGetEntitiesSearchData();
properties.search = search;
properties.language = language;
return searchEntities(properties);
}

public List<WbSearchEntitiesResult> searchEntities(String search, Long limit)
throws MediaWikiApiErrorException {
throws MediaWikiApiErrorException, IOException {
WbGetEntitiesSearchData properties = new WbGetEntitiesSearchData();
properties.search = search;
properties.language = "en";
Expand All @@ -331,7 +331,7 @@ public List<WbSearchEntitiesResult> searchEntities(String search, Long limit)
}

public List<WbSearchEntitiesResult> searchEntities(String search, String language, Long limit)
throws MediaWikiApiErrorException {
throws MediaWikiApiErrorException, IOException {
WbGetEntitiesSearchData properties = new WbGetEntitiesSearchData();
properties.search = search;
properties.language = language;
Expand All @@ -340,7 +340,7 @@ public List<WbSearchEntitiesResult> searchEntities(String search, String languag
}

public List<WbSearchEntitiesResult> searchEntities(WbGetEntitiesSearchData properties)
throws MediaWikiApiErrorException {
throws MediaWikiApiErrorException, IOException {
return this.wbSearchEntitiesAction.wbSearchEntities(properties);
}

Expand Down
Expand Up @@ -23,6 +23,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -60,7 +61,7 @@ public void setUp() throws Exception {
}

@Test
public void testWbSearchEntities() throws MediaWikiApiErrorException {
public void testWbSearchEntities() throws MediaWikiApiErrorException, IOException {
List<WbSearchEntitiesResult> results = action.wbSearchEntities("abc",
"en", null, null, null, null);

Expand All @@ -85,7 +86,7 @@ public void testWbSearchEntities() throws MediaWikiApiErrorException {
}

@Test
public void testWbSearchEntitiesEmpty() throws MediaWikiApiErrorException {
public void testWbSearchEntitiesEmpty() throws MediaWikiApiErrorException, IOException {
List<WbSearchEntitiesResult> results = action.wbSearchEntities(
"some search string with no results", "en", null, null, null,
null);
Expand All @@ -94,12 +95,12 @@ public void testWbSearchEntitiesEmpty() throws MediaWikiApiErrorException {
}

@Test(expected = IllegalArgumentException.class)
public void testIdsAndTitles() throws MediaWikiApiErrorException {
public void testIdsAndTitles() throws MediaWikiApiErrorException, IOException {
action.wbSearchEntities(null, "en", null, null, null, null);
}

@Test(expected = IllegalArgumentException.class)
public void testIdsAndSites() throws MediaWikiApiErrorException {
public void testIdsAndSites() throws MediaWikiApiErrorException, IOException {
action.wbSearchEntities("abc", null, null, null, null, null);
}
}

0 comments on commit 1b6fabe

Please sign in to comment.