1   package org.itracker.model;
2   import static org.itracker.Assert.assertEntityComparator;
3   import static org.itracker.Assert.assertEntityComparatorEquals;
4   import static org.junit.Assert.assertEquals;
5   import static org.junit.Assert.assertNotNull;
6   import static org.junit.Assert.assertTrue;
7   import static org.junit.Assert.fail;
8   
9   import org.itracker.model.Notification.Role;
10  import org.junit.After;
11  import org.junit.Before;
12  import org.junit.Test;
13  
14  public class NotificationTest {
15  	private Notification not;
16  	
17  	@Test
18  	public void testSetIssue(){
19  		try{
20  			not.setIssue(null);
21  			fail("did not throw IllegalArgumentException");
22  		} catch (IllegalArgumentException e){
23  			assertTrue(true);
24  		}
25  	}
26  	
27  	@Test
28  	public void testSetUser(){
29  		try{
30  			not.setUser(null);
31  			fail("did not throw IllegalArgumentException");
32  		} catch (IllegalArgumentException e){
33  			assertTrue(true);
34  		}
35  	}
36  	
37  	/**
38  	 * TODO remove method from Notification
39  	 */
40  	@SuppressWarnings("deprecation")
41  	@Test
42  	public void testSetNotificationRole(){
43  		not.setNotificationRole(1);
44  		assertEquals(1, not.getNotificationRole());
45  		not.setNotificationRole(10000);
46  		assertEquals(-1, not.getNotificationRole());
47  	}
48  	
49  	@Test
50  	public void testToString(){		
51  		assertNotNull("toString", not.toString());
52  	}
53  	
54  	@Test
55  	public void testUserComparator() {
56  		Notification entityA = new Notification(new User("aaa", "", "a", "a", "a@a.com", false), new Issue(), Role.ANY);
57  		Notification entityB = new Notification(new User("bbb", "", "b", "b", "b@b.com", false), new Issue(), Role.ANY);
58  
59  		assertEntityComparator("user comparator", Notification.USER_COMPARATOR, entityA, entityB);
60  		assertEntityComparator("user comparator", Notification.USER_COMPARATOR, entityA, null);
61  
62  		entityA.setUser(entityB.getUser());
63  		assertEntityComparatorEquals("user comparator", Notification.USER_COMPARATOR, entityA, entityB);
64  		assertEntityComparatorEquals("user comparator", Notification.USER_COMPARATOR, entityA, entityA);
65  		
66  	}
67  	
68  	@Test
69  	public void testIssueUserRoleComparator() {
70  		Issue issueA = new Issue();
71  		issueA.setId(1);
72  		Issue issueB = new Issue();
73  		issueB.setId(2);
74  		User userA = new User("aaa", "", "a", "a", "a@a.com", false);
75  		User userB = new User("bbb", "", "b", "b", "b@b.com", false);
76  		Notification entityA = new Notification(userA, issueA, Role.ANY);
77  		Notification entityB = new Notification(userA, issueB, Role.ANY);
78  
79  		assertEntityComparator("issue user role comparator", Notification.ISSUE_USER_ROLE_COMPARATOR, entityA, entityB);
80  		assertEntityComparator("issue user role comparator", Notification.ISSUE_USER_ROLE_COMPARATOR, entityA, null);
81  
82  		entityA.setIssue(entityB.getIssue());
83  		entityB.setUser(userB);
84  
85  		assertEntityComparator("issue user role comparator", Notification.ISSUE_USER_ROLE_COMPARATOR, entityA, entityB);
86  		assertEntityComparator("issue user role comparator", Notification.ISSUE_USER_ROLE_COMPARATOR, entityA, null);
87  		
88  		entityB.setUser(userA);
89  		entityB.setRole(Role.CREATOR);
90  
91  		assertEntityComparator("issue user role comparator", Notification.ISSUE_USER_ROLE_COMPARATOR, entityA, entityB);
92  		assertEntityComparator("issue user role comparator", Notification.ISSUE_USER_ROLE_COMPARATOR, entityA, null);
93  		
94  		entityA.setRole(entityB.getRole());
95  		assertEntityComparatorEquals("issue user role comparator", Notification.ISSUE_USER_ROLE_COMPARATOR, entityA, entityB);
96  		assertEntityComparatorEquals("issue user role comparator", Notification.ISSUE_USER_ROLE_COMPARATOR, entityA, entityA);
97  		
98  	}
99  	@Test
100 	public void testRoleComparator() {
101 		Issue issueA = new Issue();
102 		issueA.setId(1);
103 		Issue issueB = new Issue();
104 		issueB.setId(2);
105 
106 		User userA = new User("aaa", "", "a", "a", "a@a.com", false);
107 		User userB = new User("bbb", "", "b", "b", "b@b.com", false);
108 		Notification entityA = new Notification(userA, issueA, null);
109 		Notification entityB = new Notification(userB, issueA, Role.CREATOR);
110 
111 		assertEntityComparator("role comparator", Notification.TYPE_COMPARATOR, entityA, entityB);
112 		assertEntityComparator("role comparator", Notification.TYPE_COMPARATOR, entityA, null);
113 
114 		entityA.setRole(Role.ANY);
115 
116 		assertEntityComparator("role comparator", Notification.TYPE_COMPARATOR, entityA, entityB);
117 		assertEntityComparator("role comparator", Notification.TYPE_COMPARATOR, entityA, null);
118 		
119 		entityB.setRole(entityA.getRole());
120 
121 		assertEntityComparatorEquals("role comparator", Notification.TYPE_COMPARATOR, entityA, entityB);
122 		assertEntityComparatorEquals("role comparator", Notification.TYPE_COMPARATOR, entityA, entityA);
123 		
124 	}
125 	@Before
126     public void setUp() throws Exception {
127 		not = new Notification();
128     }
129 	
130 	@After
131 	public void tearDown() throws Exception {
132 		not = null;
133 	}
134 
135 }