package com.atlassian.bitbucket.mesh.cluster;

import com.atlassian.bitbucket.mesh.AbstractWiredTest;
import com.atlassian.bitbucket.mesh.client.MeshClient;
import com.atlassian.bitbucket.mesh.network.MeshManager;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Value;

import javax.annotation.Resource;

import static org.junit.Assert.assertEquals;

public class WiredClusterNodeTest extends AbstractWiredTest {

    @Resource
    private MeshClient meshClient;
    @Resource
    private MeshManager meshManager;
    @Value("${mesh.control-plane.partition-count:64}")
    private int partitionCount;

    @Test
    public void testClusterInitializedCorrectly() {
        //Verify that both the @MeshCluster nodes and the local node have been configured
        assertEquals(3, meshManager.getNodes().size());
        //Verify that the partitions have been configured
        assertEquals(partitionCount, meshManager.getPartitions().size());
    }

    @Test
    public void testWhileOffline() throws Exception {
        localNode.whileOffline(() -> {
            //The local MeshManager will always know about the local node
            assertEquals(1, meshManager.getNodes().size());
            //But it won't have any partitions mapped to it
            assertEquals(0, meshManager.getPartitions().size());

            return null;
        });

        //Topology should be restored
        assertEquals(3, meshManager.getNodes().size());
        assertEquals(partitionCount, meshManager.getPartitions().size());
    }
}