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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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.interfaces.EventBus;
  25. import com.dmdirc.interfaces.config.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.Matchers.anyMapOf;
  41. import static org.mockito.Matchers.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(), anyMapOf(String.class, String.class)))
  63. .thenReturn(Lists.newArrayList("Success."));
  64. expectedInfo = Maps.newHashMap(ImmutableMap.<String, String>builder()
  65. .put("name", NAME).put("email", EMAIL)
  66. .put("feedback", FEEDBACK)
  67. .put("version", VERSION)
  68. .put("serverInfo", SERVER_INFO)
  69. .put("dmdircInfo", DMDIRC_INFO)
  70. .build());
  71. }
  72. @Test
  73. public void testRun_WithAll() throws Exception {
  74. feedbackSender = feedbackSenderFactory.getFeedbackSender(NAME, EMAIL, FEEDBACK, VERSION,
  75. SERVER_INFO, DMDIRC_INFO);
  76. feedbackSender.run();
  77. verify(downloader).getPage("https://feedback.dmdirc.com/dialog/", expectedInfo);
  78. verify(eventBus).publishAsync(messageEvent.capture());
  79. assertEquals("Success.", messageEvent.getValue().getMessage().getMessage());
  80. }
  81. @Test
  82. public void testRun_WithoutName() throws Exception {
  83. feedbackSender = feedbackSenderFactory.getFeedbackSender("", EMAIL, FEEDBACK, VERSION,
  84. SERVER_INFO, DMDIRC_INFO);
  85. expectedInfo.remove("name");
  86. feedbackSender.run();
  87. verify(downloader).getPage("https://feedback.dmdirc.com/dialog/", expectedInfo);
  88. verify(eventBus).publishAsync(messageEvent.capture());
  89. assertEquals("Success.", messageEvent.getValue().getMessage().getMessage());
  90. }
  91. @Test
  92. public void testRun_WithoutEmail() throws Exception {
  93. feedbackSender = feedbackSenderFactory.getFeedbackSender(NAME, "", FEEDBACK, VERSION,
  94. SERVER_INFO, DMDIRC_INFO);
  95. expectedInfo.remove("email");
  96. feedbackSender.run();
  97. verify(downloader).getPage("https://feedback.dmdirc.com/dialog/", expectedInfo);
  98. verify(eventBus).publishAsync(messageEvent.capture());
  99. assertEquals("Success.", messageEvent.getValue().getMessage().getMessage());
  100. }
  101. @Test
  102. public void testRun_WithoutFeedback() throws Exception {
  103. feedbackSender = feedbackSenderFactory.getFeedbackSender(NAME, EMAIL, "", VERSION,
  104. SERVER_INFO, DMDIRC_INFO);
  105. expectedInfo.remove("feedback");
  106. feedbackSender.run();
  107. verify(downloader).getPage("https://feedback.dmdirc.com/dialog/", expectedInfo);
  108. verify(eventBus).publishAsync(messageEvent.capture());
  109. assertEquals("Success.", messageEvent.getValue().getMessage().getMessage());
  110. }
  111. @Test
  112. public void testRun_WithoutVersion() throws Exception {
  113. feedbackSender = feedbackSenderFactory.getFeedbackSender(NAME, EMAIL, FEEDBACK, "",
  114. SERVER_INFO, DMDIRC_INFO);
  115. expectedInfo.remove("version");
  116. feedbackSender.run();
  117. verify(downloader).getPage("https://feedback.dmdirc.com/dialog/", expectedInfo);
  118. verify(eventBus).publishAsync(messageEvent.capture());
  119. assertEquals("Success.", messageEvent.getValue().getMessage().getMessage());
  120. }
  121. @Test
  122. public void testRun_WithoutServerInfo() throws Exception {
  123. feedbackSender = feedbackSenderFactory.getFeedbackSender(NAME, EMAIL, FEEDBACK, VERSION,
  124. "", DMDIRC_INFO);
  125. expectedInfo.remove("serverInfo");
  126. feedbackSender.run();
  127. verify(downloader).getPage("https://feedback.dmdirc.com/dialog/", expectedInfo);
  128. verify(eventBus).publishAsync(messageEvent.capture());
  129. assertEquals("Success.", messageEvent.getValue().getMessage().getMessage());
  130. }
  131. @Test
  132. public void testRun_WithoutDMDircInfo() throws Exception {
  133. feedbackSender = feedbackSenderFactory.getFeedbackSender(NAME, EMAIL, FEEDBACK, VERSION,
  134. SERVER_INFO, "");
  135. expectedInfo.remove("dmdircInfo");
  136. feedbackSender.run();
  137. verify(downloader).getPage("https://feedback.dmdirc.com/dialog/", expectedInfo);
  138. verify(eventBus).publishAsync(messageEvent.capture());
  139. assertEquals("Success.", messageEvent.getValue().getMessage().getMessage());
  140. }
  141. @Test
  142. public void testRun_WithFail() throws Exception {
  143. when(downloader.getPage(anyString(), anyMapOf(String.class, String.class)))
  144. .thenReturn(Lists.newArrayList());
  145. feedbackSender = feedbackSenderFactory.getFeedbackSender(NAME, EMAIL, FEEDBACK, VERSION,
  146. SERVER_INFO, DMDIRC_INFO);
  147. feedbackSender.run();
  148. verify(downloader).getPage("https://feedback.dmdirc.com/dialog/", expectedInfo);
  149. verify(eventBus).publishAsync(messageEvent.capture());
  150. assertEquals("Feedback failure: Unknown response from the server.",
  151. messageEvent.getValue().getMessage().getMessage());
  152. }
  153. @Test
  154. public void testRun_WithException() throws Exception {
  155. when(downloader.getPage(anyString(), anyMapOf(String.class, String.class)))
  156. .thenThrow(new IOException("Fail."));
  157. feedbackSender = feedbackSenderFactory.getFeedbackSender(NAME, EMAIL, FEEDBACK, VERSION,
  158. SERVER_INFO, DMDIRC_INFO);
  159. feedbackSender.run();
  160. verify(downloader).getPage("https://feedback.dmdirc.com/dialog/", expectedInfo);
  161. verify(eventBus).publishAsync(messageEvent.capture());
  162. assertEquals("Feedback failure: Fail.", messageEvent.getValue().getMessage().getMessage());
  163. }
  164. }