attempts = new ArrayList<>();
+
+ final boolean isSuccess = Retryable.retry(2, (attempt) -> {
+ attempts.add(attempt);
+ throw new Exception("Fail");
+ });
+
+ assertFalse(isSuccess);
+ assertThat(attempts, contains(1, 2, 3));
+ }
+}
diff --git a/src/test/java/hudson/tasks/migrations/AddCredentialsToSmtpAuthenticationTest.java b/src/test/java/hudson/tasks/migrations/AddCredentialsToSmtpAuthenticationTest.java
new file mode 100644
index 00000000..0af4138b
--- /dev/null
+++ b/src/test/java/hudson/tasks/migrations/AddCredentialsToSmtpAuthenticationTest.java
@@ -0,0 +1,27 @@
+package hudson.tasks.migrations;
+
+import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials;
+import hudson.tasks.Mailer;
+import hudson.tasks.SMTPAuthentication;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+public class AddCredentialsToSmtpAuthenticationTest extends MigrationTest {
+ @Override
+ protected void change(Mailer.DescriptorImpl descriptor) {
+ final SMTPAuthentication authentication = descriptor.getAuthentication();
+
+ assertNotNull(authentication);
+
+ final String credentialsId = authentication.getCredentialsId();
+
+ assertNotNull(credentialsId);
+
+ final StandardUsernamePasswordCredentials migratedCredential = Util.lookupCredential(credentialsId)
+ .orElseThrow(() -> new RuntimeException("Can't find the migrated test credential"));
+
+ assertEquals("olduser", migratedCredential.getUsername());
+ assertEquals("{AQAAABAAAAAQ1UuHpGkqtUa56seSp+wJjfuiggZPi/D+t38985a5tXU=}", migratedCredential.getPassword().getPlainText());
+ }
+}
diff --git a/src/test/java/hudson/tasks/migrations/AddSmtpAuthenticationToMailerTest.java b/src/test/java/hudson/tasks/migrations/AddSmtpAuthenticationToMailerTest.java
new file mode 100644
index 00000000..fd04fb4f
--- /dev/null
+++ b/src/test/java/hudson/tasks/migrations/AddSmtpAuthenticationToMailerTest.java
@@ -0,0 +1,28 @@
+package hudson.tasks.migrations;
+
+import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials;
+import hudson.tasks.Mailer;
+import hudson.tasks.SMTPAuthentication;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+public class AddSmtpAuthenticationToMailerTest extends MigrationTest {
+
+ @Override
+ public void change(Mailer.DescriptorImpl descriptor) {
+ final SMTPAuthentication authentication = descriptor.getAuthentication();
+
+ assertNotNull(authentication);
+
+ final String credentialsId = authentication.getCredentialsId();
+
+ assertNotNull(credentialsId);
+
+ final StandardUsernamePasswordCredentials migratedCredential = Util.lookupCredential(credentialsId)
+ .orElseThrow(() -> new RuntimeException("Can't find the migrated test credential"));
+
+ assertEquals("olduser", migratedCredential.getUsername());
+ assertEquals("{AQAAABAAAAAQ1UuHpGkqtUa56seSp+wJjfuiggZPi/D+t38985a5tXU=}", migratedCredential.getPassword().getPlainText());
+ }
+}
diff --git a/src/test/java/hudson/tasks/migrations/MigrationTest.java b/src/test/java/hudson/tasks/migrations/MigrationTest.java
new file mode 100644
index 00000000..88e2f802
--- /dev/null
+++ b/src/test/java/hudson/tasks/migrations/MigrationTest.java
@@ -0,0 +1,72 @@
+package hudson.tasks.migrations;
+
+import hudson.tasks.Mailer;
+import org.junit.Rule;
+import org.junit.Test;
+import org.jvnet.hudson.test.HudsonHomeLoader;
+import org.jvnet.hudson.test.JenkinsRule;
+
+import java.io.File;
+import java.net.URL;
+
+/**
+ * Defines migration tests for the Jenkins plugin configuration data model.
+ * The format is loosely based on Ruby On Rails' ActiveRecord Migrations. Like ActiveRecord Migrations, it uses naming conventions, and a single change()
method per test case. Unlike ActiveRecord Migrations, there is no concept of migrating backwards as we do not support plugin downgrades, so we only need to test the forward path.
+ */
+public abstract class MigrationTest {
+
+ @Rule
+ public final JenkinsRule jenkins = new JenkinsRule()
+ .with(new LocalClass(this.getClass()));
+
+ /**
+ * Implement this to assert that the property in question has been migrated correctly.
+ *
+ * @param descriptor The descriptor in which the property will be found, or not found if it was removed.
+ */
+ protected abstract void change(Mailer.DescriptorImpl descriptor);
+
+ @Test
+ public void shouldMigrate() {
+ final Mailer.DescriptorImpl config = getPluginConfiguration();
+ change(config);
+ }
+
+ private Mailer.DescriptorImpl getPluginConfiguration() {
+ return (Mailer.DescriptorImpl) jenkins.getInstance().getDescriptor(Mailer.class);
+ }
+
+ /**
+ * Adaptation of Local for when we just want to load one Hudson home per class, rather than one per test.
+ */
+ private static class LocalClass implements HudsonHomeLoader {
+
+ private final Class> testClass;
+
+ public LocalClass(Class> testClass) {
+ this.testClass = testClass;
+ }
+
+ @Override
+ public File allocate() throws Exception {
+ URL res = findDataResource();
+ if(!res.getProtocol().equals("file"))
+ throw new AssertionError("Test data is not available in the file system: "+res);
+ File home = new File(res.toURI());
+ System.err.println("Loading $JENKINS_HOME from " + home);
+
+ return new CopyExisting(home).allocate();
+ }
+
+ private URL findDataResource() {
+ for( String suffix : SUFFIXES ) {
+ URL res = testClass.getResource(testClass.getSimpleName() + suffix);
+ if(res!=null) return res;
+ }
+
+ throw new AssertionError("No test resource was found for "+testClass);
+ }
+
+ private static final String[] SUFFIXES = {"/", ".zip"};
+ }
+}
diff --git a/src/test/java/hudson/tasks/migrations/Util.java b/src/test/java/hudson/tasks/migrations/Util.java
new file mode 100644
index 00000000..95ba5a7c
--- /dev/null
+++ b/src/test/java/hudson/tasks/migrations/Util.java
@@ -0,0 +1,19 @@
+package hudson.tasks.migrations;
+
+import com.cloudbees.plugins.credentials.CredentialsProvider;
+import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials;
+import com.cloudbees.plugins.credentials.domains.DomainRequirement;
+import hudson.model.Item;
+
+import java.util.List;
+import java.util.Optional;
+
+public class Util {
+ static Optional lookupCredential(String id) {
+ final List credentials = CredentialsProvider.lookupCredentials(StandardUsernamePasswordCredentials.class, (Item) null, null, (List) null);
+
+ return credentials.stream()
+ .filter(cred -> cred.getId().equals(id))
+ .findFirst();
+ }
+}
diff --git a/src/test/java/jenkins/plugins/mailer/MailerJCasCCompatibilityTest.java b/src/test/java/jenkins/plugins/mailer/MailerJCasCCompatibilityTest.java
index 68ff7e54..43d16299 100644
--- a/src/test/java/jenkins/plugins/mailer/MailerJCasCCompatibilityTest.java
+++ b/src/test/java/jenkins/plugins/mailer/MailerJCasCCompatibilityTest.java
@@ -1,8 +1,11 @@
package jenkins.plugins.mailer;
+import org.jvnet.hudson.test.RestartableJenkinsRule;
+
import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertNotNull;
import org.jvnet.hudson.test.RestartableJenkinsRule;
@@ -15,7 +18,7 @@ public class MailerJCasCCompatibilityTest extends RoundTripAbstractTest {
protected void assertConfiguredAsExpected(RestartableJenkinsRule restartableJenkinsRule, String s) {
Mailer.DescriptorImpl descriptor = Mailer.descriptor();
assertNotNull("Mailer can not be found", descriptor);
- assertEquals(String.format("Wrong authentication. Username expected %s but received %s", "fakeuser", descriptor.getAuthentication().getUsername()), "fakeuser", descriptor.getAuthentication().getUsername());
+ assertEquals(String.format("Wrong authentication. Credential ID expected %s but received %s", "foo", descriptor.getAuthentication().getCredentialsId()), "foo", descriptor.getAuthentication().getCredentialsId());
assertEquals(String.format("Wrong charset. Expected %s but received %s", "UTF-8", descriptor.getCharset()), "UTF-8", descriptor.getCharset());
assertEquals(String.format("Wrong default suffix. Expected %s but received %s", "@mydomain.com", descriptor.getDefaultSuffix()), "@mydomain.com", descriptor.getDefaultSuffix());
assertEquals(String.format("Wrong ReplayTo address. Expected %s but received %s", "noreplay@mydomain.com", descriptor.getReplyToAddress()), "noreplay@mydomain.com", descriptor.getReplyToAddress());
@@ -27,6 +30,6 @@ protected void assertConfiguredAsExpected(RestartableJenkinsRule restartableJenk
@Override
protected String stringInLogExpected() {
- return "Setting class hudson.tasks.SMTPAuthentication.username = fakeuser";
+ return "Setting class hudson.tasks.SMTPAuthentication.credentialsId = foo";
}
}
diff --git a/src/test/resources/hudson/tasks/MailerTest/testMigrateOldData/hudson.tasks.Mailer.xml b/src/test/resources/hudson/tasks/MailerTest/testMigrateOldData/hudson.tasks.Mailer.xml
index 580d1b6e..4621d6c1 100644
--- a/src/test/resources/hudson/tasks/MailerTest/testMigrateOldData/hudson.tasks.Mailer.xml
+++ b/src/test/resources/hudson/tasks/MailerTest/testMigrateOldData/hudson.tasks.Mailer.xml
@@ -1,8 +1,9 @@
@mydomain.com
- olduser
- {AQAAABAAAAAQ1UuHpGkqtUa56seSp+wJjfuiggZPi/D+t38985a5tXU=}
+
+ foo
+
noreplay@mydomain.com
old.data.smtp.host
true
diff --git a/src/test/resources/hudson/tasks/migrations/AddCredentialsToSmtpAuthenticationTest/hudson.tasks.Mailer.xml b/src/test/resources/hudson/tasks/migrations/AddCredentialsToSmtpAuthenticationTest/hudson.tasks.Mailer.xml
new file mode 100644
index 00000000..fe9ca388
--- /dev/null
+++ b/src/test/resources/hudson/tasks/migrations/AddCredentialsToSmtpAuthenticationTest/hudson.tasks.Mailer.xml
@@ -0,0 +1,13 @@
+
+
+ @mydomain.com
+
+ olduser
+ {AQAAABAAAAAQ1UuHpGkqtUa56seSp+wJjfuiggZPi/D+t38985a5tXU=}
+
+ noreplay@mydomain.com
+ old.data.smtp.host
+ true
+ 808080
+ UTF-8
+
\ No newline at end of file
diff --git a/src/test/resources/hudson/tasks/migrations/AddSmtpAuthenticationToMailerTest/hudson.tasks.Mailer.xml b/src/test/resources/hudson/tasks/migrations/AddSmtpAuthenticationToMailerTest/hudson.tasks.Mailer.xml
new file mode 100644
index 00000000..580d1b6e
--- /dev/null
+++ b/src/test/resources/hudson/tasks/migrations/AddSmtpAuthenticationToMailerTest/hudson.tasks.Mailer.xml
@@ -0,0 +1,11 @@
+
+
+ @mydomain.com
+ olduser
+ {AQAAABAAAAAQ1UuHpGkqtUa56seSp+wJjfuiggZPi/D+t38985a5tXU=}
+ noreplay@mydomain.com
+ old.data.smtp.host
+ true
+ 808080
+ UTF-8
+
\ No newline at end of file
diff --git a/src/test/resources/jenkins/plugins/mailer/configuration-as-code.yaml b/src/test/resources/jenkins/plugins/mailer/configuration-as-code.yaml
index c10400f5..7ff8c2b6 100644
--- a/src/test/resources/jenkins/plugins/mailer/configuration-as-code.yaml
+++ b/src/test/resources/jenkins/plugins/mailer/configuration-as-code.yaml
@@ -1,8 +1,7 @@
unclassified:
mailer:
authentication:
- password: "{AQAAABAAAAAQerzX1sPSYnExkhL/IPqqen3IXjJDVYLe/PR1+30xdA8=}"
- username: "fakeuser"
+ credentialsId: "foo"
charset: "UTF-8"
defaultSuffix: "@mydomain.com"
replyToAddress: "noreplay@mydomain.com"
@@ -10,3 +9,14 @@ unclassified:
smtpPort: "808080"
useSsl: true
useTls: true
+
+credentials:
+ system:
+ domainCredentials:
+ - credentials:
+ - usernamePassword:
+ scope: SYSTEM
+ id: "foo"
+ username: "fakeuser"
+ password: "{AQAAABAAAAAQerzX1sPSYnExkhL/IPqqen3IXjJDVYLe/PR1+30xdA8=}"
+ description: "Mail SMTP auth"
\ No newline at end of file