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

com.teststeps.thekla4j.websocket.stomp.spp.activities.Messages Maven / Gradle / Ivy

package com.teststeps.thekla4j.websocket.stomp.spp.activities;

import com.teststeps.thekla4j.activityLog.annotations.Action;
import com.teststeps.thekla4j.commons.error.ActivityError;
import com.teststeps.thekla4j.core.base.activities.Interaction;
import com.teststeps.thekla4j.core.base.persona.Actor;
import com.teststeps.thekla4j.utils.vavr.LiftTry;
import com.teststeps.thekla4j.websocket.stomp.core.Destination;
import com.teststeps.thekla4j.websocket.stomp.core.StompDestination;
import com.teststeps.thekla4j.websocket.stomp.core.StompFrame;
import com.teststeps.thekla4j.websocket.stomp.core.StompHeaders;
import com.teststeps.thekla4j.websocket.stomp.spp.abilities.UseWebsocketWithStomp;
import io.vavr.Function1;
import io.vavr.collection.List;
import io.vavr.control.Either;
import io.vavr.control.Try;

import java.util.function.Predicate;

@Action("getting messages of destination @{destination}")
public class Messages extends Interaction>> {

  private final Destination destination;

  private Predicate payloadFilter;
  private Predicate headerFilter;

  private final Function1> payloadParser;

  @Override
  protected Either>> performAs(Actor actor, Void result) {

    return UseWebsocketWithStomp.as(actor)
      .flatMap(ability -> ability.atDestination(destination))
      .flatMap(StompDestination::messages)
      .map(l -> l.filter(f -> headerFilter.test(f.headers)))
      .map(l -> l.map(f -> payloadParser.apply(f.payload).map(p -> StompFrame.of(f.command, f.headers, p))))
      .map(l -> l.map(t -> t.onFailure(System.err::println)))
      .map(LiftTry.fromList())
      .flatMap(ActivityError.toEither("Error while parsing StompFrame"))
      .map(frames -> frames.filter(f -> payloadFilter.test(f.payload)));
  }


  public static Messages of(Destination destination) {
    return new Messages<>(destination, x -> true, x -> true, Try::success);
  }

  public static 

Messages

of(Destination destination, Function1> payloadParser) { return new Messages<>(destination, x -> true, x-> true, payloadParser); } public Messages filterByHeader(Predicate filter) { this.headerFilter = filter; return this; } public Messages filterByPayload(Predicate filter) { this.payloadFilter = filter; return this; } public Messages(Destination dest, Predicate payloadFilter, Predicate headerFilter, Function1> p) { this.destination = dest; this.payloadFilter = payloadFilter; this.headerFilter = headerFilter; this.payloadParser = p; } }