<commit_before>/*
 * Copyright 2006-2015 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.consol.citrus.actions;

import com.consol.citrus.condition.Condition;
import com.consol.citrus.container.Wait;
import com.consol.citrus.context.TestContext;
import com.consol.citrus.exceptions.CitrusRuntimeException;
import org.mockito.Mockito;
import org.springframework.util.StringUtils;
import org.testng.annotations.Test;

import static org.mockito.Mockito.*;

import static org.testng.Assert.fail;

/**
 * @author <NAME>
 * @since 2.4
 */
public class WaitTest {
    private TestContext contextMock = Mockito.mock(TestContext.class);
    private Condition conditionMock = Mockito.mock(Condition.class);
    private long startTime;
    private long endTime;

    @Test
    public void shouldSatisfyWaitConditionOnFirstAttempt() throws Exception {
        String seconds = "10";
        String interval = "1000";

        Wait testling = getWaitAction(seconds, interval);

        reset(contextMock, conditionMock);
        prepareContextMock(seconds, interval);
        when(conditionMock.getName()).thenReturn("check");
        when(conditionMock.isSatisfied(contextMock)).thenReturn(Boolean.TRUE);
        when(conditionMock.getSuccessMessage(contextMock)).thenReturn("Condition success!");
        startTimer();
        testling.doExecute(contextMock);
        stopTimer();

        assertConditionExecutedWithinSeconds("1");
    }

    @Test
    public void shouldSatisfyWaitConditionOnLastAttempt() throws Exception {
        String seconds = "4";
        String interval = "1000";

        Wait testling = getWaitAction(seconds, interval);

        reset(contextMock, conditionMock);
        prepareContextMock(seconds, interval);
        when(conditionMock.getName()).thenReturn("check");
        when(conditionMock.isSatisfied(contextMock)).thenReturn(Boolean.FALSE);
        when(conditionMock.isSatisfied(contextMock)).thenReturn(Boolean.TRUE);
        when(conditionMock.getSuccessMessage(contextMock)).thenReturn("Condition success!");
        startTimer();
        testling.doExecute(contextMock);
        stopTimer();

        assertConditionExecutedWithinSeconds(seconds);
    }

    @Test
    public void shouldSatisfyWaitConditionWithBiggerIntervalThanTimeout() throws Exception {
        String seconds = "1";
        String interval = "10000";

        Wait testling = getWaitAction(seconds, interval);

        reset(contextMock, conditionMock);
        prepareContextMock(seconds, interval);
        when(conditionMock.getName()).thenReturn("check");
        when(conditionMock.isSatisfied(contextMock)).thenReturn(Boolean.TRUE);
        when(conditionMock.getSuccessMessage(contextMock)).thenReturn("Condition success!");
        startTimer();
        testling.doExecute(contextMock);
        stopTimer();

        assertConditionExecutedWithinSeconds(seconds);
    }

    @Test
    public void shouldNotSatisfyWaitCondition() throws Exception {
        String seconds = "3";
        String interval = "1000";

        Wait testling = getWaitAction(seconds, interval);

        reset(contextMock, conditionMock);
        prepareContextMock(seconds, interval);
        when(conditionMock.getName()).thenReturn("check");
        when(conditionMock.isSatisfied(contextMock)).thenReturn(Boolean.FALSE);
        when(conditionMock.getErrorMessage(contextMock)).thenReturn("Condition failed!");
        startTimer();
        try {
            testling.doExecute(contextMock);
            fail("Was expecting CitrusRuntimeException to be thrown");
        } catch (CitrusRuntimeException e) {
            // expected
        }
        stopTimer();

        assertConditionExecutedWithinSeconds(seconds);
    }


    @Test
    public void shouldNotSatisfyWaitConditionWithBiggerIntervalThanTimeout() throws Exception {
        String seconds = "1";
        String interval = "10000";

        Wait testling = getWaitAction(seconds, interval);

        reset(contextMock, conditionMock);
        prepareContextMock(seconds, interval);
        when(conditionMock.getName()).thenReturn("check");
        when(conditionMock.isSatisfied(contextMock)).thenReturn(Boolean.FALSE);
        when(conditionMock.getErrorMessage(contextMock)).thenReturn("Condition failed!");
        startTimer();
        try {
            testling.doExecute(contextMock);
            fail("Was expecting CitrusRuntimeException to be thrown");
        } catch (CitrusRuntimeException e) {
            // expected
        }
        stopTimer();

        assertConditionExecutedWithinSeconds(seconds);
    }

    private void prepareContextMock(String waitTime, String interval) {
        when(contextMock.replaceDynamicContentInString(waitTime)).thenReturn(waitTime);
        when(contextMock.replaceDynamicContentInString(interval)).thenReturn(interval);
    }

