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

com.github.grzesiek_galezowski.test_environment.implementation_details.SynchronizationAssertDsl Maven / Gradle / Ivy

There is a newer version: 1.2.5
Show newest version
package com.github.grzesiek_galezowski.test_environment.implementation_details;

import autofixture.publicinterface.Any;
import autofixture.publicinterface.InstanceOf;
import com.google.common.base.Throwables;

import java.util.function.Consumer;
import java.util.function.Function;

/**
 * Created by astral 16.03.2016.
 */
public class SynchronizationAssertDsl {
  private final T wrappedMock;
  private final T realThing;

  public SynchronizationAssertDsl(final T wrappedMock, final T realThing) {

    this.wrappedMock = wrappedMock;
    this.realThing = realThing;
  }

  public  SynchronizationAssertDsl2 whenReceives(
      final CheckedFunction methodCallToVerify,
      final Class clazz) {
    return whenReceives(throwing(methodCallToVerify), clazz);
  }

  public  SynchronizationAssertDsl2 whenReceives(
      final CheckedFunction methodCallToVerify,
      final InstanceOf clazz) {
    return whenReceives(throwing(methodCallToVerify), clazz);
  }

  public SynchronizationAssertDsl2 whenReceives(final CheckedConsumer consumer) {
    return whenReceives(throwing(consumer));
  }

  private  SynchronizationAssertDsl2 whenReceives(
      final Function methodCallToVerify,
      final Class clazz) {

    final TReturn retVal = Any.anonymous(clazz);
    return dslOver(workflow(forCheckingSynchronizationOf(methodCallToVerify, retVal)));
  }

  private  SynchronizationAssertDsl2 whenReceives(
      final Function methodCallToVerify,
      final InstanceOf clazz) {
    final TReturn retVal = Any.anonymous(clazz);
    return dslOver(workflow(forCheckingSynchronizationOf(methodCallToVerify, retVal)));
  }

  private SynchronizationAssertDsl2 whenReceives(final Consumer consumer) {
    return dslOver(workflow(forCheckingSynchronizationOf(consumer)));
  }

  private SynchronizationAssertDsl2 dslOver(final SynchronizationAssertionWorkflow workflow) {
    return new SynchronizationAssertDsl2<>(realThing,
        workflow);
  }

  private  StepsForSynchronizingOnFunction forCheckingSynchronizationOf(final Function methodCallToVerify, final TReturn retVal) {
    return new StepsForSynchronizingOnFunction<>(methodCallToVerify, retVal);
  }

  private StepsForSynchronizingOnCommand forCheckingSynchronizationOf(final Consumer consumer) {
    return new StepsForSynchronizingOnCommand<>(consumer);
  }

  private SynchronizationAssertionWorkflow workflow(final SynchronizationAssertionSteps stepsForSynchronizingOnCommand) {
    return new SynchronizationAssertionWorkflow<>(this.wrappedMock, realThing,
        stepsForSynchronizingOnCommand);
  }

  private  Function throwing(
      final CheckedFunction methodCallToVerify) {
    return x -> {
      try {
        return methodCallToVerify.apply(x);
      } catch(Throwable e) {
        Throwables.throwIfUnchecked(e);
        throw new RuntimeException(e);
      }
    };
  }

  private Consumer throwing(
      final CheckedConsumer consumer) {
    return x -> {
      try {
        consumer.accept(x);
      } catch(Throwable e) {
        Throwables.throwIfUnchecked(e);
        throw new RuntimeException(e);
      }
    };
  }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy