package it.com.atlassian.bitbucket.integrity.pull;

import com.atlassian.bitbucket.test.BaseFuncTest;
import com.atlassian.bitbucket.test.DefaultFuncTestData;
import com.atlassian.bitbucket.test.PullRequestRestTestHelper;
import com.atlassian.httpclient.api.HttpStatus;
import com.google.common.collect.ImmutableMap;
import com.jayway.restassured.RestAssured;
import it.com.atlassian.bitbucket.integrity.IntegrityTestConstants;
import it.com.atlassian.bitbucket.integrity.RestoreIgnoreRule;
import it.com.atlassian.bitbucket.integrity.SetupUtil;
import it.com.atlassian.bitbucket.integrity.annotation.Ahead;
import it.com.atlassian.bitbucket.integrity.annotation.Behind;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import javax.ws.rs.core.MediaType;

public class PullRequestIntegritySetup extends BaseFuncTest {

    @Rule
    public RestoreIgnoreRule restoreIgnoreRule = new RestoreIgnoreRule();

    private ImmutableMap<String, String> pullRequestData = new ImmutableMap.Builder<String, String>()
            .put("username", DefaultFuncTestData.getAdminUser())
            .put("password", DefaultFuncTestData.getAdminPassword())
            .put("title", IntegrityTestConstants.PR_TITLE)
            .put("description", "")
            .put("sourceProject", IntegrityTestConstants.PR_PROJ)
            .put("sourceRepo", IntegrityTestConstants.PR_REPO)
            .put("sourceBranch", IntegrityTestConstants.BASIC_BRANCHING)
            .put("targetProject", IntegrityTestConstants.PR_PROJ)
            .put("targetRepo", IntegrityTestConstants.PR_REPO)
            .put("targetBranch", IntegrityTestConstants.MASTER)
            .build();


    @Before
    public void commonConfiguration() {
        SetupUtil.createProject(IntegrityTestConstants.PR_PROJ);
        SetupUtil.createRepo(IntegrityTestConstants.PR_PROJ, IntegrityTestConstants.PR_REPO);
    }

    @Behind
    @Test
    public void beforeFirstSnapshot() {
        long pullRequestId = SetupUtil.createPullRequest(
                pullRequestData.get("username"), pullRequestData.get("password"), pullRequestData.get("title"), pullRequestData.get("description"),
                pullRequestData.get("sourceProject"), pullRequestData.get("sourceRepo"), pullRequestData.get("sourceBranch"),
                pullRequestData.get("targetProject"), pullRequestData.get("targetRepo"), pullRequestData.get("targetBranch"));

        SetupUtil.storeValue(IntegrityTestConstants.PR_TITLE, pullRequestId);
    }

    @Ahead
    @Test
    public void beforeSecondSnapshot() {
        long pullRequestId = SetupUtil.createPullRequest(
                pullRequestData.get("username"), pullRequestData.get("password"), pullRequestData.get("title"), pullRequestData.get("description"),
                pullRequestData.get("sourceProject"), pullRequestData.get("sourceRepo"), pullRequestData.get("sourceBranch"),
                pullRequestData.get("targetProject"), pullRequestData.get("targetRepo"), pullRequestData.get("targetBranch"));

        SetupUtil.storeValue(IntegrityTestConstants.PR_TITLE, pullRequestId);

        String pullRequestMergeUrl = PullRequestRestTestHelper.getPullRequestURL(IntegrityTestConstants.PR_PROJ,
                IntegrityTestConstants.PR_REPO, pullRequestId) + "/merge?version=0";

        RestAssured.expect()
                .statusCode(HttpStatus.OK.code)
                .log().ifError()
                .given()
                .auth().preemptive()
                .basic(DefaultFuncTestData.getAdminUser(), DefaultFuncTestData.getAdminPassword())
                .contentType(MediaType.APPLICATION_JSON)
                .when()
                .post(pullRequestMergeUrl);
    }
}
