package com.atlassian.bitbucket.mesh.network;

import com.atlassian.bitbucket.mesh.AbstractWiredTest;
import com.atlassian.bitbucket.mesh.rpc.v1.ManagementServiceGrpc;
import com.atlassian.bitbucket.mesh.rpc.v1.RpcGetNodeInfoRequest;
import com.atlassian.bitbucket.mesh.rpc.v1.RpcGetNodeInfoResponse;
import org.junit.Test;

import javax.annotation.Resource;

import static org.hamcrest.Matchers.anEmptyMap;
import static org.hamcrest.Matchers.not;

public class GrpcChannelRegistryTest extends AbstractWiredTest {

    @Resource
    private MeshManager meshManager;
    @Resource
    private GrpcChannelRegistry registry;

    @Test
    public void testGetChannel() {
        try {
            RpcGetNodeInfoResponse nodeInfo = registry.getChannel(meshManager.getLocalNode())
                    .newStub(ManagementServiceGrpc::newBlockingStub)
                    .getNodeInfo(RpcGetNodeInfoRequest.getDefaultInstance());
            assertThat(nodeInfo.getPropertiesMap(), not(anEmptyMap()));
        } finally {
            registry.clear(); //Don't leave any stray channels
        }
    }
}
