View Javadoc
1   /**
2    * Copyright 2011 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  
16  package org.kuali.mobility.reporting.service;
17  
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.reporting.dao.ReportingDao;
24  import org.kuali.mobility.reporting.dao.ReportingDaoImpl;
25  import org.kuali.mobility.reporting.entity.Submission;
26  import org.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  import org.unitils.UnitilsJUnit4TestClassRunner;
29  import org.unitils.database.annotations.Transactional;
30  import org.unitils.database.util.TransactionMode;
31  import org.unitils.orm.jpa.JpaUnitils;
32  import org.unitils.orm.jpa.annotation.JpaEntityManagerFactory;
33  
34  import javax.persistence.EntityManagerFactory;
35  import javax.persistence.PersistenceUnit;
36  import java.sql.Timestamp;
37  import java.util.List;
38  
39  import static org.junit.Assert.assertTrue;
40  
41  /**
42   * @author Kuali Mobility Team (mobility.collab@kuali.org)
43   */
44  @RunWith(UnitilsJUnit4TestClassRunner.class)
45  @JpaEntityManagerFactory(persistenceUnit="mdot")
46  public class ReportingServiceImplTest {
47  
48      private static final Logger LOG = LoggerFactory.getLogger(ReportingServiceImplTest.class);
49  
50      @PersistenceUnit
51      private EntityManagerFactory entityManagerFactory;
52  
53      private ReportingServiceImpl service;
54      private ReportingDao dao;
55  
56      @BeforeClass
57      public static void setUpClass() throws Exception {
58      }
59  
60      @AfterClass
61      public static void tearDownClass() throws Exception {
62      }
63  
64      @Before
65      public void preTest() {
66             setService(new ReportingServiceImpl());
67             setDao(new ReportingDaoImpl());
68             getService().setReportingDao(getDao());
69             JpaUnitils.injectEntityManagerInto(getDao());
70      }
71  
72  
73      @Test
74      @Transactional(TransactionMode.ROLLBACK)
75      public void testReportingTransaction() throws Exception {
76  
77          java.util.Date date= new java.util.Date();
78          Timestamp timestamp = new Timestamp(date.getTime());
79  
80          Submission submission1 = new Submission();
81          submission1.setParentId(new Long(1));
82          submission1.setRevisionNumber(new Long(123));
83          submission1.setRevisionUserId("user1");
84          submission1.setName("Nurul");
85          submission1.setType("TYPE1");
86          submission1.setGroup("KME");
87          submission1.setActive(10);
88          submission1.setPostDate(timestamp);
89          submission1.setArchivedDate(timestamp);
90          submission1.setIpAddress("192.168.54.32");
91          submission1.setUserAgent("Chrome");
92          submission1.setUserId("nurul1988");
93          submission1.setVersionNumber(new Long(12345));
94  
95          assertTrue("ID is not null.",submission1.getId()==null);
96  
97            Long id1 = getService().saveSubmission(submission1);
98          LOG.debug("Submission 1 ID is: "+id1);
99          assertTrue("Failed to save Submission1", id1 != null);
100 
101         Submission submission2 = new Submission();
102         submission2.setParentId(new Long(1));
103         submission2.setRevisionNumber(new Long(234));
104         submission2.setRevisionUserId("user2");
105         submission2.setName("Joe");
106         submission2.setGroup("KME");
107         submission2.setActive(15);
108         submission2.setPostDate(timestamp);
109         submission2.setArchivedDate(timestamp);
110         submission2.setIpAddress("192.168.54.28");
111         submission2.setUserAgent("Chrome");
112         submission2.setUserId("joeswan");
113         submission2.setVersionNumber(new Long(23546));
114 
115         assertTrue("ID is not null.",submission2.getId()==null);
116 
117         Long id2 = getService().saveSubmission(submission2);
118         assertTrue("Failed to save Submission2", id2 != null);
119 
120         submission2.setType("TYPE2");
121 
122         Long id2s = getService().saveSubmission(submission2);
123 
124         assertTrue("Submission 2 updated but inserted a new row.",id2.compareTo(id2s) == 0);
125 
126         List<Submission>submissionList1 = getService().findAllSubmissions();
127         assertTrue("No Submission found.",submissionList1!=null && submissionList1.size() > 0);
128         assertTrue("Expected 2 Submissions, found "+submissionList1.size(),submissionList1.size() == 2);
129 
130         Submission submission3 = getService().findSubmissionById(submission2.getId());
131         assertTrue("Failed to find submission by ID",submission3 != null);
132         assertTrue("Submission found for ID "+submission2.getId()+" but objects are not equal.",submission2.equals(submission3));
133 
134 
135         List<Submission> submissionList2 = getService().findAllSubmissionsByParentId(new Long(1));
136         assertTrue("Failed to find submission by parent ID",submissionList2 != null && submissionList2.size() > 0);
137         assertTrue("Expected 2 Submissions, found "+submissionList2.size(),submissionList2.size() == 2);
138     }
139 
140     public EntityManagerFactory getEntityManagerFactory() {
141         return entityManagerFactory;
142     }
143 
144     public void setEntityManagerFactory(EntityManagerFactory entityManagerFactory) {
145         this.entityManagerFactory = entityManagerFactory;
146     }
147 
148     public ReportingDao getDao() {
149         return dao;
150     }
151 
152     public void setDao(ReportingDao dao) {
153         this.dao = dao;
154     }
155 
156     public ReportingServiceImpl getService() {
157         return service;
158     }
159 
160     public void setService(ReportingServiceImpl service) {
161         this.service = service;
162     }
163 
164 }