/*
 * Copyright 2018, TeamDev. All rights reserved.
 *
 * Redistribution and use in source and/or binary forms, with or without
 * modification, must retain the above copyright notice and the following
 * disclaimer.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package io.spine.server.entity;

import com.google.protobuf.Empty;
import com.google.protobuf.Timestamp;
import io.spine.base.Time;
import io.spine.core.Event;
import io.spine.core.EventClass;
import io.spine.testing.server.TestEventFactory;
import io.spine.time.LocalDate;
import io.spine.time.LocalDates;
import io.spine.time.ZonedDateTimes;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Stream;

import static java.util.stream.Collectors.toList;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
 * @author Dmytro Dashenkov
 */
@DisplayName("WhiteListEventFilter should")
class WhiteListEventFilterTest {

    private static final Set<EventClass> WHITE_LIST = EventClass.setOf(
            Timestamp.class,
            LocalDate.class
    );

    private static final TestEventFactory eventFactory =
            TestEventFactory.newInstance(WhiteListEventFilterTest.class);

    @Test
    @DisplayName("allow eventFactory of white list type")
    void acceptAllowed() {
        WhiteListFilter filter = WhiteListFilter.allowEvents(WHITE_LIST);
        Event event = eventFactory.createEvent(Timestamp.getDefaultInstance());
        Optional<Event> result = filter.filter(event);
        assertTrue(result.isPresent());
        assertEquals(event, result.get());
    }

    @Test
    @DisplayName("allow bulk eventFactory of white list types")
    void acceptAllowedBulk() {
        WhiteListFilter filter = WhiteListFilter.allowEvents(WHITE_LIST);
        List<Event> events = Stream.of(Time.getCurrentTime(),
                                       LocalDates.now())
                                   .map(eventFactory::createEvent)
                                   .collect(toList());
        Collection<Event> filtered = filter.filter(events);
        assertEquals(events.size(), filtered.size());
        assertTrue(events.containsAll(filtered));
    }

    @Test
    @DisplayName("filter out non-allowed events")
    void filterOut() {
        WhiteListFilter filter = WhiteListFilter.allowEvents(WHITE_LIST);
        List<Event> events = Stream.of(Time.getCurrentTime(),
                                       ZonedDateTimes.now())
                                   .map(eventFactory::createEvent)
                                   .collect(toList());
        Collection<Event> filtered = filter.filter(events);
        assertEquals(1, filtered.size());
        assertTrue(events.contains(events.get(0)));
    }

    @Test
    @DisplayName("not allow events out from the white list")
    void denyEvents() {
        WhiteListFilter filter = WhiteListFilter.allowEvents(WHITE_LIST);
        Event event = eventFactory.createEvent(Empty.getDefaultInstance());
        Optional<Event> result = filter.filter(event);
        assertFalse(result.isPresent());
    }
}

Add tests for the WhiteListFilter.