Skip to content

Commit

Permalink
Implemented hide/unhide for a submission. Also fixed testing for my p…
Browse files Browse the repository at this point in the history
…rev. work.
  • Loading branch information
jonnykry committed Jun 12, 2014
1 parent 24ff482 commit e49f64a
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 13 deletions.
22 changes: 22 additions & 0 deletions src/main/java/com/github/jreddit/submissions/Submission.java
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,28 @@ public void unsave() throws IOException, ParseException {
ApiEndpointUtils.SUBMISSION_UNSAVE, user.getCookie());
}

/**
* This function hides a submission.
* @throws IOException If connection fails
* @throws ParseException If JSON parsing fails
*/
public void hide() throws IOException, ParseException {
restClient.post(
"id=" + fullName + "&uh=" + user.getModhash(),
ApiEndpointUtils.SUBMISSION_HIDE, user.getCookie());
}

/**
* This function unhides a submission.
* @throws IOException If connection fails
* @throws ParseException If JSON parsing fails
*/
public void unhide() throws IOException, ParseException {
restClient.post(
"id=" + fullName + "&uh=" + user.getModhash(),
ApiEndpointUtils.SUBMISSION_UNHIDE, user.getCookie());
}

/** Upvote/downvote a submission */
private JSONObject voteResponse(int dir) throws IOException, ParseException {
return (JSONObject) restClient.post(
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/github/jreddit/utils/ApiEndpointUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ public class ApiEndpointUtils {

public static final String SUBMISSION_UNSAVE = REDDIT_BASE_API_ENDPOINT + "/unsave";

public static final String SUBMISSION_HIDE = REDDIT_BASE_API_ENDPOINT + "/hide";

public static final String SUBMISSION_UNHIDE = REDDIT_BASE_API_ENDPOINT + "/unhide";

public static final String SUBMISSION_COMMENTS = "/r/%s/comments/%s.json?%s";

/* User specific constants */
Expand Down
35 changes: 27 additions & 8 deletions src/test/java/com/github/jreddit/submissions/SubmissionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@
import java.io.IOException;

import static com.github.jreddit.testsupport.JsonHelpers.createSubmission;
import static com.github.jreddit.utils.ApiEndpointUtils.SUBMISSION_MARK_AS_NSFW;
import static com.github.jreddit.utils.ApiEndpointUtils.SUBMISSION_UNMARK_AS_NSFW;
import static com.github.jreddit.utils.ApiEndpointUtils.SUBMISSION_SAVE;
import static com.github.jreddit.utils.ApiEndpointUtils.SUBMISSION_UNSAVE;
import static com.github.jreddit.utils.ApiEndpointUtils.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

Expand Down Expand Up @@ -43,7 +40,7 @@ public void setUp() {

@Test
public void markNSFW() throws IOException, ParseException {
underTest = new Submission(createSubmission(REDDIT_OBJ_ID, false));
underTest = new Submission(createSubmission(REDDIT_OBJ_ID, false, false, false));
underTest.setUser(user);
underTest.setRestClient(restClient);
underTest.markNSFW();
Expand All @@ -53,7 +50,7 @@ public void markNSFW() throws IOException, ParseException {

@Test
public void unmarkNSFW() throws IOException, ParseException {
underTest = new Submission(createSubmission(REDDIT_OBJ_ID, true));
underTest = new Submission(createSubmission(REDDIT_OBJ_ID, true, false, false));
underTest.setUser(user);
underTest.setRestClient(restClient);
underTest.unmarkNSFW();
Expand All @@ -63,7 +60,7 @@ public void unmarkNSFW() throws IOException, ParseException {

@Test
public void save() throws IOException, ParseException {
underTest = new Submission(createSubmission(REDDIT_OBJ_ID, false));
underTest = new Submission(createSubmission(REDDIT_OBJ_ID, false, true, false));
underTest.setUser(user);
underTest.setRestClient(restClient);
underTest.save();
Expand All @@ -74,12 +71,34 @@ public void save() throws IOException, ParseException {

@Test
public void unsave() throws IOException, ParseException {
underTest = new Submission(createSubmission(REDDIT_OBJ_ID, false));
underTest = new Submission(createSubmission(REDDIT_OBJ_ID, false, false, false));
underTest.setUser(user);
underTest.setRestClient(restClient);
underTest.unsave();

verify(restClient).post("id=" + REDDIT_OBJ_ID + "&uh=" + MOD_HASH,
SUBMISSION_UNSAVE, COOKIE);
}

@Test
public void hide() throws IOException, ParseException {
underTest = new Submission(createSubmission(REDDIT_OBJ_ID, false, false, true));
underTest.setUser(user);
underTest.setRestClient(restClient);
underTest.hide();

verify(restClient).post("id=" + REDDIT_OBJ_ID + "&uh=" + MOD_HASH,
SUBMISSION_HIDE, COOKIE);
}

@Test
public void unhide() throws IOException, ParseException {
underTest = new Submission(createSubmission(REDDIT_OBJ_ID, false, false, false));
underTest.setUser(user);
underTest.setRestClient(restClient);
underTest.unhide();

verify(restClient).post("id=" + REDDIT_OBJ_ID + "&uh=" + MOD_HASH,
SUBMISSION_UNHIDE, COOKIE);
}
}
11 changes: 6 additions & 5 deletions src/test/java/com/github/jreddit/testsupport/JsonHelpers.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,12 @@ public static JSONArray emptyJsonArray() {
return new JSONArray();
}

public static JSONObject createSubmission(String redditObjId, boolean nsfw) {
return createSubmission(redditObjId, nsfw, null, new JSONObject());
public static JSONObject createSubmission(String redditObjId, boolean nsfw, boolean saved, boolean hidden) {
return createSubmission(redditObjId, nsfw, saved, hidden, null, new JSONObject());
}

public static JSONObject createSubmission(String redditObjId, boolean nsfw, JSONObject media, JSONObject mediaEmbed) {
public static JSONObject createSubmission(String redditObjId, boolean nsfw, boolean saved, boolean hidden,
JSONObject media, JSONObject mediaEmbed) {
JSONObject submission = new JSONObject();
submission.put("approved_by", null);
submission.put("author", "jReddittest");
Expand All @@ -191,7 +192,7 @@ public static JSONObject createSubmission(String redditObjId, boolean nsfw, JSON
submission.put("downs", 0L);
submission.put("edited", false);
submission.put("gilded", 0);
submission.put("hidden", false);
submission.put("hidden", hidden);
submission.put("id", "1ikxpg");
submission.put("is_self", false);
submission.put("likes", true);
Expand All @@ -204,7 +205,7 @@ public static JSONObject createSubmission(String redditObjId, boolean nsfw, JSON
submission.put("num_reports", 0);
submission.put("over_18", nsfw);
submission.put("permalink", "/r/jReddit/comments/1ikxpg/thekarangoeljreddit_github/");
submission.put("saved", false);
submission.put("saved", saved);
submission.put("score", 1L);
submission.put("secure_media", null);
submission.put("secure_media_embed", new JSONObject());
Expand Down

0 comments on commit e49f64a

Please sign in to comment.