1   package org.itracker.selenium;
2   
3   import java.io.IOException;
4   import java.util.Iterator;
5   
6   import org.apache.log4j.Logger;
7   import org.itracker.model.User;
8   import org.itracker.persistence.dao.UserDAO;
9   import org.junit.Test;
10  
11  import com.dumbster.smtp.SimpleSmtpServer;
12  import com.dumbster.smtp.SmtpMessage;
13  
14  /**
15   * Verifies the functionality of new issue creation.
16   * 
17   * @author Andrey Sergievskiy <seas@andreysergievskiy.com>
18   */
19  public class CreateIssueTest extends AbstractSeleniumTestCase {
20  	
21  	private static final Logger log = Logger.getLogger(CreateIssueTest.class);
22  
23  	/**
24       * 1. Login into the system with some particular user
25       *    (admin_test1 in our case, which already has 4 issues, 2 of them
26       *     are unassigned).
27       * 2. Go to "Project List" page (by clicking "Project List" link).
28       * 3. Click "Create" link for selected project.
29       * 4. Fill issue fields with some data (we remember description
30       *    in our case, to check if new issue has appeared).
31       * 5. After submitting new issue, we are at "View Issues" page and
32       *    check if new issue has appeared.
33       * 6. Go to "Portal Home" page and check if new issue has appeared
34       *    in "Unassigned" area and "Created" area.
35       * @throws java.io.IOException
36       */
37      @Test
38      public void testCreateUnassignedIssue() throws IOException {
39          closeSession();
40  
41          final String descriptionValue = "Issue to be assigned.";
42          final String historyValue = "Issue to be assigned history.";
43          
44          selenium.open("http://" + applicationHost + ":" + applicationPort + "/"
45                  + applicationPath);
46          assertTrue(selenium.isElementPresent("//.[@name='login']"));
47          assertTrue(selenium.isElementPresent("//.[@name='password']"));
48          assertTrue(selenium.isElementPresent("//.[@value='Login']"));
49          selenium.type("//.[@name='login']", "admin_test1");
50          selenium.type("//.[@name='password']", "admin_test1");
51          selenium.click("//.[@value='Login']");
52          selenium.waitForPageToLoad(SE_TIMEOUT);
53          
54          // Click "Projects List".
55          selenium.click("listprojects");
56          selenium.waitForPageToLoad(SE_TIMEOUT);
57          
58          // Click issue creation link (usually it's named "Create").
59          assertTrue(selenium.isElementPresent("//tr[starts-with(@id, 'project.')]/td[3][text()='test_name']/../td[1]/a[2]"));
60          selenium.click("//tr[starts-with(@id, 'project.')]/td[3][text()='test_name']/../td[1]/a[2]");
61          selenium.waitForPageToLoad(SE_TIMEOUT);
62          
63          assertTrue(selenium.isElementPresent("description"));
64          assertTrue(selenium.isElementPresent("ownerId"));
65          assertTrue(selenium.isElementPresent("creatorId"));
66          assertTrue(selenium.isElementPresent("severity"));
67          assertTrue(selenium.isElementPresent("versions"));
68          
69          selenium.type("//td[@id='description']/input", descriptionValue);
70  		selenium.type("history", historyValue);
71  		
72          selenium.select("//td[@id='ownerId']/select", "value=-1");
73          final UserDAO userDao = (UserDAO)applicationContext.getBean("userDAO");
74          final User user = userDao.findByLogin("admin_test1");
75          assertTrue(null != user);
76          final long userId = user.getId();
77          selenium.select("//td[@id='creatorId']/select", "value=" + userId);
78          
79          final SimpleSmtpServer smtpServer = SimpleSmtpServer.start(2525);
80          
81          try {
82  	        selenium.click("//td[@id='submit']/input");
83  	        selenium.waitForPageToLoad(SE_TIMEOUT);
84  	        assertEquals("sent notifications", 1, smtpServer.getReceivedEmailSize());
85  	        final Iterator<SmtpMessage> iter =
86  	                (Iterator<SmtpMessage>)smtpServer.getReceivedEmail();
87  	        // Checking email notification for creator.
88  	        final SmtpMessage smtpMessage1 = iter.next();
89  	        log.info("testCreateUnassignedIssue, raw:\n" + smtpMessage1.getBody());
90  	        final String smtpMessageBody1 = smtpMessage1.getBody();
91  	        log.info("testCreateUnassignedIssue, received:\n" + smtpMessageBody1);
92  	        assertTrue(smtpMessageBody1.contains(descriptionValue));
93  	        assertTrue(smtpMessageBody1.contains(historyValue));
94          } finally {
95          	smtpServer.stop();
96          }
97          
98          
99          
100         // Check that the total number of issues is 5 now (4 from db + 1 our).
101         assertTrue(selenium.isElementPresent("issues"));
102         assertEquals(5, selenium.getXpathCount("//tr[starts-with(@id, 'issue.')]"));
103         assertTrue(selenium.isElementPresent("//tr[starts-with(@id, 'issue.')]/td[11][text()='" + descriptionValue + "']"));
104         
105         selenium.open("http://" + applicationHost + ":" + applicationPort + "/"
106                 + applicationPath + "/portalhome.do");
107         // Check that just created issue has appeared in "Unassigned" area.
108         assertTrue(selenium.isElementPresent("//tr[starts-with(@id,'unassignedIssue.')]/td[5][text()='test_name']/../td[11][text()='" + descriptionValue + "']"));
109         
110         // Check that just created issue has appeared in "Created" area.
111         assertTrue(selenium.isElementPresent("xpath=//tr[starts-with(@id,'createdIssue.')]/td[5][text()='test_name']/../td[11][text()='" + descriptionValue + "']"));
112         
113         // Check that number of watched items is 0.
114         assertEquals(0, selenium.getXpathCount("//tr[starts-with(@id, 'watchedIssue.')]"));        
115     }
116     
117     /**
118      * TODO
119      * @throws java.io.IOException
120      */
121     @Test
122     public void testCreateAssignedIssue() throws IOException {
123         closeSession();
124         
125         final String descriptionValue = "Issue to be unassigned.";
126         final String historyValue = "Issue to be unassigned history.";
127         
128         selenium.open("http://" + applicationHost + ":" + applicationPort + "/"
129                 + applicationPath);
130         assertTrue(selenium.isElementPresent("//.[@name='login']"));
131         assertTrue(selenium.isElementPresent("//.[@name='password']"));
132         assertTrue(selenium.isElementPresent("//.[@value='Login']"));
133         selenium.type("//.[@name='login']", "admin_test1");
134         selenium.type("//.[@name='password']", "admin_test1");
135         selenium.click("//.[@value='Login']");
136         selenium.waitForPageToLoad(SE_TIMEOUT);
137         
138         // Clicking "Project List" link.        
139         selenium.click("listprojects");
140         selenium.waitForPageToLoad(SE_TIMEOUT);
141         
142         // Click issue creation link (usually it's named "Create").
143         assertTrue(selenium.isElementPresent("//tr[starts-with(@id, 'project.')]/td[3][text()='test_name']/../td[1]/a[2]"));
144         selenium.click("//tr[starts-with(@id, 'project.')]/td[3][text()='test_name']/../td[1]/a[2]");
145         selenium.waitForPageToLoad(SE_TIMEOUT);
146         
147         assertTrue(selenium.isElementPresent("description"));
148         assertTrue(selenium.isElementPresent("ownerId"));
149         assertTrue(selenium.isElementPresent("creatorId"));
150         assertTrue(selenium.isElementPresent("severity"));
151         assertTrue(selenium.isElementPresent("versions"));
152 
153         
154 		selenium.type("//input[@name='description']", descriptionValue);
155 		selenium.type("history", historyValue);
156         final UserDAO userDao = (UserDAO)applicationContext.getBean("userDAO");
157         final User user = userDao.findByLogin("admin_test1");
158         assertTrue(null != user);
159         final long userId = user.getId();
160         selenium.select("//td[@id='ownerId']/select", "value=" + userId);        
161         selenium.select("//td[@id='creatorId']/select", "value=" + userId);
162         final SimpleSmtpServer smtpServer = SimpleSmtpServer.start(2525);
163         try {
164 	        selenium.click("//td[@id='submit']/input");
165 	        selenium.waitForPageToLoad(SE_TIMEOUT);
166 	        assertEquals("sent notifications", 2, smtpServer.getReceivedEmailSize());
167 	        final Iterator<SmtpMessage> iter =
168 	                (Iterator<SmtpMessage>)smtpServer.getReceivedEmail();
169 	        // Checking email notification for creator.
170 	        final SmtpMessage smtpMessage1 = iter.next();
171 	        log.info("testCreateAssignedIssue, raw:\n " + smtpMessage1.getBody());
172 	        final String smtpMessageBody1 = smtpMessage1.getBody();
173 	        log.info("testCreateAssignedIssue, received:\n " + smtpMessageBody1);
174 	        assertTrue(smtpMessageBody1.contains(descriptionValue));
175 	        assertTrue(smtpMessageBody1.contains(historyValue));
176 	        // Checking email notification for owner.
177 	        final SmtpMessage smtpMessage2 = iter.next();
178 	        final String smtpMessageBody2 = smtpMessage2.getBody();
179 	        assertTrue(smtpMessageBody2.contains(descriptionValue));
180 	        assertTrue(smtpMessageBody2.contains(historyValue));
181 		} finally {
182         	smtpServer.stop();
183         }
184         // Checking that our new issue has appeared in "View Issues".
185         assertTrue(selenium.isElementPresent("issues"));
186         assertEquals(5, selenium.getXpathCount("//tr[starts-with(@id, 'issue.')]"));
187         assertTrue(selenium.isElementPresent("//tr[starts-with(@id, 'issue.')]/td[11][text()='" + descriptionValue + "']"));
188         
189         selenium.open("http://" + applicationHost + ":" + applicationPort + "/"
190                 + applicationPath + "/portalhome.do");
191         // Checking that our new issue has not appeared in "Unassigned" area.
192         assertFalse(selenium.isElementPresent("//tr[starts-with(@id,'unassignedIssue.')]/td[5][text()='test_name']/../td[11][text()='" + descriptionValue + "']"));
193         
194         // Checking that our new issue has appeared in "Created" area.
195         assertTrue(selenium.isElementPresent("//tr[starts-with(@id,'createdIssue.')]/td[5][text()='test_name']/../td[11][text()='" + descriptionValue + "']"));
196         
197         // Check that "Watched" area is still empty.
198         assertEquals(0, selenium.getXpathCount("//tr[starts-with(@id, 'watchedIssue.')]"));
199     }
200     
201     @Override
202     protected String[] getDataSetFiles() {
203         return new String[]{
204                 "dataset/languagebean_init_dataset.xml",
205                 "dataset/languagebean_dataset.xml",
206                 "dataset/userpreferencesbean_dataset.xml",
207                 "dataset/userbean_dataset.xml",
208                 "dataset/projectbean_dataset.xml",
209                 "dataset/permissionbean_dataset.xml",
210                 "dataset/versionbean_dataset.xml",
211                 "dataset/issuebean_dataset.xml",
212                 "dataset/issueversionrel_dataset.xml",
213                 "dataset/issueactivitybean_dataset.xml",
214                 "dataset/issuehistorybean_dataset.xml"
215         };
216     }
217 
218    
219     @Override
220     protected String[] getConfigLocations() {
221         return new String[]{ "application-context.xml"};
222     }
223 }