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

io.eventuate.tram.sagas.testing.commandhandling.SagaParticipantStubCommandHandler Maven / Gradle / Ivy

There is a newer version: 0.24.0.RELEASE
Show newest version
package io.eventuate.tram.sagas.testing.commandhandling;

import io.eventuate.tram.commands.common.Command;
import io.eventuate.tram.commands.consumer.CommandHandler;
import io.eventuate.tram.commands.consumer.CommandHandlerArgs;
import io.eventuate.tram.commands.consumer.CommandMessage;
import io.eventuate.tram.messaging.common.Message;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Predicate;

import static java.util.Collections.singletonList;

public class SagaParticipantStubCommandHandler extends CommandHandler {

  private String commandChannel;
  private final Predicate expectedCommand;
  private Function, Message> replyBuilder;
  private final List> receivedCommands = new ArrayList<>();

  public SagaParticipantStubCommandHandler(String commandChannel, Class expectedCommandClass, Predicate expectedCommand, Function, Message> replyBuilder) {
    super(commandChannel, Optional.empty(), expectedCommandClass, args -> {
      // Dummy - override invoke method
      return null;
    });
    this.commandChannel = commandChannel;
    this.expectedCommand = expectedCommand;
    this.replyBuilder = replyBuilder;
  }

  @Override
  public List invokeMethod(CommandHandlerArgs args) {
    CommandMessage cm = args.getCommandMessage();
    receivedCommands.add(cm);
    return singletonList(replyBuilder.apply(cm));
  }

  @Override
  public boolean handles(Message message) {
    return message.getRequiredHeader(Message.DESTINATION).equals(commandChannel) && super.handles(message) && expectedCommand.test(message);
  }

  public  boolean handles(String channel, Class commandClass) {
    return commandChannel.equals(channel) && this.getCommandClass().equals(commandClass);
  }

  public void verifyCommandReceived() {
    if (receivedCommands.isEmpty())
      throw new RuntimeException("Did not receive command");
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy