View Javadoc

1   /**
2    * Copyright 2011-2013 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   */
15  package org.kuali.mobility.notification.dao;
16  
17  import org.apache.log4j.Logger;
18  import org.junit.AfterClass;
19  import org.junit.Before;
20  import org.junit.BeforeClass;
21  import org.junit.Test;
22  import org.junit.runner.RunWith;
23  import org.kuali.mobility.notification.entity.Notification;
24  import org.kuali.mobility.notification.entity.UserNotification;
25  import org.unitils.UnitilsJUnit4TestClassRunner;
26  import org.unitils.database.annotations.Transactional;
27  import org.unitils.database.util.TransactionMode;
28  import org.unitils.orm.jpa.JpaUnitils;
29  import org.unitils.orm.jpa.annotation.JpaEntityManagerFactory;
30  
31  import javax.persistence.EntityManagerFactory;
32  import javax.persistence.PersistenceUnit;
33  import java.sql.Timestamp;
34  import java.util.Calendar;
35  import java.util.Date;
36  import java.util.List;
37  
38  import static org.junit.Assert.assertTrue;
39  
40  /**
41   * @author Kuali Mobility Team (mobility.collab@kuali.org)
42   */
43  @RunWith(UnitilsJUnit4TestClassRunner.class)
44  @JpaEntityManagerFactory(persistenceUnit="mdot")
45  public class NotificationDaoImplTest {
46      private static final Logger LOG = Logger.getLogger(NotificationDaoImplTest.class);
47  
48      @PersistenceUnit
49      private EntityManagerFactory entityManagerFactory;
50  
51      private NotificationDao dao;
52  
53      @BeforeClass
54      public static void setUpClass() throws Exception {
55      }
56  
57      @AfterClass
58      public static void tearDownClass() throws Exception {
59      }
60  
61      @Before
62      public void preTest() {
63          setDao(new NotificationDaoImpl());
64          JpaUnitils.injectEntityManagerInto(getDao());
65      }
66  
67      @Test
68      @Transactional(TransactionMode.ROLLBACK)
69      public void testNotificationTransactions() {
70          Notification n1 = new Notification();
71          n1.setTitle("Test Title 1");
72          n1.setMessage("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec tempus massa et dapibus ullamcorper. Donec porta adipiscing dui vitae ullamcorper.");
73          n1.setNotificationType(Long.valueOf(42));
74          n1.setPrimaryCampus("ALL");
75          Calendar cal = Calendar.getInstance();
76          n1.setStartDate(new Timestamp( cal.getTimeInMillis()));
77          cal.add(Calendar.DATE,1);
78          n1.setEndDate(new Timestamp(cal.getTimeInMillis()));
79  
80          assertTrue("Noticiation 1 ID is not null.",n1.getNotificationId()==null);
81  
82          Long n1ID = getDao().saveNotification(n1);
83  
84          LOG.debug("Notification 1 ID is: "+n1ID);
85          assertTrue("Notification 1 not saved.", n1ID != null);
86  
87          Notification n2 = new Notification();
88          n2.setTitle("Test Title 2");
89          n2.setMessage("Aenean convallis ut arcu vitae scelerisque. Sed id augue vestibulum, porta lacus a, malesuada erat. Vestibulum ut auctor ante.");
90          n2.setNotificationType(Long.valueOf(9));
91          n2.setPrimaryCampus("ALL");
92          cal.add(Calendar.DATE,-1);
93          n2.setStartDate(new Timestamp( cal.getTimeInMillis()));
94          cal.add(Calendar.DATE,2);
95          n2.setEndDate(new Timestamp(cal.getTimeInMillis()));
96  
97          assertTrue("Noticiation 2 ID is not null.",n2.getNotificationId()==null);
98  
99          Long n2ID = getDao().saveNotification(n2);
100 
101         assertTrue("Notification 2 not saved.",n2ID!=null);
102 
103         n2.setPrimaryCampus("BL");
104 
105         Long n2ID2 = getDao().saveNotification(n2);
106 
107         assertTrue("Notification 2 updated but inserted a new row.",n2ID.compareTo(n2ID2) == 0);
108 
109         List<Notification> notifications = getDao().findAllNotifications();
110 
111         assertTrue("No notifications found.",notifications!=null && notifications.size() > 0);
112         assertTrue("Expected 2 notifications, found "+notifications.size(),notifications.size() == 2);
113 
114         cal = Calendar.getInstance();
115         notifications = getDao().findAllValidNotifications( new Date(cal.getTimeInMillis()) );
116 
117         assertTrue("Expected 2 notifications valid today, found "+notifications.size(),notifications.size()==2);
118 
119         cal.add(Calendar.DATE,1);
120         cal.add(Calendar.MINUTE,120);
121 
122         notifications = getDao().findAllValidNotifications( new Date(cal.getTimeInMillis()) );
123 
124         assertTrue("Expected 1 notification valid today, found "+notifications.size(),notifications.size()==1);
125 
126         Notification n3 = getDao().findNotificationById(n2.getNotificationId());
127 
128         assertTrue("Failed to find notification by ID",n3 != null);
129         assertTrue("Notification found for ID "+n2.getNotificationId()+" but objects are not equal.",n2.equals(n3));
130 
131         UserNotification un1 = new UserNotification();
132         un1.setDeviceId("1234567890");
133         un1.setNotificationId(n1ID);
134         cal = Calendar.getInstance();
135         cal.add(Calendar.MINUTE,180);
136         un1.setNotifyDate(new Date(cal.getTimeInMillis()));
137         un1.setPersonId(Long.valueOf(1999));
138 
139         assertTrue("User notification 1 has an ID and shouldn't.", un1.getUserNotificationId() == null );
140 
141         Long un1ID = getDao().saveUserNotification(un1);
142 
143         assertTrue("User notification 1 failed to save.",un1ID != null);
144 
145         UserNotification un2 = new UserNotification();
146         un2.setDeviceId("0987654321");
147         un2.setNotificationId(n2ID);
148         un2.setNotifyDate(new Date(cal.getTimeInMillis()));
149         un2.setPersonId(Long.valueOf(1999));
150 
151         assertTrue("User notification 2 has an ID and shouldn't.", un2.getUserNotificationId() == null );
152 
153         Long un2ID = getDao().saveUserNotification(un2);
154 
155         assertTrue("User notification 2 failed to save.",un2ID != null);
156 
157         cal.add(Calendar.DATE,1);
158         un2.setNotifyDate(new Date(cal.getTimeInMillis()));
159 
160         Long un2ID2 = getDao().saveUserNotification(un2);
161 
162         assertTrue("User notification 2 was inserted not updated.",un2ID.compareTo(un2ID2)==0);
163 
164         UserNotification lookup = getDao().findUserNotificationById(un1ID);
165 
166         assertTrue("No user notification found by ID.",lookup != null);
167         assertTrue("Incorrect user notification found by ID.",lookup.equals(un1));
168 
169         lookup = getDao().findUserNotificationByNotificationId(n2.getNotificationId());
170 
171         assertTrue("No user notification found by notification ID.",lookup != null);
172         assertTrue("Incorrect user notification found by notification ID.",lookup.equals(un2));
173 
174         lookup = getDao().findUserNotificationById(Long.valueOf(0));
175 
176         assertTrue("Found user notification and should not have.",lookup == null);
177 
178         lookup = getDao().findUserNotificationByNotificationId(Long.valueOf(0));
179 
180         assertTrue("Found user notification by notification id and should not have.",lookup == null);
181 
182         List<UserNotification> userNotifications = getDao().findAllUserNotificationsByDeviceId("1234567890");
183 
184         assertTrue("No user notifications found for device id 1234567890.",userNotifications != null && userNotifications.size() > 0);
185         assertTrue("Multiple user notifications found for device, should be 1.",userNotifications.size() == 1);
186 
187         userNotifications = getDao().findAllUserNotificationsByPersonId(Long.valueOf(0));
188 
189         assertTrue("Found user notifications for invalid user id.",userNotifications == null || userNotifications.isEmpty());
190 
191         userNotifications = getDao().findAllUserNotificationsByPersonId(Long.valueOf(1999));
192 
193         assertTrue("No user notifications found for valid user.",userNotifications!=null && userNotifications.size()>0);
194         assertTrue("Expected 2 user notifications and found "+userNotifications.size(),userNotifications.size()==2);
195 
196         getDao().deleteUserNotificationById(un1.getUserNotificationId());
197         getDao().deleteUserNotificationById(un2.getUserNotificationId());
198 
199         userNotifications = getDao().findAllUserNotificationsByPersonId(Long.valueOf(1999));
200 
201         assertTrue("Failed to delete user notifications.",userNotifications==null || userNotifications.isEmpty());
202 
203         getDao().deleteNotificationById(n2.getNotificationId());
204         getDao().deleteNotificationById(n1.getNotificationId());
205         notifications = getDao().findAllNotifications();
206 
207         assertTrue("Failed to delete notifications, should have found zero.",notifications.size() == 0);
208 
209     }
210 
211     public EntityManagerFactory getEntityManagerFactory() {
212         return entityManagerFactory;
213     }
214 
215     public void setEntityManagerFactory(EntityManagerFactory entityManagerFactory) {
216         this.entityManagerFactory = entityManagerFactory;
217     }
218 
219     public NotificationDao getDao() {
220         return dao;
221     }
222 
223     public void setDao(NotificationDao dao) {
224         this.dao = dao;
225     }
226 }