content: /**
   * Copyright (c) 2016-present, RxJava Contributors.
   *
   * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
   * compliance with the License. You may obtain a copy of the License at
   *
   * http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software distributed under the License is
   * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
   * the License for the specific language governing permissions and limitations under the License.
   */
  
  package io.reactivex.interop.internal.operators;
  
  import static io.reactivex.interop.RxJava3Interop.*;
  import static org.mockito.ArgumentMatchers.any;
  import static org.mockito.ArgumentMatchers.anyString;
  import static org.mockito.Mockito.*;
  
  import java.util.NoSuchElementException;
  
  import org.junit.*;
  import org.mockito.InOrder;
  
  import io.reactivex.common.functions.Predicate;
  import io.reactivex.flowable.Flowable;
  import io.reactivex.interop.TestHelper;
  import io.reactivex.observable.*;
  
  public class FlowableFirstTest {
  
      SingleObserver<Object> wo;
  
      MaybeObserver<Object> wm;
  
      private static final Predicate<String> IS_D = new Predicate<String>() {
          @Override
          public boolean test(String value) {
              return "d".equals(value);
          }
      };
  
      @Before
      public void before() {
          wo = TestHelper.mockSingleObserver();
          wm = TestHelper.mockMaybeObserver();
      }
  
      @Test
      public void testFirstOrElseOfNone() {
          Flowable<String> src = Flowable.empty();
          first(src, "default").subscribe(wo);
  
          verify(wo, times(1)).onSuccess(anyString());
          verify(wo, times(1)).onSuccess("default");
          verify(wo, never()).onError(any(Throwable.class));
      }
  
      @Test
      public void testFirstOrElseOfSome() {
          Flowable<String> src = Flowable.just("a", "b", "c");
          first(src, "default").subscribe(wo);
  
          verify(wo, times(1)).onSuccess(anyString());
          verify(wo, times(1)).onSuccess("a");
          verify(wo, never()).onError(any(Throwable.class));
      }
  
      @Test
      public void testFirstOrElseWithPredicateOfNoneMatchingThePredicate() {
          Flowable<String> src = Flowable.just("a", "b", "c");
          first(src.filter(IS_D), "default").subscribe(wo);
  
          verify(wo, times(1)).onSuccess(anyString());
          verify(wo, times(1)).onSuccess("default");
          verify(wo, never()).onError(any(Throwable.class));
      }
  
      @Test
      public void testFirstOrElseWithPredicateOfSome() {
          Flowable<String> src = Flowable.just("a", "b", "c", "d", "e", "f");
          first(src.filter(IS_D), "default").subscribe(wo);
  
          verify(wo, times(1)).onSuccess(anyString());
          verify(wo, times(1)).onSuccess("d");
          verify(wo, never()).onError(any(Throwable.class));
      }
  
      @Test
      public void testFirst() {
          Maybe<Integer> observable = firstElement(Flowable.just(1, 2, 3));
  
          observable.subscribe(wm);
  
          InOrder inOrder = inOrder(wm);
          inOrder.verify(wm, times(1)).onSuccess(1);
          inOrder.verifyNoMoreInteractions();
      }
  
      @Test
      public void testFirstWithOneElement() {
          Maybe<Integer> observable = firstElement(Flowable.just(1));
  
          observable.subscribe(wm);
  
          InOrder inOrder = inOrder(wm);
          inOrder.verify(wm, times(1)).onSuccess(1);
          inOrder.verifyNoMoreInteractions();
      }
  
      @Test
      public void testFirstWithEmpty() {
          Maybe<Integer> observable = firstElement(Flowable.<Integer> empty());
  
          observable.subscribe(wm);
  
          InOrder inOrder = inOrder(wm);
          inOrder.verify(wm).onComplete();
          inOrder.verify(wm, never()).onError(any(Throwable.class));
          inOrder.verifyNoMoreInteractions();
      }
  
      @Test
      public void testFirstWithPredicate() {
          Maybe<Integer> observable = firstElement(Flowable.just(1, 2, 3, 4, 5, 6)
                  .filter(new Predicate<Integer>() {
                      @Override
                      public boolean test(Integer t1) {
                          return t1 % 2 == 0;
                      }
                  })
                  );
  
          observable.subscribe(wm);
  
          InOrder inOrder = inOrder(wm);
          inOrder.verify(wm, times(1)).onSuccess(2);
          inOrder.verifyNoMoreInteractions();
      }
  
      @Test
      public void testFirstWithPredicateAndOneElement() {
          Maybe<Integer> observable = firstElement(Flowable.just(1, 2)
                  .filter(new Predicate<Integer>() {
                      @Override
                      public boolean test(Integer t1) {
                          return t1 % 2 == 0;
                      }
                  })
                  );
  
          observable.subscribe(wm);
  
          InOrder inOrder = inOrder(wm);
          inOrder.verify(wm, times(1)).onSuccess(2);
          inOrder.verifyNoMoreInteractions();
      }
  
      @Test
      public void testFirstWithPredicateAndEmpty() {
          Maybe<Integer> observable = firstElement(Flowable.just(1)
                  .filter(new Predicate<Integer>() {
                      @Override
                      public boolean test(Integer t1) {
                          return t1 % 2 == 0;
                      }
                  })
                  );
  
          observable.subscribe(wm);
  
          InOrder inOrder = inOrder(wm);
          inOrder.verify(wm).onComplete();
          inOrder.verify(wm, never()).onError(any(Throwable.class));
          inOrder.verifyNoMoreInteractions();
      }
  
      @Test
      public void testFirstOrDefault() {
          Single<Integer> observable = first(Flowable.just(1, 2, 3)
                  , 4);
  
          observable.subscribe(wo);
  
          InOrder inOrder = inOrder(wo);
          inOrder.verify(wo, times(1)).onSuccess(1);
          inOrder.verifyNoMoreInteractions();
      }
  
      @Test
      public void testFirstOrDefaultWithOneElement() {
          Single<Integer> observable = first(Flowable.just(1), 2);
  
          observable.subscribe(wo);
  
          InOrder inOrder = inOrder(wo);
          inOrder.verify(wo, times(1)).onSuccess(1);
          inOrder.verifyNoMoreInteractions();
      }
  
      @Test
      public void testFirstOrDefaultWithEmpty() {
          Single<Integer> observable = first(Flowable.<Integer> empty()
                  , 1);
  
          observable.subscribe(wo);
  
          InOrder inOrder = inOrder(wo);
          inOrder.verify(wo, times(1)).onSuccess(1);
          inOrder.verifyNoMoreInteractions();
      }
  
      @Test
      public void testFirstOrDefaultWithPredicate() {
          Single<Integer> observable = first(Flowable.just(1, 2, 3, 4, 5, 6)
                  .filter(new Predicate<Integer>() {
                      @Override
                      public boolean test(Integer t1) {
                          return t1 % 2 == 0;
                      }
                  })
                  , 8);
  
          observable.subscribe(wo);
  
          InOrder inOrder = inOrder(wo);
          inOrder.verify(wo, times(1)).onSuccess(2);
          inOrder.verifyNoMoreInteractions();
      }
  
      @Test
      public void testFirstOrDefaultWithPredicateAndOneElement() {
          Single<Integer> observable = first(Flowable.just(1, 2)
                  .filter(new Predicate<Integer>() {
                      @Override
                      public boolean test(Integer t1) {
                          return t1 % 2 == 0;
                      }
                  })
                  , 4);
  
          observable.subscribe(wo);
  
          InOrder inOrder = inOrder(wo);
          inOrder.verify(wo, times(1)).onSuccess(2);
          inOrder.verifyNoMoreInteractions();
      }
  
      @Test
      public void testFirstOrDefaultWithPredicateAndEmpty() {
          Single<Integer> observable = first(Flowable.just(1)
                  .filter(new Predicate<Integer>() {
                      @Override
                      public boolean test(Integer t1) {
                          return t1 % 2 == 0;
                      }
                  })
                  , 2);
  
          observable.subscribe(wo);
  
          InOrder inOrder = inOrder(wo);
          inOrder.verify(wo, times(1)).onSuccess(2);
          inOrder.verifyNoMoreInteractions();
      }
  
      @Test
      public void firstOrErrorNoElement() {
          firstOrError(Flowable.empty()
              )
              .test()
              .assertNoValues()
              .assertError(NoSuchElementException.class);
      }
  
      @Test
      public void firstOrErrorOneElement() {
          firstOrError(Flowable.just(1)
              )
              .test()
              .assertNoErrors()
              .assertValue(1);
      }
  
      @Test
      public void firstOrErrorMultipleElements() {
          firstOrError(Flowable.just(1, 2, 3)
              )
              .test()
              .assertNoErrors()
              .assertValue(1);
      }
  
      @Test
      public void firstOrErrorError() {
          firstOrError(Flowable.error(new RuntimeException("error"))
              )
              .test()
              .assertNoValues()
              .assertErrorMessage("error")
              .assertError(RuntimeException.class);
      }
  
  }
  