/*
 * 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.master.block;

import alluxio.Constants;
import alluxio.master.AlluxioMaster;
import alluxio.rest.RestApiTest;
import alluxio.rest.TestCase;
import alluxio.wire.BlockInfo;
import alluxio.wire.BlockInfoTest;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;

import java.util.HashMap;
import java.util.Map;

import javax.ws.rs.HttpMethod;

/**
 * Test cases for {@link BlockMasterClientRestServiceHandler}.
 */
@RunWith(PowerMockRunner.class)
@PrepareForTest(BlockMaster.class)
public final class BlockMasterClientRestApiTest extends RestApiTest {
  private BlockMaster mBlockMaster;

  @Before
  public void before() throws Exception {
    AlluxioMaster alluxioMaster = mResource.get().getMaster().getInternalMaster();
    mBlockMaster = PowerMockito.mock(BlockMaster.class);
    // Replace the block master created by LocalAlluxioClusterResource with a mock.
    BlockMaster blockMaster = Whitebox.getInternalState(alluxioMaster, "mBlockMaster");
    blockMaster.stop();
    Whitebox.setInternalState(alluxioMaster, "mBlockMaster", mBlockMaster);
    mHostname = mResource.get().getHostname();
    mPort = mResource.get().getMaster().getWebLocalPort();
    mServicePrefix = BlockMasterClientRestServiceHandler.SERVICE_PREFIX;
  }

  @Test
  public void serviceNameTest() throws Exception {
    new TestCase(mHostname, mPort, getEndpoint(BlockMasterClientRestServiceHandler.SERVICE_NAME),
        NO_PARAMS, HttpMethod.GET, Constants.BLOCK_MASTER_CLIENT_SERVICE_NAME).run();
  }

  @Test
  public void serviceVersionTest() throws Exception {
    new TestCase(mHostname, mPort, getEndpoint(BlockMasterClientRestServiceHandler.SERVICE_VERSION),
        NO_PARAMS, HttpMethod.GET, Constants.BLOCK_MASTER_CLIENT_SERVICE_VERSION).run();
  }

  @Test
  public void getBlockInfoTest() throws Exception {
    Map<String, String> params = new HashMap<>();
    params.put("blockId", "1");

    BlockInfo blockInfo = BlockInfoTest.createRandom();
    Mockito.doReturn(blockInfo).when(mBlockMaster).getBlockInfo(Mockito.anyLong());

    new TestCase(mHostname, mPort, getEndpoint(BlockMasterClientRestServiceHandler.GET_BLOCK_INFO),
        params, HttpMethod.GET, blockInfo).run();

    Mockito.verify(mBlockMaster).getBlockInfo(Mockito.anyLong());
  }
}

@@ -568,45 +568,8 @@
 ts;%0A
-import alluxio.master.AlluxioMaster;%0A
 impo
@@ -678,342 +678,236 @@
 io.w
-ire.BlockInfoTest;%0A%0Aimport org.junit.Before;%0Aimport org.junit.Test;%0Aimport org.junit.runner.RunWith;%0Aimport org.mockito.Mockito;%0Aimport org.powermock.api.mockito.PowerMockito;%0Aimport org.powermock.core.classloader.annotations.PrepareForTest;%0Aimport org.powermock.modules.junit4.PowerMockRunner;%0Aimport org.powermock.reflect.Whitebox;%0A
+orker.block.BlockWorker;%0A%0Aimport com.fasterxml.jackson.databind.ObjectMapper;%0Aimport com.google.common.collect.Iterables;%0Aimport org.junit.Assert;%0Aimport org.junit.Before;%0Aimport org.junit.Test;%0A%0Aimport java.io.FileOutputStream;
 %0Aimp
@@ -1059,75 +1059,8 @@
  */%0A
-@RunWith(PowerMockRunner.class)%0A@PrepareForTest(BlockMaster.class)%0A
 publ
@@ -1128,44 +1128,8 @@
 st %7B
-%0A  private BlockMaster mBlockMaster;
 %0A%0A  
@@ -1182,420 +1182,8 @@
 n %7B%0A
-    AlluxioMaster alluxioMaster = mResource.get().getMaster().getInternalMaster();%0A    mBlockMaster = PowerMockito.mock(BlockMaster.class);%0A    // Replace the block master created by LocalAlluxioClusterResource with a mock.%0A    BlockMaster blockMaster = Whitebox.getInternalState(alluxioMaster, %22mBlockMaster%22);%0A    blockMaster.stop();%0A    Whitebox.setInternalState(alluxioMaster, %22mBlockMaster%22, mBlockMaster);%0A
     
@@ -1935,231 +1935,567 @@
     
-Map%3CString, String%3E params = new HashMap%3C%3E();%0A    params.put(%22blockId%22, %221%22);%0A%0A    BlockInfo blockInfo = BlockInfoTest.createRandom();%0A    Mockito.doReturn(blockInfo).when(mBlockMaster).getBlockInfo(Mockito.anyLong(
+long sessionId = 1;%0A    long blockId = 2;%0A    String tierAlias = %22MEM%22;%0A    long initialBytes = 3;%0A%0A    BlockWorker blockWorker = mResource.get().getWorker().getBlockWorker();%0A    String file = blockWorker.createBlock(sessionId, blockId, tierAlias, initialBytes);%0A    FileOutputStream outStream = new FileOutputStream(file);%0A    outStream.write(%22abc%22.getBytes());%0A    outStream.close();%0A    blockWorker.commitBlock(sessionId, blockId);%0A%0A    Map%3CString, String%3E params = new HashMap%3C%3E();%0A    params.put(%22blockId%22, Long.toString(blockId
 ));%0A
-%0A
    
+ String response =
  new
@@ -2513,32 +2513,40 @@
 Hostname, mPort,
+%0A       
  getEndpoint(Blo
@@ -2594,24 +2594,16 @@
 K_INFO),
-%0A       
  params,
@@ -2622,89 +2622,328 @@
 GET,
- blockInfo).run(
+%0A        null).call();%0A    BlockInfo blockInfo = new ObjectMapper().readValue(response, BlockInfo.class
 );%0A
-%0A
     
-Mockito.verify(mBlockMaster).getBlockInfo(Mockito.anyLong
+Assert.assertEquals(blockId, blockInfo.getBlockId());%0A    Assert.assertEquals(initialBytes, blockInfo.getLength());%0A    Assert.assertEquals(%22MEM%22, Iterables.getOnlyElement(blockInfo.getLocations()).getTierAlias
 ());

Make BlockMasterClientRestApiTest an integration test