package com.atlassian.bitbucket.mesh;

import com.atlassian.bitbucket.mesh.repository.ManagedRepository;
import com.atlassian.bitbucket.mesh.test.annotation.DirtiesRepo;
import com.atlassian.bitbucket.mesh.test.annotation.Repo;
import com.atlassian.bitbucket.mesh.test.cluster.StandaloneMeshControlPlane;
import org.junit.Test;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.TestPropertySource;

import javax.annotation.Resource;

import static com.atlassian.bitbucket.mesh.MeshConstants.PROFILE_STANDALONE;
import static java.time.Duration.ofSeconds;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.io.FileMatchers.anExistingFileOrDirectory;
import static org.junit.Assert.assertTrue;

@ActiveProfiles(value = {PROFILE_STANDALONE}, inheritProfiles = false) //Required for MeshCoreWiring
@TestPropertySource(properties = "storage.partition-cleanup-delay=5")
public class PartitionCleanupTest extends AbstractWiredTest {

    @Resource
    private StandaloneMeshControlPlane controlPlane;
    @Repo
    private ManagedRepository repository;

    @DirtiesRepo
    @Test
    public void testRemovingAPartitionRemovesTheFilesAfterTheCleanupDelay() {
        String partition = repository.getPartition();
        assertTrue(controlPlane.removePartition(partition));

        awaitUntil(() -> repository.getPath().toFile(), is(not(anExistingFileOrDirectory())), ofSeconds(10));
    }
}
