@Test public void post() throws Exception { PostDirective post = new PostDirective(Arrays.asList("always", "unstable")); assertGenerateDirective(post, "post {\n" + " always {\n" + " // One or more steps need to be included within each condition's block.\n" + " }\n" + " unstable {\n" + " // One or more steps need to be included within each condition's block.\n" + " }\n" + "}"); }
@Test public void parametersSingle() throws Exception { List<ParameterDefinition> p = new ArrayList<>(); p.add(new StringParameterDefinition("SOME_STRING", "some default", "Hey, a description with a ' in it.")); ParametersDirective params = new ParametersDirective(p); assertGenerateDirective(params, "parameters {\n" + " string defaultValue: 'some default', description: 'Hey, a description with a \\' in it.', name: 'SOME_STRING'" + trimParamOrEmpty() + "\n" + "}"); }
Object v = getValue(param, d); if (v != null) { if (v instanceof Describable) { o.accumulate(param.getName(), staplerJsonForDescr((Describable)v)); } else if (v instanceof List && !((List) v).isEmpty()) { JSONArray a = new JSONArray(); for (Object obj : (List) v) { if (obj instanceof Describable) { a.add(staplerJsonForDescr((Describable) obj)); } else if (obj instanceof Number) { a.add(obj.toString());
/** * Tests a form submitting part of the generator. * * @param desc * The describable we'll translate to JSON. * @param responseText * Expected directive snippet to be generated */ private void assertGenerateDirective(@Nonnull AbstractDirective desc, @Nonnull String responseText) throws Exception { // First, make sure the expected response text actually matches the toGroovy for the directive. assertEquals(desc.toGroovy(true), responseText); // Then submit the form with the appropriate JSON (we generate it from the directive, but it matches the form JSON exactly) JenkinsRule.WebClient wc = r.createWebClient(); WebRequest wrs = new WebRequest(new URL(r.getURL(), DirectiveGenerator.GENERATE_URL), HttpMethod.POST); List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new NameValuePair("json", staplerJsonForDescr(desc).toString())); // WebClient.addCrumb *replaces* rather than *adds*: params.add(new NameValuePair(r.jenkins.getCrumbIssuer().getDescriptor().getCrumbRequestField(), r.jenkins.getCrumbIssuer().getCrumb(null))); wrs.setRequestParameters(params); WebResponse response = wc.getPage(wrs).getWebResponse(); assertEquals("text/plain", response.getContentType()); assertEquals(responseText, response.getContentAsString().trim()); }
@Test public void simpleStage() throws Exception { StageDirective stage = new StageDirective(Collections.emptyList(), "bob", StageDirective.StageContentType.STEPS); assertGenerateDirective(stage, "stage('bob') {\n" + " steps {\n" + " // One or more steps need to be included within the steps block.\n" + " }\n" + "}"); }
@Test public void parametersMultiple() throws Exception { List<ParameterDefinition> p = new ArrayList<>(); p.add(new StringParameterDefinition("SOME_STRING", "some default", "Hey, a description with a ' in it.")); p.add(new BooleanParameterDefinition("SOME_BOOL", true, "This will default to true.")); ParametersDirective params = new ParametersDirective(p); assertGenerateDirective(params, "parameters {\n" + " string defaultValue: 'some default', description: 'Hey, a description with a \\' in it.', name: 'SOME_STRING'" + trimParamOrEmpty() + "\n" + " booleanParam defaultValue: true, description: 'This will default to true.', name: 'SOME_BOOL'\n" + "}"); }
@Test public void simpleParallelStage() throws Exception { StageDirective stage = new StageDirective(Collections.emptyList(), "bob", StageDirective.StageContentType.PARALLEL); assertGenerateDirective(stage, "stage('bob') {\n" + " parallel {\n" + " // One or more stages need to be included within the parallel block.\n" + " }\n" + "}"); }
@Test public void fullInput() throws Exception { InputDirective input = new InputDirective("hello"); input.setId("banana"); input.setOk("Yeah, do it"); input.setSubmitter("bob"); input.setSubmitterParameter("subParam"); List<ParameterDefinition> params = new ArrayList<>(); params.add(new StringParameterDefinition("aString", "steve", "Hey, a string")); params.add(new BooleanParameterDefinition("aBool", true, "A boolean now")); input.setParameters(params); assertGenerateDirective(input, "input {\n" + " message 'hello'\n" + " id 'banana'\n" + " ok 'Yeah, do it'\n" + " submitter 'bob'\n" + " submitterParameter 'subParam'\n" + " parameters {\n" + // StringParameterDefinition added trim field in 2.90 " string defaultValue: 'steve', description: 'Hey, a string', name: 'aString'" + trimParamOrEmpty() + "\n" + " booleanParam defaultValue: true, description: 'A boolean now', name: 'aBool'\n" + " }\n" + "}"); }
@Test public void simpleInput() throws Exception { InputDirective input = new InputDirective("hello"); assertGenerateDirective(input, "input {\n" + " message 'hello'\n" + "}"); }
@Test public void triggersSingle() throws Exception { List<Trigger> t = new ArrayList<>(); t.add(new TimerTrigger("@daily")); TriggersDirective triggers = new TriggersDirective(t); assertGenerateDirective(triggers, "triggers {\n" + " cron '@daily'\n" + "}"); }
@Issue("JENKINS-46809") @Test public void simpleSequentialStage() throws Exception { StageDirective stage = new StageDirective(Collections.emptyList(), "bob", StageDirective.StageContentType.STAGES); assertGenerateDirective(stage, "stage('bob') {\n" + " stages {\n" + " // One or more stages need to be included within the stages block.\n" + " }\n" + "}"); }
@Test public void triggersMultiple() throws Exception { List<Trigger> t = new ArrayList<>(); t.add(new TimerTrigger("@daily")); t.add(new SCMTrigger("@hourly")); TriggersDirective triggers = new TriggersDirective(t); assertGenerateDirective(triggers, "triggers {\n" + " cron '@daily'\n" + " pollSCM '@hourly'\n" + "}"); }
@Test public void environment() throws Exception { List<EnvironmentDirective.NameAndValue> envList = new ArrayList<>(); envList.add(new EnvironmentDirective.NameAndValue("BOB", "steve")); envList.add(new EnvironmentDirective.NameAndValue("WHAT", "${BOB} says hi")); EnvironmentDirective env = new EnvironmentDirective(envList); assertGenerateDirective(env, "environment {\n" + " BOB = \"steve\"\n" + " WHAT = \"${BOB} says hi\"\n" + "}"); }
@Test public void libraries() throws Exception { List<LibrariesDirective.NameAndVersion> libList = new ArrayList<>(); libList.add(new LibrariesDirective.NameAndVersion("first-lib")); LibrariesDirective.NameAndVersion second = new LibrariesDirective.NameAndVersion("second-lib"); second.setVersion("master"); libList.add(second); LibrariesDirective libs = new LibrariesDirective(libList); assertGenerateDirective(libs, "libraries {\n" + " lib('first-lib')\n" + " lib('second-lib@master')\n" + "}"); }
@Test public void tools() throws Exception { ToolsDirective tools = new ToolsDirective(Collections.singletonList(new ToolsDirective.SymbolAndName("maven::::apache-maven-3.0.1"))); assertGenerateDirective(tools, "tools {\n" + " maven 'apache-maven-3.0.1'\n" + "}"); }
@Test public void whenAllOf() throws Exception { List<DeclarativeStageConditional<?>> nested = new ArrayList<>(); nested.add(new BranchConditional("that-branch")); nested.add(new BranchConditional("this-branch")); WhenDirective when = new WhenDirective(new AllOfConditional(nested), false); assertGenerateDirective(when, "when {\n" + " allOf {\n" + " branch 'that-branch'\n" + " branch 'this-branch'\n" + " }\n" + "}"); }
@Test public void whenAnyOf() throws Exception { List<DeclarativeStageConditional<?>> nested = new ArrayList<>(); nested.add(new BranchConditional("that-branch")); nested.add(new BranchConditional("this-branch")); WhenDirective when = new WhenDirective(new AnyOfConditional(nested), true); assertGenerateDirective(when, "when {\n" + " anyOf {\n" + " branch 'that-branch'\n" + " branch 'this-branch'\n" + " }\n" + " beforeAgent true\n" + "}"); }
@Test public void agentLabel() throws Exception { AgentDirective agent = new AgentDirective(new Label("some-label")); assertGenerateDirective(agent, "agent {\n" + " label 'some-label'\n" + "}"); }
@Test public void whenBranch() throws Exception { WhenDirective when = new WhenDirective(new BranchConditional("some-pattern"), true); assertGenerateDirective(when, "when {\n" + " branch 'some-pattern'\n" + " beforeAgent true\n" + "}"); }
@Test public void options() throws Exception { List<Describable> o = new ArrayList<>(); o.add(new BuildDiscarderProperty(new LogRotator("4", "", "", ""))); o.add(new SkipDefaultCheckout(true)); TimeoutStep timeout = new TimeoutStep(10); timeout.setUnit(TimeUnit.HOURS); o.add(timeout); OptionsDirective options = new OptionsDirective(o); assertGenerateDirective(options, "options {\n" + " buildDiscarder logRotator(artifactDaysToKeepStr: '', artifactNumToKeepStr: '', daysToKeepStr: '4', numToKeepStr: '')\n" + " skipDefaultCheckout true\n" + " timeout(time: 10, unit: 'HOURS')\n" + "}"); }