PageRenderTime 44ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/src/test/java/it/com/burningcode/jira/plugin/JIRAComponentWatcherIntegrationTest.java

https://bitbucket.org/badazzhindu/jira-component-watcher-plugin
Java | 337 lines | 174 code | 61 blank | 102 comment | 5 complexity | 272ac6430ccdfc27df3895563dd921c1 MD5 | raw file
  1. /* Copyright (c) 2008, 2009, Ray Barham
  2. * All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. * * Redistributions of source code must retain the above copyright
  7. * notice, this list of conditions and the following disclaimer.
  8. * * Redistributions in binary form must reproduce the above copyright
  9. * notice, this list of conditions and the following disclaimer in the
  10. * documentation and/or other materials provided with the distribution.
  11. * * Neither the name of the project nor the
  12. * names of its contributors may be used to endorse or promote products
  13. * derived from this software without specific prior written permission.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY Ray Barham ''AS IS'' AND ANY
  16. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL Ray Barham BE LIABLE FOR ANY
  19. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  22. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. package it.com.burningcode.jira.plugin;
  27. import static it.com.burningcode.jira.IntegrationTestHelper.COMPONENT_ART_ID;
  28. import static it.com.burningcode.jira.IntegrationTestHelper.COMPONENT_CODE_ID;
  29. import static it.com.burningcode.jira.IntegrationTestHelper.COMPONENT_OTHER_ID;
  30. import static it.com.burningcode.jira.IntegrationTestHelper.COMPONENT_WATCHERS_LINK;
  31. import static it.com.burningcode.jira.IntegrationTestHelper.PLUGIN_KEY;
  32. import static it.com.burningcode.jira.IntegrationTestHelper.PROJECT_NAME;
  33. import java.io.FileNotFoundException;
  34. import java.io.IOException;
  35. import java.net.BindException;
  36. import java.util.ArrayList;
  37. import java.util.HashMap;
  38. import java.util.Map;
  39. import javax.mail.MessagingException;
  40. import org.xml.sax.SAXException;
  41. import com.atlassian.jira.webtests.EmailFuncTestCase;
  42. import com.atlassian.jira.webtests.JIRAServerSetup;
  43. import com.meterware.httpunit.WebForm;
  44. /**
  45. * Integration test for the JIRA Component Watcher Plugin.
  46. *
  47. * TODO: Add test for adding user watchers in the group area and group watchers in the user area
  48. *
  49. * @author Ray Barham
  50. */
  51. public class JIRAComponentWatcherIntegrationTest extends EmailFuncTestCase {
  52. @Override
  53. protected void configureAndStartSmtpServer() {
  54. try {
  55. mailService.configureAndStartGreenMail(JIRAServerSetup.SMTP);
  56. } catch (BindException e) {
  57. fail("Error configuring and starting greenmail server");
  58. }
  59. assertTrue("Mail server not running.", mailService.isRunning());
  60. navigation.gotoResource("OutgoingMailServers.jspa");
  61. if (tester.getDialog().isLinkPresent("deleteSMTP")) {
  62. tester.clickLink("deleteSMTP");
  63. tester.submit("Delete");
  64. }
  65. tester.clickLinkWithText("Configure new SMTP mail server");
  66. tester.setFormElement("name", "Local Test Server");
  67. tester.setFormElement("from", EmailFuncTestCase.DEAFULT_FROM_ADDRESS);
  68. tester.setFormElement("prefix", EmailFuncTestCase.DEFAULT_SUBJECT_PREFIX);
  69. tester.setFormElement("serverName", "localhost");
  70. String port = String.valueOf(mailService.getSmtpPort());
  71. log("Setting SMTP server to 'localhost:" + port + "'");
  72. tester.setFormElement("port", port);
  73. tester.submit("Add");
  74. tester.assertLinkNotPresentWithText("Configure new SMTP mail server");
  75. tester.assertTextPresent("Local Test Server");
  76. }
  77. protected WebForm getFormByName(String formName){
  78. WebForm[] forms = form.getForms();
  79. WebForm watcherForm = null;
  80. for(WebForm form : forms){
  81. log.log(form.getName());
  82. if(form.getName().equals(formName))
  83. return form;
  84. }
  85. if(watcherForm == null)
  86. fail("No form found with name " + formName);
  87. return null;
  88. }
  89. protected void gotoProjectComponentWatchers(String project){
  90. administration.project().viewProject(project);
  91. navigation.clickLinkWithExactText(COMPONENT_WATCHERS_LINK);
  92. }
  93. @Override
  94. protected void setUpTest() {
  95. super.setUpTest();
  96. }
  97. /**
  98. * Tests accessing the component watchers page via the "Edit Component Watchers" link
  99. * @throws FileNotFoundException
  100. */
  101. public void testAccessingComponentWatchersPage() {
  102. administration.restoreData("WithoutComponentWatchers.zip");
  103. administration.project().viewProject(PROJECT_NAME);
  104. tester.assertLinkPresentWithText(COMPONENT_WATCHERS_LINK);
  105. navigation.clickLinkWithExactText(COMPONENT_WATCHERS_LINK);
  106. assertions.getTextAssertions().assertTextPresent("JIRA Component Watcher - Test");
  107. }
  108. /**
  109. * Tests adding users and groups as watchers to a component.
  110. * @throws SAXException
  111. * @throws IOException
  112. */
  113. public void testAddingComponentWatchers() throws IOException, SAXException {
  114. administration.restoreData("WithoutComponentWatchers.zip");
  115. gotoProjectComponentWatchers(PROJECT_NAME);
  116. // Test adding users
  117. tester.clickLink("editUserWatchers_" + COMPONENT_ART_ID);
  118. assertions.getTextAssertions().assertTextPresent("Add Users");
  119. tester.getDialog().setFormParameter("watcherField", ADMIN_USERNAME);
  120. tester.getDialog().submit("add_watchers");
  121. assertions.getTextAssertions().assertTextPresent(ADMIN_FULLNAME + " (" + ADMIN_USERNAME + ")");
  122. tester.assertFormElementPresent("removeWatcher_" + ADMIN_USERNAME);
  123. tester.clickLink("return_link");
  124. tester.assertElementPresent("component_" + COMPONENT_ART_ID + "_user_watcher_" + ADMIN_USERNAME);
  125. // Test adding groups
  126. tester.clickLink("editGroupWatchers_" + COMPONENT_CODE_ID);
  127. assertions.getTextAssertions().assertTextPresent("Add Groups");
  128. form.selectOptionsByValue("groupWatchers[]", new String[]{JIRA_DEV_GROUP, JIRA_ADMIN_GROUP});
  129. tester.getDialog().submit("add_watchers");
  130. assertions.getTextAssertions().assertTextPresent(JIRA_DEV_GROUP);
  131. tester.assertFormElementPresent("removeWatcher_" + JIRA_DEV_GROUP);
  132. assertions.getTextAssertions().assertTextPresent(JIRA_ADMIN_GROUP);
  133. tester.assertFormElementPresent("removeWatcher_" + JIRA_ADMIN_GROUP);
  134. tester.clickLink("return_link");
  135. tester.assertElementPresent("component_" + COMPONENT_CODE_ID + "_group_watcher_" + JIRA_DEV_GROUP);
  136. tester.assertElementPresent("component_" + COMPONENT_CODE_ID + "_group_watcher_" + JIRA_ADMIN_GROUP);
  137. }
  138. public void testAddingDuplicateComponentWatchers(){
  139. administration.restoreData("WithComponentWatchers.zip");
  140. gotoProjectComponentWatchers(PROJECT_NAME);
  141. // Test adding users with a user already added
  142. tester.clickLink("editUserWatchers_" + COMPONENT_ART_ID);
  143. tester.getDialog().setFormParameter("watcherField", ADMIN_USERNAME + "," + BOB_USERNAME);
  144. tester.getDialog().submit("add_watchers");
  145. assertions.getTextAssertions().assertTextPresent(""" + ADMIN_USERNAME + "" is already a watcher on this component.");
  146. // Make sure the other user was adding along side the duplicate user
  147. assertions.getTextAssertions().assertTextPresent(ADMIN_FULLNAME + " (" + ADMIN_USERNAME + ")");
  148. tester.assertFormElementPresent("removeWatcher_" + ADMIN_USERNAME);
  149. assertions.getTextAssertions().assertTextPresent(BOB_FULLNAME + " (" + BOB_USERNAME + ")");
  150. tester.assertFormElementPresent("removeWatcher_" + BOB_USERNAME);
  151. tester.clickLink("return_link");
  152. tester.assertElementPresent("component_" + COMPONENT_ART_ID + "_user_watcher_" + ADMIN_USERNAME);
  153. tester.assertElementPresent("component_" + COMPONENT_ART_ID + "_user_watcher_" + BOB_USERNAME);
  154. }
  155. public void testEnableComponentWatchers(){
  156. administration.restoreData("WithoutComponentWatchers.zip");
  157. assertTrue(administration.plugins().isPluginEnabled(PLUGIN_KEY));
  158. administration.plugins().disablePlugin(PLUGIN_KEY);
  159. assertTrue(administration.plugins().isPluginDisabled(PLUGIN_KEY));
  160. }
  161. /**
  162. * Tests the notifications with notification type specified
  163. * @throws BindException
  164. * @throws InterruptedException
  165. * @throws IOException
  166. * @throws MessagingException
  167. */
  168. public void testNotificationsWithNotificationType() throws InterruptedException, MessagingException, IOException {
  169. administration.restoreData("WithComponentWatchers.zip");
  170. configureAndStartSmtpServer();
  171. // Made user a component watcher
  172. gotoProjectComponentWatchers(PROJECT_NAME);
  173. tester.clickLink("editUserWatchers_" + COMPONENT_OTHER_ID);
  174. tester.getDialog().setFormParameter("watcherField", BOB_USERNAME);
  175. tester.getDialog().submit("add_watchers");
  176. // Update the notification scheme
  177. navigation.gotoResource("ViewNotificationSchemes.jspa");
  178. tester.clickLinkWithText("Default Notification Scheme");
  179. tester.clickLink("add_1");
  180. // Verify that the component watcher notification type exists
  181. tester.assertRadioOptionValuePresent("type", "Component_Watchers");
  182. tester.checkRadioOption("type", "Component_Watchers");
  183. tester.submit("Add");
  184. // Create issue that should be received by two users
  185. String subject = "Test issue with notification set";
  186. Map<String, String[]> params = new HashMap<String, String[]>();
  187. params.put("components", new String[]{String.valueOf(COMPONENT_OTHER_ID)});
  188. navigation.issue().createIssue(PROJECT_NAME, ISSUE_TYPE_BUG, subject, params);
  189. flushMailQueueAndWait(2);
  190. // Check that only email was sent to admin and bob
  191. assertCorrectNumberEmailsSent(2);
  192. ArrayList<String> recipients = new ArrayList<String>();
  193. recipients.add(ADMIN_EMAIL);
  194. recipients.add(BOB_EMAIL);
  195. assertRecipientsHaveMessages(recipients);
  196. }
  197. /**
  198. * Test removing all users and groups using the "All" checkbox
  199. *
  200. * TODO: Uses javascript to select all watcher. Find a way to do integration test w/ javascript.
  201. */
  202. /*
  203. public void testRemoveAllCheckbox() {
  204. testAddingComponentWatchers();
  205. WebLink[] links = page.getLinksWithExactText("Edit");
  206. log.log("Found "+links.length+" 'Edit' links.");
  207. for(WebLink link : links){
  208. log.log("Clicking link: "+link.asText());
  209. navigation.clickLink(link);
  210. tester.checkCheckbox("removeAllWatchers");
  211. tester.getDialog().submit("remove_watchers");
  212. if(link.getID().startsWith("editUserWatchers_")){
  213. log.log("editUserWatchers");
  214. assertFalse("Admin still found after deletion", tester.getDialog().hasFormParameterNamed("removeWatcher_"+ADMIN));
  215. assertFalse("Bob still found after deletion", tester.getDialog().hasFormParameterNamed("removeWatcher_"+USER_BOB));
  216. }else if(link.getID().startsWith("editGroupWatchers_")){
  217. log.log("editGroupWatchers");
  218. assertFalse("jira-users still found after deletion", tester.getDialog().hasFormParameterNamed("removeWatcher_jira-users"));
  219. assertFalse("jira-developers still found after deletion", tester.getDialog().hasFormParameterNamed("removeWatcher_jira-developers"));
  220. }
  221. tester.clickLink("return_link");
  222. }
  223. }*/
  224. /**
  225. * Tests the notifications without notification type specifid
  226. * @throws BindException
  227. * @throws InterruptedException
  228. * @throws IOException
  229. * @throws MessagingException
  230. */
  231. public void testNotificationsWithoutNotificationType() throws InterruptedException, MessagingException, IOException {
  232. administration.restoreData("WithComponentWatchers.zip");
  233. configureAndStartSmtpServer();
  234. // Made user a component watcher
  235. gotoProjectComponentWatchers(PROJECT_NAME);
  236. tester.clickLink("editUserWatchers_" + COMPONENT_OTHER_ID);
  237. tester.getDialog().setFormParameter("watcherField", BOB_USERNAME);
  238. tester.getDialog().submit("add_watchers");
  239. // Create issue that should only be received by one user
  240. String subject = "Test issue without notification set";
  241. Map<String, String[]> params = new HashMap<String, String[]>();
  242. params.put("components", new String[]{String.valueOf(COMPONENT_OTHER_ID)});
  243. navigation.issue().createIssue(PROJECT_NAME, ISSUE_TYPE_BUG, subject, params);
  244. flushMailQueueAndWait(1);
  245. // Check that only email was sent to admin
  246. assertCorrectNumberEmailsSent(1);
  247. ArrayList<String> recipients = new ArrayList<String>();
  248. recipients.add(ADMIN_EMAIL);
  249. assertRecipientsHaveMessages(recipients);
  250. }
  251. /**
  252. * Tests removing a user and group as watchers to a component.
  253. * @throws SAXException
  254. * @throws IOException
  255. */
  256. public void testRemovingComponentWatchers() throws IOException, SAXException {
  257. administration.restoreData("WithComponentWatchers.zip");
  258. gotoProjectComponentWatchers(PROJECT_NAME);
  259. tester.clickLink("editUserWatchers_" + COMPONENT_ART_ID);
  260. WebForm watcherForm = getFormByName("deleteWatchersForm");
  261. watcherForm.setCheckbox("removeWatcher_" + ADMIN_USERNAME, true);
  262. watcherForm.submit();
  263. tester.clickLink("return_link");
  264. tester.assertElementNotPresent("component_" + COMPONENT_ART_ID + "_user_watcher_" + ADMIN_USERNAME);
  265. tester.clickLink("editGroupWatchers_" + COMPONENT_CODE_ID);
  266. watcherForm = getFormByName("deleteWatchersForm");
  267. watcherForm.setCheckbox("removeWatcher_" + JIRA_ADMIN_GROUP, true);
  268. watcherForm.submit();
  269. tester.clickLink("return_link");
  270. tester.assertElementPresent("component_" + COMPONENT_CODE_ID + "_group_watcher_" + JIRA_DEV_GROUP);
  271. tester.assertElementNotPresent("component_" + COMPONENT_CODE_ID + "_group_watcher_" + JIRA_ADMIN_GROUP);
  272. }
  273. }