package it.com.atlassian.stash.rest.pull;

import com.atlassian.bitbucket.permission.Permission;
import com.jayway.restassured.RestAssured;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import static javax.ws.rs.core.Response.Status.NO_CONTENT;

public class PullRequestWatchResourceTest extends BasePullRequestResourceTest {
    private static final String PR_WATCH_TEST = "prwatchtest";
    private static final String AUTHOR = PR_WATCH_TEST + "_author";
    private static final String WATCHER = PR_WATCH_TEST + "_watcher";

    @BeforeClass
    public static void setUpContext() throws Exception {
        getClassTestContext()
                .project(PR_WATCH_TEST)
                .repository(PR_WATCH_TEST, PR_WATCH_TEST, REPO_ZIP)
                .user(AUTHOR).projectPermission(PR_WATCH_TEST, AUTHOR, Permission.PROJECT_READ)
                .user(WATCHER).projectPermission(PR_WATCH_TEST, WATCHER, Permission.PROJECT_READ);
    }

    @Before
    public void setupPullRequest() {
        pullRequest = createPullRequest(testContext, AUTHOR, PR_WATCH_TEST);
    }

    @Test
    public void testWatchPullRequest() {
        RestAssured.expect()
                .statusCode(NO_CONTENT.getStatusCode())
                .log().ifError()
                .given()
                .auth().preemptive().basic(WATCHER, WATCHER)
                .contentType("application/json")
                .when()
                .post(pullRequest.getWatchUrl());
    }

    @Test
    public void testUnwatchPullRequest() {
        RestAssured.expect()
                .statusCode(NO_CONTENT.getStatusCode())
                .log().ifError()
                .given()
                .auth().preemptive().basic(WATCHER, WATCHER)
                .contentType("application/json")
                .when()
                .delete(pullRequest.getWatchUrl());
    }
}
