Skip to content
This repository was archived by the owner on Oct 30, 2018. It is now read-only.

Commit a655d05

Browse files
committed
#8 update all tests + general refactoring
1 parent 6d5396f commit a655d05

32 files changed

+612
-1197
lines changed

pom.xml

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
</properties>
1515

1616
<dependencies>
17+
<dependency>
18+
<groupId>log4j</groupId>
19+
<artifactId>log4j</artifactId>
20+
<version>1.2.17</version>
21+
</dependency>
1722
<dependency>
1823
<groupId>commons-io</groupId>
1924
<artifactId>commons-io</artifactId>
@@ -24,19 +29,21 @@
2429
<artifactId>commons-lang3</artifactId>
2530
<version>3.4</version>
2631
</dependency>
32+
33+
<!-- libs for parsing api response -->
2734
<dependency>
2835
<groupId>org.jsoup</groupId>
2936
<artifactId>jsoup</artifactId>
3037
<version>1.8.2</version>
3138
</dependency>
32-
33-
3439
<dependency>
35-
<groupId>log4j</groupId>
36-
<artifactId>log4j</artifactId>
37-
<version>1.2.17</version>
40+
<groupId>org.json</groupId>
41+
<artifactId>json</artifactId>
42+
<version>20141113</version>
3843
</dependency>
3944

45+
46+
<!-- test -->
4047
<dependency>
4148
<groupId>junit</groupId>
4249
<artifactId>junit</artifactId>

src/main/java/com/alchemyapi/api/AlchemyApi.java

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,14 @@
1515
import com.alchemyapi.api.parameters.TaxonomyParameters;
1616
import com.alchemyapi.api.parameters.TextParameters;
1717
import org.apache.commons.io.IOUtils;
18+
import org.apache.commons.lang3.StringUtils;
1819
import org.apache.log4j.Logger;
20+
import org.json.JSONObject;
1921
import org.jsoup.Jsoup;
2022
import org.jsoup.nodes.Document;
2123
import org.jsoup.nodes.Element;
2224
import org.jsoup.parser.Parser;
23-
import org.xml.sax.SAXException;
2425

25-
import javax.xml.parsers.ParserConfigurationException;
26-
import javax.xml.xpath.XPathExpressionException;
2726
import java.io.DataOutputStream;
2827
import java.io.IOException;
2928
import java.net.HttpURLConnection;
@@ -33,6 +32,9 @@
3332
import static org.apache.commons.lang3.StringUtils.length;
3433
import static org.apache.commons.lang3.StringUtils.trimToEmpty;
3534

