package org.jenkinsci.plugins.workflow.steps.input;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import hudson.model.BooleanParameterDefinition;
import hudson.model.queue.QueueTaskFuture;
import java.util.Arrays;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.cps.CpsFlowExecution;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.jenkinsci.plugins.workflow.support.steps.input.InputAction;
import org.jenkinsci.plugins.workflow.support.steps.input.InputStepExecution;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
/**
* @author Kohsuke Kawaguchi
*/
public class InputStepTest extends Assert {
@Rule
public JenkinsRule j = new JenkinsRule();
/**
* Try out a parameter.
*/
@Test
public void parameter() throws Exception {
// job setup
WorkflowJob foo = j.jenkins.createProject(WorkflowJob.class, "foo");
foo.setDefinition(new CpsFlowDefinition(StringUtils.join(Arrays.asList(
"echo('before');",
"def x = input message:'Do you want chocolate?', id:'Icecream', ok: 'Purchase icecream', parameters: [new BooleanParameterDefinition('chocolate',false,'Favorite icecream flavor')];",
"echo(\"after: ${x}\");"),"\n")));
// get the build going, and wait until workflow pauses
QueueTaskFuture<WorkflowRun> q = foo.scheduleBuild2(0);
WorkflowRun b = q.getStartCondition().get();
CpsFlowExecution e = (CpsFlowExecution) b.getExecutionPromise().get();
while (b.getAction(InputAction.class)==null) {
e.waitForSuspension();
}
// make sure we are pausing at the right state that reflects what we wrote in the program
InputAction a = b.getAction(InputAction.class);
assertEquals(1, a.getExecutions().size());
InputStepExecution is = a.getExecution("Icecream");
assertEquals("Do you want chocolate?", is.getInput().getMessage());
assertEquals(1, is.getInput().getParameters().size());
j.assertEqualDataBoundBeans(is.getInput().getParameters().get(0), new BooleanParameterDefinition("chocolate", false, "Favorite icecream flavor"));
// submit the input, and run workflow to the completion
HtmlPage p = j.createWebClient().getPage(b, a.getUrlName());
j.submit(p.getFormByName(is.getId()),"proceed");
assertEquals(0, a.getExecutions().size());
q.get();
// make sure 'x' gets assigned to false
System.out.println(b.getLog());
assertTrue(b.getLog().contains("after: false"));
}
}