1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kns.workflow;
17
18 import org.apache.commons.lang.exception.ExceptionUtils;
19 import org.junit.Assert;
20 import org.junit.Test;
21 import org.kuali.rice.core.api.uif.RemotableAttributeError;
22 import org.kuali.rice.core.api.util.type.KualiDecimal;
23 import org.kuali.rice.kew.api.document.search.DocumentSearchCriteria;
24 import org.kuali.rice.kew.api.document.search.DocumentSearchResults;
25 import org.kuali.rice.kew.api.exception.WorkflowException;
26 import org.kuali.rice.kew.docsearch.service.DocumentSearchService;
27 import org.kuali.rice.kew.doctype.bo.DocumentType;
28 import org.kuali.rice.kew.service.KEWServiceLocator;
29 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
30 import org.kuali.rice.krad.UserSession;
31 import org.kuali.rice.krad.datadictionary.exception.UnknownDocumentTypeException;
32 import org.kuali.rice.krad.service.DocumentService;
33 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
34 import org.kuali.rice.krad.test.document.AccountWithDDAttributesDocument;
35 import org.kuali.rice.krad.util.GlobalVariables;
36 import org.kuali.rice.kns.workflow.attribute.DataDictionarySearchableAttribute;
37 import org.kuali.rice.krad.test.KRADTestCase;
38
39 import java.sql.Date;
40 import java.sql.Timestamp;
41 import java.util.ArrayList;
42 import java.util.Calendar;
43 import java.util.Collections;
44 import java.util.HashMap;
45 import java.util.List;
46 import java.util.Map;
47 import java.util.concurrent.Callable;
48
49 import static junit.framework.Assert.assertEquals;
50 import static junit.framework.Assert.fail;
51
52
53
54
55
56
57 public class DataDictionarySearchableAttributeTest extends KRADTestCase {
58
59 @Override
60 public void setUp() throws Exception {
61 super.setUp();
62 GlobalVariables.setUserSession(new UserSession("quickstart"));
63 }
64
65 private final static String ACCOUNT_WITH_DD_ATTRIBUTES_DOCUMENT_NAME = "AccountWithDDAttributes";
66
67 enum DOCUMENT_FIXTURE {
68 NORMAL_DOCUMENT("Testing NORMAL_DOCUMENT", new Integer(1234567890), "John Doe", new KualiDecimal(501.77), createDate(2009, Calendar.OCTOBER, 15), createTimestamp(2009, Calendar.NOVEMBER, 1, 0, 0, 0), "SecondState", true),
69 ZERO_NUMBER_DOCUMENT("Testing ZERO_NUMBER_DOCUMENT", new Integer(0), "Jane Doe", new KualiDecimal(-100), createDate(2009, Calendar.OCTOBER, 16), createTimestamp(2015, Calendar.NOVEMBER, 2, 0, 0, 0), "FirstState", true),
70 FALSE_AWAKE_DOCUMENT("Testing FALSE_AWAKE_DOCUMENT", new Integer(987654321), "John D'oh", new KualiDecimal(0.0), createDate(2006, Calendar.OCTOBER, 17), createTimestamp(1900, Calendar.NOVEMBER, 3, 0, 0, 0), "FourthState", false),
71 ODD_NAME_DOCUMENT("Testing ODD_NAME_DOCUMENT", new Integer(88), "_", new KualiDecimal(10000051.0), createDate(2009, Calendar.OCTOBER, 18), createTimestamp(2009, Calendar.NOVEMBER, 4, 0, 0, 0), "FourthState", true),
72 ODD_TIMESTAMP_DOCUMENT("Testing ODD_TIMESTAMP_DOCUMENT", new Integer(9000), "Shane Kloe", new KualiDecimal(4.54), createDate(2012, Calendar.OCTOBER, 19), createTimestamp(2007, Calendar.NOVEMBER, 5, 12, 4, 38), "ThirdState", false),
73 ANOTHER_ODD_NAME_DOCUMENT("Testing ANOTHER_ODD_NAME_DOCUMENT", new Integer(1234567889), "---", new KualiDecimal(501), createDate(2009, Calendar.APRIL, 20), createTimestamp(2009, Calendar.NOVEMBER, 6, 12, 59, 59), "ThirdState", true),
74 INVALID_STATE_DOCUMENT("Testing INVALID_STATE_DOCUMENT", new Integer(99999), "AAAAAAAAA", new KualiDecimal(2.22), createDate(2009, Calendar.OCTOBER, 21), createTimestamp(2009, Calendar.NOVEMBER, 7, 0, 0, 1), "SeventhState", true),
75 WILDCARD_NAME_DOCUMENT("Testing WILDCARD_NAME_DOCUMENT", new Integer(1), "Sh*ne><K!=e?", new KualiDecimal(771.05), createDate(2054, Calendar.OCTOBER, 22), createTimestamp(2008, Calendar.NOVEMBER, 8, 12, 0, 0), "FirstState", true);
76
77 private String accountDocumentDescription;
78 private Integer accountNumber;
79 private String accountOwner;
80 private KualiDecimal accountBalance;
81 private Date accountOpenDate;
82 private Timestamp accountUpdateDateTime;
83 private String accountState;
84 private boolean accountAwake;
85
86 private DOCUMENT_FIXTURE(String accountDocumentDescription, Integer accountNumber, String accountOwner, KualiDecimal accountBalance, Date accountOpenDate, Timestamp accountUpdateDateTime, String accountState, boolean accountAwake) {
87 this.accountDocumentDescription = accountDocumentDescription;
88 this.accountNumber = accountNumber;
89 this.accountOwner = accountOwner;
90 this.accountBalance = accountBalance;
91 this.accountOpenDate = accountOpenDate;
92 this.accountUpdateDateTime = accountUpdateDateTime;
93 this.accountState = accountState;
94 this.accountAwake = accountAwake;
95 }
96
97 public AccountWithDDAttributesDocument getDocument(DocumentService docService) throws WorkflowException {
98 AccountWithDDAttributesDocument acctDoc = (AccountWithDDAttributesDocument) docService.getNewDocument(ACCOUNT_WITH_DD_ATTRIBUTES_DOCUMENT_NAME);
99 acctDoc.getDocumentHeader().setDocumentDescription(this.accountDocumentDescription);
100 acctDoc.setAccountNumber(this.accountNumber);
101 acctDoc.setAccountOwner(this.accountOwner);
102 acctDoc.setAccountBalance(this.accountBalance);
103 acctDoc.setAccountOpenDate(this.accountOpenDate);
104 acctDoc.setAccountUpdateDateTime(this.accountUpdateDateTime);
105 acctDoc.setAccountState(this.accountState);
106 acctDoc.setAccountAwake(this.accountAwake);
107
108 return acctDoc;
109 }
110 }
111
112
113
114
115 @Test
116 public void testWildcardsAndMultiSelectsOnDDSearchableAttributes() throws Exception {
117 DocumentService docService = KRADServiceLocatorWeb.getDocumentService();
118
119 DocumentType docType = KEWServiceLocator.getDocumentTypeService().findByName("AccountWithDDAttributes");
120 String principalName = "quickstart";
121 String principalId = KimApiServiceLocator.getPersonService().getPersonByPrincipalName(principalName).getPrincipalId();
122
123
124 docService.routeDocument(DOCUMENT_FIXTURE.NORMAL_DOCUMENT.getDocument(docService), "Routing NORMAL_DOCUMENT", null);
125 docService.routeDocument(DOCUMENT_FIXTURE.ZERO_NUMBER_DOCUMENT.getDocument(docService), "Routing ZERO_NUMBER_DOCUMENT", null);
126 docService.routeDocument(DOCUMENT_FIXTURE.FALSE_AWAKE_DOCUMENT.getDocument(docService), "Routing FALSE_AWAKE_DOCUMENT", null);
127 docService.routeDocument(DOCUMENT_FIXTURE.ODD_NAME_DOCUMENT.getDocument(docService), "Routing ODD_NAME_DOCUMENT", null);
128 docService.routeDocument(DOCUMENT_FIXTURE.ODD_TIMESTAMP_DOCUMENT.getDocument(docService), "Routing ODD_TIMESTAMP_DOCUMENT", null);
129 docService.routeDocument(DOCUMENT_FIXTURE.ANOTHER_ODD_NAME_DOCUMENT.getDocument(docService), "Routing ANOTHER_ODD_NAME_DOCUMENT", null);
130 docService.routeDocument(DOCUMENT_FIXTURE.INVALID_STATE_DOCUMENT.getDocument(docService), "Routing INVALID_STATE_DOCUMENT", null);
131 docService.routeDocument(DOCUMENT_FIXTURE.WILDCARD_NAME_DOCUMENT.getDocument(docService), "Routing WILDCARD_NAME_DOCUMENT", null);
132
133
134
135 assertDDSearchableAttributeWildcardsWork(docType, principalId, "accountNumber",
136 new String[] {"!1234567890", "9???9", ">1", "987654321|1234567889", "<100", ">=99999", "<=-42", ">9000|<=1", "<1|>=1234567890",
137 ">1234567889&&<1234567890", ">=88&&<=99999", "0|>10&&<10000", "9000..1000000", "0..100|>1234567889", "1..10000&&>50", "250..50"},
138 new int[] {7 , -1 , 6 , 2 , 3 , 4 , -1 , 6 , 2,
139 0 , 3 , 3 , 2 , 4 , 2 , 0});
140
141
142
143 assertDDSearchableAttributeWildcardsWork(docType, principalId, "accountOwner",
144 new String[] {"!John Doe", "!John*", "!John Doe&&!Shane Kloe", "!Jane ???", "!Jane Doe!John Doe", "_", "_|---", "Sh*ne><K!=e",
145 ">Jane Doe", "<Shane Kloe", ">=Johnny", "<=John D'oh", ">John Doe|<---", ">=AAAAAAAAA&&<=Jane Doe", ">---&&!John D'oh",
146 "<Shane Kloe&&!John*", "*oe", "???? Doe", "Jane Doe..John Doe", "AAAAAAAAA..Shane Kloe&&!John Doe", "John D'oh|---..Jane Doe"},
147 new int[] {7 , 6 , 6 , 7 , 6 , 1 , 2 , 8,
148 5 , 6 , 3 , 4 , 3 , 2 , 6,
149 4 , 3 , 2 , 3 , 5 , 4});
150
151
152 assertDDSearchableAttributeWildcardsWork(docType, principalId, "accountBalance",
153 new String[] {"501.??", "*.54" , "!2.22", "10000051.0|771.05", "<0.0", ">501", "<=4.54", ">=-99.99", ">4.54|<=-1", ">=0&&<501.77", "!",
154 "<=0|>=10000051", ">501&&<501.77", "-100|>771.05", "2.22..501", "-100..4.54&&<=0", "2.22|501.77..10000051.0", "Zero",
155 "-$100", "<(501)&&>=($2.22)", "$4.54|<(1)", "($0.00)..$771.05", ">=$(500)", ")501(", "4.54$", "$501..0"},
156 new int[] {-1 , -1 , 7 , 2 , 1 , 3 , 4 , 7 , 5 , 4 , -1,
157 3 , 0 , 2 , 3 , 2 , 4 , -1,
158 1 , 2 , 3 , 6 , -1 , -1 , -1 , 0});
159
160
161
162 assertDDSearchableAttributeWildcardsWork(docType, principalId, "accountOpenDate",
163 new String[] {"!10/15/2009", "Unknown", "10/15/2009|10/21/2009", "10/22/????", "*/*/05", ">10/17/06", "<=12-31-09&&>=10/16/2009",
164 ">101809&&<102012", ">=10/22/2054|<10/16/2009", ">2-29-12|<=10/21/09", "<2009", ">=10/19/2012|04/20/09", ">2/29/09", "2009..2008",
165 "10/15/2009..10/21/2009", "1/1/2009..10/20/2009|10/22/2054", "<=06/32/03", ">2008&&<2011|10/17/06",
166 "<02/26/10500", ">05-07-333", ">=03/26/1001", "<=11-11-9900"},
167 new int[] {-1 , -1 , 2 , -1 , -1 , 7 , 3,
168 2 , 4 , 8 , 1 , 3 , -1 , -1,
169 4 , 5 , -1 , 6 ,
170 -1 , -1 , 8 , 8});
171
172
173
174 assertDDSearchableAttributeWildcardsWork(docType, principalId, "accountStateMultiselect",
175 new String[][] {{"FirstState"}, {"SecondState"}, {"ThirdState"}, {"FourthState"}, {"FirstState","ThirdState"},
176 {"SecondState","FourthState"}, {"ThirdState","SecondState"}, {"FourthState","FirstState","SecondState"}, {"SeventhState"},
177 {"ThirdState","FirstState","SecondState","FourthState"}},
178 new int[] {2 , 1 , 2 , 2 , 4,
179 3 , 3 , 5 , 1,
180 7});
181
182
183
184 assertDDSearchableAttributeWildcardsWork(docType, principalId, "accountAwake", new String[] {"Y", "N"}, new int[] {6, 2});
185
186
187
188
189
190
191
192
193 assertDDSearchableAttributeWildcardsWork(docType, principalId, "accountUpdateDateTime",
194 new String[] {"!11/01/2009 00:00:00", "11/02/2015 00:00:00|11/06/2009 12:59:59", "11/??/2009 ??:??:??", ">110609 12:59:59",
195 "<=2009 1:2:3", ">=11/06/09 12:59:59", "<11/8/2008 12:00 PM", "Blank",
196 "11/3/1900 00:00:00|>11-7-09 00:00:01", "02/29/2008 07:00:00..11/04/2009 00:00:00",
197 "11/1/09 00:00:00..11/06/09 12:59:59|11/03/1900 00:00:00", "2009..2008", "2000..2009&&>=110507 12:4:38",
198 "<=11/08/2008 12:00 AM", ">=01-01-1000 00:00:00", ">12/31/999 23:59:59", "<01-01-10000 00:00:00", "<=12/31/9999 23:59:59"},
199 new int[] {-1 , 2 , -1 , 2,
200 3 , 3 , 2 , -1,
201 2 , 3,
202 4 , -1 , 2,
203 2 , 8 , -1 , -1 , 8});
204 }
205
206
207
208
209
210
211
212
213
214 private static Date createDate(int year, int month, int day) {
215 Calendar date = Calendar.getInstance();
216 date.set(year, month, day, 0, 0, 0);
217 return new java.sql.Date(date.getTimeInMillis());
218 }
219
220
221
222
223
224
225
226
227
228
229
230
231 private static Timestamp createTimestamp(int year, int month, int day, int hour, int minute, int second) {
232 Calendar date = Calendar.getInstance();
233 date.set(year, month, day, hour, minute, second);
234 return new java.sql.Timestamp(date.getTimeInMillis());
235 }
236
237
238
239
240
241
242
243
244
245
246
247 private void assertDDSearchableAttributeWildcardsWork(DocumentType docType, String principalId, String fieldName, Object[] searchValues,
248 int[] resultSizes) throws Exception {
249 if (!(searchValues instanceof String[]) && !(searchValues instanceof String[][])) {
250 throw new IllegalArgumentException("'searchValues' parameter has to be either a String[] or a String[][]");
251 }
252 DocumentSearchCriteria.Builder criteria = null;
253 DocumentSearchResults results = null;
254 DocumentSearchService docSearchService = KEWServiceLocator.getDocumentSearchService();
255 for (int i = 0; i < resultSizes.length; i++) {
256 if (searchValues[i].toString().equalsIgnoreCase("Zero")) {
257 int num = 1 + 1;
258 }
259 criteria = DocumentSearchCriteria.Builder.create();
260 criteria.setDocumentTypeName(docType.getName());
261 if (searchValues instanceof String[][]) {
262 String[] innerArray = (String[]) searchValues[i];
263 for (int j=0; j<innerArray.length; j++) {
264 criteria.addDocumentAttributeValue(fieldName, innerArray[j]);
265 }
266 } else {
267 criteria.addDocumentAttributeValue(fieldName, searchValues[i].toString());
268 }
269
270 try {
271 results = docSearchService.lookupDocuments(principalId, criteria.build());
272 if (resultSizes[i] < 0) {
273 Assert.fail(fieldName + "'s search at loop index " + i + " should have thrown an exception");
274 }
275 if(resultSizes[i] != results.getSearchResults().size()){
276 assertEquals(fieldName + "'s search results at loop index " + i + " returned the wrong number of documents.", resultSizes[i], results.getSearchResults().size());
277 }
278 } catch (Exception ex) {
279 if (resultSizes[i] >= 0) {
280 LOG.error("exception", ex);
281 Assert.fail(fieldName
282 + "'s search at loop index "
283 + i
284 + " for search value '"
285 + searchValues[i]
286 + "' should not have thrown an exception");
287 }
288 }
289 GlobalVariables.clear();
290 }
291 }
292
293
294
295
296 @Test
297 public void testValidateUserSearchInputsNoCast() throws Exception {
298 DataDictionarySearchableAttribute searchableAttribute = new DataDictionarySearchableAttribute();
299 final DocumentService documentService = KRADServiceLocatorWeb.getDocumentService();
300
301 try {
302 AccountWithDDAttributesDocument document = DOCUMENT_FIXTURE.NORMAL_DOCUMENT.getDocument(documentService);
303 documentService.saveDocument(document);
304 final String documentNumber = document.getDocumentNumber();
305 } catch (UnknownDocumentTypeException udte) {
306 fail("CI failure - https://jira.kuali.org/browse/KULRICE-9289 " + udte.getMessage() + ExceptionUtils.getStackTrace(udte));
307 }
308
309 Exception caughtException;
310 List foundErrors;
311
312 caughtException = null;
313 foundErrors = new ArrayList();
314 DocumentSearchCriteria.Builder criteria = DocumentSearchCriteria.Builder.create();
315 criteria.setDocumentTypeName(ACCOUNT_WITH_DD_ATTRIBUTES_DOCUMENT_NAME);
316 Map<String, List<String>> simpleParamMap = new HashMap<String, List<String>>();
317 simpleParamMap.put("accountState", Collections.singletonList("FirstState"));
318 criteria.setDocumentAttributeValues(simpleParamMap);
319 try {
320 foundErrors = searchableAttribute.validateDocumentAttributeCriteria(null, criteria.build());
321 } catch (RuntimeException re) {
322 caughtException = re;
323 }
324 Assert.assertNull("Found Exception " + caughtException, caughtException);
325 Assert.assertTrue("There were errors: " + foundErrors, (foundErrors == null || foundErrors.isEmpty()));
326
327 caughtException = null;
328 foundErrors = new ArrayList();
329 Map<String, List<String>> listParamMap = new HashMap<String, List<String>>();
330 List<String> paramValues = new ArrayList<String>();
331 paramValues.add("FirstState");
332 paramValues.add("SecondState");
333 listParamMap.put("accountState", paramValues);
334 criteria.setDocumentAttributeValues(listParamMap);
335 try {
336 foundErrors = searchableAttribute.validateDocumentAttributeCriteria(null, criteria.build());
337 } catch (RuntimeException re) {
338 caughtException = re;
339 }
340 Assert.assertNull("Found Exception " + caughtException, caughtException);
341 Assert.assertTrue("There were errors: " + foundErrors, (foundErrors == null || foundErrors.isEmpty()));
342 }
343
344
345
346
347 @Test
348 public void testErrorMessageResolution() throws Exception {
349 final DataDictionarySearchableAttribute searchableAttribute = new DataDictionarySearchableAttribute();
350 final DocumentSearchCriteria.Builder criteria = DocumentSearchCriteria.Builder.create();
351
352
353
354
355 List<RemotableAttributeError> errors = GlobalVariables.doInNewGlobalVariables(new Callable<List<RemotableAttributeError>>() {
356 public List<RemotableAttributeError> call() {
357 GlobalVariables.getMessageMap().putError("fake.property", "error.custom", "the error message");
358 return searchableAttribute.validateDocumentAttributeCriteria(null, criteria.build());
359 }
360 });
361 Assert.assertEquals(1, errors.size());
362 assertEquals("the error message", errors.get(0).getMessage());
363 }
364
365
366
367
368 @Test
369 public void testMultiSelectIntegration() throws Exception {
370 final DocumentService docService = KRADServiceLocatorWeb.getDocumentService();
371
372 DocumentType docType = KEWServiceLocator.getDocumentTypeService().findByName("AccountWithDDAttributes");
373 String principalName = "quickstart";
374 String principalId = KimApiServiceLocator.getPersonService().getPersonByPrincipalName(principalName).getPrincipalId();
375
376
377 docService.routeDocument(DOCUMENT_FIXTURE.NORMAL_DOCUMENT.getDocument(docService), "Routing NORMAL_DOCUMENT", null);
378
379 assertDDSearchableAttributeWildcardsWork(docType, principalId, "accountStateMultiselect",
380 new String[][] {{"FirstState"}, {"SecondState"}, {"ThirdState"}, {"FourthState"}, {"FirstState", "SecondState"}, {"FirstState","ThirdState"}, {"FirstState", "FourthState"}, {"SecondState", "ThirdState"}, {"SecondState", "FourthState"}, {"ThirdState", "FourthState"}, {"FirstState", "SecondState", "ThirdState"}, {"FirstState", "ThirdState", "FourthState"}, {"SecondState", "ThirdState", "FourthState"}, {"FirstState","SecondState", "ThirdState", "FourthState"}},
381 new int[] { 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 1 });
382
383 assertDDSearchableAttributeWildcardsWork(docType, principalId, "accountOpenDate",
384 new String[][] {{"10/15/2009"}, {"10/15/2009","10/17/2009"}, {"10/14/2009","10/16/2009"}},
385 new int[] { 1, 1, 0 });
386
387 assertDDSearchableAttributeWildcardsWork(docType, principalId, "accountBalance",
388 new String[][] {{"501.77"},{"501.77", "63.54"},{"501.78","501.74"}, {"502.00"}, {"0.00"} },
389 new int[] { 1, 1, 0, 0, 0 });
390
391 assertDDSearchableAttributeWildcardsWork(docType, principalId, "accountNumber",
392 new String[][] {{"1234567890"},{"1234567890", "9876543210"},{"9876543210","77774"}, {"88881"}, {"0"} },
393 new int[] { 1, 1, 0, 0, 0 });
394 }
395 }