package com.atlassian.bitbucket.mesh.capability;

import com.atlassian.bitbucket.mesh.AbstractGrpcServiceTest;
import com.atlassian.bitbucket.mesh.MeshConstants;
import com.atlassian.bitbucket.mesh.rpc.*;
import com.atlassian.bitbucket.mesh.util.Version;
import org.junit.Test;

public class GrpcCapabilityServiceTest extends AbstractGrpcServiceTest {

    @Test
    public void testGetCapabilities() {
        RpcGetCapabilitiesResponse response = blockingStub()
                .getCapabilities(RpcGetCapabilitiesRequest.getDefaultInstance());

        assertNotNull(response);
        assertEquals(1, response.getCapabilitiesCount());
        verifyApiCapability(response.getCapabilitiesList().get(0));
    }

    private static void verifyApiCapability(RpcCapability capability) {
        assertSame(RpcCapability.CapabilityOneofCase.SUPPORTED_API_VERSIONS, capability.getCapabilityOneofCase());
        RpcApiCapability apiCapability = capability.getSupportedApiVersions();
        Version minVersion = new Version(apiCapability.getMinVersion());
        Version maxVersion = new Version(apiCapability.getMaxVersion());
        assertEquals(MeshConstants.API_COMPATIBILITY_VERSION, minVersion);
        assertEquals(MeshConstants.API_CURRENT_VERSION, maxVersion);
    }

    private CapabilityServiceGrpc.CapabilityServiceBlockingStub blockingStub() {
        return CapabilityServiceGrpc.newBlockingStub(managedChannel);
    }
}
