protected void testAllCallsAreDelegated(CommentManager commentManager) throws PortalException {

    long userId = RandomTestUtil.randomLong();
    long groupId = RandomTestUtil.randomLong();
    String className = RandomTestUtil.randomString();
    long classPK = RandomTestUtil.randomLong();
    String userName = RandomTestUtil.randomString();
    String subject = RandomTestUtil.randomString();
    String body = RandomTestUtil.randomString();
    long commentId = RandomTestUtil.randomLong();

    ServiceContext serviceContext = new ServiceContext();

    _commentManagerImpl.addComment(userId, groupId, className, classPK, body, serviceContext);

    Mockito.verify(commentManager)
        .addComment(userId, groupId, className, classPK, body, serviceContext);

    when(commentManager.addComment(
            userId, groupId, className, classPK, userName, subject, body, _serviceContextFunction))
        .thenReturn(commentId);

    Assert.assertEquals(
        commentId,
        _commentManagerImpl.addComment(
            userId, groupId, className, classPK, userName, subject, body, _serviceContextFunction));

    _commentManagerImpl.addDiscussion(userId, groupId, className, classPK, userName);

    Mockito.verify(commentManager).addDiscussion(userId, groupId, className, classPK, userName);

    _commentManagerImpl.deleteComment(commentId);

    Mockito.verify(commentManager).deleteComment(commentId);

    _commentManagerImpl.deleteDiscussion(className, classPK);

    Mockito.verify(commentManager).deleteDiscussion(className, classPK);

    int commentsCount = RandomTestUtil.randomInt();

    when(commentManager.getCommentsCount(className, classPK)).thenReturn(commentsCount);

    Assert.assertEquals(commentsCount, _commentManagerImpl.getCommentsCount(className, classPK));
  }
/** @author André de Oliveira */
public class ReplicasClusterListenerTest {

  @Before
  public void setUp() {
    MockitoAnnotations.initMocks(this);

    setEmbeddedCluster(true);
    setMasterExecutor(true);

    Mockito.when(_replicasClusterContext.getClusterSize()).thenReturn(_REPLICAS + 1);

    Mockito.when(_replicasClusterContext.getReplicasManager()).thenReturn(_replicasManager);

    Mockito.when(_replicasClusterContext.getTargetIndexNames()).thenReturn(_INDICES);

    _replicasClusterListener = new ReplicasClusterListener(_replicasClusterContext);
  }

  @Test
  public void testAHappyDay() {
    processClusterEvent();
    assertReplicasChanged();
  }

  @Test
  public void testLiferayClusterReportsEmpty() {
    Mockito.when(_replicasClusterContext.getClusterSize()).thenReturn(0);

    processClusterEvent();

    Mockito.verify(_replicasManager).updateNumberOfReplicas(0, _INDICES);
  }

  @Test
  public void testMasterTokenAcquired() {
    masterTokenAcquired();

    assertReplicasChanged();
  }

  @Test
  public void testMasterTokenReleased() {
    masterTokenReleased();

    assertReplicasUnchanged();
  }

  @Test
  public void testNonMasterLiferayNodeDoesNothing() {
    setMasterExecutor(false);

    processClusterEvent();

    assertReplicasUnchanged();
  }

  @Test
  public void testRemoteElasticsearchClusterIsLeftAlone() {
    setEmbeddedCluster(false);

    processClusterEvent();

    assertReplicasUnchanged();
  }

  @Test
  public void testResilientToUpdateFailures() {
    Throwable throwable = new RuntimeException();

    Mockito.doThrow(throwable)
        .when(_replicasManager)
        .updateNumberOfReplicas(Mockito.anyInt(), (String[]) Mockito.anyVararg());

    try (CaptureHandler captureHandler =
        JDKLoggerTestUtil.configureJDKLogger(
            ReplicasClusterListener.class.getName(), Level.WARNING)) {

      masterTokenAcquired();

      List<LogRecord> logRecords = captureHandler.getLogRecords();

      Assert.assertEquals(1, logRecords.size());

      LogRecord logRecord = logRecords.get(0);

      Assert.assertEquals("Unable to update number of replicas", logRecord.getMessage());
      Assert.assertSame(throwable, logRecord.getThrown());
    }
  }

  protected void assertReplicasChanged() {
    Mockito.verify(_replicasManager).updateNumberOfReplicas(_REPLICAS, _INDICES);
  }

  protected void assertReplicasUnchanged() {
    Mockito.verify(_replicasManager, Mockito.never())
        .updateNumberOfReplicas(Mockito.anyInt(), (String[]) Mockito.anyVararg());
  }

  protected void masterTokenAcquired() {
    _replicasClusterListener.masterTokenAcquired();
  }

  protected void masterTokenReleased() {
    _replicasClusterListener.masterTokenReleased();
  }

  protected void processClusterEvent() {
    _replicasClusterListener.processClusterEvent(ClusterEvent.join());
  }

  protected void setEmbeddedCluster(boolean value) {
    Mockito.when(_replicasClusterContext.isEmbeddedOperationMode()).thenReturn(value);
  }

  protected void setMasterExecutor(boolean value) {
    Mockito.when(_replicasClusterContext.isMaster()).thenReturn(value);
  }

  private static final String[] _INDICES =
      new String[] {RandomTestUtil.randomString(), RandomTestUtil.randomString()};

  private static final int _REPLICAS = RandomTestUtil.randomInt() - 1;

  @Mock private ReplicasClusterContext _replicasClusterContext;

  private ReplicasClusterListener _replicasClusterListener;

  @Mock private ReplicasManager _replicasManager;
}