001    /**
002     * Copyright 2004-2012 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.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            //Test for exact match
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    }