All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.grzesiek_galezowski.test_environment.buffer.implementation.SearchResult Maven / Gradle / Ivy

package com.github.grzesiek_galezowski.test_environment.buffer.implementation;

import com.github.grzesiek_galezowski.test_environment.buffer.exceptions.MismatchException;
import com.github.grzesiek_galezowski.test_environment.buffer.interfaces.BufferObserver;
import com.github.grzesiek_galezowski.test_environment.buffer.interfaces.MatchCountCondition;
import org.assertj.core.api.Condition;

import java.util.ArrayList;
import java.util.List;

public class SearchResult {
  private final List matchingResult = new ArrayList<>();
  private final List matchingDescriptions = new ArrayList<>();
  private final MatchCountCondition matchCountCondition;
  private final Condition condition;
  private final BufferObserver observer;

  public SearchResult(
      final MatchCountCondition matchCountCondition,
      final Condition condition,
      final BufferObserver observer) {

    this.matchCountCondition = matchCountCondition;
    this.condition = condition;
    this.observer = observer;
  }

  public void add(final T receivedObject, final boolean isMatch, final String matchDescription) {
    matchingResult.add(isMatch);
    matchingDescriptions.add(matchDescription);
    if(isMatch) {
      observer.matchAttemptSuccessful(
          receivedObject,
          this.condition,
          matchDescription);
    } else {
      observer.matchAttemptFailed(
          receivedObject,
          this.condition,
          matchDescription
      );
    }
  }

  public void conclude() {
    this.observer.searchingFinished(this.matchingResult.stream().filter(c -> c).count());
  }

  public void assertFoundAccordingToSpecification() {
    if (!foundAccordingToSpecification()) {
      throw mismatchException();
    }
  }

  public MismatchException mismatchException() {
    return new MismatchException(
        this.condition, matchingDescriptions, matchingResult, matchCountCondition);
  }

  public boolean foundAccordingToSpecification() {
    return this.matchCountCondition.matchFound(matchingResult);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy