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 publish to multiple channels. Add unit tests. #100

Merged
merged 1 commit into from
May 31, 2015
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
10 changes: 5 additions & 5 deletions src/main/java/jenkins/plugins/slack/StandardSlackService.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public boolean publish(String message) {
}

public boolean publish(String message, String color) {
boolean result = true;
for (String roomId : roomIds) {
String url = "https://" + teamDomain + "." + host + "/services/hooks/jenkins-ci?token=" + token;
logger.info("Posting: to " + roomId + " on " + teamDomain + " using " + url +": " + message + " " + color);
Expand Down Expand Up @@ -67,21 +68,20 @@ public boolean publish(String message, String color) {
String response = post.getResponseBodyAsString();
if(responseCode != HttpStatus.SC_OK) {
logger.log(Level.WARNING, "Slack post may have failed. Response: " + response);
return false;
result = false;
}
return true;
} catch (Exception e) {
logger.log(Level.WARNING, "Error posting to Slack", e);
return false;
result = false;
} finally {
logger.info("Posting succeeded");
post.releaseConnection();
}
}
return false;
return result;
}

private HttpClient getHttpClient() {
protected HttpClient getHttpClient() {
HttpClient client = new HttpClient();
if (Jenkins.getInstance() != null) {
ProxyConfiguration proxy = Jenkins.getInstance().proxy;
Expand Down
34 changes: 34 additions & 0 deletions src/test/java/jenkins/plugins/slack/HttpClientStub.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package jenkins.plugins.slack;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpStatus;

public class HttpClientStub extends HttpClient {

private int numberOfCallsToExecuteMethod;
private int httpStatus;
private boolean failAlternateResponses = false;

@Override
public int executeMethod(HttpMethod httpMethod) {
numberOfCallsToExecuteMethod++;
if (failAlternateResponses && (numberOfCallsToExecuteMethod % 2 == 0)) {
return HttpStatus.SC_NOT_FOUND;
} else {
return httpStatus;
}
}

public int getNumberOfCallsToExecuteMethod() {
return numberOfCallsToExecuteMethod;
}

public void setHttpStatus(int httpStatus) {
this.httpStatus = httpStatus;
}

public void setFailAlternateResponses(boolean failAlternateResponses) {
this.failAlternateResponses = failAlternateResponses;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package jenkins.plugins.slack;

public class StandardSlackServiceStub extends StandardSlackService {

private HttpClientStub httpClientStub;

public StandardSlackServiceStub(String teamDomain, String token, String roomId) {
super(teamDomain, token, roomId);
}

@Override
public HttpClientStub getHttpClient() {
return httpClientStub;
}

public void setHttpClient(HttpClientStub httpClientStub) {
this.httpClientStub = httpClientStub;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package jenkins.plugins.slack;

import jenkins.plugins.slack.StandardSlackService;
import org.junit.Before;
import org.apache.http.HttpStatus;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class StandardSlackServiceTest {

/**
Expand Down Expand Up @@ -33,4 +36,68 @@ public void invalidTokenShouldFail() {
StandardSlackService service = new StandardSlackService("tinyspeck", "token", "#general");
service.publish("message");
}

@Test
public void publishToASingleRoomSendsASingleMessage() {
StandardSlackServiceStub service = new StandardSlackServiceStub("domain", "token", "#room1");
HttpClientStub httpClientStub = new HttpClientStub();
service.setHttpClient(httpClientStub);
service.publish("message");
assertEquals(1, service.getHttpClient().getNumberOfCallsToExecuteMethod());
}

@Test
public void publishToMultipleRoomsSendsAMessageToEveryRoom() {
StandardSlackServiceStub service = new StandardSlackServiceStub("domain", "token", "#room1,#room2,#room3");
HttpClientStub httpClientStub = new HttpClientStub();
service.setHttpClient(httpClientStub);
service.publish("message");
assertEquals(3, service.getHttpClient().getNumberOfCallsToExecuteMethod());
}

@Test
public void successfulPublishToASingleRoomReturnsTrue() {
StandardSlackServiceStub service = new StandardSlackServiceStub("domain", "token", "#room1");
HttpClientStub httpClientStub = new HttpClientStub();
httpClientStub.setHttpStatus(HttpStatus.SC_OK);
service.setHttpClient(httpClientStub);
assertTrue(service.publish("message"));
}

@Test
public void successfulPublishToMultipleRoomsReturnsTrue() {
StandardSlackServiceStub service = new StandardSlackServiceStub("domain", "token", "#room1,#room2,#room3");
HttpClientStub httpClientStub = new HttpClientStub();
httpClientStub.setHttpStatus(HttpStatus.SC_OK);
service.setHttpClient(httpClientStub);
assertTrue(service.publish("message"));
}

@Test
public void failedPublishToASingleRoomReturnsFalse() {
StandardSlackServiceStub service = new StandardSlackServiceStub("domain", "token", "#room1");
HttpClientStub httpClientStub = new HttpClientStub();
httpClientStub.setHttpStatus(HttpStatus.SC_NOT_FOUND);
service.setHttpClient(httpClientStub);
assertFalse(service.publish("message"));
}

@Test
public void singleFailedPublishToMultipleRoomsReturnsFalse() {
StandardSlackServiceStub service = new StandardSlackServiceStub("domain", "token", "#room1,#room2,#room3");
HttpClientStub httpClientStub = new HttpClientStub();
httpClientStub.setFailAlternateResponses(true);
httpClientStub.setHttpStatus(HttpStatus.SC_OK);
service.setHttpClient(httpClientStub);
assertFalse(service.publish("message"));
}

@Test
public void publishToEmptyRoomReturnsTrue() {
StandardSlackServiceStub service = new StandardSlackServiceStub("domain", "token", "");
HttpClientStub httpClientStub = new HttpClientStub();
httpClientStub.setHttpStatus(HttpStatus.SC_OK);
service.setHttpClient(httpClientStub);
assertTrue(service.publish("message"));
}
}