    private Wait getWaitAction(String waitTimeSeconds, String interval) {
        Wait testling = new Wait();<commit_msg>Do not use doExecute<commit_after>/*
 * Copyright 2006-2015 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.consol.citrus.actions;

import com.consol.citrus.condition.Condition;
import com.consol.citrus.container.Wait;
import com.consol.citrus.context.TestContext;
import com.consol.citrus.exceptions.CitrusRuntimeException;
import org.mockito.Mockito;
import org.springframework.util.StringUtils;
import org.testng.annotations.Test;

import static org.mockito.Mockito.*;

import static org.testng.Assert.fail;

/**
 * @author <NAME>er
 * @since 2.4
 */
public class WaitTest {
    private TestContext contextMock = Mockito.mock(TestContext.class);
    private Condition conditionMock = Mockito.mock(Condition.class);
    private long startTime;
    private long endTime;

    @Test
    public void shouldSatisfyWaitConditionOnFirstAttempt() throws Exception {
        String seconds = "10";
        String interval = "1000";

        Wait testling = getWaitAction(seconds, interval);

        reset(contextMock, conditionMock);
        prepareContextMock(seconds, interval);
        when(conditionMock.getName()).thenReturn("check");
        when(conditionMock.isSatisfied(contextMock)).thenReturn(Boolean.TRUE);
        when(conditionMock.getSuccessMessage(contextMock)).thenReturn("Condition success!");
        startTimer();
        testling.execute(contextMock);
        stopTimer();

        assertConditionExecutedWithinSeconds("1");
    }

    @Test
    public void shouldSatisfyWaitConditionOnLastAttempt() throws Exception {
        String seconds = "4";
        String interval = "1000";

        Wait testling = getWaitAction(seconds, interval);

        reset(contextMock, conditionMock);
        prepareContextMock(seconds, interval);
        when(conditionMock.getName()).thenReturn("check");
        when(conditionMock.isSatisfied(contextMock)).thenReturn(Boolean.FALSE);
        when(conditionMock.isSatisfied(contextMock)).thenReturn(Boolean.TRUE);
        when(conditionMock.getSuccessMessage(contextMock)).thenReturn("Condition success!");
        startTimer();
        testling.execute(contextMock);
        stopTimer();

        assertConditionExecutedWithinSeconds(seconds);
    }

    @Test
    public void shouldSatisfyWaitConditionWithBiggerIntervalThanTimeout() throws Exception {
        String seconds = "1";
        String interval = "10000";

        Wait testling = getWaitAction(seconds, interval);

        reset(contextMock, conditionMock);
        prepareContextMock(seconds, interval);
        when(conditionMock.getName()).thenReturn("check");
        when(conditionMock.isSatisfied(contextMock)).thenReturn(Boolean.TRUE);
        when(conditionMock.getSuccessMessage(contextMock)).thenReturn("Condition success!");
        startTimer();
        testling.execute(contextMock);
        stopTimer();

        assertConditionExecutedWithinSeconds(seconds);
    }

    @Test
    public void shouldNotSatisfyWaitCondition() throws Exception {
        String seconds = "3";
        String interval = "1000";

        Wait testling = getWaitAction(seconds, interval);

        reset(contextMock, conditionMock);
        prepareContextMock(seconds, interval);
        when(conditionMock.getName()).thenReturn("check");
        when(conditionMock.isSatisfied(contextMock)).thenReturn(Boolean.FALSE);
        when(conditionMock.getErrorMessage(contextMock)).thenReturn("Condition failed!");
        startTimer();
        try {
            testling.execute(contextMock);
            fail("Was expecting CitrusRuntimeException to be thrown");
        } catch (CitrusRuntimeException e) {
            // expected
        }
        stopTimer();

        assertConditionExecutedWithinSeconds(seconds);
    }


    @Test
    public void shouldNotSatisfyWaitConditionWithBiggerIntervalThanTimeout() throws Exception {
        String seconds = "1";
        String interval = "10000";

        Wait testling = getWaitAction(seconds, interval);

        reset(contextMock, conditionMock);
        prepareContextMock(seconds, interval);
        when(conditionMock.getName()).thenReturn("check");
        when(conditionMock.isSatisfied(contextMock)).thenReturn(Boolean.FALSE);
        when(conditionMock.getErrorMessage(contextMock)).thenReturn("Condition failed!");
        startTimer();
        try {
            testling.execute(contextMock);
            fail("Was expecting CitrusRuntimeException to be thrown");
        } catch (CitrusRuntimeException e) {
            // expected
        }
        stopTimer();

        assertConditionExecutedWithinSeconds(seconds);
    }

    private void prepareContextMock(String waitTime, String interval) {
        when(contextMock.replaceDynamicContentInString(waitTime)).thenReturn(waitTime);
        when(contextMock.replaceDynamicContentInString(interval)).thenReturn(interval);
    }

    private Wait getWaitAction(String waitTimeSeconds, String interval) {
        Wait testling = new Wait();
{'fraction_non_alphanumeric': 0.0989399293286219, 'fraction_numerical': 0.007791972456283411, 'mean_word_length': 3.486991869918699, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3111537', 'n_tokens_mistral': 2835, 'n_tokens_neox': 2702, 'n_words': 622}