From 44d595a949c5cebc12a111fe2504aa75949a4c64 Mon Sep 17 00:00:00 2001 From: jair-chaguay Date: Sat, 29 Jul 2023 23:05:04 -0500 Subject: [PATCH 1/2] Implemnting Patterns of Desing: Singleton --- .../go/task/slack/config/ConfigProvider.java | 109 +++++++++------- src/test/resources/json/task_config.json | 110 ++++++++-------- .../resources/json/task_config_env_var.json | 120 +++++++++--------- 3 files changed, 177 insertions(+), 162 deletions(-) diff --git a/src/main/java/com/vincit/go/task/slack/config/ConfigProvider.java b/src/main/java/com/vincit/go/task/slack/config/ConfigProvider.java index 86eb948..f532f47 100644 --- a/src/main/java/com/vincit/go/task/slack/config/ConfigProvider.java +++ b/src/main/java/com/vincit/go/task/slack/config/ConfigProvider.java @@ -1,47 +1,62 @@ -package com.vincit.go.task.slack.config; - -import com.vincit.go.task.slack.model.ChannelType; -import com.vincit.go.task.slack.model.ColorType; -import com.vincit.go.task.slack.utils.Required; -import com.vincit.go.task.slack.utils.Secure; - -import java.util.HashMap; -import java.util.Map; - -import static com.vincit.go.task.slack.config.FieldUtils.createField; - -public class ConfigProvider { - - public static final String CHANNEL = "Channel"; - public static final String CHANNEL_TYPE = "ChannelType"; - public static final String MESSAGE = "Message"; - public static final String TITLE = "Title"; - public static final String ICON_OR_EMOJI = "IconOrEmoji"; - public static final String WEBHOOK_URL = "WebhookUrl"; - public static final String DISPLAY_NAME = "DisplayName"; - public static final String COLOR = "Color"; - public static final String COLOR_TYPE = "ColorType"; - public static final String MARKDOWN_IN_TEXT = "MarkdownInText"; - public static final String FAIL_ON_ERROR = "FailOnError"; - - private static Map> fieldConfig = new HashMap<>(); - - static { - fieldConfig.put(WEBHOOK_URL, createField("", Secure.NO, Required.YES)); - fieldConfig.put(CHANNEL, createField("", Secure.NO, Required.YES)); - fieldConfig.put(CHANNEL_TYPE, createField(ChannelType.CHANNEL.getDisplayValue(), Secure.NO, Required.YES)); - fieldConfig.put(TITLE, createField("", Secure.NO, Required.NO)); - fieldConfig.put(ICON_OR_EMOJI, createField("", Secure.NO, Required.NO)); - fieldConfig.put(MESSAGE, createField("", Secure.NO, Required.NO)); - fieldConfig.put(DISPLAY_NAME, createField("", Secure.NO, Required.NO)); - fieldConfig.put(COLOR_TYPE, createField(ColorType.NONE.getDisplayValue(), Secure.NO, Required.YES)); - fieldConfig.put(COLOR, createField("", Secure.NO, Required.NO)); - fieldConfig.put(MARKDOWN_IN_TEXT, createField("", Secure.NO, Required.NO)); - fieldConfig.put(FAIL_ON_ERROR, createField("", Secure.NO, Required.NO)); - } - - public static Map getFieldConfig() { - return fieldConfig; - } - -} +package com.vincit.go.task.slack.config; + +import com.vincit.go.task.slack.model.ChannelType; +import com.vincit.go.task.slack.model.ColorType; +import com.vincit.go.task.slack.utils.Required; +import com.vincit.go.task.slack.utils.Secure; + +import java.util.HashMap; +import java.util.Map; + +import static com.vincit.go.task.slack.config.FieldUtils.createField; + +public class ConfigProvider { + private static ConfigProvider instance; + public static final String CHANNEL = "Channel"; + public static final String CHANNEL_TYPE = "ChannelType"; + public static final String MESSAGE = "Message"; + public static final String TITLE = "Title"; + public static final String ICON_OR_EMOJI = "IconOrEmoji"; + public static final String WEBHOOK_URL = "WebhookUrl"; + public static final String DISPLAY_NAME = "DisplayName"; + public static final String COLOR = "Color"; + public static final String COLOR_TYPE = "ColorType"; + public static final String MARKDOWN_IN_TEXT = "MarkdownInText"; + public static final String FAIL_ON_ERROR = "FailOnError"; + + private static Map> fieldConfig = new HashMap<>(); + + static { + fieldConfig.put(WEBHOOK_URL, createField("", Secure.NO, Required.YES)); + fieldConfig.put(CHANNEL, createField("", Secure.NO, Required.YES)); + fieldConfig.put(CHANNEL_TYPE, createField(ChannelType.CHANNEL.getDisplayValue(), Secure.NO, Required.YES)); + fieldConfig.put(TITLE, createField("", Secure.NO, Required.NO)); + fieldConfig.put(ICON_OR_EMOJI, createField("", Secure.NO, Required.NO)); + fieldConfig.put(MESSAGE, createField("", Secure.NO, Required.NO)); + fieldConfig.put(DISPLAY_NAME, createField("", Secure.NO, Required.NO)); + fieldConfig.put(COLOR_TYPE, createField(ColorType.NONE.getDisplayValue(), Secure.NO, Required.YES)); + fieldConfig.put(COLOR, createField("", Secure.NO, Required.NO)); + fieldConfig.put(MARKDOWN_IN_TEXT, createField("", Secure.NO, Required.NO)); + fieldConfig.put(FAIL_ON_ERROR, createField("", Secure.NO, Required.NO)); + } + + + + private ConfigProvider(){ + + } + + public static ConfigProvider getInstance(){ + if(instance==null){ + instance=new ConfigProvider(); + } + return instance; + } + + + + public static Map getFieldConfig() { + return fieldConfig; + } + +} diff --git a/src/test/resources/json/task_config.json b/src/test/resources/json/task_config.json index a4b7380..d96bf5a 100644 --- a/src/test/resources/json/task_config.json +++ b/src/test/resources/json/task_config.json @@ -1,55 +1,55 @@ -{ - "config": { - "Message": { - "secure": false, - "required": false, - "value": "message" - }, - "Title": { - "secure": false, - "required": false, - "value": "title" - }, - "IconOrEmoji": { - "secure": false, - "required": false, - "value": ":tada:" - }, - "Channel": { - "secure": false, - "required": false, - "value": "channel" - }, - "ChannelType": { - "secure": false, - "required": false, - "value": "Channel" - }, - "WebhookUrl": { - "secure": false, - "required": false, - "value": "https://example.org" - }, - "DisplayName": { - "secure": false, - "required": false, - "value": "display-name" - }, - "ColorType": { - "secure": false, - "required": false, - "value": "Custom" - }, - "Color": { - "secure": false, - "required": false, - "value": "f00f00" - } - }, - "context": { - "environmentVariables": { - "VAR1": "Value1", - "VAR2": "Value2" - } - } -} +{ + "config": { + "Message": { + "secure": false, + "required": false, + "value": "message" + }, + "Title": { + "secure": false, + "required": false, + "value": "title" + }, + "IconOrEmoji": { + "secure": false, + "required": false, + "value": ":tada:" + }, + "Channel": { + "secure": false, + "required": false, + "value": "channel" + }, + "ChannelType": { + "secure": false, + "required": false, + "value": "Channel" + }, + "WebhookUrl": { + "secure": false, + "required": false, + "value": "https://example.org" + }, + "DisplayName": { + "secure": false, + "required": false, + "value": "display-name" + }, + "ColorType": { + "secure": false, + "required": false, + "value": "Custom" + }, + "Color": { + "secure": false, + "required": false, + "value": "f00f00" + } + }, + "context": { + "environmentVariables": { + "VAR1": "Value1", + "VAR2": "Value2" + } + } +} diff --git a/src/test/resources/json/task_config_env_var.json b/src/test/resources/json/task_config_env_var.json index 9b181f7..3310abb 100644 --- a/src/test/resources/json/task_config_env_var.json +++ b/src/test/resources/json/task_config_env_var.json @@ -1,60 +1,60 @@ -{ - "config": { - "Message": { - "secure": false, - "required": false, - "value": "$message" - }, - "Title": { - "secure": false, - "required": false, - "value": "$title" - }, - "IconOrEmoji": { - "secure": false, - "required": false, - "value": "$emoji" - }, - "Channel": { - "secure": false, - "required": false, - "value": "$channel" - }, - "ChannelType": { - "secure": false, - "required": false, - "value": "Channel" - }, - "WebhookUrl": { - "secure": false, - "required": false, - "value": "$webhookUrl" - }, - "DisplayName": { - "secure": false, - "required": false, - "value": "$displayName" - }, - "ColorType": { - "secure": false, - "required": false, - "value": "Custom" - }, - "Color": { - "secure": false, - "required": false, - "value": "$color" - } - }, - "context": { - "environmentVariables": { - "message": "Message value", - "channel": "Channel value", - "emoji": "Emoji value", - "displayName": "Display value", - "title": "Title value", - "webhookUrl": "https://example.org/hook", - "color": "f00baa" - } - } -} +{ + "config": { + "Message": { + "secure": false, + "required": false, + "value": "$message" + }, + "Title": { + "secure": false, + "required": false, + "value": "$title" + }, + "IconOrEmoji": { + "secure": false, + "required": false, + "value": "$emoji" + }, + "Channel": { + "secure": false, + "required": false, + "value": "$channel" + }, + "ChannelType": { + "secure": false, + "required": false, + "value": "Channel" + }, + "WebhookUrl": { + "secure": false, + "required": false, + "value": "$webhookUrl" + }, + "DisplayName": { + "secure": false, + "required": false, + "value": "$displayName" + }, + "ColorType": { + "secure": false, + "required": false, + "value": "Custom" + }, + "Color": { + "secure": false, + "required": false, + "value": "$color" + } + }, + "context": { + "environmentVariables": { + "message": "Message value", + "channel": "Channel value", + "emoji": "Emoji value", + "displayName": "Display value", + "title": "Title value", + "webhookUrl": "https://example.org/hook", + "color": "f00baa" + } + } +} From aa3d794e563f617675447bff90203759a8ffa428 Mon Sep 17 00:00:00 2001 From: jair-chaguay Date: Sat, 29 Jul 2023 23:07:59 -0500 Subject: [PATCH 2/2] Implemnting Patterns of Desing: Decorator --- .../executor/SlackExecutorComponent.java | 20 +++++++ .../executor/SlackExecutorDecorator.java | 27 ++++++++++ .../executor/SlackExecutorFactory_1.java | 9 ++++ .../executor/SlackExecutorInterface.java | 15 ++++++ .../SlackExecutorLogginDecorator.java | 28 ++++++++++ .../SlackExecutorTimingDecorator.java | 25 +++++++++ .../task/slack/executor/SlackExecutor_1.java | 53 +++++++++++++++++++ 7 files changed, 177 insertions(+) create mode 100644 src/main/java/com/vincit/go/task/slack/executor/SlackExecutorComponent.java create mode 100644 src/main/java/com/vincit/go/task/slack/executor/SlackExecutorDecorator.java create mode 100644 src/main/java/com/vincit/go/task/slack/executor/SlackExecutorFactory_1.java create mode 100644 src/main/java/com/vincit/go/task/slack/executor/SlackExecutorInterface.java create mode 100644 src/main/java/com/vincit/go/task/slack/executor/SlackExecutorLogginDecorator.java create mode 100644 src/main/java/com/vincit/go/task/slack/executor/SlackExecutorTimingDecorator.java create mode 100644 src/main/java/com/vincit/go/task/slack/executor/SlackExecutor_1.java diff --git a/src/main/java/com/vincit/go/task/slack/executor/SlackExecutorComponent.java b/src/main/java/com/vincit/go/task/slack/executor/SlackExecutorComponent.java new file mode 100644 index 0000000..b70fc9c --- /dev/null +++ b/src/main/java/com/vincit/go/task/slack/executor/SlackExecutorComponent.java @@ -0,0 +1,20 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package com.vincit.go.task.slack.executor; + +import java.io.IOException; + +/** + * + * @author alexc + */ +public class SlackExecutorComponent implements SlackExecutorInterface{ + + @Override + public void sendMessage(TaskSlackMessage message) throws IOException { + + } + +} diff --git a/src/main/java/com/vincit/go/task/slack/executor/SlackExecutorDecorator.java b/src/main/java/com/vincit/go/task/slack/executor/SlackExecutorDecorator.java new file mode 100644 index 0000000..163f665 --- /dev/null +++ b/src/main/java/com/vincit/go/task/slack/executor/SlackExecutorDecorator.java @@ -0,0 +1,27 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package com.vincit.go.task.slack.executor; + +import java.io.IOException; + +/** + * + * @author alexc + */ +public class SlackExecutorDecorator implements SlackExecutorInterface{ + + + private SlackExecutorInterface component; + + public SlackExecutorDecorator(SlackExecutorInterface component){ + this.component=component; + } + @Override + public void sendMessage(TaskSlackMessage message) throws IOException { + component.sendMessage(message); + } + + +} diff --git a/src/main/java/com/vincit/go/task/slack/executor/SlackExecutorFactory_1.java b/src/main/java/com/vincit/go/task/slack/executor/SlackExecutorFactory_1.java new file mode 100644 index 0000000..df7f91b --- /dev/null +++ b/src/main/java/com/vincit/go/task/slack/executor/SlackExecutorFactory_1.java @@ -0,0 +1,9 @@ +package com.vincit.go.task.slack.executor; + +public class SlackExecutorFactory_1 { + + public SlackExecutor forDestination(TaskSlackDestination destination) { + return new SlackExecutor(destination); + } + +} diff --git a/src/main/java/com/vincit/go/task/slack/executor/SlackExecutorInterface.java b/src/main/java/com/vincit/go/task/slack/executor/SlackExecutorInterface.java new file mode 100644 index 0000000..b9e47a8 --- /dev/null +++ b/src/main/java/com/vincit/go/task/slack/executor/SlackExecutorInterface.java @@ -0,0 +1,15 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Interface.java to edit this template + */ +package com.vincit.go.task.slack.executor; + +import java.io.IOException; + +/** + * + * @author alexc + */ +public interface SlackExecutorInterface { + void sendMessage(TaskSlackMessage message) throws IOException; +} diff --git a/src/main/java/com/vincit/go/task/slack/executor/SlackExecutorLogginDecorator.java b/src/main/java/com/vincit/go/task/slack/executor/SlackExecutorLogginDecorator.java new file mode 100644 index 0000000..7c6347c --- /dev/null +++ b/src/main/java/com/vincit/go/task/slack/executor/SlackExecutorLogginDecorator.java @@ -0,0 +1,28 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package com.vincit.go.task.slack.executor; + +import java.io.IOException; + +/** + * + * @author alexc + */ + +public class SlackExecutorLogginDecorator extends SlackExecutorDecorator{ + private SlackExecutorDecorator component; + + public SlackExecutorLogginDecorator(SlackExecutorInterface component) { + super(component); + } + + + + @Override + public void sendMessage(TaskSlackMessage message) throws IOException { + component.sendMessage(message); + } + +} diff --git a/src/main/java/com/vincit/go/task/slack/executor/SlackExecutorTimingDecorator.java b/src/main/java/com/vincit/go/task/slack/executor/SlackExecutorTimingDecorator.java new file mode 100644 index 0000000..9c75c07 --- /dev/null +++ b/src/main/java/com/vincit/go/task/slack/executor/SlackExecutorTimingDecorator.java @@ -0,0 +1,25 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package com.vincit.go.task.slack.executor; + +import java.io.IOException; + +/** + * + * @author alexc + */ +public class SlackExecutorTimingDecorator extends SlackExecutorDecorator{ + private SlackExecutorInterface component; + public SlackExecutorTimingDecorator(SlackExecutorInterface component) { + super(component); + } + @Override + public void sendMessage(TaskSlackMessage message) throws IOException { + long startTime=System.currentTimeMillis(); + super.sendMessage(message); + long endTime=System.currentTimeMillis(); + System.out.println("Tiempo de EjecuciĆ³n de SlackExecutor"+(endTime - startTime)+ "ms"); + } +} diff --git a/src/main/java/com/vincit/go/task/slack/executor/SlackExecutor_1.java b/src/main/java/com/vincit/go/task/slack/executor/SlackExecutor_1.java new file mode 100644 index 0000000..1fe565e --- /dev/null +++ b/src/main/java/com/vincit/go/task/slack/executor/SlackExecutor_1.java @@ -0,0 +1,53 @@ +package com.vincit.go.task.slack.executor; + +import com.vincit.go.task.slack.model.ChannelType; +import com.vincit.go.task.slack.model.MarkdownField; +import in.ashwanthkumar.slack.webhook.Slack; +import in.ashwanthkumar.slack.webhook.SlackAttachment; + +import java.io.IOException; + +public class SlackExecutor_1 implements SlackExecutorInterface{ + + private Slack slack; + private TaskSlackDestination destination; + + SlackExecutor_1(Slack slack, TaskSlackDestination destination) { + this.slack = slack; + this.destination = destination; + } + + SlackExecutor_1(TaskSlackDestination destination) { + this.destination = destination; + this.slack = new Slack(destination.getWebhookUrl()); + } + SlackExecutor_1(){ + + } + + @Override + public void sendMessage(TaskSlackMessage message) throws IOException { + SlackAttachment attachment = new SlackAttachment(message.getMessage()) + .color(message.getColor()) + .title(message.getTitle()); + + for (MarkdownField field : message.getMarkdownIns()) { + attachment.addMarkdownIn(field.getApiValue()); + } + + updateChannel(destination); + + slack.displayName(message.getDisplayName()); + slack.icon(message.getIconOrEmoji()); + + slack.push(attachment); + } + + private void updateChannel(TaskSlackDestination destination) { + if (destination.getChannelType() == ChannelType.CHANNEL) { + slack.sendToChannel(destination.getChannel()); + } else { + slack.sendToUser(destination.getChannel()); + } + } +}