com.github.grzesiek_galezowski.test_environment.buffer.implementation.DefaultReceivedObjectBuffer Maven / Gradle / Ivy
package com.github.grzesiek_galezowski.test_environment.buffer.implementation;
import com.github.grzesiek_galezowski.test_environment.buffer.interfaces.*;
import lombok.val;
import org.assertj.core.api.Condition;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.StreamSupport;
/**
* Created by grzes on 26.06.2017.
*/
public final class DefaultReceivedObjectBuffer implements Buffer {
private final BufferItemNotificationSubscribers
subscribers;
private List receivedObjects = new ArrayList<>();
private BufferObserver observer;
private SearchCommandFactory searchCommandFactory;
public DefaultReceivedObjectBuffer(final BufferObserver observer, final BufferItemNotificationSubscribers subscribers) {
this.observer = observer;
this.searchCommandFactory = new SearchCommandFactory<>(observer, receivedObjects);
this.subscribers = subscribers;
}
@Override
public void store(final T object) {
receivedObjects.add(object);
subscribers.notifySubscribersAbout(object);
}
@Override
public void store(final T[] objects) {
Arrays.stream(objects).forEach(item -> store(item));
}
@Override
public void store(final Iterable objects) {
StreamSupport.stream(objects.spliterator(), false)
.forEach(item -> store(item));
}
@Override
public void assertContains(final MatchCountCondition matchCountCondition, final Condition condition) {
val searchResult = blankResultFor(condition, matchCountCondition);
val searchCommand = searchCommandFactory.searchFor(condition, searchResult);
searchCommand.performSearch();
searchResult.assertFoundAccordingToSpecification();
}
@Override
public boolean contains(final MatchCountCondition matchCountCondition, final Condition condition) {
val searchResult = blankResultFor(condition, matchCountCondition);
val searchCommand = searchCommandFactory.searchFor(condition, searchResult);
searchCommand.performSearch();
return searchResult.foundAccordingToSpecification();
}
@Override
public boolean isEmpty() {
return receivedObjects.isEmpty();
}
@Override
public void clearItems() {
receivedObjects.clear();
}
@Override
public void subscribeForItems(final ItemSubscriber subscriber) {
subscribers.add(subscriber);
}
@Override
public void subscribeFor(final Condition condition, final ItemSubscriber subscriber) {
subscribers.add(subscriber, condition);
}
//todo add toString that prints current buffer
@Override
public Poll poll() {
return AwaitilityPoll.on(this, observer);
}
@Override
public Poll pollFor(final Duration duration) {
return AwaitilityPoll.on(this, duration, observer);
}
private SearchResult blankResultFor(final Condition expected, final MatchCountCondition matchCountCondition) {
return new SearchResult(matchCountCondition, expected, observer);
}
}