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