View Javadoc

1   /**
2    * Copyright 2005-2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.ken.dao;
17  
18  import java.sql.Timestamp;
19  import java.util.ArrayList;
20  import java.util.Calendar;
21  import java.util.HashMap;
22  import java.util.List;
23  
24  import org.kuali.rice.ken.bo.NotificationBo;
25  import org.kuali.rice.ken.bo.NotificationChannelBo;
26  import org.kuali.rice.ken.bo.NotificationContentTypeBo;
27  import org.kuali.rice.ken.bo.NotificationPriorityBo;
28  import org.kuali.rice.ken.bo.NotificationProducerBo;
29  import org.kuali.rice.ken.bo.NotificationRecipientBo;
30  import org.kuali.rice.ken.bo.NotificationSenderBo;
31  import org.kuali.rice.ken.test.util.MockObjectsUtil;
32  import org.kuali.rice.ken.util.NotificationConstants;
33  
34  /**
35   * This class test basic persistence for the Notification business object.  In addition, 
36   * it also tests basic persistence for the NotificationSender and NotificationRecipient bos 
37   * since those bos are mostly persisted and retrieved through the parent Notification 
38   * instance.
39   * 
40   * @author Kuali Rice Team (rice.collab@kuali.org)
41   */
42  public class NotificationDaoTest extends BusinessObjectPersistenceTestCaseBase {
43      Long id = new Long(-1);
44      NotificationPriorityBo mockPriority = MockObjectsUtil.getTestPriority1();
45      NotificationContentTypeBo mockContentType = MockObjectsUtil.getTestContentType1();
46      NotificationChannelBo mockChannel = MockObjectsUtil.getTestChannel1();
47      NotificationProducerBo mockProducer = MockObjectsUtil.getTestProducer1();
48      
49      NotificationBo notification = new NotificationBo();
50  
51      private String deliveryType = NotificationConstants.DELIVERY_TYPES.ACK;
52      private Timestamp sendDateTime = new Timestamp(Calendar.getInstance().getTimeInMillis());
53      private Timestamp autoRemoveDateTime = null;
54      private String content = "Notification Content!";
55  
56      /**
57       * @see org.kuali.rice.ken.dao.BusinessObjectPersistenceTestCaseBase#setup()
58       */
59      @Override
60      protected void setup() {
61  	super.setup();
62  	businessObjectDao.save(mockPriority);
63  	businessObjectDao.save(mockContentType);
64  	businessObjectDao.save(mockChannel);
65  	businessObjectDao.save(mockProducer);
66      }
67  
68      /**
69       * @see org.kuali.rice.ken.dao.BusinessObjectPersistenceTestCaseBase#delete()
70       */
71      @Override
72      protected boolean delete() {
73  	try {
74  	    businessObjectDao.delete(notification);
75  	} catch(Exception e) {
76  	    return false;
77  	}
78  	return true;
79      }
80      
81      /**
82       * @see org.kuali.rice.ken.dao.BusinessObjectPersistenceTestCaseBase#retrieve()
83       */
84      @Override
85      protected boolean retrieve() {
86  	notification = new NotificationBo();
87  	
88  	HashMap criteria = new HashMap();
89  	
90  	criteria.put(NotificationConstants.BO_PROPERTY_NAMES.ID, id);
91  	notification = (NotificationBo) businessObjectDao.findByPrimaryKey(NotificationBo.class, criteria);
92  	
93  	boolean success = true;
94  	
95  	success &= notification != null;
96  	success &= notification.getContentType().getName().equals(MockObjectsUtil.getTestContentType1().getName());
97  	success &= notification.getRecipients().size()==2;
98  	success &= notification.getSenders().size()==2;
99          success &= notification.getCreationDateTime()!=null;
100 	success &= notification.getAutoRemoveDateTime()==null;
101 	success &= notification.getDeliveryType().equals(NotificationConstants.DELIVERY_TYPES.ACK);
102 	success &= notification.getProcessingFlag().equals(NotificationConstants.PROCESSING_FLAGS.UNRESOLVED);
103 	
104 	return success;
105     }
106     
107     /**
108      * @see org.kuali.rice.ken.dao.BusinessObjectPersistenceTestCaseBase#insert()
109      */
110     @Override
111     protected boolean insert() {
112 	List<NotificationRecipientBo> recipients = new ArrayList();
113 	recipients.add(MockObjectsUtil.getTestRecipient1());
114 	recipients.add(MockObjectsUtil.getTestRecipient2());
115 	
116 	List<NotificationSenderBo> senders = new ArrayList();
117 	senders.add(MockObjectsUtil.getTestSender1());
118 	senders.add(MockObjectsUtil.getTestSender2());
119 	
120 	notification = MockObjectsUtil.buildTestNotification(deliveryType, sendDateTime, autoRemoveDateTime, mockContentType, 
121 		content, mockPriority, mockProducer, mockChannel, recipients, senders);
122 	try {
123 	    businessObjectDao.save(notification);
124 	    id = new Long(notification.getId());
125 	} catch(Exception e) {
126         LOG.error("Error saving notification", e);
127 	    return false;
128 	}
129 	return true;
130     }
131     
132     /**
133      * @see org.kuali.rice.ken.dao.BusinessObjectPersistenceTestCaseBase#update()
134      */
135     @Override
136     protected boolean update() {
137 	notification.setDeliveryType(NotificationConstants.DELIVERY_TYPES.FYI);
138 	
139 	try {
140 	    businessObjectDao.save(notification);
141 	} catch(Exception e) {
142 	    return false;
143 	}
144 	return true;
145     }
146     
147     /**
148      * @see org.kuali.rice.ken.dao.BusinessObjectPersistenceTestCaseBase#validateChanges()
149      */
150     @Override
151     protected boolean validateChanges() {
152 	retrieve();  //retrieve fresh again
153 	
154 	boolean success = true;
155 	
156 	success &= notification.getDeliveryType().equals(NotificationConstants.DELIVERY_TYPES.FYI);
157 	    
158 	return success;
159     }
160 }