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

jp.co.moneyforward.autotest.framework.action.Resolver Maven / Gradle / Ivy

The newest version!
package jp.co.moneyforward.autotest.framework.action;

import com.github.dakusui.actionunit.core.Context;
import com.github.valid8j.pcond.forms.Printables;
import org.slf4j.Logger;

import java.util.List;
import java.util.Map;
import java.util.function.Function;

import static com.github.valid8j.classic.Requires.requireNonNull;
import static java.lang.String.format;

///
/// `Resolver` figures out a variable designated by `variableName` in a context.
/// Note that a resolver takes a variable name only, and it returns a value for a given variable name.
///
/// A resolver provides a "namespace" of variables available for a scene.
///
/// @param variableName     A name of a variable whose value is to be resolved by this object.
/// @param resolverFunction A function that resolves the value.
///
public record Resolver(String variableName, Function resolverFunction) {
  private static final Logger logger = org.slf4j.LoggerFactory.getLogger(Resolver.class);
  ///
  /// Typically, this function is called by a method `resolverFor` and the `variableName` passed to it should be used as `variableNameInScene` for this method.
  ///
  /// @param variableName      A name of a variable whose value is to be resolved.
  /// @param variableStoreName A name of a scene in which the value of the variable is resolved.
  /// @return A function that gives the value of `variableNameInScene` from a `Context` object.
  ///
  public static Function resolver(String variableName, String variableStoreName) {
    return Printables.function(format("resolve[%s][%s]", variableName, variableStoreName),
                               context -> resolve(variableName, variableStoreName, context));
  }
  
  private static Object resolve(String variableName, String variableStoreName, Context context) {
    if (context.defined(variableStoreName)) {
      Map variableStore = context.valueOf(variableStoreName);
      if (!variableStore.containsKey(variableName)) {
        logger.warn("A variable: '{}' was not found in a variable store: '{}'.", variableName, variableStoreName);
      }
      logger.trace("A variable store: '{}' was not found in a context.", variableStoreName);
      return variableStore.get(variableName);
    }
    return null;
  }
  
  ///
  /// Returns a `Resolver` object, which resolves a value of a variable designated by `variableName` exported by a scene `sceneName`.
  /// A `sceneName` must be a name of a scene, which is guaranteed to be performed one and only once during one test execution.
  ///
  /// @param variableName      A name of a variable to be resolved by the returned `Resolver`.
  /// @param variableStoreName A name of a scene by which `variableName` is exported.
  /// @return A `Resolver` object.
  ///
  public static Resolver resolverFor(String variableName, String variableStoreName) {
    return new Resolver(variableName, resolver(variableName, variableStoreName));
  }
  
  ///
  /// Returns a list of resolvers for variables specified by `variableNames`.
  /// Resolvers in the list try to find a variable in a variable store specified by `variableStoreName`.
  ///
  /// @param variableNames     Names of variables to be resolved by returned resolvers.
  /// @param variableStoreName A name of variable store from which values of variables are looked up.
  /// @return A list of resolvers.
  ///
  public static List resolversFor(List variableNames, String variableStoreName) {
    requireNonNull(variableStoreName);
    return variableNames.stream()
                        .map(n -> new Resolver(n, (Context c) -> c.>valueOf(variableStoreName).get(n)))
                        .toList();
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy