View Javadoc
1   package org.itracker.services;
2   
3   import org.itracker.model.Issue;
4   import org.itracker.model.Notification;
5   import org.itracker.model.Notification.Role;
6   import org.itracker.model.Notification.Type;
7   import org.itracker.persistence.dao.IssueDAO;
8   import org.itracker.persistence.dao.NotificationDAO;
9   import org.itracker.persistence.dao.UserDAO;
10  import org.junit.Test;
11  
12  import javax.mail.internet.InternetAddress;
13  import java.util.List;
14  
15  import static org.itracker.Assert.*;
16  public class NotificationServiceIT extends AbstractServicesIntegrationTest {
17  
18      private NotificationService notificationService;
19  
20      // DAOs
21      private NotificationDAO notificationDAO;
22      private IssueDAO issueDAO;
23      private UserDAO userDAO;
24  
25      @Test
26      public void testSendNotification() {
27          try {
28              notificationService.sendNotification(issueDAO.findByPrimaryKey(1), Type.CREATED, "http://");
29          } catch (Exception e) {
30              fail(e.getMessage());
31          }
32  
33          try {
34              notificationService.sendNotification(notificationDAO.findById(1), Type.CREATED, "http://");
35          } catch (Exception e) {
36              fail(e.getMessage());
37          }
38  
39          try {
40              notificationService.sendNotification((Issue) null, Type.CREATED, "http://");
41          } catch (Exception e) {
42              fail(e.getMessage());
43          }
44  
45          try {
46              notificationService.sendNotification(
47                      issueDAO.findByPrimaryKey(1),
48                      Type.CREATED,
49                      "http://",
50                      new InternetAddress[]{},
51                      2);
52          } catch (Exception e) {
53              fail(e.getMessage());
54          }
55  
56      }
57  
58      @Test
59      public void testAddIssueNotification() {
60          Notification notification = new Notification(
61                  userDAO.findByPrimaryKey(2),
62                  issueDAO.findByPrimaryKey(1),
63                  Role.ANY
64          );
65          boolean added = notificationService.addIssueNotification(notification);
66          assertTrue("notification added", added);
67          assertNotNull("notification.id", notification.getId());
68  
69          notification = new Notification(
70                  userDAO.findByPrimaryKey(2),
71                  issueDAO.findByPrimaryKey(2),
72                  Role.ANY
73          );
74          added = notificationService.addIssueNotification(notification);
75          assertTrue("notification added", added);
76          assertNotNull("notification.id", notification.getId());
77      }
78  
79      @Test
80      public void testGetIssueNotifications() {
81          Issue issue1 = issueDAO.findByPrimaryKey(1);
82          Issue issue2 = issueDAO.findByPrimaryKey(2);
83  
84          //
85          // Issue 1
86          //
87          List<Notification> notifications = notificationService.getIssueNotifications(issue1);
88          assertNotNull(notifications);
89          assertEquals("notifications.size", 3, notifications.size()); // 1 in db + 2 primary (owner + creator)
90  
91          notifications = notificationService.getIssueNotifications(issue1, true, true);
92          assertNotNull(notifications);
93          // returns 1, see testPrimaryIssueNotifications()
94  //		assertEquals( "notifications.size", 2, notifications.size() ); // 2 primary (owner + creator)
95  
96          //
97          // Issue 2
98          //
99  
100         notifications = notificationService.getIssueNotifications(issue2);
101         assertNotNull(notifications);
102         assertEquals("notifications.size", 2, notifications.size());// 2 primary (owner + creator)
103 
104 
105     }
106 
107     @Test
108     //TODO: should be different owner and creator, projectowners
109     public void testPrimaryIssueNotifications() {
110         Issue issue = issueDAO.findByPrimaryKey(1);
111         assertEquals(0, issue.getProject().getOwners().size());
112         assertTrue(issue.getOwner() != null);
113         issueDAO.save(issue);
114 
115         List<Notification> notifications =
116                 notificationService.getPrimaryIssueNotifications(issue);
117         assertNotNull(notifications);
118         // only the owner
119         assertEquals("notifications.size", 1, notifications.size());
120 
121         assertEquals("notifications.user", notifications.get(0).getUser(), issue.getOwner());
122 
123 
124         issue.setOwner(null);
125         issueDAO.save(issue);
126 
127         assertTrue(issue.getOwner() == null);
128 
129         // only the creator
130         assertEquals("notifications.size", 1, notifications.size());
131 
132         assertEquals("notifications.user", notifications.get(0).getUser(), issue.getCreator());
133     }
134 
135     @Test
136     public void testHasIssueNotification() {
137 
138         Issue issue1 = issueDAO.findByPrimaryKey(1);
139 
140         // Passing null
141         assertFalse(notificationService.hasIssueNotification(null, 2));
142         assertFalse(notificationService.hasIssueNotification(issue1, (Integer)null));
143 
144         //
145         // Issue 1
146         //
147         boolean hasIssueNotification = notificationService.hasIssueNotification(issue1, 2);
148         assertTrue("issue 1, user 2, hasIssueNotification", hasIssueNotification);
149 
150         hasIssueNotification = notificationService.hasIssueNotification(issue1, 3);
151         assertFalse("issue 1, user 2, hasIssueNotification", hasIssueNotification);
152 
153         // Test with Role
154         hasIssueNotification = notificationService.hasIssueNotification(issue1, 2, Role.ANY);
155         assertTrue("issue 1, user 2, hasIssueNotification", hasIssueNotification);
156 
157         hasIssueNotification = notificationService.hasIssueNotification(issue1, 3, Role.ANY);
158         assertFalse("issue 1 user 3, hasIssueNotification", hasIssueNotification);
159     }
160 
161 
162     @Test
163     public void testRemoveIssueNotification() {
164         Notification notification = new Notification(
165                 userDAO.findByPrimaryKey(3),
166                 issueDAO.findByPrimaryKey(1),
167                 Role.ANY
168         );
169         notificationDAO.save(notification);
170         Integer id = notification.getId();
171         assertNotNull(id);
172 
173         boolean removed = notificationService.removeIssueNotification(id);
174         assertTrue("removed", removed);
175         assertNull("removed notification", notificationDAO.findById(id));
176     }
177 
178 
179     @Override
180     public void onSetUp() throws Exception {
181         super.onSetUp();
182         this.notificationService = (NotificationService) applicationContext.getBean("notificationService");
183         this.issueDAO = (IssueDAO) applicationContext.getBean("issueDAO");
184         this.userDAO = (UserDAO) applicationContext.getBean("userDAO");
185         this.notificationDAO = (NotificationDAO) applicationContext.getBean("notificationDAO");
186     }
187 
188     @Override
189     protected String[] getDataSetFiles() {
190         return new String[]{
191                 "dataset/userpreferencesbean_dataset.xml",
192                 "dataset/userbean_dataset.xml",
193                 "dataset/projectbean_dataset.xml",
194                 "dataset/versionbean_dataset.xml",
195                 "dataset/issuebean_dataset.xml",
196                 "dataset/notificationbean_dataset.xml",
197         };
198     }
199 
200 
201 }