/*
 * SonarQube
 * Copyright (C) 2009-2024 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.sonar.server.es;

import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.sonar.server.es.IndexingListener.FAIL_ON_ERROR;

public class FailOnErrorIndexingListenerTest {

  @Test
  public void onFinish_must_throw_ISE_when_an_error_is_present() {
    IndexingResult indexingResult = new IndexingResult();

    indexingResult.incrementRequests();

    assertThatThrownBy(() -> FAIL_ON_ERROR.onFinish(indexingResult))
      .isInstanceOf(IllegalStateException.class)
      .hasMessage("Unrecoverable indexing failures: 1 errors among 1 requests. "
        + "Check Elasticsearch logs for further details.");
  }

  @Test
  public void onFinish_must_not_throw_any_exception_if_no_failure() {
    IndexingResult indexingResult = new IndexingResult();

    indexingResult.incrementRequests();
    indexingResult.incrementSuccess();

    assertThatCode(() -> FAIL_ON_ERROR.onFinish(indexingResult))
      .doesNotThrowAnyException();
  }
}
