package it.com.atlassian.stash.rest.repository.sync;

import com.atlassian.bitbucket.test.RepositoryTestHelper;
import com.atlassian.bitbucket.test.TestContext;
import com.atlassian.bitbucket.test.rest.RestAuthentication;
import com.atlassian.bitbucket.test.rest.repository.sync.RestRefSyncHelper;
import com.atlassian.bitbucket.permission.Permission;
import it.com.atlassian.stash.BaseRestTest;
import org.junit.BeforeClass;
import org.springframework.core.io.ClassPathResource;

import java.io.IOException;

/**
 * A base class for ref sync REST tests.
 */
public abstract class BaseRefSyncTest extends BaseRestTest {

    protected static final String OWNER_PROJECT = "SYNC";
    protected static final String OWNER = "rs-owner";
    protected static final String REPOSITORY_SLUG = "ref-sync";
    protected static final String USER = "rs-user";
    protected static final String USER_PROJECT = "~RS-USER";

    protected static RestRefSyncHelper refSyncHelper;

    @BeforeClass
    public static void createHelper() throws Exception {
        refSyncHelper = new RestRefSyncHelper(RestAuthentication.authenticated(USER), USER_PROJECT, REPOSITORY_SLUG);
    }

    protected static void prepareFork(TestContext testContext) {
        testContext.repositoryPermission(OWNER_PROJECT, REPOSITORY_SLUG, USER, Permission.REPO_READ)
                .fork(OWNER_PROJECT, REPOSITORY_SLUG, USER, USER);
    }

    protected static void prepareUpstream(TestContext testContext) throws IOException {
        testContext.project(OWNER_PROJECT, "Ref Sync")
                .repository(OWNER_PROJECT, REPOSITORY_SLUG, new ClassPathResource(RepositoryTestHelper.DEFAULT_REPOSITORY))
                .user(OWNER)
                .user(USER)
                .projectPermission(OWNER_PROJECT, OWNER, Permission.PROJECT_ADMIN);
    }
}
