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