Skip to content

Commit

Permalink
Fixed merge conflicts for Pull Request jReddit#45 - refactoring of th…
Browse files Browse the repository at this point in the history
…e unit tests to avoid running them agains jReddit
  • Loading branch information
raulrene committed Apr 16, 2014
2 parents 443cd05 + 972db68 commit 40ce865
Show file tree
Hide file tree
Showing 41 changed files with 1,587 additions and 683 deletions.
9 changes: 5 additions & 4 deletions examples/GenerousBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import com.github.jreddit.submissions.Submissions.Page;
import com.github.jreddit.submissions.Submissions.Popularity;
import com.github.jreddit.user.User;
import com.github.jreddit.utils.Utils;

import com.github.jreddit.utils.restclient.HttpRestClient;
import com.github.jreddit.utils.restclient.RestClient;


/**
Expand All @@ -17,9 +17,10 @@ public static void main(String[] args) throws Exception {
String[] subreddits = { "programming", "funny", "wtf", "java",
"todayilearned", "redditdev" };

Utils.setUserAgent("Generous-Bot");
RestClient restClient = new HttpRestClient();
restClient.setUserAgent("Generous-Bot");

User user = new User("user", "password");
User user = new User(restclient, "user", "password");
user.connect();

for (int i = 0; i < subreddits.length; i++) {
Expand Down
11 changes: 7 additions & 4 deletions examples/OverviewBot.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@

import com.github.jreddit.submissions.Submission;
import com.github.jreddit.user.User;
import com.github.jreddit.utils.Utils;
import com.github.jreddit.utils.restclient.RestClient;
import com.github.jreddit.utils.restclient.HttpRestClient;

import java.util.List;

Expand All @@ -12,9 +13,11 @@
*/
public final class OverviewBot {
public static void main(String[] args) throws Exception {
Utils.setUserAgent("Overview-Bot");
User user = new User("user", "password");
user.connect();
RestClient restClient = new HttpRestClient();
restClient.setUserAgent("Overview-Bot");

User user = new User(restclient, "user", "password");
user.connect();

//Submission Array from user profile
List<Submission> submissions = user.getDislikedSubmissions();
Expand Down
32 changes: 31 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
Expand Down Expand Up @@ -66,6 +67,9 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- dependencies versions -->
<com.googlecode.jsonsimple.version>1.1.1</com.googlecode.jsonsimple.version>
<org.apache.httpcomponents.httpclient.version>4.3.3</org.apache.httpcomponents.httpclient.version>
<org.apache.commons.io.version>1.3.2</org.apache.commons.io.version>
<org.mockito.version>1.9.5</org.mockito.version>
<junit.version>4.8.1</junit.version>
<maven.source.plugin.version>2.2.1</maven.source.plugin.version>
<maven.javadoc.plugin.version>2.9.1</maven.javadoc.plugin.version>
Expand Down Expand Up @@ -177,5 +181,31 @@
<version>${junit.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${org.mockito.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.3.1</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>${org.apache.httpcomponents.httpclient.version}</version>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>${org.apache.commons.io.version}</version>
</dependency>
</dependencies>
</project>
12 changes: 10 additions & 2 deletions src/main/java/com/github/jreddit/Thing.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,20 @@ public abstract class Thing {
/**
* The kind of this thing. (i.e "t2" for users)
*/
public String kind;
protected String kind;

/**
* The full name of this thing.
*
* @see <a href="https://github.com/reddit/reddit/wiki/API">Reddit API Reference</a>
*/
public String fullName;
protected String fullName;

public String getKind() {
return kind;
}

public String getFullName() {
return fullName;
}
}
27 changes: 11 additions & 16 deletions src/main/java/com/github/jreddit/captcha/Captcha.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,12 @@

import com.github.jreddit.user.User;
import com.github.jreddit.utils.ApiEndpointUtils;
import com.github.jreddit.utils.Utils;
import com.github.jreddit.utils.restclient.RestClient;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import javax.imageio.ImageIO;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

/**
* This class corresponds to the Reddit's captcha class.
Expand All @@ -22,8 +18,13 @@
*/
public class Captcha {

private static final String IMAGE_FORMAT = "png";
private static final String IMAGE_PATH = "captcha." + IMAGE_FORMAT;
private final CaptchaDownloader captchaDownloader;
private final RestClient restClient;

public Captcha(RestClient restClient, CaptchaDownloader captchaDownloader) {
this.restClient = restClient;
this.captchaDownloader = captchaDownloader;
}

/**
* Generates and saves a new reddit captcha in the working directory
Expand All @@ -35,17 +36,11 @@ public String newCaptcha(User user) {
String iden = null;

try {
// Get the captcha iden
JSONObject obj = Utils.post("", ApiEndpointUtils.CAPTCHA_NEW, user.getCookie());
JSONObject obj = (JSONObject) restClient.post(null, ApiEndpointUtils.CAPTCHA_NEW, user.getCookie()).getResponseObject();
iden = (String) ((JSONArray) ((JSONArray) ((JSONArray) obj.get("jquery")).get(11)).get(3)).get(0);
System.out.println("Received CAPTCHA iden: " + iden);

// Get the corresponding captcha image
URL url = new URL(ApiEndpointUtils.REDDIT_BASE_URL + "/captcha/" + iden + ".png");
RenderedImage captcha = ImageIO.read(url);

// Write the file to disk
ImageIO.write(captcha, IMAGE_FORMAT, new File(IMAGE_PATH));
captchaDownloader.getCaptchaImage(iden);

} catch (MalformedURLException e) {
System.out.println("Invalid URL for retrieving captcha");
Expand All @@ -66,7 +61,7 @@ public boolean needsCaptcha(User user) {
boolean needsCaptcha = false;

try {
needsCaptcha = (Boolean) Utils.get(ApiEndpointUtils.CAPTCHA_NEEDS, user.getCookie());
needsCaptcha = (Boolean) restClient.get(ApiEndpointUtils.CAPTCHA_NEEDS, user.getCookie()).getResponseObject();
} catch (Exception e) {
System.err.println("Error verifying if the user needs a captcha");
}
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/com/github/jreddit/captcha/CaptchaDownloader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.github.jreddit.captcha;

import com.github.jreddit.utils.ApiEndpointUtils;

import javax.imageio.ImageIO;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;

public class CaptchaDownloader {
private static final String IMAGE_FORMAT = "png";
private static final String IMAGE_PATH = "captcha." + IMAGE_FORMAT;

public void getCaptchaImage(String iden) throws IOException {
URL url = new URL(ApiEndpointUtils.REDDIT_BASE_URL + "/captcha/" + iden + ".png");
RenderedImage captcha = ImageIO.read(url);
ImageIO.write(captcha, IMAGE_FORMAT, new File(IMAGE_PATH));
}
}
20 changes: 13 additions & 7 deletions src/main/java/com/github/jreddit/message/Messages.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.github.jreddit.message;

import com.github.jreddit.user.User;
import com.github.jreddit.utils.ApiEndpointUtils;
import com.github.jreddit.utils.TypePrefix;
import com.github.jreddit.user.User;
import com.github.jreddit.utils.Utils;
import com.github.jreddit.utils.restclient.HttpRestClient;
import com.github.jreddit.utils.restclient.RestClient;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

Expand All @@ -21,6 +22,11 @@
public class Messages {

public static final int ALL_MESSAGES = -1;
private final RestClient restClient;

public Messages(RestClient restClient) {
this.restClient = restClient;
}

/**
* Get the list of messages of a certain type for a user
Expand All @@ -36,7 +42,7 @@ public List<Message> getMessages(User user, int maxMessages, MessageType message
List<Message> messages = null;

try {
JSONObject object = (JSONObject) Utils.get(String.format(ApiEndpointUtils.MESSAGE_GET, messageType.getValue()), user.getCookie());
JSONObject object = (JSONObject) restClient.get(String.format(ApiEndpointUtils.MESSAGE_GET, messageType.getValue()), user.getCookie()).getResponseObject();
JSONObject data = (JSONObject) object.get("data");
messages = buildList((JSONArray) data.get("children"), maxMessages);

Expand Down Expand Up @@ -66,10 +72,10 @@ public boolean compose(User user, String to, String subject, String text, String
}

try {
JSONObject object = Utils.post("captcha=" + captchaTry + "&iden=" + iden +
JSONObject object = (JSONObject) restClient.post("captcha=" + captchaTry + "&iden=" + iden +
"&subject=" + subject + "&text=" + text + "&to=" + to +
"&uh=" + user.getModhash(),
ApiEndpointUtils.MESSAGE_COMPOSE, user.getCookie());
ApiEndpointUtils.MESSAGE_COMPOSE, user.getCookie()).getResponseObject();

if (object.toJSONString().contains(".error.USER_REQUIRED")) {
System.err.println("Please login first.");
Expand Down Expand Up @@ -98,7 +104,7 @@ public boolean compose(User user, String to, String subject, String text, String
*/
public void readMessage(String fullName, User user) {
try {
Utils.post("id=" + fullName + "&uh=" + user.getModhash(), ApiEndpointUtils.MESSAGE_READ, user.getCookie());
restClient.post("id=" + fullName + "&uh=" + user.getModhash(), ApiEndpointUtils.MESSAGE_READ, user.getCookie());
} catch (Exception e) {
System.err.println("Error reading message: " + fullName);
}
Expand All @@ -113,7 +119,7 @@ public void readMessage(String fullName, User user) {
*/
public void unreadMessage(String fullName, User user) {
try {
Utils.post("id=" + fullName + "&uh=" + user.getModhash(), ApiEndpointUtils.MESSAGE_READ, user.getCookie());
restClient.post("id=" + fullName + "&uh=" + user.getModhash(), ApiEndpointUtils.MESSAGE_READ, user.getCookie());
} catch (Exception e) {
System.err.println("Error marking message: " + fullName + " as unread");
}
Expand Down
32 changes: 19 additions & 13 deletions src/main/java/com/github/jreddit/submissions/Submission.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.github.jreddit.submissions;

import com.github.jreddit.exception.InvalidCookieException;
import com.github.jreddit.Thing;
import com.github.jreddit.exception.InvalidCookieException;
import com.github.jreddit.user.User;
import com.github.jreddit.utils.ApiEndpointUtils;
import com.github.jreddit.utils.Utils;
import com.github.jreddit.utils.restclient.HttpRestClient;
import com.github.jreddit.utils.restclient.RestClient;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.ParseException;
Expand All @@ -20,6 +21,8 @@
*/
public class Submission extends Thing {

private RestClient restClient;

/* This is the user that will vote on a submission. */
private User user;

Expand All @@ -37,6 +40,7 @@ public class Submission extends Thing {
private long score;

public Submission() {
restClient = new HttpRestClient();
}

/**
Expand All @@ -60,6 +64,12 @@ public Submission(JSONObject obj) {
} catch (Exception e) {
System.err.println("Error creating Submission");
}
restClient = new HttpRestClient();
}

// this is very stinky..
public void setRestClient(RestClient restClient) {
this.restClient = restClient;
}

public void setUpVotes(long upVotes) {
Expand Down Expand Up @@ -114,10 +124,6 @@ public void setUser(User user) {
this.user = user;
}

public String getFullName() {
return fullName;
}

public String getURL() {
return url;
}
Expand All @@ -141,8 +147,8 @@ public Submission(User user, String fullName, String url) {
* @throws ParseException If JSON parsing fails
*/
public void comment(String text) throws IOException, ParseException {
JSONObject object = Utils.post("thing_id=" + fullName + "&text=" + text
+ "&uh=" + user.getModhash(), ApiEndpointUtils.SUBMISSION_COMMENT, user.getCookie());
JSONObject object = (JSONObject) restClient.post("thing_id=" + fullName + "&text=" + text
+ "&uh=" + user.getModhash(), ApiEndpointUtils.SUBMISSION_COMMENT, user.getCookie()).getResponseObject();

if (object.toJSONString().contains(".error.USER_REQUIRED"))
throw new InvalidCookieException("Cookie not present");
Expand Down Expand Up @@ -266,13 +272,13 @@ public boolean isNSFW() throws IOException, ParseException {
}

public void markNSFW() throws IOException, ParseException {
Utils.post(
restClient.post(
"id=" + fullName + "&uh=" + user.getModhash(),
ApiEndpointUtils.SUBMISSION_MARK_AS_NSFW, user.getCookie());
}

public void unmarkNSFW() throws IOException, ParseException {
Utils.post(
restClient.post(
"id=" + fullName + "&uh=" + user.getModhash(),
ApiEndpointUtils.SUBMISSION_UNMARK_AS_NSFW, user.getCookie());
}
Expand Down Expand Up @@ -351,14 +357,14 @@ public void downVote() throws IOException, ParseException {
}

private JSONObject voteResponse(int dir) throws IOException, ParseException {
return Utils.post(
return (JSONObject) restClient.post(
"id=" + fullName + "&dir=" + dir + "&uh=" + user.getModhash(),
ApiEndpointUtils.SUBMISSION_VOTE, user.getCookie());
ApiEndpointUtils.SUBMISSION_VOTE, user.getCookie()).getResponseObject();
}

private JSONObject info(String urlPath) throws IOException, ParseException {
urlPath += "/info.json";
Object object = Utils.get(urlPath, user.getCookie());
Object object = restClient.get(urlPath, user.getCookie()).getResponseObject();

JSONArray array = (JSONArray) object;
JSONObject obj = (JSONObject) array.get(0);
Expand Down
Loading

0 comments on commit 40ce865

Please sign in to comment.