1 package org.kuali.ole.service;
2
3 import org.joda.time.Hours;
4 import org.junit.Before;
5 import org.junit.Test;
6 import org.junit.runner.RunWith;
7 import org.kuali.ole.OLEConstants;
8 import org.kuali.ole.deliver.loan.LoanProcessor;
9 import org.kuali.ole.patron.bo.OlePatronDocument;
10 import org.kuali.rice.krad.service.KRADServiceLocator;
11 import org.springframework.test.context.ContextConfiguration;
12 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
13 import org.springframework.test.context.transaction.TransactionConfiguration;
14 import org.springframework.transaction.annotation.Transactional;
15
16 import java.util.Date;
17 import java.util.HashMap;
18 import java.util.List;
19 import java.util.Map;
20
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertNotNull;
23 import static org.junit.Assert.assertTrue;
24
25
26
27
28
29
30
31
32 @ContextConfiguration(locations = {"classpath:/SpringBeans.xml"})
33 @RunWith(value = SpringJUnit4ClassRunner.class)
34 @TransactionConfiguration(defaultRollback = true)
35 public class OleCirculationPolicyServiceImplTest {
36
37 private OleCirculationPolicyServiceImpl oleCirculationPolicyService;
38 private OlePatronDocument olePatronDocument;
39
40 @Before
41 public void setUp() throws Exception {
42 oleCirculationPolicyService = new OleCirculationPolicyServiceImpl();
43
44 }
45
46 @Test
47 @Transactional
48 public void testIsValidBarcode() throws Exception {
49 String patronBarcode = getOlePatronDocument().getBarcode();
50 LoanProcessor loanProcessor = new LoanProcessor();
51 String pattern = loanProcessor.getParameter(OLEConstants.PATRON_DIGIT_ROUTINE_PATTERN);
52 assertTrue(!oleCirculationPolicyService.isValidBarcode(patronBarcode,pattern));
53 }
54
55 @Test
56 @Transactional
57 public void testGetPatronMembershipExpireDate() throws Exception {
58 String patronBarcode = getOlePatronDocument().getBarcode();
59 assertNotNull(oleCirculationPolicyService.getPatronMembershipExpireDate(patronBarcode));
60 }
61
62 @Test
63 @Transactional
64 public void testGetNoOfItemsLoaned() throws Exception {
65 String patronBarcode = getOlePatronDocument().getBarcode();
66 assertEquals(0,oleCirculationPolicyService.getNoOfItemsLoaned(getOlePatronId(patronBarcode),false));
67 }
68
69 @Test
70 @Transactional
71 public void testCalculateLoanDueDate() throws Exception {
72 assertNotNull(oleCirculationPolicyService.calculateLoanDueDate("10-Hour"));
73 }
74
75 private String getOlePatronId(String barcode){
76 Map<String, String> criteria = new HashMap<String, String>();
77 criteria.put("barcode", barcode);
78 List<OlePatronDocument> olePatronDocument = (List<OlePatronDocument>) KRADServiceLocator.getBusinessObjectService().findMatching(OlePatronDocument.class,criteria);
79 return olePatronDocument.get(0).getOlePatronId();
80 }
81
82 public OlePatronDocument getOlePatronDocument() {
83 if(olePatronDocument ==null){
84 olePatronDocument = new OlePatronDocument();
85 olePatronDocument.setBorrowerType("1");
86 olePatronDocument.setBarcode("5001234567890123");
87 olePatronDocument.setExpirationDate(new Date());
88 KRADServiceLocator.getBusinessObjectService().save(olePatronDocument);
89 }
90 return olePatronDocument;
91 }
92 }