Skip to content
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
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>5.5</version>
<version>5.6</version>
<relativePath />
</parent>

Expand Down Expand Up @@ -184,6 +184,11 @@
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
20 changes: 10 additions & 10 deletions src/test/java/jenkins/plugins/slack/MessageBuilderEscapeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import jenkins.plugins.slack.logging.BuildAwareLogger;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class MessageBuilderEscapeTest {
class MessageBuilderEscapeTest {

private static ActiveNotifier.MessageBuilder messageBuilder;

@BeforeClass
public static void setupMessageBuilder() {
@BeforeAll
static void setupMessageBuilder() {
AbstractBuild build = mock(AbstractBuild.class);
AbstractProject project = mock(AbstractProject.class);
AbstractProject job = mock(AbstractProject.class);
Expand All @@ -32,31 +32,31 @@ public static void setupMessageBuilder() {
}

@Test
public void testEscapeAnchor() {
void testEscapeAnchor() {
String input = "<a href='target'>test</a>";
String expected = "<'target'|test>";
String escaped = messageBuilder.escape(input);
assertEquals(expected, escaped);
}

@Test
public void testEscapePercent() {
void testEscapePercent() {
String input = "hello % world";
String expected = "hello % world";
String escaped = messageBuilder.escape(input);
assertEquals(expected, escaped);
}

@Test
public void testEscapeBraces() {
void testEscapeBraces() {
String input = "something { is } odd";
String expected = "something { is } odd";
String escaped = messageBuilder.escape(input);
assertEquals(expected, escaped);
}

@Test
public void testEscapeBracesInLink() {
void testEscapeBracesInLink() {
String input = "<a href='target'>test { case }</a>";
String expected = "<'target'|test { case }>";
String escaped = messageBuilder.escape(input);
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/jenkins/plugins/slack/SlackApiTLSTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.junit.Test;
import org.junit.jupiter.api.Test;

import static org.hamcrest.Matchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

public class SlackApiTLSTest {
class SlackApiTLSTest {

public static final String SLACK_API_TEST = "https://slack.com/api/api.test";
private static final String SLACK_API_TEST = "https://slack.com/api/api.test";

@Test
public void connectToAPI() {
void connectToAPI() {
try (CloseableHttpClient httpClient = HttpClient.getCloseableHttpClient(null)) {
HttpPost post = new HttpPost(SLACK_API_TEST);
post.setHeader("Content-Type", "application/json; charset=utf-8");
Expand Down
52 changes: 20 additions & 32 deletions src/test/java/jenkins/plugins/slack/SlackNotifierTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,58 +3,46 @@
import hudson.FilePath;
import hudson.model.TaskListener;
import hudson.util.FormValidation;
import java.util.Arrays;
import java.util.Collection;
import junit.framework.TestCase;
import net.sf.json.JSONArray;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;

@RunWith(Parameterized.class)
public class SlackNotifierTest extends TestCase {
import static org.junit.jupiter.api.Assertions.assertEquals;

private SlackNotifierStub.DescriptorImplStub descriptor;
private SlackServiceStub slackServiceStub;
private boolean response;
private FormValidation.Kind expectedResult;
@WithJenkins
class SlackNotifierTest {

@Rule
public final JenkinsRule rule = new JenkinsRule();
@SuppressWarnings({"unused", "FieldCanBeLocal"})
private JenkinsRule rule;
private SlackNotifierStub.DescriptorImplStub descriptor;

@Before
@Override
public void setUp() {
@BeforeEach
void setUp(JenkinsRule rule) {
this.rule = rule;
descriptor = new SlackNotifierStub.DescriptorImplStub();
}

public SlackNotifierTest(SlackServiceStub slackServiceStub, boolean response, FormValidation.Kind expectedResult) {
this.slackServiceStub = slackServiceStub;
this.response = response;
this.expectedResult = expectedResult;
}

@Parameterized.Parameters
public static Collection<Object[]> businessTypeKeys() {
return Arrays.asList(new Object[][]{
static Object[][] businessTypeKeys() {
return new Object[][]{
{new SlackServiceStub(), true, FormValidation.Kind.OK},
{new SlackServiceStub(), false, FormValidation.Kind.ERROR},
{null, false, FormValidation.Kind.ERROR}
});
};
}

@Test
public void testDoTestConnection() {
@ParameterizedTest
@MethodSource("businessTypeKeys")
void testDoTestConnection(SlackServiceStub slackServiceStub, boolean response, FormValidation.Kind expectedResult) {
if (slackServiceStub != null) {
slackServiceStub.setResponse(response);
}
descriptor.setSlackService(slackServiceStub);
FormValidation result = descriptor
.doTestConnection("baseUrl", "teamDomain", "authTokenCredentialId", false, "room", false, ":+1:", "slack", null);
assertEquals(result.kind, expectedResult);
assertEquals(expectedResult, result.kind);
}

public static class SlackServiceStub implements SlackService {
Expand Down
32 changes: 16 additions & 16 deletions src/test/java/jenkins/plugins/slack/SlackNotifierUnitTest.java
Original file line number Diff line number Diff line change
@@ -1,73 +1,73 @@
package jenkins.plugins.slack;

import hudson.util.FormValidation;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class SlackNotifierUnitTest {
class SlackNotifierUnitTest {
private SlackNotifier slackNotifier = new SlackNotifier(CommitInfoChoice.AUTHORS);
private SlackNotifierStub.DescriptorImplStub descriptor;

@Before
public void setUp() {
@BeforeEach
void setUp() {
descriptor = new SlackNotifierStub.DescriptorImplStub();
}

@Test
public void isAnyCustomMessagePopulatedReturnsFalseWhenNonePopulated() {
void isAnyCustomMessagePopulatedReturnsFalseWhenNonePopulated() {
assertFalse(slackNotifier.isAnyCustomMessagePopulated());
}

@Test
public void isAnyCustomMessagePopulatedReturnsTrueWhenOnePopulated() {
void isAnyCustomMessagePopulatedReturnsTrueWhenOnePopulated() {
slackNotifier.setCustomMessage("hi");

assertTrue(slackNotifier.isAnyCustomMessagePopulated());
}

@Test
public void isAnyCustomMessagePopulatedReturnsTrueWhenMultiplePopulated() {
void isAnyCustomMessagePopulatedReturnsTrueWhenMultiplePopulated() {
slackNotifier.setCustomMessage("hi");
slackNotifier.setCustomMessageFailure("hii");

assertTrue(slackNotifier.isAnyCustomMessagePopulated());
}

@Test
public void doCheckBaseUrl_emptyIsValid() {
void doCheckBaseUrl_emptyIsValid() {
FormValidation formValidation = descriptor.doCheckBaseUrl("", null);

assertThat(formValidation.kind, is(FormValidation.Kind.OK));
}

@Test
public void doCheckBaseUrl_setIsValid() {
void doCheckBaseUrl_setIsValid() {
FormValidation formValidation = descriptor.doCheckBaseUrl("", null);

assertThat(formValidation.kind, is(FormValidation.Kind.OK));
}

@Test
public void doCheckBaseUrl_baseUrlAndTeamDomainSet_is_invalid() {
void doCheckBaseUrl_baseUrlAndTeamDomainSet_is_invalid() {
FormValidation formValidation = descriptor.doCheckBaseUrl("a", "a");

assertThat(formValidation.kind, is(FormValidation.Kind.ERROR));
}

@Test
public void doCheckBaseUrl_teamDomain_only_set_is_valid() {
void doCheckBaseUrl_teamDomain_only_set_is_valid() {
FormValidation formValidation = descriptor.doCheckBaseUrl(null, "a");

assertThat(formValidation.kind, is(FormValidation.Kind.OK));
}

@Test
public void doCheckBaseUrl_including_jenkins_ci_hook_is_invalid() {
void doCheckBaseUrl_including_jenkins_ci_hook_is_invalid() {
FormValidation formValidation = descriptor.doCheckBaseUrl("https://jenkins-slack-plugin-testing.slack.com/services/hooks/jenkins-ci", null);

assertThat(formValidation.kind, is(FormValidation.Kind.ERROR));
Expand Down
Loading
Loading