1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.hr.time.clock.location.validation;
17
18 import java.sql.Date;
19 import java.sql.Timestamp;
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.HashMap;
23 import java.util.List;
24 import java.util.Map;
25
26 import org.apache.log4j.Logger;
27 import org.junit.Assert;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.kuali.hr.job.Job;
31 import org.kuali.hr.test.KPMETestCase;
32 import org.kuali.hr.time.clock.location.ClockLocationRule;
33 import org.kuali.hr.time.clock.location.ClockLocationRuleIpAddress;
34 import org.kuali.hr.time.clocklog.ClockLog;
35 import org.kuali.hr.time.service.base.TkServiceLocator;
36 import org.kuali.rice.krad.service.BusinessObjectService;
37 import org.kuali.rice.krad.service.KRADServiceLocator;
38 import org.kuali.rice.krad.util.GlobalVariables;
39
40
41
42
43 public class ClockLocationRuleTest extends KPMETestCase {
44
45 private static final Logger LOG = Logger.getLogger(ClockLocationRuleTest.class);
46
47 private static final String IP_ADDRESS_ONE = "127.0.0.1";
48 private static final String IP_ADDRESS_TWO = "127.0.1.1";
49
50 private BusinessObjectService boService;
51
52 @Before
53 public void setUp() throws Exception {
54 super.setUp();
55 boService = KRADServiceLocator.getBusinessObjectService();
56 clearBusinessObjects(ClockLocationRule.class);
57 }
58
59 public ClockLocationRule createClr(String ipAddress, Long workArea, String principalId, Long jobNumber ) {
60 Timestamp ts_now = new Timestamp(System.currentTimeMillis());
61 Date date_now = new Date(System.currentTimeMillis());
62 ClockLocationRule clr = new ClockLocationRule();
63 clr.setDept("TEST");
64 clr.setWorkArea(1234L);
65 clr.setPrincipalId("12345");
66 clr.setJobNumber(0L);
67 clr.setActive(true);
68 clr.setTimestamp(ts_now);
69 clr.setEffectiveDate(date_now);
70 ClockLocationRuleIpAddress anIp = new ClockLocationRuleIpAddress();
71 anIp.setIpAddress(ipAddress);
72 List<ClockLocationRuleIpAddress> aList = new ArrayList<ClockLocationRuleIpAddress>();
73 aList.add(anIp);
74 clr.setIpAddresses(aList);
75
76 boService.save(clr);
77 for(ClockLocationRuleIpAddress ip : clr.getIpAddresses()) {
78 ip.setTkClockLocationRuleId(clr.getTkClockLocationRuleId());
79 boService.save(ip);
80 }
81 return clr;
82 }
83
84 public void deleteCLR(ClockLocationRule clr) {
85 boService.delete(clr);
86 for(ClockLocationRuleIpAddress ip : clr.getIpAddresses()) {
87 boService.delete(ip);
88 }
89 }
90
91 @SuppressWarnings("unchecked")
92 @Test
93 public void testSave() throws Exception {
94 ClockLocationRule clr = new ClockLocationRule();
95 Timestamp ts_now = new Timestamp(System.currentTimeMillis());
96 Date date_now = new Date(System.currentTimeMillis());
97 clr.setPrincipalId("12345");
98 clr.setJobNumber(0L);
99 clr.setActive(true);
100 clr.setTimestamp(ts_now);
101 clr.setEffectiveDate(date_now);
102 ClockLocationRuleIpAddress anIp = new ClockLocationRuleIpAddress();
103 anIp.setIpAddress(IP_ADDRESS_ONE);
104 List<ClockLocationRuleIpAddress> aList = new ArrayList<ClockLocationRuleIpAddress>();
105 aList.add(anIp);
106 clr.setIpAddresses(aList);
107
108 Assert.assertNull("Should not have ObjectId before persist.", clr.getObjectId());
109 boService.save(clr);
110 Assert.assertNotNull("Should have ObjectId after persist.", clr.getObjectId());
111
112 for(ClockLocationRuleIpAddress ip : clr.getIpAddresses()) {
113 ip.setTkClockLocationRuleId(clr.getTkClockLocationRuleId());
114 boService.save(ip);
115 }
116
117 Collection<ClockLocationRule> collection = boService.findAll(ClockLocationRule.class);
118 Assert.assertEquals("One entry should be in list.", 1, collection.size());
119
120 for (ClockLocationRule crule : collection) {
121
122 TkServiceLocator.getClockLocationRuleService().populateIPAddressesForCLR(crule);
123 Assert.assertEquals(crule.getIpAddresses().size(), 1);
124 Assert.assertEquals(crule.getIpAddresses().get(0).getIpAddress(), IP_ADDRESS_ONE);
125 }
126 }
127
128 @SuppressWarnings("unchecked")
129 @Test
130 public void testFindMatching() throws Exception {
131 ClockLocationRule clr = this.createClr(IP_ADDRESS_ONE, 1234L, "1234", 0L);
132
133 LOG.info("ID:" + clr.getTkClockLocationRuleId() + " oID: " + clr.getObjectId());
134 for(ClockLocationRuleIpAddress ip : clr.getIpAddresses()) {
135 ip.setTkClockLocationRuleId(clr.getTkClockLocationRuleId());
136 boService.save(ip);
137 }
138 clr = this.createClr(IP_ADDRESS_TWO,1234L, "1234", 0L);
139
140 LOG.info("ID:" + clr.getTkClockLocationRuleId() + " oID: " + clr.getObjectId());
141 Assert.assertEquals("Should have two records saved", 2, boService.findAll(ClockLocationRule.class).size());
142 Map<String, Object> matchMap = new HashMap<String, Object>();
143 matchMap = new HashMap<String, Object>();
144 matchMap.put("dept", "TEST");
145 Collection<ClockLocationRule> found = boService.findMatching(ClockLocationRule.class, matchMap);
146 Assert.assertEquals(2, found.size());
147
148 }
149
150 @Test
151 public void testClockLocationRuleFetch() throws Exception{
152 ClockLocationRule clr = this.createClr(IP_ADDRESS_ONE, 1234L, "1234", 0L);
153 List<ClockLocationRule> clockLocationRule = TkServiceLocator.getClockLocationRuleService().getClockLocationRule("TEST", 1234L,
154 "12345", 0L, new Date(System.currentTimeMillis()));
155
156 Assert.assertTrue("Clock Location Rule pulled back correctly",clockLocationRule.size()==1);
157 boService.delete(clr);
158 clr = this.createClr(IP_ADDRESS_ONE, -1L, "%", -1L);
159
160 clockLocationRule = TkServiceLocator.getClockLocationRuleService().getClockLocationRule("TEST", 1234L,
161 "12345", 0L, new Date(System.currentTimeMillis()));
162 Assert.assertTrue("Clock Location Rule pulled back correctly",clockLocationRule.size()==1);
163 }
164
165 @Test
166 public void testClockLocationRuleProcessing() {
167 ClockLocationRule clr = this.createClr(IP_ADDRESS_ONE, 1234L, "1234", 0L);
168
169
170 ClockLog clockLog = new ClockLog();
171 clockLog.setJob(new Job());
172 clockLog.setIpAddress(IP_ADDRESS_ONE);
173 clockLog.setWorkArea(1234L);
174 clockLog.setPrincipalId("12345");
175 clockLog.setJobNumber(0L);
176 clockLog.getJob().setDept("TEST");
177
178 TkServiceLocator.getClockLocationRuleService().processClockLocationRule(clockLog, new Date(System.currentTimeMillis()));
179
180 Assert.assertTrue("clock location rule no error",GlobalVariables.getMessageMap().hasNoWarnings());
181
182 boService.delete(clr);
183
184 clr = this.createClr(IP_ADDRESS_ONE, 1234L, "1234", 0L);
185
186 clockLog = new ClockLog();
187 clockLog.setJob(new Job());
188 clockLog.setIpAddress("127.127.127.127");
189 clockLog.setWorkArea(1234L);
190 clockLog.setPrincipalId("12345");
191 clockLog.setJobNumber(0L);
192 clockLog.getJob().setDept("TEST");
193
194 TkServiceLocator.getClockLocationRuleService().processClockLocationRule(clockLog, new Date(System.currentTimeMillis()));
195
196 Assert.assertTrue("clock location rule no error",GlobalVariables.getMessageMap().hasWarnings());
197
198 }
199
200 @Test
201 public void testClockLocationIPAddress() {
202
203 ClockLog clockLog = new ClockLog();
204 clockLog.setJob(new Job());
205 clockLog.setIpAddress(IP_ADDRESS_ONE);
206 clockLog.setWorkArea(1234L);
207 clockLog.setPrincipalId("12345");
208 clockLog.setJobNumber(0L);
209 clockLog.getJob().setDept("TEST");
210
211 this.processRuleWithIPNoWarning(clockLog, "%");
212 this.processRuleWithIPNoWarning(clockLog, "127.%");
213 this.processRuleWithIPNoWarning(clockLog, "127.0.%");
214 this.processRuleWithIPNoWarning(clockLog, "127.0.0.%");
215 this.processRuleWithIPNoWarning(clockLog, IP_ADDRESS_ONE);
216 this.processRuleWithIPNoWarning(clockLog, "%.%.%.%");
217
218 this.processRuleWithIPWithWarning(clockLog, "128.%");
219 }
220
221 public void processRuleWithIPNoWarning(ClockLog clockLog, String ipAddress) {
222 ClockLocationRule clr = this.createClr(ipAddress, 1234L, "1234", 0L);
223 TkServiceLocator.getClockLocationRuleService().processClockLocationRule(clockLog, new Date(System.currentTimeMillis()));
224 Assert.assertTrue("clock location rule no warning message",GlobalVariables.getMessageMap().hasNoWarnings());
225 }
226
227 public void processRuleWithIPWithWarning(ClockLog clockLog, String ipAddress) {
228 clearBusinessObjects(ClockLocationRule.class);
229 ClockLocationRule clr = this.createClr(ipAddress, 1234L, "12345", 0L);
230 TkServiceLocator.getClockLocationRuleService().processClockLocationRule(clockLog, new Date(System.currentTimeMillis()));
231 Assert.assertFalse("clock location rule with warning message",GlobalVariables.getMessageMap().hasNoWarnings());
232 Assert.assertTrue("clock location rule with 1 warning message",(GlobalVariables.getMessageMap().getWarningCount()== 1));
233 }
234
235 @SuppressWarnings("unchecked")
236 public void clearBusinessObjects(Class clazz) {
237 boService.deleteMatching(clazz, new HashMap());
238 }
239
240
241 }