/*
 * The Alluxio Open Foundation licenses this work under the Apache License, version 2.0
 * (the "License"). You may not use this work except in compliance with the License, which is
 * available at www.apache.org/licenses/LICENSE-2.0
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied, as more fully set forth in the License.
 *
 * See the NOTICE file distributed with this work for information regarding copyright ownership.
 */

package alluxio.worker.block;

import alluxio.AbstractClient;
import alluxio.Constants;
import alluxio.exception.ExceptionMessage;
import alluxio.thrift.AlluxioService;
import alluxio.thrift.AlluxioService.Client;
import alluxio.thrift.BlockMasterClientService;

import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;

import java.io.IOException;
import java.net.InetSocketAddress;

/**
 * Unit tests for {@link AbstractClient}.
 */
public final class AbstractClientTest {
  private static final String SERVICE_NAME = "Test Service Name";
  private static final int CLIENT_SERVICE_VERSION = 1;

  private final class TestClient extends AbstractClient {
    private TestClient() {
      super(Mockito.mock(InetSocketAddress.class), "");
    }

    @Override
    protected Client getClient() {
      return null;
    }

    @Override
    protected String getServiceName() {
      return SERVICE_NAME;
    }

    @Override
    protected long getServiceVersion() {
      return CLIENT_SERVICE_VERSION;
    }

    public void checkVersion(AlluxioService.Client thriftClient) throws IOException {
      checkVersion(thriftClient, Constants.BLOCK_MASTER_CLIENT_SERVICE_VERSION);
    }
  }

  /**
   * Tests for an unsupported version.
   */
  @Test
  public void unsupportedVersionTest() throws Exception {
    final BlockMasterClientService.Client thriftClient =
        Mockito.mock(BlockMasterClientService.Client.class);
    Mockito.when(thriftClient.getServiceVersion()).thenReturn(0L);

    try (TestClient client = new TestClient()) {
      client.checkVersion(thriftClient);
      Assert.fail("checkVersion() should fail");
    } catch (IOException e) {
      Assert.assertEquals(ExceptionMessage.INCOMPATIBLE_VERSION.getMessage(SERVICE_NAME,
          CLIENT_SERVICE_VERSION, 0), e.getMessage());
    }
  }
}

@@ -573,34 +573,8 @@
 nt;%0A
-import alluxio.Constants;%0A
 impo
@@ -698,56 +698,8 @@
 ent;
-%0Aimport alluxio.thrift.BlockMasterClientService;
 %0A%0Aim
@@ -996,63 +996,8 @@
 me%22;
-%0A  private static final int CLIENT_SERVICE_VERSION = 1;
 %0A%0A  
@@ -1373,38 +1373,17 @@
  return 
-CLIENT_SERVICE_VERSION
+1
 ;%0A    %7D%0A
@@ -1446,16 +1446,30 @@
 ftClient
+, long version
 ) throws
@@ -1489,16 +1489,22 @@
 %7B%0A      
+super.
 checkVer
@@ -1526,117 +1526,28 @@
 nt, 
-Constants.BLOCK_MASTER_CLIENT_SERVICE_VERSION);%0A    %7D%0A  %7D%0A%0A  /**%0A   * Tests for an unsupported version.%0A   */
+version);%0A    %7D%0A  %7D%0A
 %0A  @
@@ -1583,20 +1583,16 @@
 dVersion
-Test
 () throw
@@ -1615,33 +1615,23 @@
   final 
-BlockMasterClient
+Alluxio
 Service.
@@ -1651,24 +1651,16 @@
 Client =
-%0A       
  Mockito
@@ -1669,25 +1669,15 @@
 ock(
-BlockMasterClient
+Alluxio
 Serv
@@ -1757,17 +1757,17 @@
 nReturn(
-0
+1
 L);%0A%0A   
@@ -1850,16 +1850,19 @@
 ftClient
+, 0
 );%0A     
@@ -2023,16 +2023,23 @@
 CE_NAME,
+ 0, 1),
 %0A       
@@ -2045,50 +2045,344 @@
     
-CLIENT_SERVICE_VERSION, 0), e.getMessage()
+e.getMessage());%0A    %7D%0A  %7D%0A%0A  @Test%0A  public void supportedVersion() throws Exception %7B%0A    final AlluxioService.Client thriftClient = Mockito.mock(AlluxioService.Client.class);%0A    Mockito.when(thriftClient.getServiceVersion()).thenReturn(1L);%0A%0A    try (TestClient client = new TestClient()) %7B%0A      client.checkVersion(thriftClient, 1
 );%0A 

Clean up AbstractClientTest