diff --git a/src/main/java/org/jenkinsci/plugins/credentialsbinding/impl/SSHUserPrivateKeyBinding.java b/src/main/java/org/jenkinsci/plugins/credentialsbinding/impl/SSHUserPrivateKeyBinding.java index 1bd0ae05..d59786ba 100644 --- a/src/main/java/org/jenkinsci/plugins/credentialsbinding/impl/SSHUserPrivateKeyBinding.java +++ b/src/main/java/org/jenkinsci/plugins/credentialsbinding/impl/SSHUserPrivateKeyBinding.java @@ -27,6 +27,7 @@ import hudson.Extension; import hudson.FilePath; import hudson.Launcher; +import hudson.Util; import hudson.model.Run; import hudson.model.TaskListener; import hudson.util.Secret; @@ -53,8 +54,8 @@ public class SSHUserPrivateKeyBinding extends MultiBinding { } @DataBoundSetter - public void setUsernameVariable(@Nonnull final String usernameVariable) { - this.usernameVariable = usernameVariable; + public void setUsernameVariable(@CheckForNull String usernameVariable) { + this.usernameVariable = Util.fixEmptyAndTrim(usernameVariable); } @CheckForNull @@ -63,8 +64,8 @@ public String getUsernameVariable() { } @DataBoundSetter - public void setPassphraseVariable(@Nonnull final String passphraseVariable) { - this.passphraseVariable = passphraseVariable; + public void setPassphraseVariable(@CheckForNull String passphraseVariable) { + this.passphraseVariable = Util.fixEmptyAndTrim(passphraseVariable); } @CheckForNull diff --git a/src/test/java/org/jenkinsci/plugins/credentialsbinding/impl/CertificateMultiBindingTest.java b/src/test/java/org/jenkinsci/plugins/credentialsbinding/impl/CertificateMultiBindingTest.java index bf7225eb..f1f240b8 100644 --- a/src/test/java/org/jenkinsci/plugins/credentialsbinding/impl/CertificateMultiBindingTest.java +++ b/src/test/java/org/jenkinsci/plugins/credentialsbinding/impl/CertificateMultiBindingTest.java @@ -93,6 +93,8 @@ public void setUp() throws IOException { } } + // TODO configRoundtrip to test form, null hygiene on @DataBoundSetter + @Test public void basics() throws Exception { String alias = "androiddebugkey"; diff --git a/src/test/java/org/jenkinsci/plugins/credentialsbinding/impl/SSHUserPrivateKeyTest.java b/src/test/java/org/jenkinsci/plugins/credentialsbinding/impl/SSHUserPrivateKeyBindingTest.java similarity index 87% rename from src/test/java/org/jenkinsci/plugins/credentialsbinding/impl/SSHUserPrivateKeyTest.java rename to src/test/java/org/jenkinsci/plugins/credentialsbinding/impl/SSHUserPrivateKeyBindingTest.java index 27c43451..d2e606b3 100644 --- a/src/test/java/org/jenkinsci/plugins/credentialsbinding/impl/SSHUserPrivateKeyTest.java +++ b/src/test/java/org/jenkinsci/plugins/credentialsbinding/impl/SSHUserPrivateKeyBindingTest.java @@ -34,6 +34,7 @@ import hudson.util.Secret; import org.jenkinsci.plugins.credentialsbinding.MultiBinding; import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition; +import org.jenkinsci.plugins.workflow.cps.SnippetizerTester; import org.jenkinsci.plugins.workflow.job.WorkflowJob; import org.jenkinsci.plugins.workflow.job.WorkflowRun; import org.jenkinsci.plugins.workflow.steps.StepConfigTester; @@ -51,7 +52,7 @@ import org.junit.ClassRule; import org.jvnet.hudson.test.BuildWatcher; -public class SSHUserPrivateKeyTest { +public class SSHUserPrivateKeyBindingTest { @Rule public RestartableJenkinsRule story = new RestartableJenkinsRule(); @ClassRule public static BuildWatcher bw = new BuildWatcher(); @@ -119,21 +120,22 @@ public CredentialsScope getScope() { } @Test public void configRoundTrip() throws Exception { - story.addStep(new Statement() { - @Override public void evaluate() throws Throwable { - SSHUserPrivateKey c = new DummyPrivateKey("creds", "bob", "secret", "the-key"); - CredentialsProvider.lookupStores(story.j.jenkins).iterator().next().addCredentials(Domain.global(), c); - SSHUserPrivateKeyBinding binding = new SSHUserPrivateKeyBinding("keyFile", "creds"); - binding.setPassphraseVariable("passphrase"); - binding.setUsernameVariable("user"); - BindingStep s = new StepConfigTester(story.j).configRoundTrip(new BindingStep( - Collections.singletonList(binding))); - story.j.assertEqualDataBoundBeans(s.getBindings(), Collections.singletonList(binding)); - } + story.then(r -> { + SnippetizerTester st = new SnippetizerTester(r); + SSHUserPrivateKey c = new DummyPrivateKey("creds", "bob", "secret", "the-key"); + CredentialsProvider.lookupStores(story.j.jenkins).iterator().next().addCredentials(Domain.global(), c); + SSHUserPrivateKeyBinding binding = new SSHUserPrivateKeyBinding("keyFile", "creds"); + BindingStep s = new StepConfigTester(story.j).configRoundTrip(new BindingStep(Collections.singletonList(binding))); + st.assertRoundTrip(s, "withCredentials([sshUserPrivateKey(credentialsId: 'creds', keyFileVariable: 'keyFile')]) {\n // some block\n}"); + r.assertEqualDataBoundBeans(s.getBindings(), Collections.singletonList(binding)); + binding.setPassphraseVariable("passphrase"); + binding.setUsernameVariable("user"); + s = new StepConfigTester(story.j).configRoundTrip(new BindingStep(Collections.singletonList(binding))); + st.assertRoundTrip(s, "withCredentials([sshUserPrivateKey(credentialsId: 'creds', keyFileVariable: 'keyFile', passphraseVariable: 'passphrase', usernameVariable: 'user')]) {\n // some block\n}"); + r.assertEqualDataBoundBeans(s.getBindings(), Collections.singletonList(binding)); }); } - @Test public void basics() throws Exception { final String credentialsId = "creds"; final String username = "bob";