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.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            Assert.assertFalse("clock log should have 'false' as unapprovedIP.", clockLog.getUnapprovedIP());
182    
183            boService.delete(clr);
184            
185            clr = this.createClr(IP_ADDRESS_ONE, 1234L, "1234", 0L);
186            
187            clockLog = new ClockLog();
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, new Date(System.currentTimeMillis()));
196            
197            Assert.assertTrue("clock location rule no error",GlobalVariables.getMessageMap().hasWarnings());
198            Assert.assertTrue("clock log should have 'true' as unapprovedIP.", clockLog.getUnapprovedIP());
199    
200        }
201        
202        @Test
203        public void testClockLocationIPAddress() {
204            //Test for exact match
205            ClockLog clockLog = new ClockLog();
206            clockLog.setJob(new Job());
207            clockLog.setIpAddress(IP_ADDRESS_ONE);
208            clockLog.setWorkArea(1234L);
209            clockLog.setPrincipalId("12345");
210            clockLog.setJobNumber(0L);
211            clockLog.getJob().setDept("TEST");
212            
213            this.processRuleWithIPNoWarning(clockLog, "%");
214            this.processRuleWithIPNoWarning(clockLog, "127.%");
215            this.processRuleWithIPNoWarning(clockLog, "127.0.%");
216            this.processRuleWithIPNoWarning(clockLog, "127.0.0.%");
217            this.processRuleWithIPNoWarning(clockLog, IP_ADDRESS_ONE);
218            this.processRuleWithIPNoWarning(clockLog, "%.%.%.%");
219            
220            this.processRuleWithIPWithWarning(clockLog, "128.%");
221        }
222        
223        public void processRuleWithIPNoWarning(ClockLog clockLog, String ipAddress) {
224            ClockLocationRule clr = this.createClr(ipAddress, 1234L, "1234", 0L);
225            TkServiceLocator.getClockLocationRuleService().processClockLocationRule(clockLog, new Date(System.currentTimeMillis()));
226            Assert.assertTrue("clock location rule no warning message",GlobalVariables.getMessageMap().hasNoWarnings());
227            Assert.assertFalse("clock log should have 'false' as unapprovedIP.", clockLog.getUnapprovedIP());
228        }
229        
230        public void processRuleWithIPWithWarning(ClockLog clockLog, String ipAddress) {
231            clearBusinessObjects(ClockLocationRule.class);
232            ClockLocationRule clr = this.createClr(ipAddress, 1234L, "12345", 0L);
233            TkServiceLocator.getClockLocationRuleService().processClockLocationRule(clockLog, new Date(System.currentTimeMillis()));
234            Assert.assertFalse("clock location rule with warning message",GlobalVariables.getMessageMap().hasNoWarnings());
235            Assert.assertTrue("clock location rule with 1 warning message",(GlobalVariables.getMessageMap().getWarningCount()== 1));
236            Assert.assertTrue("clock log should have 'true' as unapprovedIP.", clockLog.getUnapprovedIP());
237        }
238    
239        @SuppressWarnings("unchecked")
240        public void clearBusinessObjects(Class clazz) {
241            boService.deleteMatching(clazz, new HashMap());
242        }
243        
244     
245    }