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

import alluxio.exception.status.UnavailableException;
import alluxio.thrift.FileSystemCommand;

import com.google.common.collect.Lists;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

/**
 * Tests {@link FileWorkerMasterSyncExecutor}.
 */
@RunWith(PowerMockRunner.class)
@PrepareForTest({FileDataManager.class, FileSystemMasterClient.class})
public final class FileWorkerMasterSyncExecutorTest {
  private FileDataManager mFileDataManager;
  private FileSystemMasterClient mFileSystemMasterClient;
  private FileWorkerMasterSyncExecutor mFileWorkerMasterSyncExecutor;

  @Before
  public void before() {
    mFileDataManager = Mockito.mock(FileDataManager.class);
    mFileSystemMasterClient = Mockito.mock(FileSystemMasterClient.class);
    mFileWorkerMasterSyncExecutor = new FileWorkerMasterSyncExecutor(mFileDataManager,
        mFileSystemMasterClient, new AtomicReference<>(10L));
  }

  /**
   * {@link FileDataManager#clearPersistedFiles(java.util.List)} is not called when the heartbeat
   * of {@link FileSystemMasterClient} fails.
   */
  @Test
  public void heartbeatFailure() throws Exception {
    List<Long> persistedFiles = Lists.newArrayList(1L);
    Mockito.when(mFileDataManager.getPersistedFiles()).thenReturn(persistedFiles);
    // first time fails, second time passes
    Mockito.when(mFileSystemMasterClient.heartbeat(Mockito.anyLong(), Mockito.eq(persistedFiles)))
        .thenThrow(new UnavailableException("failure"));
    mFileWorkerMasterSyncExecutor.heartbeat();
    Mockito.verify(mFileDataManager, Mockito.never()).clearPersistedFiles(persistedFiles);
  }

  /**
   * Verifies {@link FileDataManager#clearPersistedFiles(java.util.List)} is called when the
   * heartbeat is successful.
   */
  @Test
  public void heartbeat() throws Exception {
    List<Long> persistedFiles = Lists.newArrayList(1L);
    Mockito.when(mFileDataManager.getPersistedFiles()).thenReturn(persistedFiles);
    // first time fails, second time passes
    Mockito.when(mFileSystemMasterClient.heartbeat(Mockito.anyLong(), Mockito.eq(persistedFiles)))
        .thenReturn(new FileSystemCommand());
    mFileWorkerMasterSyncExecutor.heartbeat();
    Mockito.verify(mFileDataManager).clearPersistedFiles(persistedFiles);
  }
}

@@ -537,16 +537,261 @@
 .file;%0A%0A
+import static org.mockito.Mockito.mock;%0Aimport static org.mockito.Mockito.when;%0Aimport static org.mockito.Mockito.anyLong;%0Aimport static org.mockito.Mockito.eq;%0Aimport static org.mockito.Mockito.verify;%0Aimport static org.mockito.Mockito.never;%0A%0A
 import a
@@ -1003,36 +1003,8 @@
 th;%0A
-import org.mockito.Mockito;%0A
 impo
@@ -1638,24 +1638,16 @@
 nager = 
-Mockito.
 mock(Fil
@@ -1697,24 +1697,16 @@
 lient = 
-Mockito.
 mock(Fil
@@ -2159,32 +2159,24 @@
 st(1L);%0A    
-Mockito.
 when(mFileDa
@@ -2278,32 +2278,24 @@
  passes%0A    
-Mockito.
 when(mFileSy
@@ -2317,32 +2317,24 @@
 t.heartbeat(
-Mockito.
 anyLong(), M
@@ -2328,32 +2328,24 @@
 (anyLong(), 
-Mockito.
 eq(persisted
@@ -2457,32 +2457,24 @@
 beat();%0A    
-Mockito.
 verify(mFile
@@ -2486,24 +2486,16 @@
 anager, 
-Mockito.
 never())
@@ -2781,32 +2781,24 @@
 st(1L);%0A    
-Mockito.
 when(mFileDa
@@ -2904,24 +2904,16 @@
 ses%0A    
-Mockito.
 when(mFi
@@ -2943,24 +2943,16 @@
 artbeat(
-Mockito.
 anyLong(
@@ -2954,24 +2954,16 @@
 Long(), 
-Mockito.
 eq(persi
@@ -3072,24 +3072,16 @@
 ();%0A    
-Mockito.
 verify(m

Use static imports for standard test utilities in class FileWorkerMasterSyncExecutorTest