You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FeedbackSenderTest.java 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Copyright (c) 2006-2015 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.ui.core.feedback;
  23. import com.dmdirc.events.StatusBarMessageEvent;
  24. import com.dmdirc.events.eventbus.EventBus;
  25. import com.dmdirc.config.provider.AggregateConfigProvider;
  26. import com.dmdirc.util.io.Downloader;
  27. import com.google.common.collect.ImmutableMap;
  28. import com.google.common.collect.Lists;
  29. import com.google.common.collect.Maps;
  30. import java.io.IOException;
  31. import java.util.Map;
  32. import org.junit.Before;
  33. import org.junit.Test;
  34. import org.junit.runner.RunWith;
  35. import org.mockito.ArgumentCaptor;
  36. import org.mockito.Captor;
  37. import org.mockito.Mock;
  38. import org.mockito.runners.MockitoJUnitRunner;
  39. import static junit.framework.TestCase.assertEquals;
  40. import static org.mockito.ArgumentMatchers.anyMap;
  41. import static org.mockito.ArgumentMatchers.anyString;
  42. import static org.mockito.Mockito.verify;
  43. import static org.mockito.Mockito.when;
  44. @RunWith(MockitoJUnitRunner.class)
  45. public class FeedbackSenderTest {
  46. private static final String NAME = "Bob Dole";
  47. private static final String EMAIL = "bob@dole.com";
  48. private static final String FEEDBACK = "DMDirc Rocks.";
  49. private static final String VERSION = "0.1";
  50. private static final String SERVER_INFO = "serverInfo";
  51. private static final String DMDIRC_INFO = "dmdircInfo";
  52. @Mock private AggregateConfigProvider config;
  53. @Mock private Downloader downloader;
  54. @Mock private EventBus eventBus;
  55. @Captor private ArgumentCaptor<StatusBarMessageEvent> messageEvent;
  56. private FeedbackSender feedbackSender;
  57. private FeedbackSenderFactory feedbackSenderFactory;
  58. private Map<String, String> expectedInfo;
  59. @Before
  60. public void setUp() throws Exception {
  61. feedbackSenderFactory = new FeedbackSenderFactory(config, downloader, eventBus);
  62. when(downloader.getPage(anyString(), anyMap())).thenReturn(Lists.newArrayList("Success."));
  63. expectedInfo = Maps.newHashMap(ImmutableMap.<String, String>builder()
  64. .put("name", NAME).put("email", EMAIL)
  65. .put("feedback", FEEDBACK)
  66. .put("version", VERSION)
  67. .put("serverInfo", SERVER_INFO)
  68. .put("dmdircInfo", DMDIRC_INFO)
  69. .build());
  70. }
  71. @Test
  72. public void testRun_WithAll() throws Exception {
  73. feedbackSender = feedbackSenderFactory.getFeedbackSender(NAME, EMAIL, FEEDBACK, VERSION,
  74. SERVER_INFO, DMDIRC_INFO);
  75. feedbackSender.run();
  76. verify(downloader).getPage("https://feedback.dmdirc.com/dialog/", expectedInfo);
  77. verify(eventBus).publishAsync(messageEvent.capture());
  78. assertEquals("Success.", messageEvent.getValue().getMessage().getMessage());
  79. }
  80. @Test
  81. public void testRun_WithoutName() throws Exception {
  82. feedbackSender = feedbackSenderFactory.getFeedbackSender("", EMAIL, FEEDBACK, VERSION,
  83. SERVER_INFO, DMDIRC_INFO);
  84. expectedInfo.remove("name");
  85. feedbackSender.run();
  86. verify(downloader).getPage("https://feedback.dmdirc.com/dialog/", expectedInfo);
  87. verify(eventBus).publishAsync(messageEvent.capture());
  88. assertEquals("Success.", messageEvent.getValue().getMessage().getMessage());
  89. }
  90. @Test
  91. public void testRun_WithoutEmail() throws Exception {
  92. feedbackSender = feedbackSenderFactory.getFeedbackSender(NAME, "", FEEDBACK, VERSION,
  93. SERVER_INFO, DMDIRC_INFO);
  94. expectedInfo.remove("email");
  95. feedbackSender.run();
  96. verify(downloader).getPage("https://feedback.dmdirc.com/dialog/", expectedInfo);
  97. verify(eventBus).publishAsync(messageEvent.capture());
  98. assertEquals("Success.", messageEvent.getValue().getMessage().getMessage());
  99. }
  100. @Test
  101. public void testRun_WithoutFeedback() throws Exception {
  102. feedbackSender = feedbackSenderFactory.getFeedbackSender(NAME, EMAIL, "", VERSION,
  103. SERVER_INFO, DMDIRC_INFO);
  104. expectedInfo.remove("feedback");
  105. feedbackSender.run();
  106. verify(downloader).getPage("https://feedback.dmdirc.com/dialog/", expectedInfo);
  107. verify(eventBus).publishAsync(messageEvent.capture());
  108. assertEquals("Success.", messageEvent.getValue().getMessage().getMessage());
  109. }
  110. @Test
  111. public void testRun_WithoutVersion() throws Exception {
  112. feedbackSender = feedbackSenderFactory.getFeedbackSender(NAME, EMAIL, FEEDBACK, "",
  113. SERVER_INFO, DMDIRC_INFO);
  114. expectedInfo.remove("version");
  115. feedbackSender.run();
  116. verify(downloader).getPage("https://feedback.dmdirc.com/dialog/", expectedInfo);
  117. verify(eventBus).publishAsync(messageEvent.capture());
  118. assertEquals("Success.", messageEvent.getValue().getMessage().getMessage());
  119. }
  120. @Test
  121. public void testRun_WithoutServerInfo() throws Exception {
  122. feedbackSender = feedbackSenderFactory.getFeedbackSender(NAME, EMAIL, FEEDBACK, VERSION,
  123. "", DMDIRC_INFO);
  124. expectedInfo.remove("serverInfo");
  125. feedbackSender.run();
  126. verify(downloader).getPage("https://feedback.dmdirc.com/dialog/", expectedInfo);
  127. verify(eventBus).publishAsync(messageEvent.capture());
  128. assertEquals("Success.", messageEvent.getValue().getMessage().getMessage());
  129. }
  130. @Test
  131. public void testRun_WithoutDMDircInfo() throws Exception {
  132. feedbackSender = feedbackSenderFactory.getFeedbackSender(NAME, EMAIL, FEEDBACK, VERSION,
  133. SERVER_INFO, "");
  134. expectedInfo.remove("dmdircInfo");
  135. feedbackSender.run();
  136. verify(downloader).getPage("https://feedback.dmdirc.com/dialog/", expectedInfo);
  137. verify(eventBus).publishAsync(messageEvent.capture());
  138. assertEquals("Success.", messageEvent.getValue().getMessage().getMessage());
  139. }
  140. @Test
  141. public void testRun_WithFail() throws Exception {
  142. when(downloader.getPage(anyString(), anyMap())).thenReturn(Lists.newArrayList());
  143. feedbackSender = feedbackSenderFactory.getFeedbackSender(NAME, EMAIL, FEEDBACK, VERSION,
  144. SERVER_INFO, DMDIRC_INFO);
  145. feedbackSender.run();
  146. verify(downloader).getPage("https://feedback.dmdirc.com/dialog/", expectedInfo);
  147. verify(eventBus).publishAsync(messageEvent.capture());
  148. assertEquals("Feedback failure: Unknown response from the server.",
  149. messageEvent.getValue().getMessage().getMessage());
  150. }
  151. @Test
  152. public void testRun_WithException() throws Exception {
  153. when(downloader.getPage(anyString(), anyMap())).thenThrow(new IOException("Fail."));
  154. feedbackSender = feedbackSenderFactory.getFeedbackSender(NAME, EMAIL, FEEDBACK, VERSION,
  155. SERVER_INFO, DMDIRC_INFO);
  156. feedbackSender.run();
  157. verify(downloader).getPage("https://feedback.dmdirc.com/dialog/", expectedInfo);
  158. verify(eventBus).publishAsync(messageEvent.capture());
  159. assertEquals("Feedback failure: Fail.", messageEvent.getValue().getMessage().getMessage());
  160. }
  161. }