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

import com.atlassian.bitbucket.test.DefaultFuncTestData;
import com.atlassian.bitbucket.test.PullRequestTestHelper;
import com.atlassian.bitbucket.test.TaskTestHelper;
import com.jayway.restassured.RestAssured;
import org.junit.BeforeClass;
import org.junit.Test;

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

public class RepositoryCascadingDeleteTest extends AbstractTaskResourceTest {

    @BeforeClass
    public static void setupProject() throws Exception {
        defaultSetupProject(getClassTestContext());
    }

    @Test
    public void testDeleteRepositoryDeletesTasksCreatedInPullRequests() throws Exception {
        String repoSlug = "testDeleteRepositoryDeletesTasksCreatedInPullRequests";

        setupRepository(testContext, PROJECT_NAME, repoSlug);
        PullRequestTestHelper pullRequest = testContext.pullRequest(
                new PullRequestTestHelper.Builder(
                        USER_PR_AUTHOR, USER_PR_AUTHOR,
                        PR_TITLE, PR_TITLE,
                        PROJECT_NAME, repoSlug, SOURCE_BRANCH,
                        PROJECT_NAME, repoSlug, TARGET_BRANCH));

        PullRequestComment comment = createComment(pullRequest);
        TaskTestHelper task = createTask(testContext, PROJECT_NAME, repoSlug, comment.pullRequestId, comment.commentId, "a comment");

        deleteRepository(PROJECT_NAME, repoSlug);

        RestAssured
            .given()
                .auth().preemptive().basic(USER_PR_AUTHOR, USER_PR_AUTHOR)
            .when()
                .get(task.getResourceUrl())
            .then()
                .log().ifValidationFails()
                .statusCode(NOT_FOUND.getStatusCode());
    }

    @Test
    public void testDeletingRepositoryDoesNotDeleteTasksInOtherRepositories() throws Exception {
        String repoSlug1 = "testDeletingRepositoryDoesNotDeleteTasksInOtherRepositories1";
        String repoSlug2 = "testDeletingRepositoryDoesNotDeleteTasksInOtherRepositories2";

        setupRepository(testContext, PROJECT_NAME, repoSlug1);
        setupRepository(testContext, PROJECT_NAME, repoSlug2);

        PullRequestTestHelper pullRequest = testContext.pullRequest(
                new PullRequestTestHelper.Builder(
                        USER_PR_AUTHOR, USER_PR_AUTHOR,
                        PR_TITLE, PR_TITLE,
                        PROJECT_NAME, repoSlug1, SOURCE_BRANCH,
                        PROJECT_NAME, repoSlug1, TARGET_BRANCH));

        PullRequestComment comment = createComment(pullRequest);
        TaskTestHelper task = createTask(testContext, PROJECT_NAME, repoSlug1, comment.pullRequestId, comment.commentId, "a comment");

        deleteRepository(PROJECT_NAME, repoSlug2);

        RestAssured
            .given()
                .auth().preemptive().basic(USER_PR_AUTHOR, USER_PR_AUTHOR)
            .when()
                .get(task.getResourceUrl())
            .then()
                .log().ifValidationFails()
                .statusCode(OK.getStatusCode());
    }

    private void deleteRepository(String projectKey, String repoSlug) {
        RestAssured
            .given()
                .auth().preemptive().basic(DefaultFuncTestData.getAdminUser(), DefaultFuncTestData.getAdminPassword())
            .when()
                .delete(DefaultFuncTestData.getRestURL() + "/projects/" + projectKey + "/repos/" + repoSlug)
            .then()
                .log().ifValidationFails()
                .statusCode(ACCEPTED.getStatusCode());
    }
}
