View Javadoc

1   /**
2    * Copyright 2011-2013 The Kuali Foundation Licensed under the Educational Community
3    * License, Version 2.0 (the "License"); you may not use this file except in
4    * compliance with the License. You may obtain a copy of the License at
5    *
6    * http://www.osedu.org/licenses/ECL-2.0
7    *
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11   * License for the specific language governing permissions and limitations under
12   * the License.
13   */
14  package org.kuali.mobility.reporting.dao;
15  import java.util.List;
16  import java.sql.Timestamp;
17  import java.util.Calendar;
18  import org.apache.log4j.Logger;
19  import org.junit.AfterClass;
20  import org.junit.Before;
21  import org.junit.BeforeClass;
22  import org.junit.Test;
23  import org.junit.runner.RunWith;
24  import static org.junit.Assert.assertTrue;
25  import javax.persistence.EntityManagerFactory;
26  import javax.persistence.PersistenceUnit;
27  import org.unitils.UnitilsJUnit4TestClassRunner;
28  import org.unitils.database.annotations.Transactional;
29  import org.unitils.database.util.TransactionMode;
30  import org.unitils.orm.jpa.JpaUnitils;
31  import org.unitils.orm.jpa.annotation.JpaEntityManagerFactory;
32  import org.kuali.mobility.reporting.entity.Submission;
33  
34  /**
35   * @author Kuali Mobility Team (mobility.collab@kuali.org)
36   */
37  @RunWith(UnitilsJUnit4TestClassRunner.class)
38  @JpaEntityManagerFactory(persistenceUnit="mdot")
39  public class ReportingDaoImplTest {
40  
41      private static final Logger LOG = Logger.getLogger(ReportingDaoImplTest.class);
42  
43      @PersistenceUnit
44      private EntityManagerFactory entityManagerFactory;
45  
46      private ReportingDao dao;
47  
48      @BeforeClass
49      public static void setUpClass() throws Exception {
50      }
51  
52      @AfterClass
53      public static void tearDownClass() throws Exception {
54      }
55  
56      @Before
57      public void preTest() {
58          setDao(new ReportingDaoImpl());
59          JpaUnitils.injectEntityManagerInto(getDao());
60      }
61  
62  
63      @Test
64      @Transactional(TransactionMode.ROLLBACK)
65      public void testReportingTransaction() throws Exception {
66  
67          java.util.Date date= new java.util.Date();
68          Timestamp timestamp = new Timestamp(date.getTime());
69  
70          Submission submission1 = new Submission();
71          submission1.setParentId(new Long(1));
72          submission1.setRevisionNumber(new Long(123));
73          submission1.setRevisionUserId("user1");
74          submission1.setName("Nurul");
75          submission1.setType("TYPE1");
76          submission1.setGroup("KME");
77          submission1.setActive(10);
78          submission1.setPostDate(timestamp);
79          submission1.setArchivedDate(timestamp);
80          submission1.setIpAddress("192.168.54.32");
81          submission1.setUserAgent("Chrome");
82          submission1.setUserId("nurul1988");
83          submission1.setVersionNumber(new Long(12345));
84  
85          assertTrue("ID is not null.",submission1.getId()==null);
86  
87          Long id1 = dao.saveSubmission(submission1);
88          LOG.debug("Submission 1 ID is: "+id1);
89          assertTrue("Failed to save Submission1", id1 != null);
90  
91          Submission submission2 = new Submission();
92          submission2.setParentId(new Long(1));
93          submission2.setRevisionNumber(new Long(234));
94          submission2.setRevisionUserId("user2");
95          submission2.setName("Joe");
96          submission2.setGroup("KME");
97          submission2.setActive(15);
98          submission2.setPostDate(timestamp);
99          submission2.setArchivedDate(timestamp);
100         submission2.setIpAddress("192.168.54.28");
101         submission2.setUserAgent("Chrome");
102         submission2.setUserId("joeswan");
103         submission2.setVersionNumber(new Long(23546));
104 
105         assertTrue("ID is not null.",submission2.getId()==null);
106 
107         Long id2 = dao.saveSubmission(submission2);
108         assertTrue("Failed to save Submission2", id2 != null);
109 
110         submission2.setType("TYPE2");
111 
112         Long id2s = dao.saveSubmission(submission2);
113 
114         assertTrue("Submission 2 updated but inserted a new row.",id2.compareTo(id2s) == 0);
115 
116         List<Submission>submissionList1 = dao.findAllSubmissions();
117         assertTrue("No Submission found.",submissionList1!=null && submissionList1.size() > 0);
118         assertTrue("Expected 2 Submissions, found "+submissionList1.size(),submissionList1.size() == 2);
119 
120         Submission submission3 = dao.findSubmissionById(submission2.getId());
121         assertTrue("Failed to find submission by ID",submission3 != null);
122         assertTrue("Submission found for ID "+submission2.getId()+" but objects are not equal.",submission2.equals(submission3));
123 
124 
125         List<Submission> submissionList2 = dao.findAllSubmissionsByParentId(new Long(1));
126         assertTrue("Failed to find submission by parent ID",submissionList2 != null && submissionList2.size() > 0);
127         assertTrue("Expected 2 Submissions, found "+submissionList2.size(),submissionList2.size() == 2);
128     }
129 
130     public EntityManagerFactory getEntityManagerFactory() {
131         return entityManagerFactory;
132     }
133 
134     public void setEntityManagerFactory(EntityManagerFactory entityManagerFactory) {
135         this.entityManagerFactory = entityManagerFactory;
136     }
137 
138     public ReportingDao getDao() {
139         return dao;
140     }
141 
142     public void setDao(ReportingDao dao) {
143         this.dao = dao;
144     }
145 }