Search in sources :

Example 1 with GherkinStepCondition

use of com.github.noraui.gherkin.GherkinStepCondition in project NoraUi by NoraUi.

the class Step method runAllStepsInLoop.

/**
 * Runs a bunch of steps for a Gherkin loop.
 *
 * @param loopedSteps
 *            GherkinConditionedLoopedStep steps to run
 * @throws TechnicalException
 *             is thrown if you have a technical error (format, configuration, datas, ...) in NoraUi.
 */
protected void runAllStepsInLoop(List<GherkinConditionedLoopedStep> loopedSteps) throws TechnicalException {
    for (final GherkinConditionedLoopedStep loopedStep : loopedSteps) {
        final List<GherkinStepCondition> stepConditions = new ArrayList<>();
        final String[] expecteds = loopedStep.getExpected().split(";");
        final String[] actuals = loopedStep.getActual().split(";");
        if (actuals.length != expecteds.length) {
            throw new TechnicalException(Messages.getMessage(TechnicalException.TECHNICAL_EXPECTED_ACTUAL_SIZE_DIFFERENT));
        }
        for (int i = 0; i < expecteds.length; i++) {
            stepConditions.add(new GherkinStepCondition(loopedStep.getKey(), expecteds[i], actuals[i]));
        }
        boolean found = false;
        for (final Entry<String, Method> elem : Context.getCucumberMethods().entrySet()) {
            final Matcher matcher = Pattern.compile("value=(.*)\\)").matcher(elem.getKey());
            if (matcher.find()) {
                final Matcher matcher2 = Pattern.compile(matcher.group(1)).matcher(loopedStep.getStep());
                if (matcher2.find()) {
                    Object[] tab;
                    if (elem.getValue().isAnnotationPresent(Conditioned.class)) {
                        tab = new Object[matcher2.groupCount() + 1];
                        tab[matcher2.groupCount()] = stepConditions;
                    } else {
                        tab = new Object[matcher2.groupCount()];
                    }
                    for (int i = 0; i < matcher2.groupCount(); i++) {
                        final Parameter param = elem.getValue().getParameters()[i];
                        if (param.getType() == int.class) {
                            final int ii = Integer.parseInt(matcher2.group(i + 1));
                            tab[i] = ii;
                        } else if (param.getType() == boolean.class) {
                            tab[i] = Boolean.parseBoolean(matcher2.group(i + 1));
                        } else {
                            tab[i] = matcher2.group(i + 1);
                        }
                    }
                    try {
                        found = true;
                        elem.getValue().invoke(NoraUiInjector.getNoraUiInjectorSource().getInstance(elem.getValue().getDeclaringClass()), tab);
                        break;
                    } catch (final Exception e) {
                        throw new TechnicalException("\"" + loopedStep.getStep() + "\"", e.getCause());
                    }
                }
            }
        }
        if (!found) {
            throw new TechnicalException(String.format(Messages.getMessage(TechnicalException.TECHNICAL_ERROR_STEP_UNDEFINED), loopedStep.getStep()));
        }
    }
}
Also used : TechnicalException(com.github.noraui.exception.TechnicalException) Matcher(java.util.regex.Matcher) GherkinConditionedLoopedStep(com.github.noraui.gherkin.GherkinConditionedLoopedStep) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) GherkinStepCondition(com.github.noraui.gherkin.GherkinStepCondition) ParseException(java.text.ParseException) FailureException(com.github.noraui.exception.FailureException) TechnicalException(com.github.noraui.exception.TechnicalException) Parameter(java.lang.reflect.Parameter)

Example 2 with GherkinStepCondition

use of com.github.noraui.gherkin.GherkinStepCondition in project NoraUi by NoraUi.

the class CommonSteps method doUntil.

/**
 * Does steps execution until a given condition is unverified.
 *
 * @param actual
 *            actual value for global condition.
 * @param expected
 *            expected value for global condition.
 * @param key
 *            key of 'expected' values ('actual' values)
 * @param breakCondition
 *            'expected' values
 * @param tries
 *            number of max tries (no infinity loop).
 * @param conditions
 *            list of steps run in a loop.
 */
@Lorsque("Si '(.*)' vérifie '(.*)', je fais jusqu'à '(.*)' respecte '(.*)' avec '(.*)' essais maxi:")
@Then("If '(.*)' matches '(.*)', I do until '(.*)' respects '(.*)' with '(.*)' max tries:")
public void doUntil(String actual, String expected, String key, String breakCondition, int tries, List<GherkinConditionedLoopedStep> conditions) {
    try {
        if (new GherkinStepCondition("doUntilKey", expected, actual).checkCondition()) {
            int i = 0;
            do {
                i++;
                runAllStepsInLoop(conditions);
            } while (!Pattern.compile(breakCondition).matcher(Context.getValue(key) == null ? "" : Context.getValue(key)).find() && i <= tries);
        }
    } catch (final TechnicalException e) {
        throw new AssertError(Messages.getMessage(TechnicalException.TECHNICAL_SUBSTEP_ERROR_MESSAGE) + e.getMessage());
    }
}
Also used : TechnicalException(com.github.noraui.exception.TechnicalException) GherkinStepCondition(com.github.noraui.gherkin.GherkinStepCondition) AssertError(com.github.noraui.exception.AssertError) Lorsque(cucumber.api.java.fr.Lorsque) Then(cucumber.api.java.en.Then)

Example 3 with GherkinStepCondition

use of com.github.noraui.gherkin.GherkinStepCondition in project NoraUi by NoraUi.

the class CommonSteps method whileDo.

/**
 * While a given condition is verified, does steps execution.
 *
 * @param actual
 *            actual value for global condition.
 * @param expected
 *            expected value for global condition.
 * @param key
 *            key of 'expected' values ('actual' values)
 * @param breakCondition
 *            'expected' values
 * @param tries
 *            number of max tries (no infinity loop).
 * @param conditions
 *            list of steps run in a loop.
 */
@Lorsque("Si '(.*)' vérifie '(.*)', Tant que '(.*)' respecte '(.*)' je fais avec '(.*)' essais maxi:")
@Then("If '(.*)' matches '(.*)', While '(.*)' respects '(.*)' I do with '(.*)' max tries:")
public void whileDo(String actual, String expected, String key, String breakCondition, int tries, List<GherkinConditionedLoopedStep> conditions) {
    try {
        if (new GherkinStepCondition("whileDoKey", expected, actual).checkCondition()) {
            int i = 0;
            while (!Pattern.compile(breakCondition).matcher(Context.getValue(key) == null ? "" : Context.getValue(key)).find() && i <= tries) {
                i++;
                runAllStepsInLoop(conditions);
            }
        }
    } catch (final TechnicalException e) {
        throw new AssertError(Messages.getMessage(TechnicalException.TECHNICAL_SUBSTEP_ERROR_MESSAGE) + e.getMessage());
    }
}
Also used : TechnicalException(com.github.noraui.exception.TechnicalException) GherkinStepCondition(com.github.noraui.gherkin.GherkinStepCondition) AssertError(com.github.noraui.exception.AssertError) Lorsque(cucumber.api.java.fr.Lorsque) Then(cucumber.api.java.en.Then)

Example 4 with GherkinStepCondition

use of com.github.noraui.gherkin.GherkinStepCondition in project NoraUi by NoraUi.

the class ConditionedInterceptor method displayConditionsAtTheBeginningOfMethod.

/**
 * Display all conditions at the beginning of method.
 *
 * @param conditions
 *            list of 'expected' values condition and 'actual' values ({@link com.github.noraui.gherkin.GherkinStepCondition}).
 */
private void displayConditionsAtTheBeginningOfMethod(List<GherkinStepCondition> conditions) {
    int i = 0;
    for (GherkinStepCondition gherkinCondition : conditions) {
        i++;
        logger.debug("  expected contition N°{}={}", i, gherkinCondition.getExpected());
        logger.debug("  actual   contition N°{}={}", i, gherkinCondition.getActual());
    }
}
Also used : GherkinStepCondition(com.github.noraui.gherkin.GherkinStepCondition)

Example 5 with GherkinStepCondition

use of com.github.noraui.gherkin.GherkinStepCondition in project NoraUi by NoraUi.

the class ConditionedInterceptor method invoke.

/**
 * {@inheritDoc}
 */
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
    // 
    Method m = invocation.getMethod();
    if (m.isAnnotationPresent(Conditioned.class)) {
        Object[] arg = invocation.getArguments();
        if (arg.length > 0 && arg[arg.length - 1] instanceof List && !((List) arg[arg.length - 1]).isEmpty() && ((List) arg[arg.length - 1]).get(0) instanceof GherkinStepCondition) {
            List<GherkinStepCondition> conditions = (List) arg[arg.length - 1];
            displayMessageAtTheBeginningOfMethod(m.getName(), conditions);
            if (!checkConditions(conditions)) {
                Context.getCurrentScenario().write(Messages.getMessage(SKIPPED_DUE_TO_CONDITIONS));
                return Void.TYPE;
            }
        }
    }
    logger.debug("NORAUI ConditionedInterceptor invoke method {}", invocation.getMethod());
    return invocation.proceed();
}
Also used : List(java.util.List) Method(java.lang.reflect.Method) GherkinStepCondition(com.github.noraui.gherkin.GherkinStepCondition)

Aggregations

GherkinStepCondition (com.github.noraui.gherkin.GherkinStepCondition)6 TechnicalException (com.github.noraui.exception.TechnicalException)3 AssertError (com.github.noraui.exception.AssertError)2 GherkinConditionedLoopedStep (com.github.noraui.gherkin.GherkinConditionedLoopedStep)2 Then (cucumber.api.java.en.Then)2 Lorsque (cucumber.api.java.fr.Lorsque)2 Method (java.lang.reflect.Method)2 NoraUiInjectorSource (com.github.noraui.cucumber.injector.NoraUiInjectorSource)1 ConditionedInterceptor (com.github.noraui.cucumber.interceptor.ConditionedInterceptor)1 FailureException (com.github.noraui.exception.FailureException)1 Parameter (java.lang.reflect.Parameter)1 ParseException (java.text.ParseException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Matcher (java.util.regex.Matcher)1 Before (org.junit.Before)1