/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 org.apache.ignite.internal.util.subscription;

import static org.apache.ignite.internal.testframework.matchers.CompletableFutureMatcher.willSucceedFast;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Flow.Publisher;
import java.util.concurrent.Flow.Subscription;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import org.apache.ignite.internal.testframework.flow.TestFlowUtils;
import org.apache.ignite.internal.util.SubscriptionUtils;
import org.junit.jupiter.api.Test;

class IterableToPublisherAdapterTest {
    private static final int NUM_ITEMS = 10;

    private final List<Integer> list = IntStream.range(0, NUM_ITEMS).boxed().collect(Collectors.toList());

    private final List<Integer> accumulator = new ArrayList<>();
    private final Publisher<Integer> publisher = SubscriptionUtils.fromIterable(list);
    private final CompletableFuture<Void> done = new CompletableFuture<>();

    private final Subscription subscription = TestFlowUtils.subscribeToPublisher(accumulator, publisher, done);

    @Test
    void requestMax() {
        subscription.request(Long.MAX_VALUE);
        assertThat(done, willSucceedFast());
        assertThat(accumulator, equalTo(list));
    }

    @Test
    void requestExact() {
        subscription.request(NUM_ITEMS);
        assertThat(done, willSucceedFast());
        assertThat(accumulator, equalTo(list));
    }

    @Test
    void requestMore() {
        subscription.request(NUM_ITEMS + 1);
        assertThat(done, willSucceedFast());
        assertThat(accumulator, equalTo(list));
    }

    @Test
    void requestLess() {
        subscription.request(NUM_ITEMS - 1);
        assertThat(done.isDone(), is(false));
        assertThat(accumulator, hasSize(NUM_ITEMS - 1));

        subscription.request(1);
        assertThat(done, willSucceedFast());
        assertThat(accumulator, equalTo(list));
    }

    @Test
    void emptySource() {
        List<Integer> list = List.of();

        List<Integer> accumulator = new ArrayList<>();
        Publisher<Integer> publisher = SubscriptionUtils.fromIterable(list);
        CompletableFuture<Void> done = new CompletableFuture<>();

        Subscription subscription = TestFlowUtils.subscribeToPublisher(accumulator, publisher, done);
        subscription.request(Long.MAX_VALUE);
        assertThat(done, willSucceedFast());
        assertThat(accumulator, equalTo(list));
    }
}
