1 package org.kuali.ole.ingest;
2
3 import org.junit.Test;
4 import org.junit.runner.RunWith;
5 import org.kuali.ole.ingest.pojo.MatchBo;
6 import org.kuali.rice.krad.service.KRADServiceLocator;
7 import org.kuali.rice.krms.api.KrmsApiServiceLocator;
8 import org.kuali.rice.krms.api.engine.*;
9 import org.kuali.rice.krms.framework.engine.BasicRule;
10 import org.kuali.rice.krms.impl.repository.AgendaBo;
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.io.File;
17 import java.net.URL;
18 import java.util.*;
19
20 import static junit.framework.Assert.assertEquals;
21 import static junit.framework.Assert.assertNotNull;
22 import static junit.framework.Assert.assertTrue;
23
24
25
26
27
28
29
30
31 @ContextConfiguration(locations = {"classpath:/SpringBeans.xml"})
32 @RunWith(value = SpringJUnit4ClassRunner.class)
33 @TransactionConfiguration(defaultRollback = true)
34 public class KrmsBuilder_UT {
35
36 private static final String NAMESPACE_CODE_SELECTOR = "namespaceCode";
37 private static final String NAME_SELECTOR = "name";
38
39 private KrmsBuilder krmsBuilder = new KrmsBuilder();
40
41 @Test
42 @Transactional
43 public void testPersistLoan() throws Exception{
44
45 URL resource = getClass().getResource("Loan_Patron.xml");
46 File file = new File(resource.toURI());
47 String krmsXML = new FileUtil().readFile(file);
48 List<String> agendas = krmsBuilder.persistKrmsFromFileContent(krmsXML);
49 assertEquals(1,agendas.size());
50 assertNotNull(testEngineResultsLoan());
51 }
52
53 @Test
54 @Transactional
55 public void testDeleteLoan() throws Exception{
56
57 URL resource = getClass().getResource("Loan_Patron.xml");
58 File file = new File(resource.toURI());
59 String krmsXML = new FileUtil().readFile(file);
60 assertNotNull(krmsBuilder.persistKrmsFromFileContent(krmsXML).get(0));
61 assertNotNull(krmsBuilder.persistKrmsFromFileContent(krmsXML).get(0));
62
63 }
64
65 public EngineResults testEngineResultsLoan(){
66 Engine engine = KrmsApiServiceLocator.getEngine();
67
68 HashMap<String, Object> agendaValue = new HashMap<String, Object>();
69 agendaValue.put("nm", "CIRC_POLICY");
70 List<AgendaBo> agendaBos = (List<AgendaBo>) KRADServiceLocator.getBusinessObjectService().findMatching(AgendaBo.class, agendaValue);
71 AgendaBo agendaBo = agendaBos.get(0);
72 HashMap<String, String> map = new HashMap<String, String>();
73 map.put("AGENDA_NAME", agendaBo.getName());
74 List<MatchBo> matchBos = (List<MatchBo>) KRADServiceLocator.getBusinessObjectService().findMatching(MatchBo.class, map);
75
76 SelectionCriteria selectionCriteria =
77 SelectionCriteria.createCriteria(null, getSelectionContext(agendaBo.getContext().getName()), getAgendaContext("CIRC_POLICY"));
78
79 ExecutionOptions executionOptions = new ExecutionOptions();
80 executionOptions.setFlag(ExecutionFlag.LOG_EXECUTION, true);
81
82 Facts.Builder factBuilder = Facts.Builder.create();
83 boolean barcodeStatus = true;
84 Date expirationDate = new Date(2012,10,05);
85 Integer loanedItems = new Integer("5");
86 String itemStatus = "AVAILABLE";
87 String itemType="Book";
88 Date dueDate= new Date();
89
90 HashMap<String,Object> termValues = new HashMap<String, Object>() ;
91 termValues.put("barcodeStatus",barcodeStatus?"true":"false");
92 termValues.put("expirationDate",expirationDate);
93 termValues.put("loanedItems",loanedItems) ;
94 termValues.put("itemStatus",itemStatus);
95 termValues.put("itemType",itemType);
96 termValues.put("dueDate",dueDate!=null?dueDate.toString():"null");
97
98 for (Iterator<MatchBo> matchBoIterator = matchBos.iterator(); matchBoIterator.hasNext(); ) {
99 MatchBo matchBo = matchBoIterator.next();
100 factBuilder.addFact(matchBo.getTermName(), termValues.get((matchBo.getTermName())));
101 }
102
103
104 EngineResults engineResult = engine.execute(selectionCriteria, factBuilder.build(), executionOptions);
105 List<ResultEvent> allResults = engineResult.getAllResults();
106 boolean results = true;
107 boolean flag = false;
108 int i=0;
109 StringBuffer failures = new StringBuffer();
110 for (Iterator<ResultEvent> resultEventIterator = allResults.iterator(); resultEventIterator.hasNext(); ) {
111 ResultEvent resultEvent = resultEventIterator.next();
112 if (resultEvent.getType().equals("Rule Evaluated")) {
113 if(!resultEvent.getResult()) {
114 BasicRule source = (BasicRule)resultEvent.getSource() ;
115
116 if(!flag){
117 failures.append("<b>This Patron is not able to borrow items because of following reasons : </b> <br/>");
118 i++;
119 flag = true;
120 }
121 if(source.getName().equals("barcodeStatusCheck")){
122 failures.append(i+". Barcode Status is False <br/>");
123 i++;
124 }else if(source.getName().equals("membershipExpirationDateCheck")){
125 failures.append(i+". Their Patron record expired on "+expirationDate+" <br/>");
126 i++;
127 } else if(source.getName().equals("borrowerLimitCheck")){
128 failures.append(i+". Patron borrowerLimit Exceeds <br/>");
129 i++;
130 } else if(source.getName().equals("itemUnAvailableCheck")){
131 failures.append(i+". Item staus is "+itemStatus+" <br/>");
132 i++;
133 } else if(source.getName().equals("itemDueDateCheck")){
134 failures.append(i+". You must provide Due Date <br/>");
135 i++;
136 }
137 }
138 results &= resultEvent.getResult();
139 }
140 }
141 assertTrue(results);
142 return engineResult;
143 }
144 protected Map<String, String> getSelectionContext(String contextName) {
145 Map<String, String> selector = new HashMap<String, String>();
146 selector.put(NAMESPACE_CODE_SELECTOR, "OLE");
147 selector.put(NAME_SELECTOR, contextName);
148 return selector;
149 }
150 protected Map<String, String> getAgendaContext(String agendaName) {
151 Map<String, String> selector = new HashMap<String, String>();
152 selector.put(NAME_SELECTOR, agendaName);
153 return selector;
154 }
155
156 @Test
157 @Transactional
158 public void testPersistLicence() throws Exception{
159
160 URL resource = getClass().getResource("license.xml");
161 File file = new File(resource.toURI());
162 String krmsXML = new FileUtil().readFile(file);
163 List<String> agendas = krmsBuilder.persistKrmsFromFileContent(krmsXML);
164 assertEquals(1,agendas.size());
165 assertNotNull(testEngineResultsLicence());
166 }
167 public EngineResults testEngineResultsLicence(){
168 Engine engine = KrmsApiServiceLocator.getEngine();
169
170 HashMap<String, Object> agendaValue = new HashMap<String, Object>();
171 agendaValue.put("nm", "LICENSE_POLICY");
172 List<AgendaBo> agendaBos = (List<AgendaBo>) KRADServiceLocator.getBusinessObjectService().findMatching(AgendaBo.class, agendaValue);
173 AgendaBo agendaBo = agendaBos.get(0);
174 HashMap<String, String> map = new HashMap<String, String>();
175 map.put("AGENDA_NAME", agendaBo.getName());
176 List<MatchBo> matchBos = (List<MatchBo>) KRADServiceLocator.getBusinessObjectService().findMatching(MatchBo.class, map);
177
178 SelectionCriteria selectionCriteria =
179 SelectionCriteria.createCriteria(null, getSelectionContext(agendaBo.getContext().getName()), getAgendaContext("LICENSE_POLICY"));
180
181 ExecutionOptions executionOptions = new ExecutionOptions();
182 executionOptions.setFlag(ExecutionFlag.LOG_EXECUTION, true);
183
184 Facts.Builder factBuilder = Facts.Builder.create();
185
186 HashMap<String,Object> termValues = new HashMap<String, Object>() ;
187 termValues.put("licenseType","NEW");
188 termValues.put("agreementMethod","SERU");
189 termValues.put("workflowName","Approval");
190
191 for (Iterator<MatchBo> matchBoIterator = matchBos.iterator(); matchBoIterator.hasNext(); ) {
192 MatchBo matchBo = matchBoIterator.next();
193 factBuilder.addFact(matchBo.getTermName(), termValues.get((matchBo.getTermName())));
194 }
195
196
197 EngineResults engineResult = engine.execute(selectionCriteria, factBuilder.build(), executionOptions);
198 List<ResultEvent> allResults = engineResult.getAllResults();
199 boolean results = true;
200 for (Iterator<ResultEvent> resultEventIterator = allResults.iterator(); resultEventIterator.hasNext(); ) {
201 ResultEvent resultEvent = resultEventIterator.next();
202 if (resultEvent.getType().equals("Rule Evaluated")) {
203 results &= resultEvent.getResult();
204 }
205 }
206 assertTrue(results);
207 return engineResult;
208 }
209 @Test
210 @Transactional
211 public void testDeleteLicense() throws Exception{
212
213 URL resource = getClass().getResource("license.xml");
214 File file = new File(resource.toURI());
215 String krmsXML = new FileUtil().readFile(file);
216 assertNotNull(krmsBuilder.persistKrmsFromFileContent(krmsXML).get(0));
217 assertNotNull(krmsBuilder.persistKrmsFromFileContent(krmsXML).get(0));
218
219 }
220 }