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.feedback.dao;
16  
17  import static org.junit.Assert.assertTrue;
18  
19  import java.sql.Timestamp;
20  import java.util.Calendar;
21  
22  import javax.persistence.EntityManagerFactory;
23  import javax.persistence.PersistenceUnit;
24  
25  import org.apache.log4j.Logger;
26  import org.junit.AfterClass;
27  import org.junit.Before;
28  import org.junit.BeforeClass;
29  import org.junit.Test;
30  import org.junit.runner.RunWith;
31  import org.kuali.mobility.feedback.entity.Feedback;
32  import org.unitils.UnitilsJUnit4TestClassRunner;
33  import org.unitils.database.annotations.Transactional;
34  import org.unitils.database.util.TransactionMode;
35  import org.unitils.orm.jpa.JpaUnitils;
36  import org.unitils.orm.jpa.annotation.JpaEntityManagerFactory;
37  
38  /**
39   * A service for doing the actual work of interacting with Campus objects.
40   *
41   * @author Kuali Mobility Team (mobility.collab@kuali.org)
42   */
43  @RunWith(UnitilsJUnit4TestClassRunner.class)
44  @JpaEntityManagerFactory(persistenceUnit="mdot")
45  public class FeedbackDaoImplTest {
46  
47  	private static final Logger LOG = Logger.getLogger(FeedbackDaoImplTest.class);
48  
49      @PersistenceUnit
50      private EntityManagerFactory entityManagerFactory;
51  
52      private FeedbackDao dao;
53  
54      @BeforeClass
55      public static void setUpClass() throws Exception {
56      }
57  
58      @AfterClass
59      public static void tearDownClass() throws Exception {
60      }
61  
62      @Before
63      public void preTest() {
64          setDao(new FeedbackDaoImpl());
65          JpaUnitils.injectEntityManagerInto(getDao());
66      }
67      
68      @Test
69      @Transactional(TransactionMode.ROLLBACK)
70      public void testFeedbackTransactions() {
71      	LOG.debug("Performing Test 1");
72      	Feedback f1 = new Feedback();
73      	f1.setVersionNumber(new Long(1));
74      	f1.setUserId("someone");
75      	f1.setUserAgent("mobile");
76      	f1.setService("bus");
77      	f1.setPostedTimestamp(new Timestamp(Calendar.getInstance().getTimeInMillis()));
78      	f1.setNoteText("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec tempus massa et dapibus ullamcorper. Donec porta adipiscing dui vitae ullamcorper.");
79      	f1.setEmail("someone@somedomain.com");
80      	f1.setDeviceType("mobile");
81      	f1.setCampus("ALL");
82      	f1.setAffiliation("Vivantech");
83      	
84      	assertTrue("Feedback 1 ID is not null.",f1.getFeedbackId()==null);
85      	
86      	getDao().saveFeedback(f1);
87  
88      	Long f1ID = f1.getFeedbackId();
89      	
90          LOG.debug("Feedback 1 ID is: "+f1ID);
91          assertTrue("Feedback 1 not saved.", f1ID != null);
92          
93          LOG.debug("Performing Test 2");
94      	Feedback f2 = new Feedback();
95      	f2.setVersionNumber(new Long(1));
96      	f2.setUserId("someone");
97      	f2.setUserAgent("mobile");
98      	f2.setService("dining");
99      	f2.setPostedTimestamp(new Timestamp(Calendar.getInstance().getTimeInMillis()));
100     	f2.setNoteText("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec tempus massa et dapibus ullamcorper. Donec porta adipiscing dui vitae ullamcorper.");
101     	f2.setEmail("someone@somedomain.com");
102     	f2.setDeviceType("mobile");
103     	f2.setCampus("ALL");
104     	f2.setAffiliation("Vivantech");
105     	
106     	assertTrue("Feedback 2 ID is not null.",f2.getFeedbackId()==null);
107     	
108     	getDao().saveFeedback(f2);
109     	
110     	Long f2ID = f2.getFeedbackId();
111 
112         LOG.debug("Feedback 2 ID is: "+ f2ID);
113         assertTrue("Feedback 2 not saved.", f2ID != null);
114         
115         f2.setCampus("BL");
116         
117         getDao().saveFeedback(f2);
118         
119         Long f2ID2 = f2.getFeedbackId(); 
120         
121         assertTrue("Feedback 2 updated but inserted a new row.",f2ID.compareTo(f2ID2) == 0);
122         
123         LOG.debug("Performing Test 3");
124         Feedback f3 = null;
125         
126         getDao().saveFeedback(f3);
127         
128         assertTrue("Feedback 3 was null but saved", f3 == null);        
129     	
130     }
131 
132 	public EntityManagerFactory getEntityManagerFactory() {
133 		return entityManagerFactory;
134 	}
135 
136 	public void setEntityManagerFactory(EntityManagerFactory entityManagerFactory) {
137 		this.entityManagerFactory = entityManagerFactory;
138 	}
139 
140 	public FeedbackDao getDao() {
141 		return dao;
142 	}
143 
144 	public void setDao(FeedbackDao dao) {
145 		this.dao = dao;
146 	}
147 
148 }