View Javadoc

1   /**
2    * Copyright 2004-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * A really basic unit test testing persistence and searching over persisted business objects.
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     		// 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 }