Skip to content

Commit

Permalink
JENKINS-35365 EMail-Ext Extended Pipeline Support - Recipient Providers
Browse files Browse the repository at this point in the history
unit tests
  • Loading branch information
davidvanlaatum committed Jun 7, 2016
1 parent e6ed1ee commit 33bb4cd
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 5 deletions.
18 changes: 15 additions & 3 deletions pom.xml
Expand Up @@ -26,7 +26,7 @@
</issueManagement>

<properties>
<powermock.version>1.4.12</powermock.version>
<powermock.version>1.6.4</powermock.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<workflow.version>1.10</workflow.version>
<jenkins.version>1.609.2</jenkins.version>
Expand Down Expand Up @@ -153,13 +153,25 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.8.5</version>
<version>1.10.19</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Expand Up @@ -60,6 +60,7 @@ public void send(final String format, final Object... args) {
while (build != null) {
if (build.getResult() != null) {
if (build.getResult().isWorseThan(Result.SUCCESS)) {
debug.send("Including build %s with status %s", build.getId(), build.getResult());
builds.add(build);
} else {
break;
Expand Down
Expand Up @@ -113,13 +113,17 @@ public Collection<? extends ChangeLogSet.AffectedFile> getAffectedFiles() {

}

public static ChangeLogSet<ChangeLogSet.Entry> makeChangeSet(final Run<?, ?> build, final String... inAuthors) {
return new MockUtilitiesChangeSet(build, inAuthors);
}

public static void addChangeSet(final WorkflowRun build, final String... inAuthors) {
MockUtilitiesChangeSet changeSet = new MockUtilitiesChangeSet(build, inAuthors);
ChangeLogSet<ChangeLogSet.Entry> changeSet = makeChangeSet(build, inAuthors);
PowerMockito.when(build.getChangeSets()).thenReturn(Collections.<ChangeLogSet<? extends ChangeLogSet.Entry>>singletonList(changeSet));
}

public static void addChangeSet(final AbstractBuild<?, ?> build, final String... inAuthors) {
MockUtilitiesChangeSet changeSet = new MockUtilitiesChangeSet(build, inAuthors);
ChangeLogSet<ChangeLogSet.Entry> changeSet = makeChangeSet(build, inAuthors);
PowerMockito.when(build.getChangeSet()).thenReturn((ChangeLogSet)changeSet);
}

Expand Down
@@ -0,0 +1,82 @@
package hudson.plugins.emailext.plugins.recipients;

import hudson.model.Run;
import hudson.model.User;
import hudson.scm.ChangeLogSet;
import hudson.tasks.Mailer;
import org.hamcrest.CoreMatchers;
import org.hamcrest.collection.IsCollectionWithSize;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import static org.junit.Assert.assertThat;
import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.when;


@RunWith(PowerMockRunner.class)
@PrepareForTest({
WorkflowRun.class
})
public class RecipientProviderUtilitiesTest {
public class Debug implements RecipientProviderUtilities.IDebug {

@Override
public void send(String format, Object... args) {

}
}

public static Set<String> usersToEMails(Set<User> users) {
Set<String> emails = new HashSet<>(users.size());
for (User user : users) {
emails.add(user.getProperty(Mailer.UserProperty.class).getAddress());
}
return emails;
}

@Test
public void getChangeSetAuthors() throws Exception {
Debug debug = new Debug();
WorkflowRun run1 = mock(WorkflowRun.class);
Set<User> authors = RecipientProviderUtilities.getChangeSetAuthors(Collections.<Run<?, ?>>singleton(run1), debug);
assertThat(authors, IsCollectionWithSize.hasSize(0));

ChangeLogSet<? extends ChangeLogSet.Entry> changeSet1 = MockUtilities.makeChangeSet(run1, "A");
ChangeLogSet<? extends ChangeLogSet.Entry> changeSet2 = MockUtilities.makeChangeSet(run1, "B");
when(run1.getChangeSets()).thenReturn(Arrays.asList(changeSet1, changeSet2));

authors = RecipientProviderUtilities.getChangeSetAuthors(Collections.<Run<?, ?>>singleton(run1), debug);
assertThat(usersToEMails(authors), CoreMatchers.<Set<String>>equalTo(new HashSet<>(Arrays.asList("A@DOMAIN", "B@DOMAIN"))));

WorkflowRun run2 = mock(WorkflowRun.class);
MockUtilities.addChangeSet(run2, "C");

authors = RecipientProviderUtilities.getChangeSetAuthors(Arrays.<Run<?, ?>>asList(run1, run2), debug);
assertThat(usersToEMails(authors), CoreMatchers.<Set<String>>equalTo(new HashSet<>(Arrays.asList("A@DOMAIN", "B@DOMAIN", "C@DOMAIN"))));
}

@Test
public void getUsersTriggeringTheBuilds() throws Exception {

}

@Test
public void getUserTriggeringTheBuild() throws Exception {

}

@Test
public void addUsers() throws Exception {

}

}

0 comments on commit 33bb4cd

Please sign in to comment.