1 package org.kuali.ole.ingest.krms.builder;
2
3 import org.junit.Test;
4 import org.junit.runner.RunWith;
5 import org.kuali.ole.DataCarrierService;
6 import org.kuali.ole.OLEConstants;
7 import org.kuali.ole.ingest.FileUtil;
8 import org.kuali.ole.ingest.pojo.MatchBo;
9 import org.kuali.ole.service.OleCirculationPolicyService;
10 import org.kuali.ole.service.OleCirculationPolicyServiceImpl;
11 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
12 import org.kuali.rice.krad.service.KRADServiceLocator;
13 import org.kuali.rice.krms.api.KrmsApiServiceLocator;
14 import org.kuali.rice.krms.api.engine.*;
15 import org.kuali.rice.krms.framework.engine.BasicRule;
16 import org.kuali.rice.krms.impl.repository.AgendaBo;
17 import org.springframework.test.context.ContextConfiguration;
18 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
19 import org.springframework.test.context.transaction.TransactionConfiguration;
20 import org.springframework.transaction.annotation.Transactional;
21
22 import java.io.File;
23 import java.net.URL;
24 import java.text.DateFormat;
25 import java.text.SimpleDateFormat;
26 import java.util.*;
27
28 import static junit.framework.Assert.*;
29
30
31
32
33
34
35
36
37 @ContextConfiguration(locations = {"classpath:/SpringBeans.xml"})
38 @RunWith(value = SpringJUnit4ClassRunner.class)
39 @TransactionConfiguration(defaultRollback = true)
40 public class OleKrmsBuilder_UT {
41
42 private static final String NAMESPACE_CODE_SELECTOR = "namespaceCode";
43 private static final String NAME_SELECTOR = "name";
44
45 private OleKrmsBuilder krmsBuilder = new OleKrmsBuilder();
46
47 @Test
48 @Transactional
49 public void testPersistLoan() throws Exception{
50 OleCirculationPolicyService oleCirculationPolicyService = new OleCirculationPolicyServiceImpl();
51 URL resource = getClass().getResource("deliver.xml");
52 DataCarrierService dataCarrierService = GlobalResourceLoader.getService(OLEConstants.DATA_CARRIER_SERVICE);
53 dataCarrierService.addData("listOfOverDueDays",new ArrayList<Integer>());
54 dataCarrierService.addData("listOfRecalledOverdueDays",new ArrayList<Integer>());
55 File file = new File(resource.toURI());
56 String krmsXML = new FileUtil().readFile(file);
57 List<String> agendas = krmsBuilder.persistKrmsFromFileContent(krmsXML);
58
59 String ruleName = null;
60 Date d = new Date(0000,03,03);
61 dataCarrierService.addData("hoursDiff",oleCirculationPolicyService.getHoursDiff(new Date(2012,11,10),new Date(2012,10,10)));
62 String agendaName = "General Checks";
63 HashMap<String,Object> termValues = new HashMap<String, Object>() ;
64 termValues.put("generalBlock","true");
65 termValues.put("expirationDate",d) ;
66 testEngineResultsLoan(agendaName,termValues,null);
67
68 agendaName = "CheckOut Validation";
69 termValues = new HashMap<String, Object>() ;
70 termValues.put("borrowerType","Graduate");
71 termValues.put("itemType","book");
72 termValues.put("location","Stacks");
73 termValues.put("numberOfItemsCheckedOut","11");
74 termValues.put("numberOfOverDueItemsCheckedOut","8");
75 termValues.put("numberOfOverDueRecalledItemsCheckedOut","45") ;
76 termValues.put("numberOfRecallOverdueDays","20") ;
77 termValues.put("numberOfClaimsReturned","6") ;
78 termValues.put("overdueFineAmt","200") ;
79 termValues.put("replacementFeeAmt","160") ;
80 termValues.put("isRenewal","true");
81 termValues.put("numberOfRenewals","2");
82 DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
83 String dateToString = formatter.format(d);
84 termValues.put("itemDueDate",dateToString);
85 termValues.put("itemStatus","inTransit") ;
86 termValues.put("digitRoutine","Yes") ;
87 termValues.put("itemBarcode","Nil") ;
88 termValues.put("numberOfPiecesReturned","5");
89 termValues.put("numberOfPiecesFromRecord","5");
90 termValues.put("numberOfPiecesFromRecord","5");
91 termValues.put("circulationLocation","CDE");
92 termValues.put("itemLocation","ABCDEFGHI");
93 testEngineResultsLoan(agendaName, termValues, ruleName + agendaName);
94
95 agendaName = "Check-in Validation";
96 termValues = new HashMap<String, Object>() ;
97 termValues.put("borrowerType","Graduate");
98 termValues.put("itemType","book");
99 termValues.put("location","Stacks");
100 termValues.put("itemLocation","Stacks");
101 termValues.put("operatorsCirculationLocation","Stacks");
102 termValues.put("itemDueDate",new Date());
103 termValues.put("numberOfPiecesOfItem","5");
104 termValues.put("itemStatus","lost");
105 termValues.put("requestType","recall/delivery");
106 testEngineResultsLoan(agendaName,termValues,ruleName+agendaName);
107
108 agendaName = "Request Validation";
109 termValues.put("borrowerType","");
110 termValues.put("itemType","");
111 termValues.put("location","");
112 termValues.put("maxNumberOfRecallRequest","");
113 termValues.put("maxNumberOfHoldRequest","");
114 termValues.put("maxNumberOfPageRequest","");
115 termValues.put("maxNumberOfRequestByBorrower","");
116 termValues.put("requestTypeId","5");
117 testEngineResultsLoan(agendaName,termValues,ruleName+agendaName);
118 }
119
120 @Test
121 @Transactional
122 public void testDeleteLoan() throws Exception{
123
124 URL resource = getClass().getResource("deliver.xml");
125 File file = new File(resource.toURI());
126 String krmsXML = new FileUtil().readFile(file);
127 assertNotNull(krmsBuilder.persistKrmsFromFileContent(krmsXML).get(0));
128 assertNotNull(krmsBuilder.persistKrmsFromFileContent(krmsXML).get(0));
129
130 }
131
132 public boolean testEngineResultsLoan(String agendaName,HashMap<String,Object> termValues,String ruleName){
133 Engine engine = KrmsApiServiceLocator.getEngine();
134
135 HashMap<String, Object> agendaValue = new HashMap<String, Object>();
136 agendaValue.put("nm", agendaName);
137 List<AgendaBo> agendaBos = (List<AgendaBo>) KRADServiceLocator.getBusinessObjectService().findMatching(AgendaBo.class, agendaValue);
138 AgendaBo agendaBo = agendaBos.get(0);
139 HashMap<String, String> map = new HashMap<String, String>();
140 map.put("AGENDA_NAME", agendaBo.getName());
141 List<MatchBo> matchBos = (List<MatchBo>) KRADServiceLocator.getBusinessObjectService().findMatching(MatchBo.class, map);
142
143 SelectionCriteria selectionCriteria =
144 SelectionCriteria.createCriteria(null, getSelectionContext(agendaBo.getContext().getName()), getAgendaContext(agendaName));
145
146 ExecutionOptions executionOptions = new ExecutionOptions();
147 executionOptions.setFlag(ExecutionFlag.LOG_EXECUTION, true);
148
149 Facts.Builder factBuilder = Facts.Builder.create();
150
151 for (Iterator<MatchBo> matchBoIterator = matchBos.iterator(); matchBoIterator.hasNext(); ) {
152 MatchBo matchBo = matchBoIterator.next();
153 factBuilder.addFact(matchBo.getTermName(), termValues.get((matchBo.getTermName())));
154 }
155
156
157 EngineResults engineResult = engine.execute(selectionCriteria, factBuilder.build(), executionOptions);
158 List<ResultEvent> allResults = engineResult.getAllResults();
159 boolean results = true;
160 boolean flag = false;
161 int i=0;
162 StringBuffer failures = new StringBuffer();
163 for (Iterator<ResultEvent> resultEventIterator = allResults.iterator(); resultEventIterator.hasNext(); ) {
164 ResultEvent resultEvent = resultEventIterator.next();
165 if (resultEvent.getType().equals("Rule Evaluated")) {
166 BasicRule source = (BasicRule)resultEvent.getSource() ;
167 if(ruleName!=null && ruleName.equals(source.getName())){
168 return resultEvent.getResult();
169 }
170 results &= resultEvent.getResult();
171 }
172 }
173 return results;
174 }
175 protected Map<String, String> getSelectionContext(String contextName) {
176 Map<String, String> selector = new HashMap<String, String>();
177 selector.put(NAMESPACE_CODE_SELECTOR, "OLE");
178 selector.put(NAME_SELECTOR, contextName);
179 return selector;
180 }
181 protected Map<String, String> getAgendaContext(String agendaName) {
182 Map<String, String> selector = new HashMap<String, String>();
183 selector.put(NAME_SELECTOR, agendaName);
184 return selector;
185 }
186 }