diff --git a/mangle-services/src/main/java/com/vmware/mangle/services/NotifierService.java b/mangle-services/src/main/java/com/vmware/mangle/services/NotifierService.java index 22ce8a7c..5b89be87 100644 --- a/mangle-services/src/main/java/com/vmware/mangle/services/NotifierService.java +++ b/mangle-services/src/main/java/com/vmware/mangle/services/NotifierService.java @@ -14,8 +14,9 @@ import java.util.List; import java.util.stream.Collectors; -import allbegray.slack.webapi.SlackWebApiClient; -import allbegray.slack.webapi.method.chats.ChatPostMessageMethod; +import com.slack.api.methods.MethodsClient; +import com.slack.api.methods.request.chat.ChatPostMessageRequest; +import com.slack.api.methods.response.chat.ChatPostMessageResponse; import lombok.extern.log4j.Log4j2; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.lang.NonNull; @@ -151,18 +152,18 @@ private boolean hasChildTasks(Task task) { @SuppressWarnings("rawtypes") private void sendNotificationToChannel(Notifier notification, NotifierHelper helper) { SlackClient client = (SlackClient) clientFactory.getNotificationClient(notification); - SlackWebApiClient apiClient = client.getClient(); - ChatPostMessageMethod method = helper.populateSlackMessage(); + MethodsClient apiClient = client.getClient(); + ChatPostMessageRequest method = helper.populateSlackMessage(); method.setUsername(notification.getSlackInfo().getSenderName()); for (String channel : notification.getSlackInfo().getChannels()) { try { method.setChannel(channel); - apiClient.postMessage(method); + apiClient.chatPostMessage(method); } catch (Exception e) { log.error("Not able to send notification to slack : {}, causes : {}", notification.getName(), e.getMessage()); } } - client.shutdown(apiClient); + client.shutdown(); } } \ No newline at end of file diff --git a/mangle-services/src/main/java/com/vmware/mangle/services/helpers/slack/Color.java b/mangle-services/src/main/java/com/vmware/mangle/services/helpers/slack/Color.java new file mode 100644 index 00000000..8ef35fc7 --- /dev/null +++ b/mangle-services/src/main/java/com/vmware/mangle/services/helpers/slack/Color.java @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2016-2019 VMware, Inc. All Rights Reserved. + * + * This product is licensed to you under the Apache License, Version 2.0 (the "License"). + * You may not use this product except in compliance with the License. + * + * This product may include a number of subcomponents with separate copyright notices + * and license terms. Your use of these subcomponents is subject to the terms and + * conditions of the subcomponent's license, as noted in the LICENSE file. + */ + +package com.vmware.mangle.services.helpers.slack; + +public enum Color { + GOOD("#007a5a"), + WARNING("#daa038"), + DANGER("#e01e5a"); + + public final String hexValue; + + Color(String hexValue) { + this.hexValue = hexValue; + } +} diff --git a/mangle-services/src/main/java/com/vmware/mangle/services/helpers/slack/NotifierHelper.java b/mangle-services/src/main/java/com/vmware/mangle/services/helpers/slack/NotifierHelper.java index f4a7143a..408c3a99 100644 --- a/mangle-services/src/main/java/com/vmware/mangle/services/helpers/slack/NotifierHelper.java +++ b/mangle-services/src/main/java/com/vmware/mangle/services/helpers/slack/NotifierHelper.java @@ -14,10 +14,9 @@ import java.util.ArrayList; import java.util.List; -import allbegray.slack.type.Attachment; -import allbegray.slack.type.Color; -import allbegray.slack.type.Field; -import allbegray.slack.webapi.method.chats.ChatPostMessageMethod; +import com.slack.api.methods.request.chat.ChatPostMessageRequest; +import com.slack.api.model.Field; +import com.slack.api.model.Attachment; import com.vmware.mangle.cassandra.model.faults.specs.CommandExecutionFaultSpec; import com.vmware.mangle.cassandra.model.faults.specs.JVMAgentFaultSpec; @@ -49,14 +48,9 @@ public NotifierHelper(T task) { this.trigger = task.getTriggers().peek(); } - public ChatPostMessageMethod populateSlackMessage() { - ChatPostMessageMethod method = new ChatPostMessageMethod(DEFAULT_CHANNEL_NAME, - CommonConstants.SLACK_HELLO_MSG + commandExecutionFaultSpec.getEndpointName() + "*"); - method.setUsername(Constants.DEFAULT_SENDER_NAME); - method.setAs_user(false); - List attachments = method.getAttachments(); + public ChatPostMessageRequest populateSlackMessage() { + List attachments = new ArrayList<>(); Attachment attachment = new Attachment(); - attachments.add(attachment); updateAttachment(attachment); List fields = new ArrayList<>(); attachment.setFields(fields); @@ -70,17 +64,25 @@ public ChatPostMessageMethod populateSlackMessage() { updateFaultEndTime(fields); if (trigger.getTaskStatus().equals(TaskStatus.FAILED)) { addField(fields, CommonConstants.SLACK_MSG_FAULT_FAILURE_REASON, trigger.getTaskFailureReason()); - fields.get(fields.size() - 1).set_short(false); + fields.get(fields.size() - 1).setValueShortEnough(false); } addField(fields, CommonConstants.SLACK_MSG_FAULT_DESCRIPTION, task.getTaskDescription()); - fields.get(fields.size() - 1).set_short(false); - return method; + fields.get(fields.size() - 1).setValueShortEnough(false); + + attachment.setFallback(CommonConstants.SLACK_HELLO_MSG + commandExecutionFaultSpec.getEndpointName() + "*"); + attachments.add(attachment); + ChatPostMessageRequest chatPostMessageRequest = ChatPostMessageRequest.builder() + .username(Constants.DEFAULT_SENDER_NAME) + .text(CommonConstants.SLACK_HELLO_MSG + commandExecutionFaultSpec.getEndpointName() + "*") + .attachments(attachments).build(); + + return chatPostMessageRequest; } private void updateAttachment(Attachment attachment) { attachment.setTitle("Fault Summary"); attachment.setFooter("Thanks,\n Mangle"); - attachment.setColor(Color.WARNING); + attachment.setColor(Color.WARNING.hexValue); } private void addField(List fields, String title, String value) { diff --git a/mangle-services/src/test/java/com/vmware/mangle/services/mockdata/NotifierMockData.java b/mangle-services/src/test/java/com/vmware/mangle/services/mockdata/NotifierMockData.java index 47c6afb4..f0055b16 100644 --- a/mangle-services/src/test/java/com/vmware/mangle/services/mockdata/NotifierMockData.java +++ b/mangle-services/src/test/java/com/vmware/mangle/services/mockdata/NotifierMockData.java @@ -16,6 +16,7 @@ import java.util.Date; import java.util.HashSet; +import com.slack.api.methods.response.chat.ChatPostMessageResponse; import lombok.Getter; import com.vmware.mangle.cassandra.model.faults.specs.CommandExecutionFaultSpec; @@ -77,4 +78,12 @@ public Task getTaskForK8SFaultTriggerSpec(TaskType taskType task.getTaskData().getFaultSpec().setNotifierNames(new HashSet<>(Arrays.asList("mangle"))); return task; } + + public ChatPostMessageResponse getSlackChatPostMessageResponse(){ + ChatPostMessageResponse chatPostMessageResponse = new ChatPostMessageResponse(); + chatPostMessageResponse.setOk(true); + chatPostMessageResponse.setChannel("dev"); + chatPostMessageResponse.setTs("1645427127.335539"); + return chatPostMessageResponse; + } } diff --git a/mangle-services/src/test/java/com/vmware/mangle/unittest/services/service/NotifierServiceTest.java b/mangle-services/src/test/java/com/vmware/mangle/unittest/services/service/NotifierServiceTest.java index 1b814cd4..e0178db2 100644 --- a/mangle-services/src/test/java/com/vmware/mangle/unittest/services/service/NotifierServiceTest.java +++ b/mangle-services/src/test/java/com/vmware/mangle/unittest/services/service/NotifierServiceTest.java @@ -24,13 +24,16 @@ import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertTrue; +import java.io.IOException; import java.util.Arrays; import java.util.Collections; import java.util.HashSet; import java.util.Optional; -import allbegray.slack.webapi.SlackWebApiClient; -import allbegray.slack.webapi.method.chats.ChatPostMessageMethod; +import com.slack.api.methods.MethodsClient; +import com.slack.api.methods.SlackApiException; +import com.slack.api.methods.request.chat.ChatPostMessageRequest; +import com.slack.api.methods.response.chat.ChatPostMessageResponse; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.MockitoAnnotations; @@ -66,17 +69,19 @@ public class NotifierServiceTest extends PowerMockTestCase { @Mock private NotifierRepository repository; @Mock - private SlackWebApiClient slackWebApiClient; + private MethodsClient methodsClient; @InjectMocks private NotifierService notificationService; private NotifierMockData slackMockData; private Notifier slackInfo; + private ChatPostMessageResponse chatPostMessageResponse; @BeforeClass public void setUpBeforeClass() { MockitoAnnotations.initMocks(this); this.slackMockData = new NotifierMockData(); this.slackInfo = slackMockData.getSlackInfo("test"); + this.chatPostMessageResponse = slackMockData.getSlackChatPostMessageResponse(); } /** @@ -89,9 +94,9 @@ public void setUpBeforeClass() { public void testTestConnection() throws MangleException { SlackClient client = spy(new SlackClient(slackInfo)); when(clientFactory.getNotificationClient(any(Notifier.class))).thenReturn(client); - doReturn(slackWebApiClient).when(client).getClient(); + doReturn(methodsClient).when(client).getClient(); doReturn(true).when(client).testConnection(); - doNothing().when(client).shutdown(any(SlackWebApiClient.class)); + doNothing().when(client).shutdown(); assertTrue(notificationService.testConnection(slackInfo)); } @@ -231,19 +236,19 @@ public void testEnableSlacksForMangleRuntimeException() { */ @SuppressWarnings("unchecked") @Test - public void testSendNotification() { + public void testSendNotification() throws SlackApiException, IOException { Task task = slackMockData.getTask(TaskType.INJECTION, TaskStatus.COMPLETED); when(repository.findByNameIn(anyCollection())).thenReturn(Arrays.asList(slackInfo, slackInfo)); SlackClient client = spy(new SlackClient(slackInfo)); when(clientFactory.getNotificationClient(any(Notifier.class))).thenReturn(client); - doReturn(slackWebApiClient).when(client).getClient(); - when(slackWebApiClient.postMessage(any(ChatPostMessageMethod.class))).thenReturn("123"); - doNothing().when(client).shutdown(any(SlackWebApiClient.class)); + doReturn(methodsClient).when(client).getClient(); + when(methodsClient.chatPostMessage(any(ChatPostMessageRequest.class))).thenReturn(chatPostMessageResponse); + doNothing().when(client).shutdown(); notificationService.sendNotification(task); verify(repository, times(1)).findByNameIn(anyCollection()); verify(client, times(2)).getClient(); - verify(slackWebApiClient, times(2)).postMessage(any(ChatPostMessageMethod.class)); - verify(client, times(2)).shutdown(any(SlackWebApiClient.class)); + verify(methodsClient, times(2)).chatPostMessage(any(ChatPostMessageRequest.class)); + verify(client, times(2)).shutdown(); } /** @@ -252,21 +257,21 @@ public void testSendNotification() { */ @SuppressWarnings("unchecked") @Test - public void testSendNotificationForFailedTask() { + public void testSendNotificationForFailedTask() throws SlackApiException, IOException { Task task = slackMockData.getTask(TaskType.INJECTION, TaskStatus.FAILED); task.setTaskData(slackMockData.getFaultsMockData().getK8sCpuJvmAgentFaultSpec()); task.getTaskData().setNotifierNames(new HashSet<>(Arrays.asList("mangle1"))); when(repository.findByNameIn(anyCollection())).thenReturn(Arrays.asList(slackInfo, slackInfo)); SlackClient client = spy(new SlackClient(slackInfo)); when(clientFactory.getNotificationClient(any(Notifier.class))).thenReturn(client); - doReturn(slackWebApiClient).when(client).getClient(); - when(slackWebApiClient.postMessage(any(ChatPostMessageMethod.class))).thenReturn("123"); - doNothing().when(client).shutdown(any(SlackWebApiClient.class)); + doReturn(methodsClient).when(client).getClient(); + when(methodsClient.chatPostMessage(any(ChatPostMessageRequest.class))).thenReturn(chatPostMessageResponse); + doNothing().when(client).shutdown(); notificationService.sendNotification(task); verify(repository, times(1)).findByNameIn(anyCollection()); verify(client, times(2)).getClient(); - verify(slackWebApiClient, times(2)).postMessage(any(ChatPostMessageMethod.class)); - verify(client, times(2)).shutdown(any(SlackWebApiClient.class)); + verify(methodsClient, times(2)).chatPostMessage(any(ChatPostMessageRequest.class)); + verify(client, times(2)).shutdown(); } /** @@ -275,21 +280,21 @@ public void testSendNotificationForFailedTask() { */ @SuppressWarnings("unchecked") @Test - public void testSendNotificationForTimeoutNull() { + public void testSendNotificationForTimeoutNull() throws SlackApiException, IOException { Task task = slackMockData.getTask(TaskType.INJECTION, TaskStatus.FAILED); task.setTaskData(slackMockData.getFaultsMockData().getDockerPauseFaultSpec()); task.getTaskData().setNotifierNames(new HashSet<>(Arrays.asList("mangle"))); when(repository.findByNameIn(anyCollection())).thenReturn(Arrays.asList(slackInfo, slackInfo)); SlackClient client = spy(new SlackClient(slackInfo)); when(clientFactory.getNotificationClient(any(Notifier.class))).thenReturn(client); - doReturn(slackWebApiClient).when(client).getClient(); - when(slackWebApiClient.postMessage(any(ChatPostMessageMethod.class))).thenReturn("123"); - doNothing().when(client).shutdown(any(SlackWebApiClient.class)); + doReturn(methodsClient).when(client).getClient(); + when(methodsClient.chatPostMessage(any(ChatPostMessageRequest.class))).thenReturn(chatPostMessageResponse); + doNothing().when(client).shutdown(); notificationService.sendNotification(task); verify(repository, times(1)).findByNameIn(anyCollection()); verify(client, times(2)).getClient(); - verify(slackWebApiClient, times(2)).postMessage(any(ChatPostMessageMethod.class)); - verify(client, times(2)).shutdown(any(SlackWebApiClient.class)); + verify(methodsClient, times(2)).chatPostMessage(any(ChatPostMessageRequest.class)); + verify(client, times(2)).shutdown(); } /** @@ -298,21 +303,21 @@ public void testSendNotificationForTimeoutNull() { */ @SuppressWarnings("unchecked") @Test - public void testSendNotificationForRemediation() { + public void testSendNotificationForRemediation() throws SlackApiException, IOException { Task task = slackMockData.getTask(TaskType.REMEDIATION, TaskStatus.FAILED); task.setTaskData(slackMockData.getFaultsMockData().getK8SCPUFaultSpec()); task.getTaskData().setNotifierNames(new HashSet<>(Arrays.asList("mangle2"))); when(repository.findByNameIn(anyCollection())).thenReturn(Arrays.asList(slackInfo, slackInfo)); SlackClient client = spy(new SlackClient(slackInfo)); when(clientFactory.getNotificationClient(any(Notifier.class))).thenReturn(client); - doReturn(slackWebApiClient).when(client).getClient(); - when(slackWebApiClient.postMessage(any(ChatPostMessageMethod.class))).thenReturn("123"); - doNothing().when(client).shutdown(any(SlackWebApiClient.class)); + doReturn(methodsClient).when(client).getClient(); + when(methodsClient.chatPostMessage(any(ChatPostMessageRequest.class))).thenReturn(chatPostMessageResponse); + doNothing().when(client).shutdown(); notificationService.sendNotification(task); verify(repository, times(1)).findByNameIn(anyCollection()); verify(client, times(2)).getClient(); - verify(slackWebApiClient, times(2)).postMessage(any(ChatPostMessageMethod.class)); - verify(client, times(2)).shutdown(any(SlackWebApiClient.class)); + verify(methodsClient, times(2)).chatPostMessage(any(ChatPostMessageRequest.class)); + verify(client, times(2)).shutdown(); } /** @@ -321,20 +326,20 @@ public void testSendNotificationForRemediation() { */ @SuppressWarnings("unchecked") @Test - public void testSendNotificationForK8SFaultTriggerSpec() { + public void testSendNotificationForK8SFaultTriggerSpec() throws SlackApiException, IOException { Task task = slackMockData.getTaskForK8SFaultTriggerSpec(TaskType.REMEDIATION, TaskStatus.FAILED); when(repository.findByNameIn(anyCollection())).thenReturn(Arrays.asList(slackInfo, slackInfo)); SlackClient client = spy(new SlackClient(slackInfo)); when(clientFactory.getNotificationClient(any(Notifier.class))).thenReturn(client); - doReturn(slackWebApiClient).when(client).getClient(); - when(slackWebApiClient.postMessage(any(ChatPostMessageMethod.class))).thenReturn("xxx"); - doNothing().when(client).shutdown(any(SlackWebApiClient.class)); + doReturn(methodsClient).when(client).getClient(); + when(methodsClient.chatPostMessage(any(ChatPostMessageRequest.class))).thenReturn(chatPostMessageResponse); + doNothing().when(client).shutdown(); notificationService.sendNotification(task); verify(repository, times(1)).findByNameIn(anyCollection()); verify(client, times(2)).getClient(); - verify(slackWebApiClient, times(2)).postMessage(any(ChatPostMessageMethod.class)); - verify(client, times(2)).shutdown(any(SlackWebApiClient.class)); + verify(methodsClient, times(2)).chatPostMessage(any(ChatPostMessageRequest.class)); + verify(client, times(2)).shutdown(); } /** @@ -343,7 +348,7 @@ public void testSendNotificationForK8SFaultTriggerSpec() { */ @SuppressWarnings("unchecked") @Test - public void testSendNotificationForChildTasks() { + public void testSendNotificationForChildTasks() throws SlackApiException, IOException { Task task = slackMockData.getTaskForK8SFaultTriggerSpec(TaskType.REMEDIATION, TaskStatus.FAILED); TaskTrigger trigger = (TaskTrigger) task.getTriggers().peek(); @@ -351,9 +356,9 @@ public void testSendNotificationForChildTasks() { when(repository.findByNameIn(anyCollection())).thenReturn(Arrays.asList(slackInfo, slackInfo)); SlackClient client = spy(new SlackClient(slackInfo)); when(clientFactory.getNotificationClient(any(Notifier.class))).thenReturn(client); - doReturn(slackWebApiClient).when(client).getClient(); - when(slackWebApiClient.postMessage(any(ChatPostMessageMethod.class))).thenReturn("77978"); - doNothing().when(client).shutdown(any(SlackWebApiClient.class)); + doReturn(methodsClient).when(client).getClient(); + when(methodsClient.chatPostMessage(any(ChatPostMessageRequest.class))).thenReturn(chatPostMessageResponse); + doNothing().when(client).shutdown(); notificationService.sendNotification(task); } @@ -363,16 +368,16 @@ public void testSendNotificationForChildTasks() { */ @SuppressWarnings("unchecked") @Test - public void testSendNotificationForNoNotifier() { + public void testSendNotificationForNoNotifier() throws SlackApiException, IOException { Task task = slackMockData.getTaskForK8SFaultTriggerSpec(TaskType.REMEDIATION, TaskStatus.FAILED); task.getTaskData().setNotifierNames(null); when(repository.findByNameIn(anyCollection())).thenReturn(Arrays.asList(slackInfo, slackInfo)); SlackClient client = spy(new SlackClient(slackInfo)); when(clientFactory.getNotificationClient(any(Notifier.class))).thenReturn(client); - doReturn(slackWebApiClient).when(client).getClient(); - when(slackWebApiClient.postMessage(any(ChatPostMessageMethod.class))).thenReturn("xxx"); - doNothing().when(client).shutdown(any(SlackWebApiClient.class)); + doReturn(methodsClient).when(client).getClient(); + when(methodsClient.chatPostMessage(any(ChatPostMessageRequest.class))).thenReturn(chatPostMessageResponse); + doNothing().when(client).shutdown(); notificationService.sendNotification(task); } @@ -382,16 +387,16 @@ public void testSendNotificationForNoNotifier() { */ @SuppressWarnings("unchecked") @Test - public void testSendNotificationForTaskDataNull() { + public void testSendNotificationForTaskDataNull() throws SlackApiException, IOException { Task task = slackMockData.getTaskForK8SFaultTriggerSpec(TaskType.REMEDIATION, TaskStatus.FAILED); task.setTaskData(null); when(repository.findByNameIn(anyCollection())).thenReturn(Arrays.asList(slackInfo, slackInfo)); SlackClient client = spy(new SlackClient(slackInfo)); when(clientFactory.getNotificationClient(any(Notifier.class))).thenReturn(client); - doReturn(slackWebApiClient).when(client).getClient(); - when(slackWebApiClient.postMessage(any(ChatPostMessageMethod.class))).thenReturn("xxx"); - doNothing().when(client).shutdown(any(SlackWebApiClient.class)); + doReturn(methodsClient).when(client).getClient(); + when(methodsClient.chatPostMessage(any(ChatPostMessageRequest.class))).thenReturn(chatPostMessageResponse); + doNothing().when(client).shutdown(); notificationService.sendNotification(task); } } \ No newline at end of file diff --git a/mangle-utils/pom.xml b/mangle-utils/pom.xml index d8ce2156..b1c14f97 100644 --- a/mangle-utils/pom.xml +++ b/mangle-utils/pom.xml @@ -223,48 +223,12 @@ azure - com.github.allbegray - slack-api + com.slack.api + slack-api-client - io.netty - netty-codec-http - - - io.netty - netty-handler - - - io.netty - netty-handler-proxy - - - io.netty - - netty-transport-native-epoll - - - - io.netty - netty-buffer - - - io.netty - netty-codec-socks - - - io.netty - netty-resolver-dns - - - com.typesafe.netty - netty-reactive-streams - - - org.asynchttpclient - - async-http-client-netty-utils - + com.squareup.okhttp3 + okhttp diff --git a/mangle-utils/src/main/java/com/vmware/mangle/utils/notification/SlackClient.java b/mangle-utils/src/main/java/com/vmware/mangle/utils/notification/SlackClient.java index 09283982..e08c8599 100644 --- a/mangle-utils/src/main/java/com/vmware/mangle/utils/notification/SlackClient.java +++ b/mangle-utils/src/main/java/com/vmware/mangle/utils/notification/SlackClient.java @@ -11,8 +11,9 @@ package com.vmware.mangle.utils.notification; -import allbegray.slack.SlackClientFactory; -import allbegray.slack.webapi.SlackWebApiClient; +import com.slack.api.Slack; +import com.slack.api.methods.MethodsClient; +import com.slack.api.methods.request.auth.AuthTestRequest; import lombok.extern.log4j.Log4j2; import com.vmware.mangle.cassandra.model.slack.Notifier; @@ -29,37 +30,40 @@ public class SlackClient implements EndpointClient { private Notifier notification; + private Slack slack; public SlackClient(Notifier notifier) { this.notification = notifier; } - public SlackWebApiClient getClient() { - return SlackClientFactory.createWebApiClient(notification.getSlackInfo().getToken()); + public MethodsClient getClient() { + this.slack = Slack.getInstance(); + return slack.methods(notification.getSlackInfo().getToken()); } @Override public boolean testConnection() throws MangleException { - SlackWebApiClient client = null; + MethodsClient client = null; try { client = getClient(); - client.auth(); + AuthTestRequest authtestRequest = AuthTestRequest.builder().build(); + client.authTest(authtestRequest); return true; } catch (Exception e) { log.error(e.getMessage()); throw new MangleException(ErrorCode.NOTIFICATION_CONNECTION_FAILED, e.getMessage()); } finally { - shutdown(client); + shutdown(); } } - public void shutdown(SlackWebApiClient client) { + public void shutdown() { try { - if (client != null) { - client.shutdown(); + if (slack != null) { + slack.close(); } } catch (Exception e) { log.error(e.getMessage()); } } -} +} \ No newline at end of file diff --git a/mangle-utils/src/test/java/com/vmware/mangle/unittest/utils/notification/SlackClientTest.java b/mangle-utils/src/test/java/com/vmware/mangle/unittest/utils/notification/SlackClientTest.java index e8e1875e..fcc70c17 100644 --- a/mangle-utils/src/test/java/com/vmware/mangle/unittest/utils/notification/SlackClientTest.java +++ b/mangle-utils/src/test/java/com/vmware/mangle/unittest/utils/notification/SlackClientTest.java @@ -11,21 +11,22 @@ package com.vmware.mangle.unittest.utils.notification; +import static org.mockito.Matchers.any; import static org.mockito.Matchers.anyString; -import static org.mockito.Mockito.doNothing; -import static org.mockito.Mockito.doThrow; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; +import static org.mockito.Mockito.*; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertNotNull; import static org.testng.Assert.assertTrue; +import java.io.IOException; import java.util.Arrays; -import allbegray.slack.SlackClientFactory; -import allbegray.slack.type.Authentication; -import allbegray.slack.webapi.SlackWebApiClient; +import com.slack.api.Slack; +import com.slack.api.methods.MethodsClient; +import com.slack.api.methods.SlackApiException; +import com.slack.api.methods.request.auth.AuthTestRequest; +import com.slack.api.methods.response.auth.AuthTestResponse; +import com.vmware.mangle.utils.exceptions.handler.ErrorCode; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.powermock.api.mockito.PowerMockito; @@ -39,7 +40,6 @@ import com.vmware.mangle.cassandra.model.slack.NotifierType; import com.vmware.mangle.cassandra.model.slack.SlackInfo; import com.vmware.mangle.utils.exceptions.MangleException; -import com.vmware.mangle.utils.exceptions.handler.ErrorCode; import com.vmware.mangle.utils.notification.SlackClient; /** @@ -47,102 +47,115 @@ * * @author kumargautam */ -@PrepareForTest(value = { SlackClientFactory.class }) -@PowerMockIgnore({ "com.sun.org.apache.xalan.internal.xsltc.trax.*" }) +@PrepareForTest(value = { Slack.class }) +@PowerMockIgnore({"javax.net.ssl.*", "com.sun.org.apache.xalan.internal.xsltc.trax.*"}) public class SlackClientTest extends PowerMockTestCase { - @Mock - private SlackWebApiClient slackWebApiClient; + private MethodsClient methodsClient; private SlackClient slackClient; private Notifier notification; + private Slack slack; @BeforeClass public void setUpBeforeClass() { MockitoAnnotations.initMocks(this); - PowerMockito.mockStatic(SlackClientFactory.class); + PowerMockito.mockStatic(Slack.class); notification = getSlackInfo(); slackClient = new SlackClient(notification); } /** * Test method for - * {@link com.vmware.mangle.utils.notification.SlackClient#getClient(java.lang.String)}. + * {@link com.vmware.mangle.utils.notification.SlackClient#getClient()}. */ @Test public void testGetClient() { - PowerMockito.mockStatic(SlackClientFactory.class); - when(SlackClientFactory.createWebApiClient(anyString())).thenReturn(slackWebApiClient); + PowerMockito.mockStatic(Slack.class); + slack = PowerMockito.mock(Slack.class); + when(Slack.getInstance()).thenReturn(slack); + when(slack.methods(anyString())).thenReturn(methodsClient); assertNotNull(slackClient.getClient()); - PowerMockito.verifyStatic(SlackClientFactory.class, times(1)); - SlackClientFactory.createWebApiClient(anyString()); + PowerMockito.verifyStatic(Slack.class, times(1)); + slack.methods(anyString()); } /** * Test method for - * {@link com.vmware.mangle.utils.notification.SlackClient#testConnection(allbegray.slack.webapi.SlackWebApiClient)}. + * {@link com.vmware.mangle.utils.notification.SlackClient#testConnection()}. * * @throws MangleException */ @Test - public void testTestConnection() throws MangleException { - PowerMockito.mockStatic(SlackClientFactory.class); - when(SlackClientFactory.createWebApiClient(anyString())).thenReturn(slackWebApiClient); - when(slackWebApiClient.auth()).thenReturn(new Authentication()); + public void testTestConnection() throws Exception { + PowerMockito.mockStatic(Slack.class); + slack = PowerMockito.mock(Slack.class); + when(Slack.getInstance()).thenReturn(slack); + when(slack.methods(anyString())).thenReturn(methodsClient); + when(methodsClient.authTest(any(AuthTestRequest.class))).thenReturn(new AuthTestResponse()); assertTrue(slackClient.testConnection()); - PowerMockito.verifyStatic(SlackClientFactory.class, times(1)); - SlackClientFactory.createWebApiClient(anyString()); - verify(slackWebApiClient, times(1)).auth(); + slack.methods(anyString()); + verify(methodsClient, times(1)).authTest(any(AuthTestRequest.class)); + PowerMockito.verifyStatic(Slack.class, times(1)); } /** * Test method for - * {@link com.vmware.mangle.utils.notification.SlackClient#testConnection(allbegray.slack.webapi.SlackWebApiClient)}. + * {@link com.vmware.mangle.utils.notification.SlackClient#testConnection()}. * * @throws MangleException */ @Test - public void testTestConnectionForMangleException() throws MangleException { - PowerMockito.mockStatic(SlackClientFactory.class); - when(SlackClientFactory.createWebApiClient(anyString())).thenReturn(slackWebApiClient); - doThrow(new IllegalArgumentException("testTestConnectionForMangleException")).when(slackWebApiClient).auth(); + public void testTestConnectionForMangleException() throws MangleException, IOException, SlackApiException { + PowerMockito.mockStatic(Slack.class); + slack = PowerMockito.mock(Slack.class); + when(Slack.getInstance()).thenReturn(slack); + when(slack.methods(anyString())).thenReturn(methodsClient); + doThrow(new IllegalArgumentException("testTestConnectionForMangleException")).when(methodsClient) + .authTest(any(AuthTestRequest.class)); try { slackClient.testConnection(); } catch (MangleException e) { assertEquals(e.getErrorCode(), ErrorCode.NOTIFICATION_CONNECTION_FAILED); - PowerMockito.verifyStatic(SlackClientFactory.class, times(1)); - SlackClientFactory.createWebApiClient(anyString()); - verify(slackWebApiClient, times(1)).auth(); + slack.methods(anyString()); + verify(methodsClient, times(1)).authTest(any(AuthTestRequest.class)); + PowerMockito.verifyStatic(Slack.class, times(1)); } } /** * Test method for - * {@link com.vmware.mangle.utils.notification.SlackClient#shutdown(allbegray.slack.webapi.SlackWebApiClient)}. + * {@link com.vmware.mangle.utils.notification.SlackClient#shutdown()}. */ @Test - public void testShutdown() { - PowerMockito.mockStatic(SlackClientFactory.class); - when(SlackClientFactory.createWebApiClient(anyString())).thenReturn(slackWebApiClient); - doNothing().when(slackWebApiClient).shutdown(); - slackClient.shutdown(slackClient.getClient()); - PowerMockito.verifyStatic(SlackClientFactory.class, times(1)); - SlackClientFactory.createWebApiClient(anyString()); - verify(slackWebApiClient, times(1)).shutdown(); + public void testShutdown() throws Exception { + PowerMockito.mockStatic(Slack.class); + slack = PowerMockito.mock(Slack.class); + when(Slack.getInstance()).thenReturn(slack); + when(slack.methods(anyString())).thenReturn(methodsClient); + doNothing().when(slack).close(); + slackClient.getClient(); + slackClient.shutdown(); + slack.methods(anyString()); + verify(slack, times(1)).close(); + PowerMockito.verifyStatic(Slack.class, times(1)); } /** * Test method for - * {@link com.vmware.mangle.utils.notification.SlackClient#shutdown(allbegray.slack.webapi.SlackWebApiClient)}. + * {@link com.vmware.mangle.utils.notification.SlackClient#shutdown()}. */ @Test - public void testShutdownForException() { - PowerMockito.mockStatic(SlackClientFactory.class); - when(SlackClientFactory.createWebApiClient(anyString())).thenReturn(slackWebApiClient); - doThrow(new IllegalArgumentException("testShutdownForException")).when(slackWebApiClient).shutdown(); - slackClient.shutdown(slackClient.getClient()); - PowerMockito.verifyStatic(SlackClientFactory.class, times(1)); - SlackClientFactory.createWebApiClient(anyString()); - verify(slackWebApiClient, times(1)).shutdown(); + public void testShutdownForException() throws Exception { + PowerMockito.mockStatic(Slack.class); + slack = PowerMockito.mock(Slack.class); + when(Slack.getInstance()).thenReturn(slack); + when(slack.methods(anyString())).thenReturn(methodsClient); + doThrow(new IllegalArgumentException("testShutdownForException")).when(slack).close(); + slackClient.getClient(); + slackClient.shutdown(); + slack.methods(anyString()); + verify(slack, times(1)).close(); + PowerMockito.verifyStatic(Slack.class, times(1)); } private Notifier getSlackInfo() { diff --git a/pom.xml b/pom.xml index 5932c399..8879046f 100644 --- a/pom.xml +++ b/pom.xml @@ -104,7 +104,7 @@ 1.11.579 1.29.0 2.10.1 - 1.7.0.RELEASE + 1.20.0 2.1.9.0 1.15 1.3.1 @@ -838,43 +838,13 @@ ${log4j.version} - com.github.allbegray - slack-api - ${allbegray.slack-api.version} + com.slack.api + slack-api-client + ${slack.slack-api.version} - io.netty - netty-codec-http - - - io.netty - netty-handler - - - io.netty - netty-handler-proxy - - - io.netty - - netty-transport-native-epoll - - - - io.netty - netty-buffer - - - io.netty - netty-codec-socks - - - io.netty - netty-resolver-dns - - - com.typesafe.netty - netty-reactive-streams + com.squareup.okhttp3 + okhttp