From b56be7654b609bf7f81059678c57a7f8fd04f3e5 Mon Sep 17 00:00:00 2001 From: dvehar Date: Wed, 13 Jun 2018 16:24:33 -0700 Subject: [PATCH 1/5] Remove dead code --- .../java/net/gpedro/integrations/slack/SlackAction.java | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/main/java/net/gpedro/integrations/slack/SlackAction.java b/src/main/java/net/gpedro/integrations/slack/SlackAction.java index b05664d..2b976fb 100644 --- a/src/main/java/net/gpedro/integrations/slack/SlackAction.java +++ b/src/main/java/net/gpedro/integrations/slack/SlackAction.java @@ -4,6 +4,7 @@ /** * @author Galimov Ruslan + * @author Desmond Vehar */ public class SlackAction { @@ -11,13 +12,7 @@ public class SlackAction { private static final String TEXT = "text"; private static final String TYPE = "type"; private static final String VALUE = "value"; - private static final String CONFIRM = "confirm"; private static final String STYLE = "style"; - private static final String OPTIONS = "options"; - private static final String OPTION_GROUPS = "option_groups"; - private static final String DATA_SOURCE = "data_source"; - private static final String SELECTED_OPTIONS = "selected_options"; - private static final String MIN_QUERY_LENGTH = "min_query_length"; private String name; private String text; From dcc34fe6456df40f8dcbad7bb5602d6c3694f03a Mon Sep 17 00:00:00 2001 From: dvehar Date: Wed, 13 Jun 2018 16:28:38 -0700 Subject: [PATCH 2/5] Change SlackAction "value" to "url" Using "value" does not work (maybe it used to). [Example](https://api.slack.com/docs/messages/builder?msg=%7B%22text%22%3A%22%3C%40W1A2BC3DD%3E%20approved%20your%20travel%20request.%20Book%20any%20airline%20you%20like%20by%20continuing%20below.%22%2C%22channel%22%3A%22C061EG9SL%22%2C%22attachments%22%3A%5B%7B%22fallback%22%3A%22Book%20your%20flights%20at%20https%3A%2F%2Fflights.example.com%2Fbook%2Fr123456%22%2C%22actions%22%3A%5B%7B%22type%22%3A%22button%22%2C%22text%22%3A%22Book%20flights%20%F0%9F%9B%AB%22%2C%22value%22%3A%22https%3A%2F%2Fflights.example.com%2Fbook%2Fr123456%22%7D%5D%7D%5D%7D) Using "url" does work. [Example](https://api.slack.com/docs/messages/builder?msg=%7B%22text%22%3A%22%3C%40W1A2BC3DD%3E%20approved%20your%20travel%20request.%20Book%20any%20airline%20you%20like%20by%20continuing%20below.%22%2C%22channel%22%3A%22C061EG9SL%22%2C%22attachments%22%3A%5B%7B%22fallback%22%3A%22Book%20your%20flights%20at%20https%3A%2F%2Fflights.example.com%2Fbook%2Fr123456%22%2C%22actions%22%3A%5B%7B%22type%22%3A%22button%22%2C%22text%22%3A%22Book%20flights%20%F0%9F%9B%AB%22%2C%22url%22%3A%22https%3A%2F%2Fflights.example.com%2Fbook%2Fr123456%22%7D%5D%7D%5D%7D) --- .../integrations/slack/SlackAction.java | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/main/java/net/gpedro/integrations/slack/SlackAction.java b/src/main/java/net/gpedro/integrations/slack/SlackAction.java index 2b976fb..ab98e20 100644 --- a/src/main/java/net/gpedro/integrations/slack/SlackAction.java +++ b/src/main/java/net/gpedro/integrations/slack/SlackAction.java @@ -11,20 +11,20 @@ public class SlackAction { private static final String NAME = "name"; private static final String TEXT = "text"; private static final String TYPE = "type"; - private static final String VALUE = "value"; + private static final String URL = "url"; private static final String STYLE = "style"; private String name; private String text; private SlackActionType type; - private String value; + private String url; private SlackActionStyle style; - public SlackAction(String name, String text, SlackActionType type, String value) { + public SlackAction(String name, String text, SlackActionType type, String url) { this.name = name; this.text = text; this.type = type; - this.value = value; + this.url = url; } public String getName() { @@ -51,12 +51,12 @@ public void setType(SlackActionType type) { this.type = type; } - public String getValue() { - return value; + public String getUrl() { + return url; } - public void setValue(String value) { - this.value = value; + public void setUrl(String url) { + this.url = url; } public SlackActionStyle getStyle() { @@ -69,15 +69,15 @@ public void setStyle(SlackActionStyle style) { public JsonObject toJson() { final JsonObject data = new JsonObject(); + data.addProperty(NAME, name); data.addProperty(TEXT, text); + data.addProperty(URL, url); if (type != null) { data.addProperty(TYPE, type.getCode()); } - data.addProperty(VALUE, value); - if (style != null) { data.addProperty(STYLE, style.getCode()); } From cc66b3777d27403553048995e1c5bd3e4f16986a Mon Sep 17 00:00:00 2001 From: dvehar Date: Fri, 22 Jun 2018 09:57:57 -0700 Subject: [PATCH 3/5] Refactor SlackAction to use an object oriented approach for each type of action Creates the concrete SlackActions: - BasicButtonSlackAction - InteractiveButtonSlackAction - InteractiveSelectSlackAction Adds tests --- pom.xml | 12 +++ .../integrations/slack/JsonSerializable.java | 7 ++ .../integrations/slack/SlackAction.java | 87 ------------------- .../integrations/slack/SlackActionStyle.java | 18 ---- .../integrations/slack/SlackActionType.java | 19 ---- .../integrations/slack/SlackAttachment.java | 1 + .../action/BasicButtonSlackAction.java | 29 +++++++ .../attachment/action/ButtonSlackAction.java | 45 ++++++++++ .../action/InteractiveButtonSlackAction.java | 62 +++++++++++++ .../action/InteractiveSelectSlackAction.java | 60 +++++++++++++ .../slack/attachment/action/SlackAction.java | 74 ++++++++++++++++ .../action/BasicButtonSlackActionTest.java | 19 ++++ .../action/ButtonSlackActionTest.java | 34 ++++++++ .../InteractiveButtonSlackActionTest.java | 49 +++++++++++ .../InteractiveSelectSlackActionTest.java | 55 ++++++++++++ .../attachment/action/SlackActionTest.java | 53 +++++++++++ 16 files changed, 500 insertions(+), 124 deletions(-) create mode 100644 src/main/java/net/gpedro/integrations/slack/JsonSerializable.java delete mode 100644 src/main/java/net/gpedro/integrations/slack/SlackAction.java delete mode 100644 src/main/java/net/gpedro/integrations/slack/SlackActionStyle.java delete mode 100644 src/main/java/net/gpedro/integrations/slack/SlackActionType.java create mode 100644 src/main/java/net/gpedro/integrations/slack/attachment/action/BasicButtonSlackAction.java create mode 100644 src/main/java/net/gpedro/integrations/slack/attachment/action/ButtonSlackAction.java create mode 100644 src/main/java/net/gpedro/integrations/slack/attachment/action/InteractiveButtonSlackAction.java create mode 100644 src/main/java/net/gpedro/integrations/slack/attachment/action/InteractiveSelectSlackAction.java create mode 100644 src/main/java/net/gpedro/integrations/slack/attachment/action/SlackAction.java create mode 100644 src/main/test/net/gpedro/integrations/slack/attachment/action/BasicButtonSlackActionTest.java create mode 100644 src/main/test/net/gpedro/integrations/slack/attachment/action/ButtonSlackActionTest.java create mode 100644 src/main/test/net/gpedro/integrations/slack/attachment/action/InteractiveButtonSlackActionTest.java create mode 100644 src/main/test/net/gpedro/integrations/slack/attachment/action/InteractiveSelectSlackActionTest.java create mode 100644 src/main/test/net/gpedro/integrations/slack/attachment/action/SlackActionTest.java diff --git a/pom.xml b/pom.xml index 4ea96f5..567a6c8 100644 --- a/pom.xml +++ b/pom.xml @@ -49,6 +49,11 @@ galimovrf@gmail.com https://github.com/galimru/ + + Desmond Vehar + desmondvehar@gmail.com + https://github.com/dvehar/ + @@ -67,6 +72,7 @@ UTF-8 1.5 2.3.1 + 4.8.1 @@ -162,5 +168,11 @@ gson ${gson.version} + + junit + junit + ${junit.version} + test + diff --git a/src/main/java/net/gpedro/integrations/slack/JsonSerializable.java b/src/main/java/net/gpedro/integrations/slack/JsonSerializable.java new file mode 100644 index 0000000..9b1cbe2 --- /dev/null +++ b/src/main/java/net/gpedro/integrations/slack/JsonSerializable.java @@ -0,0 +1,7 @@ +package net.gpedro.integrations.slack; + +import com.google.gson.JsonObject; + +public interface JsonSerializable { + JsonObject toJson(); +} diff --git a/src/main/java/net/gpedro/integrations/slack/SlackAction.java b/src/main/java/net/gpedro/integrations/slack/SlackAction.java deleted file mode 100644 index ab98e20..0000000 --- a/src/main/java/net/gpedro/integrations/slack/SlackAction.java +++ /dev/null @@ -1,87 +0,0 @@ -package net.gpedro.integrations.slack; - -import com.google.gson.JsonObject; - -/** - * @author Galimov Ruslan - * @author Desmond Vehar - */ -public class SlackAction { - - private static final String NAME = "name"; - private static final String TEXT = "text"; - private static final String TYPE = "type"; - private static final String URL = "url"; - private static final String STYLE = "style"; - - private String name; - private String text; - private SlackActionType type; - private String url; - private SlackActionStyle style; - - public SlackAction(String name, String text, SlackActionType type, String url) { - this.name = name; - this.text = text; - this.type = type; - this.url = url; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getText() { - return text; - } - - public void setText(String text) { - this.text = text; - } - - public SlackActionType getType() { - return type; - } - - public void setType(SlackActionType type) { - this.type = type; - } - - public String getUrl() { - return url; - } - - public void setUrl(String url) { - this.url = url; - } - - public SlackActionStyle getStyle() { - return style; - } - - public void setStyle(SlackActionStyle style) { - this.style = style; - } - - public JsonObject toJson() { - final JsonObject data = new JsonObject(); - - data.addProperty(NAME, name); - data.addProperty(TEXT, text); - data.addProperty(URL, url); - - if (type != null) { - data.addProperty(TYPE, type.getCode()); - } - - if (style != null) { - data.addProperty(STYLE, style.getCode()); - } - - return data; - } -} diff --git a/src/main/java/net/gpedro/integrations/slack/SlackActionStyle.java b/src/main/java/net/gpedro/integrations/slack/SlackActionStyle.java deleted file mode 100644 index 8383323..0000000 --- a/src/main/java/net/gpedro/integrations/slack/SlackActionStyle.java +++ /dev/null @@ -1,18 +0,0 @@ -package net.gpedro.integrations.slack; - -/** - * @author Galimov Ruslan - */ -public enum SlackActionStyle { - DEFAULT("default"), PRIMARY("primary"), DANGER("danger"); - - private String code; - - SlackActionStyle(String code) { - this.code = code; - } - - public String getCode() { - return code; - } -} diff --git a/src/main/java/net/gpedro/integrations/slack/SlackActionType.java b/src/main/java/net/gpedro/integrations/slack/SlackActionType.java deleted file mode 100644 index 74bccd5..0000000 --- a/src/main/java/net/gpedro/integrations/slack/SlackActionType.java +++ /dev/null @@ -1,19 +0,0 @@ -package net.gpedro.integrations.slack; - -/** - * @author Galimov Ruslan - */ -public enum SlackActionType { - BUTTON("button"), SELECT("select"); - - private String code; - - SlackActionType(String code) { - this.code = code; - } - - public String getCode() { - return code; - } - -} diff --git a/src/main/java/net/gpedro/integrations/slack/SlackAttachment.java b/src/main/java/net/gpedro/integrations/slack/SlackAttachment.java index dbc1cc8..d7f8dcf 100644 --- a/src/main/java/net/gpedro/integrations/slack/SlackAttachment.java +++ b/src/main/java/net/gpedro/integrations/slack/SlackAttachment.java @@ -9,6 +9,7 @@ import com.google.gson.JsonArray; import com.google.gson.JsonObject; import com.google.gson.JsonPrimitive; +import net.gpedro.integrations.slack.attachment.action.SlackAction; /** * Represents an attachment in a slack webhook JSON message. diff --git a/src/main/java/net/gpedro/integrations/slack/attachment/action/BasicButtonSlackAction.java b/src/main/java/net/gpedro/integrations/slack/attachment/action/BasicButtonSlackAction.java new file mode 100644 index 0000000..e9e0f04 --- /dev/null +++ b/src/main/java/net/gpedro/integrations/slack/attachment/action/BasicButtonSlackAction.java @@ -0,0 +1,29 @@ +package net.gpedro.integrations.slack.attachment.action; + +import com.google.gson.JsonObject; + +/** + * @author Desmond Vehar + */ +public class BasicButtonSlackAction extends SlackAction { + + private static final String URL = "url"; + + private String url; + + public BasicButtonSlackAction(String name, String text, String url) { + super(name, text, Type.BUTTON); + + checkArgForNull(URL, url); + this.url = url; + } + + public JsonObject toJson() { + final JsonObject data = super.toJson(); + + data.addProperty(URL, url); + + return data; + } + +} diff --git a/src/main/java/net/gpedro/integrations/slack/attachment/action/ButtonSlackAction.java b/src/main/java/net/gpedro/integrations/slack/attachment/action/ButtonSlackAction.java new file mode 100644 index 0000000..544a8ea --- /dev/null +++ b/src/main/java/net/gpedro/integrations/slack/attachment/action/ButtonSlackAction.java @@ -0,0 +1,45 @@ +package net.gpedro.integrations.slack.attachment.action; + +import com.google.gson.JsonObject; +import net.gpedro.integrations.slack.JsonSerializable; + +/** + * @author Desmond Vehar + */ +public abstract class ButtonSlackAction extends SlackAction { + private static final String STYLE = "style"; + + private Style style; + + ButtonSlackAction(String name, String text, Type type) { + super(name, text, type); + } + + public void setStyle(Style style) { + this.style = style; + } + + public JsonObject toJson() { + final JsonObject data = super.toJson(); + + if (style != null) { + data.addProperty(STYLE, style.getCode()); + } + + return data; + } + + enum Style { + DEFAULT("default"), PRIMARY("primary"), DANGER("danger"); + + private String code; + + Style(String code) { + this.code = code; + } + + public String getCode() { + return code; + } + } +} diff --git a/src/main/java/net/gpedro/integrations/slack/attachment/action/InteractiveButtonSlackAction.java b/src/main/java/net/gpedro/integrations/slack/attachment/action/InteractiveButtonSlackAction.java new file mode 100644 index 0000000..e0713da --- /dev/null +++ b/src/main/java/net/gpedro/integrations/slack/attachment/action/InteractiveButtonSlackAction.java @@ -0,0 +1,62 @@ +package net.gpedro.integrations.slack.attachment.action; + +import com.google.gson.JsonObject; +import net.gpedro.integrations.slack.JsonSerializable; + +/** + * @author Desmond Vehar + */ +public class InteractiveButtonSlackAction extends ButtonSlackAction { + + private static final String VALUE = "value"; + private static final String CONFIRM = "confirm"; + + private String value; + private Confirm confirm; + + public InteractiveButtonSlackAction(String name, String text, String value) { + super(name, text, Type.BUTTON); + + checkArgForNull(VALUE, value); + this.value = value; + } + + public void setConfirm(Confirm confirm) { + this.confirm = confirm; + } + + public JsonObject toJson() { + final JsonObject data = super.toJson(); + + data.addProperty(VALUE, value); + + if (confirm != null) { + data.add(CONFIRM, confirm.toJson()); + } + + return data; + } + + public static class Confirm implements JsonSerializable { + private String title; + private String text; + private String ok_text; + private String dismiss_text; + + public Confirm(String title, String text, String ok_text, String dismiss_text) { + this.title = title; + this.text = text; + this.ok_text = ok_text; + this.dismiss_text = dismiss_text; + } + + public JsonObject toJson() { + JsonObject data = new JsonObject(); + data.addProperty("title", title); + data.addProperty("text", text); + data.addProperty("ok_text", ok_text); + data.addProperty("dismiss_text", dismiss_text); + return data; + } + } +} diff --git a/src/main/java/net/gpedro/integrations/slack/attachment/action/InteractiveSelectSlackAction.java b/src/main/java/net/gpedro/integrations/slack/attachment/action/InteractiveSelectSlackAction.java new file mode 100644 index 0000000..cdccc79 --- /dev/null +++ b/src/main/java/net/gpedro/integrations/slack/attachment/action/InteractiveSelectSlackAction.java @@ -0,0 +1,60 @@ +package net.gpedro.integrations.slack.attachment.action; + +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; +import net.gpedro.integrations.slack.JsonSerializable; + +import java.util.ArrayList; +import java.util.List; + +/** + * @author Desmond Vehar + */ +public class InteractiveSelectSlackAction extends SlackAction { + + private static final String OPTIONS = "options"; + + private List