001 /**
002 * Copyright 2004-2013 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.hr.time.clock.location.validation;
017
018 import java.sql.Date;
019 import java.sql.Timestamp;
020 import java.util.ArrayList;
021 import java.util.Collection;
022 import java.util.HashMap;
023 import java.util.List;
024 import java.util.Map;
025
026 import org.apache.log4j.Logger;
027 import org.junit.Assert;
028 import org.junit.Before;
029 import org.junit.Test;
030 import org.kuali.hr.job.Job;
031 import org.kuali.hr.test.KPMETestCase;
032 import org.kuali.hr.time.clock.location.ClockLocationRule;
033 import org.kuali.hr.time.clock.location.ClockLocationRuleIpAddress;
034 import org.kuali.hr.time.clocklog.ClockLog;
035 import org.kuali.hr.time.service.base.TkServiceLocator;
036 import org.kuali.rice.krad.service.BusinessObjectService;
037 import org.kuali.rice.krad.service.KRADServiceLocator;
038 import org.kuali.rice.krad.util.GlobalVariables;
039
040 /**
041 * A really basic unit test testing persistence and searching over persisted business objects.
042 */
043 public class ClockLocationRuleTest extends KPMETestCase {
044
045 private static final Logger LOG = Logger.getLogger(ClockLocationRuleTest.class);
046
047 private static final String IP_ADDRESS_ONE = "127.0.0.1";
048 private static final String IP_ADDRESS_TWO = "127.0.1.1";
049
050 private BusinessObjectService boService;
051
052 @Before
053 public void setUp() throws Exception {
054 super.setUp();
055 boService = KRADServiceLocator.getBusinessObjectService();
056 clearBusinessObjects(ClockLocationRule.class);
057 }
058
059 public ClockLocationRule createClr(String ipAddress, Long workArea, String principalId, Long jobNumber ) {
060 Timestamp ts_now = new Timestamp(System.currentTimeMillis());
061 Date date_now = new Date(System.currentTimeMillis());
062 ClockLocationRule clr = new ClockLocationRule();
063 clr.setDept("TEST");
064 clr.setWorkArea(1234L);
065 clr.setPrincipalId("12345");
066 clr.setJobNumber(0L);
067 clr.setActive(true);
068 clr.setTimestamp(ts_now);
069 clr.setEffectiveDate(date_now);
070 ClockLocationRuleIpAddress anIp = new ClockLocationRuleIpAddress();
071 anIp.setIpAddress(ipAddress);
072 List<ClockLocationRuleIpAddress> aList = new ArrayList<ClockLocationRuleIpAddress>();
073 aList.add(anIp);
074 clr.setIpAddresses(aList);
075
076 boService.save(clr);
077 for(ClockLocationRuleIpAddress ip : clr.getIpAddresses()) {
078 ip.setTkClockLocationRuleId(clr.getTkClockLocationRuleId());
079 boService.save(ip);
080 }
081 return clr;
082 }
083
084 public void deleteCLR(ClockLocationRule clr) {
085 boService.delete(clr);
086 for(ClockLocationRuleIpAddress ip : clr.getIpAddresses()) {
087 boService.delete(ip);
088 }
089 }
090
091 @SuppressWarnings("unchecked")
092 @Test
093 public void testSave() throws Exception {
094 ClockLocationRule clr = new ClockLocationRule();
095 Timestamp ts_now = new Timestamp(System.currentTimeMillis());
096 Date date_now = new Date(System.currentTimeMillis());
097 clr.setPrincipalId("12345");
098 clr.setJobNumber(0L);
099 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 // There is only one
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 //Test for exact match
170 ClockLog clockLog = new ClockLog();
171 clockLog.setDocumentId("1111");
172 clockLog.setJob(new Job());
173 clockLog.setIpAddress(IP_ADDRESS_ONE);
174 clockLog.setWorkArea(1234L);
175 clockLog.setPrincipalId("12345");
176 clockLog.setJobNumber(0L);
177 clockLog.getJob().setDept("TEST");
178
179 TkServiceLocator.getClockLocationRuleService().processClockLocationRule(clockLog, new Date(System.currentTimeMillis()));
180
181 Assert.assertTrue("clock location rule no error",GlobalVariables.getMessageMap().hasNoWarnings());
182 Assert.assertFalse("clock log should have 'false' as unapprovedIP.", clockLog.getUnapprovedIP());
183
184 boService.delete(clr);
185
186 clr = this.createClr(IP_ADDRESS_ONE, 1234L, "1234", 0L);
187
188 clockLog = new ClockLog();
189 clockLog.setDocumentId("1111");
190 clockLog.setJob(new Job());
191 clockLog.setIpAddress("127.127.127.127");
192 clockLog.setWorkArea(1234L);
193 clockLog.setPrincipalId("12345");
194 clockLog.setJobNumber(0L);
195 clockLog.getJob().setDept("TEST");
196
197 TkServiceLocator.getClockLocationRuleService().processClockLocationRule(clockLog, new Date(System.currentTimeMillis()));
198
199 Assert.assertTrue("clock location rule no error",GlobalVariables.getMessageMap().hasWarnings());
200 Assert.assertTrue("clock log should have 'true' as unapprovedIP.", clockLog.getUnapprovedIP());
201
202 }
203
204 @Test
205 public void testClockLocationIPAddress() {
206 //Test for exact match
207 ClockLog clockLog = new ClockLog();
208 clockLog.setDocumentId("1111");
209 clockLog.setJob(new Job());
210 clockLog.setIpAddress(IP_ADDRESS_ONE);
211 clockLog.setWorkArea(1234L);
212 clockLog.setPrincipalId("12345");
213 clockLog.setJobNumber(0L);
214 clockLog.getJob().setDept("TEST");
215
216 this.processRuleWithIPNoWarning(clockLog, "%");
217 this.processRuleWithIPNoWarning(clockLog, "127.%");
218 this.processRuleWithIPNoWarning(clockLog, "127.0.%");
219 this.processRuleWithIPNoWarning(clockLog, "127.0.0.%");
220 this.processRuleWithIPNoWarning(clockLog, IP_ADDRESS_ONE);
221 this.processRuleWithIPNoWarning(clockLog, "%.%.%.%");
222
223 this.processRuleWithIPWithWarning(clockLog, "128.%");
224 }
225
226 public void processRuleWithIPNoWarning(ClockLog clockLog, String ipAddress) {
227 ClockLocationRule clr = this.createClr(ipAddress, 1234L, "1234", 0L);
228 TkServiceLocator.getClockLocationRuleService().processClockLocationRule(clockLog, new Date(System.currentTimeMillis()));
229 Assert.assertTrue("clock location rule no warning message",GlobalVariables.getMessageMap().hasNoWarnings());
230 Assert.assertFalse("clock log should have 'false' as unapprovedIP.", clockLog.getUnapprovedIP());
231 }
232
233 public void processRuleWithIPWithWarning(ClockLog clockLog, String ipAddress) {
234 clearBusinessObjects(ClockLocationRule.class);
235 ClockLocationRule clr = this.createClr(ipAddress, 1234L, "12345", 0L);
236 TkServiceLocator.getClockLocationRuleService().processClockLocationRule(clockLog, new Date(System.currentTimeMillis()));
237 Assert.assertFalse("clock location rule with warning message",GlobalVariables.getMessageMap().hasNoWarnings());
238 Assert.assertTrue("clock location rule with 1 warning message",(GlobalVariables.getMessageMap().getWarningCount()== 1));
239 Assert.assertTrue("clock log should have 'true' as unapprovedIP.", clockLog.getUnapprovedIP());
240 }
241
242 @SuppressWarnings("unchecked")
243 public void clearBusinessObjects(Class clazz) {
244 boService.deleteMatching(clazz, new HashMap());
245 }
246
247
248 }