<commit_before>package com.github.jreddit.action;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import org.json.simple.JSONObject;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import static org.junit.Assert.assertEquals;

import com.github.jreddit.entity.User;
import com.github.jreddit.utils.ApiEndpointUtils;
import com.github.jreddit.utils.restclient.Response;
import com.github.jreddit.utils.restclient.RestClient;
import com.github.jreddit.utils.restclient.RestResponse;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import org.apache.http.HttpResponse;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class SubmitActionsTest {

    public static final String COOKIE = "cookie";
    public static final String REDDIT_NAME = "all";
    public static final String USERNAME = "TestUser";
    public static final String MODHASH = "modhash";
    private SubmitActions submitAction;
    private RestClient restClient;
    private User user;
    private Response responseNotAuthor;
    private Response responseUserRequired;
    private Response responseEmpty;

    @Rule
    public ExpectedException exception = ExpectedException.none();

    /**
     * Mock classes it depends on.
     */
    @Before
    public void setup() {
        user = mock(User.class);
        when(user.getCookie()).thenReturn(COOKIE);
        when(user.getModhash()).thenReturn(MODHASH);
        restClient = mock(RestClient.class);
        submitAction = new SubmitActions(restClient, user);

        JSONObject errorObject = new JSONObject();
        errorObject.put("call", ".error.NOT_AUTHOR.field-thing_id");
        responseNotAuthor = new RestResponse("test", errorObject, mock(HttpResponse.class));

        errorObject = new JSONObject();
        errorObject.put("call", ".error.USER_REQUIRED");
        responseUserRequired = new RestResponse("test", errorObject, mock(HttpResponse.class));

        errorObject = new JSONObject();
        responseEmpty = new RestResponse("", errorObject, mock(HttpResponse.class));
    }

    /**
     * Test deleting a thing.
     */
    @Test
    public void testDelete() {
        // Stub REST client methods
        when(user.getModhash()).thenReturn(MODHASH);
        when(user.getCookie()).thenReturn(COOKIE);
        when(restClient.post(
                "id=" + "fullname" + "&uh=" + user.getModhash(),
                ApiEndpointUtils.DELETE, user.getCookie()))
                .thenReturn(responseEmpty);

        // Delete thing succesfully
        assertTrue(submitAction.delete("fullname"));
    }

    /**
     * Test commenting.
     */
    @Test
    public void testComment() {
        // Stub REST client methods
        when(user.getModhash()).thenReturn(MODHASH);
        when(user.getCookie()).thenReturn(COOKIE);
        when(restClient.post("thing_id=" + "fullname" + "&text=" + "text"
                + "&uh=" + user.getModhash(),
                ApiEndpointUtils.COMMENT, user.getCookie()))
                .thenReturn(responseEmpty);

        // Comment succesfully
        assertTrue(submitAction.comment("fullname", "text"));
    }

    /**
     * Test commenting without a logged in user.
     */
    @Test
    public void testCommentUserRequired() {
        // Stub REST client methods
        when(user.getModhash()).thenReturn(MODHASH);
        when(user.getCookie()).thenReturn(COOKIE);
        when(restClient.post("thing_id=" + "fullname" + "&text=" + "text"
                + "&uh=" + user.getModhash(),
                ApiEndpointUtils.COMMENT, user.getCookie()))
                .thenReturn(responseUserRequired);
        // Save System.err.println output to variable
        ByteArrayOutputStream errorOutput = new ByteArrayOutputStream();
        System.setErr(new PrintStream(errorOutput));

        // Attempt to comment
        boolean res = submitAction.comment("fullname", "text");
        assertEquals(errorOutput.toString().contains("User submission failed: please login first."), true);
        assertFalse(res);
    }

    /**
     * Test creating a Live Thread without a logged in user.
     */
    @Test<commit_msg>Added test for issue #119<commit_after>package com.github.jreddit.action;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import org.json.simple.JSONObject;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import static org.junit.Assert.assertEquals;

import com.github.jreddit.entity.User;
import com.github.jreddit.utils.ApiEndpointUtils;
import com.github.jreddit.utils.restclient.Response;
import com.github.jreddit.utils.restclient.RestClient;
import com.github.jreddit.utils.restclient.RestResponse;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import org.apache.http.HttpResponse;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class SubmitActionsTest {

    public static final String COOKIE = "cookie";
    public static final String REDDIT_NAME = "all";
    public static final String USERNAME = "TestUser";
    public static final String MODHASH = "modhash";
    private SubmitActions submitAction;
    private RestClient restClient;
    private User user;
    private Response responseNotAuthor;
    private Response responseUserRequired;
    private Response responseTenMinutesLimit;
    private Response responseEmpty;

    @Rule
    public ExpectedException exception = ExpectedException.none();

    /**
     * Mock classes it depends on.
     */
    @Before
    public void setup() {
        user = mock(User.class);
        when(user.getCookie()).thenReturn(COOKIE);
        when(user.getModhash()).thenReturn(MODHASH);
        restClient = mock(RestClient.class);
        submitAction = new SubmitActions(restClient, user);

        JSONObject errorObject = new JSONObject();
        errorObject.put("call", ".error.NOT_AUTHOR.field-thing_id");
        responseNotAuthor = new RestResponse("test", errorObject, mock(HttpResponse.class));

        errorObject = new JSONObject();
        errorObject.put("call", ".error.USER_REQUIRED");
        responseUserRequired = new RestResponse("test", errorObject, mock(HttpResponse.class));
        
        errorObject = new JSONObject();
        errorObject.put("call", ".error.RATELIMIT.field-ratelimit");
        responseTenMinutesLimit = new RestResponse("test", errorObject, mock(HttpResponse.class));

        errorObject = new JSONObject();
        responseEmpty = new RestResponse("", errorObject, mock(HttpResponse.class));
    }

    /**
     * Test deleting a thing.
     */
    @Test
    public void testDelete() {
        // Stub REST client methods
        when(user.getModhash()).thenReturn(MODHASH);
        when(user.getCookie()).thenReturn(COOKIE);
        when(restClient.post(
                "id=" + "fullname" + "&uh=" + user.getModhash(),
                ApiEndpointUtils.DELETE, user.getCookie()))
                .thenReturn(responseEmpty);

        // Delete thing succesfully
        assertTrue(submitAction.delete("fullname"));
    }

    /**
     * Test commenting.
     */
    @Test
    public void testComment() {
        // Stub REST client methods
        when(user.getModhash()).thenReturn(MODHASH);
        when(user.getCookie()).thenReturn(COOKIE);
        when(restClient.post("thing_id=" + "fullname" + "&text=" + "text"
                + "&uh=" + user.getModhash(),
                ApiEndpointUtils.COMMENT, user.getCookie()))
                .thenReturn(responseEmpty);

        // Comment succesfully
        assertTrue(submitAction.comment("fullname", "text"));
    }

    /**
     * Test commenting without a logged in user.
     */
    @Test
    public void testCommentUserRequired() {
        // Stub REST client methods
        when(user.getModhash()).thenReturn(MODHASH);
        when(user.getCookie()).thenReturn(COOKIE);
        when(restClient.post("thing_id=" + "fullname" + "&text=" + "text"
                + "&uh=" + user.getModhash(),
                ApiEndpointUtils.COMMENT, user.getCookie()))
                .thenReturn(responseUserRequired);
        // Save System.err.println output to variable
        ByteArrayOutputStream errorOutput = new ByteArrayOutputStream();
        System.setErr(new PrintStream(errorOutput));

        // Attempt to comment
        boolean res = submitAction.comment("fullname", "text");
        assertEquals(errorOutput.toString().contains("User submission failed: please login first."), true);
        assertFalse(res);
    }
    
    /**
     * Test commenting within 10 minutes from last comment.
     */
    @Test
    public void testCommentTenMinutesLimit() {
        // Stub REST client methods
        when(user.getModhash()).thenReturn(MODHASH);
        when(user.getCookie()).thenReturn(COOKIE);
        when(restClient.post("thing_id=" + "fullname" + "&text=" + "text"
                + "&uh=" + user.getModhash(),
                ApiEndpointUtils.COMMENT, user.getCookie()))
                .thenReturn(responseTenMinutesLimit);
        // Save System.err.println output to variable
        ByteArrayOutputStream errorOutput = new ByteArrayOutputStream();
        System.setErr(new PrintStream(errorOutput));

        // Attempt to comment
        boolean res = submitAction.comment("fullname", "text");
        assertEquals(errorOutput.toString().contains("User submission failed: You need to wait before posting again"), true);
        assertFalse(res);
    }

    /**
     * Test creating a Live Thread without a logged in user.
     */
    @Test
{'fraction_non_alphanumeric': 0.11748998664886515, 'fraction_numerical': 0.0005135051864023827, 'mean_word_length': 2.92503022974607, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7248755', 'n_tokens_mistral': 2681, 'n_tokens_neox': 2630, 'n_words': 583}