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