35+
/**
36+
* Created by kenny
37+
*/
3638
public class AlchemyApi {
3739

3840
private static final Logger LOGGER = Logger.getLogger(AlchemyApi.class);
@@ -146,8 +148,7 @@ public Document htmlGetRankedKeywords(final String html, final String url, final
146148
return post("HTMLGetRankedKeywords", "html", params);
147149
}
148150

149-
public Document textGetRankedKeywords(final String text) throws IOException, SAXException,
150-
ParserConfigurationException, XPathExpressionException {
151+
public Document textGetRankedKeywords(final String text) {
151152
return textGetRankedKeywords(text, new KeywordParameters());
152153
}
153154

@@ -403,11 +404,11 @@ public Document urlGetRelations(final String url, final RelationParameters param
403404
return get("URLGetRelations", "url", params);
404405
}
405406

406-
public Document HTMLGetRelations(final String html, final String url) {
407-
return HTMLGetRelations(html, url, new RelationParameters());
407+
public Document htmlGetRelations(final String html, final String url) {
408+
return htmlGetRelations(html, url, new RelationParameters());
408409
}
409410

410-
public Document HTMLGetRelations(final String html, final String url, final RelationParameters params) {
411+
public Document htmlGetRelations(final String html, final String url, final RelationParameters params) {
411412
params.setUrl(url);
412413
params.setHtml(html);
413414
return post("HTMLGetRelations", "html", params);
@@ -547,7 +548,7 @@ private Document post(final String callName, final String callPrefix, final Para
547548
}
548549
}
549550

550-
// TODO add json handling
551+
// TODO support json, by default
551552
// TODO return pojo with parsed field, but allow a "raw" xml/json getter to protect against api updates
552553
private Document doRequest(final HttpURLConnection httpURLConnection, final Parameters parameters) {
553554
try {
@@ -562,7 +563,8 @@ private Document doRequest(final HttpURLConnection httpURLConnection, final Para
562563
return praseRdf(response, parameters);
563564

564565
case Parameters.OUTPUT_JSON:
565-
throw new AlchemyApiException("Json Response not supported yet");
566+
// return parseJson(response, parameters);
567+
throw new AlchemyApiException("Json responses are not currently supported");
566568

567569
default:
568570
throw new AlchemyApiException("Unknown output mode, must be one of [xml,rdf,json]");
@@ -572,6 +574,20 @@ private Document doRequest(final HttpURLConnection httpURLConnection, final Para
572574
}
573575
}
574576

577+
private JSONObject parseJson(final String response, final Parameters parameters) {
578+
final JSONObject json = new JSONObject(response);
579+
if(json.has("results")) {
580+
final JSONObject results = json.getJSONObject("results");
581+
if(!StringUtils.equals(results.optString("status"), "OK")) {
582+
if(results.has("statusInfo")) {
583+
throw new AlchemyApiException("Error making API call: " + results.optString("statusInfo"));
584+
}
585+
throw new AlchemyApiException("Error making API call: " + results.optString("status"));
586+
}
587+
}
588+
return json;
589+
}
590+
575591
private Document parseXml(final String response, final Parameters parameters) {
576592
final Document document = Jsoup.parse(response, parameters.getEncoding(), Parser.xmlParser());
577593

@@ -586,11 +602,10 @@ private Document parseXml(final String response, final Parameters parameters) {
586602
return document;
587603
}
588604

589-
// TODO investigate rdf format
590605
private Document praseRdf(final String response, final Parameters parameters) {
591606
final Document document = Jsoup.parse(response, parameters.getEncoding(), Parser.xmlParser());
592-
593-
final Element status = document.select("RDF > Description > ResultStatus").first();
607+
System.out.println("RAW: " + response);
608+
final Element status = document.select("rdf|RDF > rdf|Description > aapi|ResultStatus").first();
594609
if (status == null || !status.text().equals("OK")) {
595610
throw new AlchemyApiException("Error making API call: " + status);
596611
}
@@ -601,10 +616,4 @@ private String buildBaseApiUrl() {
601616
return API_URL.replace("{SUB_DOMAIN}", configuration.getApiSubDomain());
602617
}
603618

604-
private String parseBaseUrl(final HttpURLConnection httpURLConnection) {
605-
final URL url = httpURLConnection.getURL();
606-
String path = url.getFile().substring(0, url.getFile().lastIndexOf('/'));
607-
return url.getProtocol() + "://" + url.getHost() + path;
608-
}
609-
610619
}

src/test/java/com/alchemyapi/STestAuthor.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,22 @@
88

99
import java.io.File;
1010

11+
/**
12+
* Created by kenny
13+
*/
1114
public class STestAuthor {
1215

1316
private final AlchemyApi alchemyApi = TestApiFactory.build(new File(System.getProperty("user.home"), ".alchemy/api.key"));
1417

1518
@Test
16-
public void parseFromTestData() {
19+
public void html() {
1720
final String html = ResourceUtils.toString("data/example.html");
1821
final Document document = alchemyApi.htmlGetAuthor(html, "http://www.test.com/");
1922
System.out.println(document);
2023
}
2124

2225
@Test
23-
public void parseFromUrl() {
26+
public void url() {
2427
final Document document = alchemyApi.urlGetAuthor("http://www.politico.com/blogs/media/2012/02/detroit-news-ed-upset-over-romney-edit-115247.html");
2528
System.out.println(document);
2629
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.alchemyapi;
2+
3+
import com.alchemyapi.api.AlchemyApi;
4+
import com.alchemyapi.api.parameters.CategoryParameters;
5+
import com.alchemyapi.api.parameters.Parameters;
6+
import com.alchemyapi.helpers.ResourceUtils;
7+
import com.alchemyapi.helpers.TestApiFactory;
8+
import org.jsoup.nodes.Document;
9+
import org.junit.Test;
10+
11+
import java.io.File;
12+
13+
/**
14+
* Created by kenny
15+
*/
16+
public class STestCategory {
17+
18+
private final AlchemyApi alchemyApi = TestApiFactory.build(new File(System.getProperty("user.home"), ".alchemy/api.key"));
19+
20+
@Test
21+
public void url() {
22+
final Document document = alchemyApi.urlGetCategory("http://www.techcrunch.com/");
23+
System.out.println(document);
24+
}
25+
26+
@Test
27+
public void text() {
28+
final Document document = alchemyApi.textGetCategory("Latest on the War in Iraq.");
29+
System.out.println(document);
30+
}
31+
32+
@Test
33+
public void html() {
34+
final String html = ResourceUtils.toString("data/example.html");
35+
final Document document = alchemyApi.htmlGetCategory(html, "http://www.test.com/");
36+
System.out.println(document);
37+
}
38+
39+
@Test
40+
public void htmlRdfFormat() {
41+
final String html = ResourceUtils.toString("data/example.html");
42+
final CategoryParameters categoryParameters = new CategoryParameters();
43+
categoryParameters.setOutputMode(Parameters.OUTPUT_RDF);
44+
final Document document2 = alchemyApi.htmlGetCategory(html, "http://www.test.com/", categoryParameters);
45+
System.out.println(document2);
46+
}
47+
48+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.alchemyapi;
2+
3+
import com.alchemyapi.api.AlchemyApi;
4+
import com.alchemyapi.api.parameters.CombinedParameters;
5+
import com.alchemyapi.helpers.TestApiFactory;
6+
import org.jsoup.nodes.Document;
7+
import org.junit.Test;
8+
9+
import java.io.File;
10+
11+
/**
12+
* Created by kenny
13+
*/
14+
public class STestCombined {
15+
16+
private final AlchemyApi alchemyApi = TestApiFactory.build(new File(System.getProperty("user.home"), ".alchemy/api.key"));
17+
18+
@Test
19+
public void url() {
20+
final Document document = alchemyApi.urlGetCombined("http://www.techcrunch.com/");
21+
System.out.println(document);
22+
}
23+
24+
@Test
25+
public void text() {
26+
final Document document = alchemyApi.textGetCombined(
27+
"Hello there, my name is Bob Jones. I live in the United States of America. Where do you live, Fred?");
28+
System.out.println(document);
29+
30+
// Only extract entities & keywords
31+
final CombinedParameters combinedParams = new CombinedParameters();
32+
combinedParams.setSentiment(true);
33+
combinedParams.setExtract("entity");
34+
combinedParams.setExtract("keyword");
35+
final Document document2 = alchemyApi.textGetCombined("Madonna enjoys tasty Pepsi. I love her style.", combinedParams);
36+
System.out.println(document2);
37+
}
38+
39+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.alchemyapi;
2+
3+
import com.alchemyapi.api.AlchemyApi;
4+
import com.alchemyapi.helpers.ResourceUtils;
5+
import com.alchemyapi.helpers.TestApiFactory;
6+
import org.jsoup.nodes.Document;
7+
import org.junit.Test;
8+
9+
import java.io.File;
10+
11+
/**
12+
* Created by kenny
13+
*/
14+
public class STestConcept {
15+
16+
private final AlchemyApi alchemyApi = TestApiFactory.build(new File(System.getProperty("user.home"), ".alchemy/api.key"));
17+
18+
@Test
19+
public void url() {
20+
final Document document = alchemyApi.urlGetRankedConcepts("http://www.techcrunch.com/");
21+
System.out.println(document);
22+
}
23+
24+
@Test
25+
public void text() {
26+
final Document document = alchemyApi.textGetRankedConcepts(
27+
"This thing has a steering wheel, tires, and an engine. Do you know what it is?");
28+
System.out.println(document);
29+
}
30+
31+
@Test
32+
public void html() {
33+
final String html = ResourceUtils.toString("data/example.html");
34+
final Document document = alchemyApi.htmlGetRankedConcepts(html, "http://www.test.com/");
35+
System.out.println(document);
36+
}
37+
38+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.alchemyapi;
2+
3+
import com.alchemyapi.api.AlchemyApi;
4+
import com.alchemyapi.helpers.ResourceUtils;
5+
import com.alchemyapi.helpers.TestApiFactory;
6+
import org.jsoup.nodes.Document;
7+
import org.junit.Test;
8+
9+
import java.io.File;
10+
11+
/**
12+
* Created by kenny
13+
*/
14+
public class STestConstraintQuery {
15+
16+
private final AlchemyApi alchemyApi = TestApiFactory.build(new File(System.getProperty("user.home"), ".alchemy/api.key"));
17+
18+
@Test
19+
public void url() {
20+
final Document document = alchemyApi.urlGetConstraintQuery("http://microformats.org/wiki/hcard", "1st link");
21+
System.out.println(document);
22+
}
23+
24+
@Test
25+
public void html() {
26+
final String html = ResourceUtils.toString("data/example.html");
27+
final Document document = alchemyApi.htmlGetConstraintQuery(html, "http://www.test.com/", "1st link");
28+
System.out.println(document);
29+
}
30+
31+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.alchemyapi;
2+
3+
import com.alchemyapi.api.AlchemyApi;
4+
import com.alchemyapi.helpers.ResourceUtils;
5+
import com.alchemyapi.helpers.TestApiFactory;
6+
import org.jsoup.nodes.Document;
7+
import org.junit.Test;
8+
9+
import java.io.File;
10+
11+
/**
12+
* Created by kenny
13+
*/
14+
public class STestEntity {
15+
16+
private final AlchemyApi alchemyApi = TestApiFactory.build(new File(System.getProperty("user.home"), ".alchemy/api.key"));
17+
18+
@Test
19+
public void url() {
20+
final Document document = alchemyApi.urlGetRankedNamedEntities("http://www.techcrunch.com/");
21+
System.out.println(document);
22+
}
23+
24+
@Test
25+
public void text() {
26+
final Document document = alchemyApi.textGetRankedNamedEntities(
27+
"Hello there, my name is Bob Jones. I live in the United States of America. Where do you live, Fred?");
28+
System.out.println(document);
29+
}
30+
31+
@Test
32+
public void html() {
33+
final String html = ResourceUtils.toString("data/example.html");
34+
final Document document = alchemyApi.htmlGetRankedNamedEntities(html, "http://www.test.com/");
35+
System.out.println(document);
36+
}
37+
38+
}

0 commit comments

Comments
 (0)