Skip to content

Commit 5b6b71a

Browse files
Merge pull request #4 from runesr/master
Added functionality for adding colon to icon/image name
2 parents cd6501d + 5f00d34 commit 5b6b71a

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

src/main/java/in/ashwanthkumar/slack/webhook/Slack.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,15 @@ public Slack displayName(String user) {
8282
* @param imageOrIcon Icon Image URL or emoji code from http://www.emoji-cheat-sheet.com/
8383
*/
8484
public Slack icon(String imageOrIcon) {
85+
if (imageOrIcon != null && !imageOrIcon.isEmpty()) {
86+
if (!imageOrIcon.startsWith(":")) {
87+
imageOrIcon = ":" + imageOrIcon;
88+
}
89+
if (!imageOrIcon.endsWith(":")) {
90+
imageOrIcon = imageOrIcon + ":";
91+
}
92+
}
93+
8594
this.icon = imageOrIcon;
8695
return this;
8796
}

src/test/java/in/ashwanthkumar/slack/webhook/SlackTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,26 @@ public void shouldThrowIllegaArgumentExceptionWhenWebHookUrlIsNotGiven() {
4040
new Slack(null);
4141
}
4242

43+
@Test
44+
public void shouldAddColonToIconName() throws IOException {
45+
Slack slack = new Slack("mockUrl", slackService);
46+
slack.icon("ghost");
47+
ArgumentCaptor<String> iconCaptor = ArgumentCaptor.forClass(String.class);
48+
SlackMessage message = new SlackMessage("hello");
49+
doNothing().when(slackService).push(anyString(), eq(message), anyString(), iconCaptor.capture(), anyString(), anyString());
50+
slack.sendToChannel("test-channel").push(message);
51+
assertThat(iconCaptor.getValue(), is(":ghost:"));
52+
}
53+
54+
@Test
55+
public void shouldNotAddColonToIconNameIfAlreadyExists() throws IOException {
56+
Slack slack = new Slack("mockUrl", slackService);
57+
slack.icon(":ghost:");
58+
ArgumentCaptor<String> iconCaptor = ArgumentCaptor.forClass(String.class);
59+
SlackMessage message = new SlackMessage("hello");
60+
doNothing().when(slackService).push(anyString(), eq(message), anyString(), iconCaptor.capture(), anyString(), anyString());
61+
slack.sendToChannel("test-channel").push(message);
62+
assertThat(iconCaptor.getValue(), is(":ghost:"));
63+
}
64+
4365
}

0 commit comments

Comments
 (0)