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