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

com.xlrit.gears.runner.scenario.RunnerTargetScenarioVisitor Maven / Gradle / Ivy

The newest version!
package com.xlrit.gears.runner.scenario;

import java.util.Optional;

import com.codeborne.selenide.ex.UIAssertionError;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.xlrit.gears.runner.runnertarget.RunnerContext;
import com.xlrit.gears.runner.runnertarget.RunnerError;
import com.xlrit.gears.runner.runnertarget.RunnerTarget;
import com.xlrit.gears.runner.runnertarget.TaskExpression;

public class RunnerTargetScenarioVisitor extends ScenarioBaseVisitor {
	private final RunnerTarget runnerTarget;
	private final RunnerContext runnerContext;
	private final ExprToJsonVisitor exprToJsonVisitor;
	private final BoolExprVisitor boolExprVisitor;

	public RunnerTargetScenarioVisitor(RunnerTarget runnerTarget, RunnerContext runnerContext) {
		this.runnerTarget = runnerTarget;
		this.runnerContext = runnerContext;
		exprToJsonVisitor = new ExprToJsonVisitor(runnerContext, runnerTarget);
		boolExprVisitor = new BoolExprVisitor(exprToJsonVisitor);
	}

	@Override
	public Void visitStartStatements(ScenarioParser.StartStatementsContext ctx) {
		for (var statement : ctx.statement()) {
			try {
				runnerContext.lineNumber = statement.getStart().getLine();
				statement.accept(this);
			}
			catch (Exception | UIAssertionError e) {
				if (runnerContext.config.debug) {
					e.printStackTrace();
				}
				runnerContext.errors.add(new RunnerError(runnerContext.lineNumber, e.getMessage()));
				return null;
			}
		}
		return null;
	}

	@Override
	public Void visitSetDateTimeStatement(ScenarioParser.SetDateTimeStatementContext ctx) {
		String dateTime = ctx.Text().getSymbol().getText();
		runnerTarget.setDateTime(dateTime.substring(1, dateTime.length() - 1));
		return null;
	}

	@Override
	public Void visitWithUserStatement(ScenarioParser.WithUserStatementContext ctx) {
		String userName = ctx.Identifier().getText();
		runnerContext.userName = userName;
		runnerTarget.login(userName);
		return null;
	}

	@Override
	public Void visitStartProcessStatement(ScenarioParser.StartProcessStatementContext ctx) {
		runnerContext.processKeyStack.push(ctx.Identifier().getSymbol().getText());
		JsonNode values;
		if (ctx.objectExpr() == null) {
			values = null;
		} else {
			values = ctx.objectExpr().accept(exprToJsonVisitor);
		}
		runnerTarget.startProcess(runnerContext.processKeyStack.peek(), values);
		return null;
	}

	@Override
	public Void visitProcessCompleteStatement(ScenarioParser.ProcessCompleteStatementContext ctx) {
		if (runnerContext.processKeyStack.isEmpty()) {
			throw new IllegalStateException("No active process found at time of process complete statement");
		}
		runnerTarget.processCompleted(runnerContext.processKeyStack.pop());
		return null;
	}

	@Override
	public Void visitLetIsStatement(ScenarioParser.LetIsStatementContext ctx) {
		JsonNode value = ctx.expr().accept(exprToJsonVisitor);
		runnerContext.variables.put(ctx.Identifier().getSymbol().getText(), value);
		return null;
	}

	@Override
	public Void visitLetFromStatement(ScenarioParser.LetFromStatementContext ctx) {
		JsonNode value = ctx.expr().accept(exprToJsonVisitor);
		if (!value.isArray()) {
			throw new IllegalArgumentException(String.format("Cannot assign singular %s to multiple variables", value));
		}
		ArrayNode values = (ArrayNode) value;
		if (values.size() != ctx.Identifier().size()) {
			throw new IllegalArgumentException(String.format("Found %d variables but %d values", ctx.Identifier().size(), values.size()));
		}
		for (int i = 0; i < values.size(); i++) {
			runnerContext.variables.put(ctx.Identifier(i).getSymbol().getText(), values.get(i));
		}
		return null;
	}

	@Override
	public Void visitAssertStatement(ScenarioParser.AssertStatementContext ctx) {
		Optional assertMessage = ctx.boolExpr().accept(boolExprVisitor);
		assertMessage.ifPresent(message -> runnerContext.errors.add(new RunnerError(runnerContext.lineNumber, message)));
		return null;
	}

	@Override
	public Void visitClaimTaskStatement(ScenarioParser.ClaimTaskStatementContext ctx) {
		if (runnerContext.processKeyStack.isEmpty()) {
			throw new IllegalStateException("No active process found at time of claim task statement");
		}
		runnerTarget.claimTask(runnerContext.processKeyStack.peek(), TaskExpression.from(ctx.taskExpr()));
		return null;
	}

	@Override
	public Void visitTransferTaskStatement(ScenarioParser.TransferTaskStatementContext ctx) {
		if (runnerContext.processKeyStack.isEmpty()) {
			throw new IllegalStateException("No active process found at time of reassign task statement");
		}
		String currentAssignee = ctx.Identifier().getText();
		runnerTarget.transferTask(runnerContext.processKeyStack.peek(), TaskExpression.from(ctx.taskExpr()), currentAssignee);
		return null;
	}

	@Override
	public Void visitSubmitTaskStatement(ScenarioParser.SubmitTaskStatementContext ctx) {
		if (runnerContext.processKeyStack.isEmpty()) {
			throw new IllegalStateException("No active process found at time of submit task statement");
		}
		TaskExpression taskExpression = TaskExpression.from(ctx.taskExpr());
		JsonNode values = ctx.objectExpr().accept(exprToJsonVisitor);
		runnerTarget.submitTask(runnerContext.processKeyStack.peek(), taskExpression, values);
		return null;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy