Search in sources :

Example 1 with GherkinConditionedLoopedStep

use of com.github.noraui.gherkin.GherkinConditionedLoopedStep 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 GherkinConditionedLoopedStep

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

the class StepUT method testRunAllStepsInLoopWithUndefinedStep.

@Test
public void testRunAllStepsInLoopWithUndefinedStep() {
    final List<GherkinConditionedLoopedStep> steps = new ArrayList<>();
    final String expected = ".+;(ETS Backbone VLAN LL2048K\\|ETS Accès L2ETH\\|Accès XDSL ETS\\|Backbone VLAN Virtuelle)";
    final String actual = "VPNtechnique;OSC_ACC-resource_type";
    Context.saveValue("OSC_ACC-resource_type", "ETS Accès L2ETH");
    final GherkinConditionedLoopedStep gherkinConditionedLoopedStep = new GherkinConditionedLoopedStep("1", "I wait '4' seconds.", expected, actual);
    steps.add(gherkinConditionedLoopedStep);
    try {
        step.runAllStepsInLoop(steps);
        Assert.fail("TechnicalException should have been thrown");
    } catch (final TechnicalException e) {
        Assert.assertEquals(String.format(Messages.getMessage(TechnicalException.TECHNICAL_ERROR_STEP_UNDEFINED), "I wait '4' seconds."), e.getMessage());
    }
}
Also used : TechnicalException(com.github.noraui.exception.TechnicalException) GherkinConditionedLoopedStep(com.github.noraui.gherkin.GherkinConditionedLoopedStep) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Aggregations

TechnicalException (com.github.noraui.exception.TechnicalException)2 GherkinConditionedLoopedStep (com.github.noraui.gherkin.GherkinConditionedLoopedStep)2 ArrayList (java.util.ArrayList)2 FailureException (com.github.noraui.exception.FailureException)1 GherkinStepCondition (com.github.noraui.gherkin.GherkinStepCondition)1 Method (java.lang.reflect.Method)1 Parameter (java.lang.reflect.Parameter)1 ParseException (java.text.ParseException)1 Matcher (java.util.regex.Matcher)1 Test (org.junit.Test)1