Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tests checkstyle #554

Merged
merged 1 commit into from
Apr 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@
<phase>validate</phase>
<configuration>
<configLocation>checkstyle.xml</configLocation>
<includeTestSourceDirectory>true</includeTestSourceDirectory>
<encoding>UTF-8</encoding>
<consoleOutput>true</consoleOutput>
<failsOnError>true</failsOnError>
Expand Down
205 changes: 104 additions & 101 deletions src/test/java/jenkins/plugins/slack/ActiveNotifierTest.java
Original file line number Diff line number Diff line change
@@ -1,149 +1,152 @@
package jenkins.plugins.slack;

import hudson.matrix.MatrixConfiguration;
import hudson.model.AbstractBuild;
import hudson.model.CauseAction;
import hudson.matrix.MatrixProject;
import hudson.matrix.MatrixRun;
import hudson.model.AbstractBuild;
import hudson.model.CauseAction;
import hudson.model.ItemGroup;
import hudson.model.Result;
import junit.framework.TestCase;
import java.util.function.Function;
import jenkins.plugins.slack.logging.BuildAwareLogger;
import jenkins.plugins.slack.matrix.MatrixTriggerMode;
import junit.framework.TestCase;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import java.util.function.Function;

import static org.mockito.Mockito.*;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@RunWith(PowerMockRunner.class)
@PrepareForTest(ActiveNotifier.class)
public class ActiveNotifierTest extends TestCase {
MatrixRun matrixRun = mock(MatrixRun.class);
SlackNotifier slackNotifier = mock(SlackNotifier.class);
SlackService slack = mock(SlackService.class);

ActiveNotifier activeNotifier;

@Before
public void setupActiveNotifier() throws Exception {
BuildAwareLogger buildAwareLogger = mock(BuildAwareLogger.class);
TokenExpander tokenExpander = mock(TokenExpander.class);
MatrixProject project = mock(MatrixProject.class);
MatrixConfiguration configuration = mock(MatrixConfiguration.class);
ItemGroup group = mock(ItemGroup.class);
MatrixRun previousBuild = mock(MatrixRun.class);
ActiveNotifier.MessageBuilder messageBuilder = mock(ActiveNotifier.MessageBuilder.class);
Function<AbstractBuild<?, ?>, SlackService> slackFactory = (Function<AbstractBuild<?, ?>, SlackService>) mock(Function.class);

when(slackNotifier.getNotifyRegression()).thenReturn(true);
when(slackNotifier.getNotifyFailure()).thenReturn(true);
when(slackNotifier.getCommitInfoChoice()).thenReturn(CommitInfoChoice.NONE);
when(group.getFullDisplayName()).thenReturn("group");
when(project.getParent()).thenReturn(group);
when(project.getLastBuild()).thenReturn(null);
when(project.getFullDisplayName()).thenReturn("project");
when(previousBuild.getPreviousCompletedBuild()).thenReturn(null);
when(previousBuild.getResult()).thenReturn(Result.SUCCESS);
when(configuration.getParent()).thenReturn(project);
when(configuration.getLastBuild()).thenReturn(previousBuild);
when(configuration.getFullDisplayName()).thenReturn("project");
when(matrixRun.getAction(CauseAction.class)).thenReturn(null);
when(matrixRun.getProject()).thenReturn(configuration);
when(matrixRun.hasChangeSetComputed()).thenReturn(false);
when(matrixRun.getNumber()).thenReturn(1);
when(matrixRun.getResult()).thenReturn(Result.FAILURE);
when(slackFactory.apply(matrixRun)).thenReturn(slack);
when(messageBuilder.toString()).thenReturn("build status message");
PowerMockito.whenNew(ActiveNotifier.MessageBuilder.class)
.withArguments(slackNotifier, matrixRun, buildAwareLogger, tokenExpander)
.thenReturn(messageBuilder);
activeNotifier = new ActiveNotifier(slackNotifier, slackFactory, buildAwareLogger, tokenExpander);
}
MatrixRun matrixRun = mock(MatrixRun.class);
SlackNotifier slackNotifier = mock(SlackNotifier.class);
SlackService slack = mock(SlackService.class);

ActiveNotifier activeNotifier;

@Before
public void setupActiveNotifier() throws Exception {
BuildAwareLogger buildAwareLogger = mock(BuildAwareLogger.class);
TokenExpander tokenExpander = mock(TokenExpander.class);
MatrixProject project = mock(MatrixProject.class);
MatrixConfiguration configuration = mock(MatrixConfiguration.class);
ItemGroup group = mock(ItemGroup.class);
MatrixRun previousBuild = mock(MatrixRun.class);
ActiveNotifier.MessageBuilder messageBuilder = mock(ActiveNotifier.MessageBuilder.class);
Function<AbstractBuild<?, ?>, SlackService> slackFactory = (Function<AbstractBuild<?, ?>, SlackService>) mock(Function.class);

when(slackNotifier.getNotifyRegression()).thenReturn(true);
when(slackNotifier.getNotifyFailure()).thenReturn(true);
when(slackNotifier.getCommitInfoChoice()).thenReturn(CommitInfoChoice.NONE);
when(group.getFullDisplayName()).thenReturn("group");
when(project.getParent()).thenReturn(group);
when(project.getLastBuild()).thenReturn(null);
when(project.getFullDisplayName()).thenReturn("project");
when(previousBuild.getPreviousCompletedBuild()).thenReturn(null);
when(previousBuild.getResult()).thenReturn(Result.SUCCESS);
when(configuration.getParent()).thenReturn(project);
when(configuration.getLastBuild()).thenReturn(previousBuild);
when(configuration.getFullDisplayName()).thenReturn("project");
when(matrixRun.getAction(CauseAction.class)).thenReturn(null);
when(matrixRun.getProject()).thenReturn(configuration);
when(matrixRun.hasChangeSetComputed()).thenReturn(false);
when(matrixRun.getNumber()).thenReturn(1);
when(matrixRun.getResult()).thenReturn(Result.FAILURE);
when(slackFactory.apply(matrixRun)).thenReturn(slack);
when(messageBuilder.toString()).thenReturn("build status message");
PowerMockito.whenNew(ActiveNotifier.MessageBuilder.class)
.withArguments(slackNotifier, matrixRun, buildAwareLogger, tokenExpander)
.thenReturn(messageBuilder);
activeNotifier = new ActiveNotifier(slackNotifier, slackFactory, buildAwareLogger, tokenExpander);
}

@Test
public void startedNotifiesMatrixRunParentOnly() {
when(slackNotifier.getMatrixTriggerMode()).thenReturn(MatrixTriggerMode.ONLY_PARENT);
@Test
public void startedNotifiesMatrixRunParentOnly() {
when(slackNotifier.getMatrixTriggerMode()).thenReturn(MatrixTriggerMode.ONLY_PARENT);

activeNotifier.started(matrixRun);
activeNotifier.started(matrixRun);

verify(slack, never()).publish(any(), any());
}
verify(slack, never()).publish(any(), any());
}

@Test
public void startedNotifiesMatrixRunConfigurationsOnly() {
when(slackNotifier.getMatrixTriggerMode()).thenReturn(MatrixTriggerMode.ONLY_CONFIGURATIONS);
@Test
public void startedNotifiesMatrixRunConfigurationsOnly() {
when(slackNotifier.getMatrixTriggerMode()).thenReturn(MatrixTriggerMode.ONLY_CONFIGURATIONS);

activeNotifier.started(matrixRun);
activeNotifier.started(matrixRun);

verify(slack).publish("build status message", "good");
}
verify(slack).publish("build status message", "good");
}

@Test
public void startedNotifiesMatrixRunParentAndConfigurations() {
when(slackNotifier.getMatrixTriggerMode()).thenReturn(MatrixTriggerMode.BOTH);
@Test
public void startedNotifiesMatrixRunParentAndConfigurations() {
when(slackNotifier.getMatrixTriggerMode()).thenReturn(MatrixTriggerMode.BOTH);

activeNotifier.started(matrixRun);
activeNotifier.started(matrixRun);

verify(slack).publish("build status message", "good");
}
verify(slack).publish("build status message", "good");
}

@Test
public void finalizedNotifiesMatrixRunParentOnly() {
when(slackNotifier.getMatrixTriggerMode()).thenReturn(MatrixTriggerMode.ONLY_PARENT);
@Test
public void finalizedNotifiesMatrixRunParentOnly() {
when(slackNotifier.getMatrixTriggerMode()).thenReturn(MatrixTriggerMode.ONLY_PARENT);

activeNotifier.finalized(matrixRun);
activeNotifier.finalized(matrixRun);

verify(slack, never()).publish(any(), any());
}
verify(slack, never()).publish(any(), any());
}

@Test
public void finalizedNotifiesMatrixRunConfigurationsOnly() {
when(slackNotifier.getMatrixTriggerMode()).thenReturn(MatrixTriggerMode.ONLY_CONFIGURATIONS);
@Test
public void finalizedNotifiesMatrixRunConfigurationsOnly() {
when(slackNotifier.getMatrixTriggerMode()).thenReturn(MatrixTriggerMode.ONLY_CONFIGURATIONS);

activeNotifier.finalized(matrixRun);
activeNotifier.finalized(matrixRun);

verify(slack).publish("build status message", "danger");
}
verify(slack).publish("build status message", "danger");
}

@Test
public void finalizedNotifiesMatrixRunParentAndConfigurations() {
when(slackNotifier.getMatrixTriggerMode()).thenReturn(MatrixTriggerMode.BOTH);
@Test
public void finalizedNotifiesMatrixRunParentAndConfigurations() {
when(slackNotifier.getMatrixTriggerMode()).thenReturn(MatrixTriggerMode.BOTH);

activeNotifier.finalized(matrixRun);
activeNotifier.finalized(matrixRun);

verify(slack).publish("build status message", "danger");
}
verify(slack).publish("build status message", "danger");
}

@Test
public void completedNotifiesMatrixRunParentOnly() {
when(slackNotifier.getMatrixTriggerMode()).thenReturn(MatrixTriggerMode.ONLY_PARENT);
@Test
public void completedNotifiesMatrixRunParentOnly() {
when(slackNotifier.getMatrixTriggerMode()).thenReturn(MatrixTriggerMode.ONLY_PARENT);

activeNotifier.completed(matrixRun);
activeNotifier.completed(matrixRun);

verify(slack, never()).publish(any(), any());
}
verify(slack, never()).publish(any(), any());
}

@Test
public void completedNotifiesMatrixRunConfigurationsOnly() {
when(slackNotifier.getMatrixTriggerMode()).thenReturn(MatrixTriggerMode.ONLY_CONFIGURATIONS);
@Test
public void completedNotifiesMatrixRunConfigurationsOnly() {
when(slackNotifier.getMatrixTriggerMode()).thenReturn(MatrixTriggerMode.ONLY_CONFIGURATIONS);

activeNotifier.completed(matrixRun);
activeNotifier.completed(matrixRun);

verify(slack).publish("build status message", "danger");
}
verify(slack).publish("build status message", "danger");
}

@Test
public void completedNotifiesMatrixRunParentAndConfigurations() {
when(slackNotifier.getMatrixTriggerMode()).thenReturn(MatrixTriggerMode.BOTH);
@Test
public void completedNotifiesMatrixRunParentAndConfigurations() {
when(slackNotifier.getMatrixTriggerMode()).thenReturn(MatrixTriggerMode.BOTH);

activeNotifier.completed(matrixRun);
activeNotifier.completed(matrixRun);

verify(slack).publish("build status message", "danger");
}
verify(slack).publish("build status message", "danger");
}
}
12 changes: 3 additions & 9 deletions src/test/java/jenkins/plugins/slack/SlackNotifierTest.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package jenkins.plugins.slack;

import hudson.matrix.MatrixAggregator;
import hudson.util.FormValidation;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import jenkins.plugins.slack.matrix.MatrixTriggerMode;
import junit.framework.TestCase;
import net.sf.json.JSONArray;
import org.junit.Before;
Expand All @@ -15,11 +12,6 @@
import org.junit.runners.Parameterized;
import org.jvnet.hudson.test.JenkinsRule;

import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

@RunWith(Parameterized.class)
public class SlackNotifierTest extends TestCase {

Expand Down Expand Up @@ -84,6 +76,8 @@ public void setResponse(boolean response) {
this.response = response;
}

public String getResponseString() { return null; }
public String getResponseString() {
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,4 @@ public void shouldNotMeetConditionIfCurrentIsSuccess() {

assertFalse(actual);
}

}
}
Loading