Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[JENKINS-14964] add test for REST API
- Loading branch information
Showing
1 changed file
with
64 additions
and
0 deletions.
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
src/test/java/org/jenkinsci/plugins/scriptler/restapi/ScriptlerRestApiTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package org.jenkinsci.plugins.scriptler.restapi; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import hudson.model.FileParameterValue.FileItemImpl; | ||
|
||
import java.io.File; | ||
import java.net.URLEncoder; | ||
|
||
import org.apache.commons.fileupload.FileItem; | ||
import org.apache.commons.io.FileUtils; | ||
import org.jenkinsci.plugins.scriptler.ScriptlerManagementHelper; | ||
import org.jenkinsci.plugins.scriptler.ScriptlerManagment; | ||
import org.jenkinsci.plugins.scriptler.config.Parameter; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.jvnet.hudson.test.JenkinsRule; | ||
|
||
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException; | ||
import com.gargoylesoftware.htmlunit.Page; | ||
|
||
public class ScriptlerRestApiTest { | ||
|
||
private static final String SCRIPT_ID = "dummy.groovy"; | ||
|
||
@Rule | ||
public JenkinsRule j = new JenkinsRule(); | ||
|
||
@Before | ||
public void setup() throws Exception { | ||
final ScriptlerManagment scriptler = j.getInstance().getExtensionList(ScriptlerManagment.class).get(0); | ||
ScriptlerManagementHelper helper = new ScriptlerManagementHelper(scriptler); | ||
File f = new File(SCRIPT_ID); | ||
FileUtils.writeStringToFile(f, "print \"hello $arg1, this is $arg2.\""); | ||
FileItem fi = new FileItemImpl(f); | ||
helper.saveScript(fi, true, SCRIPT_ID); | ||
|
||
scriptler.getConfiguration().getScriptById(SCRIPT_ID) | ||
.setParameters(new Parameter[]{ new Parameter("arg1", "world"), new Parameter("arg2", "scriptler") }); | ||
} | ||
|
||
@Test | ||
public void testSuccessWithDefaults() throws Exception { | ||
Page goTo = j.createWebClient().goTo("scriptler/run/dummy.groovy", "text/plain"); | ||
j.assertGoodStatus(goTo); | ||
|
||
assertEquals("hello world, this is scriptler.", goTo.getWebResponse().getContentAsString()); | ||
} | ||
|
||
@Test | ||
public void testSuccessWithAllChanged() throws Exception { | ||
Page goTo = j.createWebClient() | ||
.goTo("scriptler/run/dummy.groovy?script=" + URLEncoder.encode("print \"welcome, $arg1 and $arg2!\"", | ||
"UTF-8") + "&arg1=foo&arg2=bar&contentType=application/foobar", "application/foobar"); | ||
j.assertGoodStatus(goTo); | ||
|
||
assertEquals("welcome, foo and bar!", goTo.getWebResponse().getContentAsString()); | ||
} | ||
|
||
@Test(expected = FailingHttpStatusCodeException.class) | ||
public void testUnknownScript() throws Exception { | ||
j.createWebClient().goTo("scriptler/run/unknown.groovy", "text/plain"); | ||
} | ||
} |