Clover Coverage Report - kns-test 2.0.0-SNAPSHOT
Coverage timestamp: Wed Dec 31 1969 19:00:00 EST
109   316   22   15.57
24   226   0.2   3.5
7     3.14  
2    
 
  SearchAttributeIndexRequestTest       Line # 55 104 0% 20 133 0% 0.0
  SearchAttributeIndexRequestTest.DOCUMENT_FIXTURE       Line # 59 5 0% 2 7 0% 0.0
 
No Tests
 
1    /*
2    * Copyright 2006-2011 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.rice.kns.workflow;
17   
18    import org.junit.Test;
19    import org.kuali.rice.core.api.exception.RiceRuntimeException;
20    import org.kuali.rice.kew.docsearch.DocSearchCriteriaDTO;
21    import org.kuali.rice.kew.docsearch.DocSearchUtils;
22    import org.kuali.rice.kew.docsearch.DocumentSearchResult;
23    import org.kuali.rice.kew.docsearch.DocumentSearchResultComponents;
24    import org.kuali.rice.kew.docsearch.SearchAttributeCriteriaComponent;
25    import org.kuali.rice.kew.docsearch.SearchableAttribute;
26    import org.kuali.rice.kew.docsearch.service.DocumentSearchService;
27    import org.kuali.rice.kew.doctype.bo.DocumentType;
28    import org.kuali.rice.kew.engine.RouteContext;
29    import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
30    import org.kuali.rice.kew.service.KEWServiceLocator;
31    import org.kuali.rice.kew.util.KEWConstants;
32    import org.kuali.rice.kim.service.KIMServiceLocator;
33    import org.kuali.rice.kns.UserSession;
34    import org.kuali.rice.kns.service.DocumentService;
35    import org.kuali.rice.kns.service.KNSServiceLocator;
36    import org.kuali.rice.kns.service.KNSServiceLocatorWeb;
37    import org.kuali.rice.kns.test.document.SearchAttributeIndexTestDocument;
38    import org.kuali.rice.kns.util.GlobalVariables;
39    import org.kuali.rice.kns.web.ui.Field;
40    import org.kuali.rice.kns.web.ui.Row;
41    import org.kuali.test.KNSTestCase;
42   
43    import java.util.Arrays;
44    import java.util.List;
45   
46    import static org.junit.Assert.assertEquals;
47    import static org.junit.Assert.fail;
48   
49    /**
50    * This is a description of what this class does - jksmith don't forget to fill this in.
51    *
52    * @author Kuali Rice Team (rice.collab@kuali.org)
53    *
54    */
 
55    public class SearchAttributeIndexRequestTest extends KNSTestCase {
56    static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(SearchAttributeIndexRequestTest.class);
57    final static String SEARCH_ATTRIBUTE_INDEX_DOCUMENT_TEST_DOC_TYPE = "SearchAttributeIndexTestDocument";
58   
 
59    enum DOCUMENT_FIXTURE {
60    NORMAL_DOCUMENT("hippo","routing");
61   
62    private String constantString;
63    private String routingString;
 
64  0 toggle private DOCUMENT_FIXTURE(String constantString, String routingString) {
65  0 this.constantString = constantString;
66  0 this.routingString = routingString;
67    }
68   
 
69  0 toggle public SearchAttributeIndexTestDocument getDocument(DocumentService documentService) throws Exception {
70  0 SearchAttributeIndexTestDocument doc = (SearchAttributeIndexTestDocument)documentService.getNewDocument(SearchAttributeIndexRequestTest.SEARCH_ATTRIBUTE_INDEX_DOCUMENT_TEST_DOC_TYPE);
71  0 doc.initialize(constantString, routingString);
72  0 return doc;
73    }
74    }
75   
76    /**
77    * Tests that a document, which goes through a regular approval process, is indexed correctly
78    */
 
79  0 toggle @Test
80    public void regularApproveTest() throws Exception {
81  0 LOG.warn("message.delivery state: "+ KNSServiceLocator.getKualiConfigurationService().getPropertyString("message.delivery"));
82   
83  0 final DocumentService documentService = KNSServiceLocatorWeb.getDocumentService();
84  0 final String principalName = "quickstart";
85  0 final String principalId = KIMServiceLocator.getPersonService().getPersonByPrincipalName(principalName).getPrincipalId();
86  0 GlobalVariables.setUserSession(new UserSession(principalName));
87  0 RouteContext.clearCurrentRouteContext();
88   
89  0 SearchAttributeIndexTestDocument document = DOCUMENT_FIXTURE.NORMAL_DOCUMENT.getDocument(documentService);
90  0 document.getDocumentHeader().setDocumentDescription("Routed SAIndexTestDoc");
91  0 final String documentNumber = document.getDocumentNumber();
92  0 final DocumentType docType = KEWServiceLocator.getDocumentTypeService().findByName(SearchAttributeIndexRequestTest.SEARCH_ATTRIBUTE_INDEX_DOCUMENT_TEST_DOC_TYPE);
93   
94  0 documentService.routeDocument(document, "Routed SearchAttributeIndexTestDocument", null);
95   
96  0 document = (SearchAttributeIndexTestDocument)documentService.getByDocumentHeaderId(documentNumber);
97  0 DocumentRouteHeaderValue routeHeader = KEWServiceLocator.getRouteHeaderService().getRouteHeader(new Long(documentNumber));
98   
99  0 assertDDSearchableAttributesWork(docType,principalId,"routeLevelCount",
100    new String[] {"1","0","2","7"},
101    new int[] {1, 0, 0, 0}
102    );
103   
104  0 assertDDSearchableAttributesWork(docType,principalId,"constantString",
105    new String[] {"hippo","monkey"},
106    new int[] {1, 0}
107    );
108   
109  0 assertDDSearchableAttributesWork(docType,principalId,"routedString",
110    new String[] {"routing","","hippo"},
111    new int[] {1, 1, 0}
112    );
113   
114  0 GlobalVariables.setUserSession(new UserSession("user1"));
115  0 document = (SearchAttributeIndexTestDocument)documentService.getByDocumentHeaderId(documentNumber);
116  0 documentService.approveDocument(document, "User1 approved document", null);
117   
118  0 routeHeader = KEWServiceLocator.getRouteHeaderService().getRouteHeader(new Long(documentNumber));
119   
120  0 assertDDSearchableAttributesWork(docType,principalId,"routeLevelCount",
121    new String[] {"1","0","2","7"},
122    new int[] {0, 0, 1, 0}
123    );
124   
125  0 assertDDSearchableAttributesWork(docType,principalId,"constantString",
126    new String[] {"hippo","monkey"},
127    new int[] {1, 0}
128    );
129   
130  0 assertDDSearchableAttributesWork(docType,principalId,"routedString",
131    new String[] {"routing","","hippo"},
132    new int[] {1, 1, 0}
133    );
134   
135  0 LOG.info("Read Access Count not at expected value: "+document.getReadAccessCount());
136   
137  0 GlobalVariables.setUserSession(new UserSession("user2"));
138  0 document = (SearchAttributeIndexTestDocument)documentService.getByDocumentHeaderId(documentNumber);
139  0 documentService.approveDocument(document, "User1 approved document", null);
140   
141  0 routeHeader = KEWServiceLocator.getRouteHeaderService().getRouteHeader(new Long(documentNumber));
142   
143  0 assertDDSearchableAttributesWork(docType,principalId,"routeLevelCount",
144    new String[] {"1","0","2","3","4","7"},
145    new int[] {0, 0, 0, 1, 0, 0}
146    );
147   
148  0 assertDDSearchableAttributesWork(docType,principalId,"constantString",
149    new String[] {"hippo","monkey"},
150    new int[] {1, 0}
151    );
152   
153  0 assertDDSearchableAttributesWork(docType,principalId,"routedString",
154    new String[] {"routing","","hippo"},
155    new int[] {1, 1, 0}
156    );
157   
158  0 LOG.info("Read Access Count not at expected value: "+document.getReadAccessCount());
159   
160  0 GlobalVariables.setUserSession(new UserSession("user3"));
161  0 document = (SearchAttributeIndexTestDocument)documentService.getByDocumentHeaderId(documentNumber);
162  0 documentService.approveDocument(document, "User3 approved document", null);
163   
164  0 routeHeader = KEWServiceLocator.getRouteHeaderService().getRouteHeader(new Long(documentNumber));
165   
166  0 assertDDSearchableAttributesWork(docType,principalId,"routeLevelCount",
167    new String[] {"1","0","2","3","4","7"},
168    new int[] {0, 0, 0, 1, 0, 0}
169    );
170   
171  0 assertDDSearchableAttributesWork(docType,principalId,"constantString",
172    new String[] {"hippo","monkey"},
173    new int[] {1, 0}
174    );
175   
176  0 assertDDSearchableAttributesWork(docType,principalId,"routedString",
177    new String[] {"routing","","hippo"},
178    new int[] {1, 1, 0}
179    );
180   
181  0 LOG.info("Read Access Count not at expected value: "+document.getReadAccessCount());
182   
183  0 GlobalVariables.setUserSession(null);
184    }
185   
186    /**
187    * Tests that a blanket approved document is indexed correctly
188    */
 
189  0 toggle @Test
190    public void blanketApproveTest() throws Exception {
191  0 LOG.warn("message.delivery state: "+ KNSServiceLocator.getKualiConfigurationService().getPropertyString("message.delivery"));
192   
193  0 final DocumentService documentService = KNSServiceLocatorWeb.getDocumentService();
194  0 final String principalName = "admin";
195  0 final String principalId = KIMServiceLocator.getPersonService().getPersonByPrincipalName(principalName).getPrincipalId();
196  0 GlobalVariables.setUserSession(new UserSession(principalName));
197   
198  0 SearchAttributeIndexTestDocument document = DOCUMENT_FIXTURE.NORMAL_DOCUMENT.getDocument(documentService);
199  0 document.getDocumentHeader().setDocumentDescription("Blanket Approved SAIndexTestDoc");
200  0 final String documentNumber = document.getDocumentNumber();
201  0 final DocumentType docType = KEWServiceLocator.getDocumentTypeService().findByName(SearchAttributeIndexRequestTest.SEARCH_ATTRIBUTE_INDEX_DOCUMENT_TEST_DOC_TYPE);
202   
203  0 documentService.blanketApproveDocument(document, "Blanket Approved SearchAttributeIndexTestDocument", null);
204   
205  0 document = (SearchAttributeIndexTestDocument)documentService.getByDocumentHeaderId(documentNumber);
206  0 DocumentRouteHeaderValue routeHeader = KEWServiceLocator.getRouteHeaderService().getRouteHeader(new Long(documentNumber));
207   
208  0 assertDDSearchableAttributesWork(docType,principalId,"routeLevelCount",
209    new String[] {"1","0","2","3","7"},
210    new int[] {0, 0, 0, 1, 0}
211    );
212   
213  0 assertDDSearchableAttributesWork(docType,principalId,"constantString",
214    new String[] {"hippo","monkey"},
215    new int[] {1, 0}
216    );
217   
218  0 assertDDSearchableAttributesWork(docType,principalId,"routedString",
219    new String[] {"routing","","hippo"},
220    new int[] {1, 1, 0}
221    );
222   
223  0 LOG.info("Read Access Count not at expected value: "+document.getReadAccessCount());
224   
225  0 GlobalVariables.setUserSession(null);
226    }
227   
228    /**
229    * A convenience method for testing wildcards on data dictionary searchable attributes.
230    *
231    * @param docType The document type containing the attributes.
232    * @param principalId The ID of the user performing the search.
233    * @param fieldName The name of the field on the test document.
234    * @param searchValues The search expressions to test. Has to be a String array (for regular fields) or a String[] array (for multi-select fields).
235    * @param resultSizes The number of expected documents to be returned by the search; use -1 to indicate that an error should have occurred.
236    * @throws Exception
237    */
 
238  0 toggle private void assertDDSearchableAttributesWork(DocumentType docType, String principalId, String fieldName, Object[] searchValues,
239    int[] resultSizes) throws Exception {
240  0 if (!(searchValues instanceof String[]) && !(searchValues instanceof String[][])) {
241  0 throw new IllegalArgumentException("'searchValues' parameter has to be either a String[] or a String[][]");
242    }
243  0 DocSearchCriteriaDTO criteria = null;
244  0 DocumentSearchResultComponents result = null;
245  0 List<DocumentSearchResult> searchResults = null;
246  0 DocumentSearchService docSearchService = KEWServiceLocator.getDocumentSearchService();
247  0 for (int i = 0; i < resultSizes.length; i++) {
248  0 criteria = new DocSearchCriteriaDTO();
249  0 criteria.setDocTypeFullName(docType.getName());
250  0 criteria.addSearchableAttribute(this.createSearchAttributeCriteriaComponent(fieldName, searchValues[i], null, docType));
251  0 try {
252  0 result = docSearchService.getList(principalId, criteria);
253  0 searchResults = result.getSearchResults();
254  0 if (resultSizes[i] < 0) {
255  0 fail(fieldName + "'s search at loop index " + i + " should have thrown an exception");
256    }
257  0 if(resultSizes[i] != searchResults.size()){
258  0 assertEquals(fieldName + "'s search results at loop index " + i + " returned the wrong number of documents.", resultSizes[i], searchResults.size());
259    }
260    } catch (Exception ex) {
261  0 if (resultSizes[i] >= 0) {
262  0 fail(fieldName + "'s search at loop index " + i + " should not have thrown an exception");
263    }
264    }
265  0 GlobalVariables.clear();
266    }
267    }
268   
269    /*
270    * A method similar to the one from DocumentSearchTestBase. The "value" parameter has to be either a String or a String[].
271    */
 
272  0 toggle private SearchAttributeCriteriaComponent createSearchAttributeCriteriaComponent(String key,Object value,Boolean isLowerBoundValue,DocumentType docType) {
273  0 String formKey = (isLowerBoundValue == null) ? key : ((isLowerBoundValue != null && isLowerBoundValue.booleanValue()) ? KEWConstants.SearchableAttributeConstants.RANGE_LOWER_BOUND_PROPERTY_PREFIX + key : KEWConstants.SearchableAttributeConstants.RANGE_UPPER_BOUND_PROPERTY_PREFIX + key);
274  0 String savedKey = key;
275  0 SearchAttributeCriteriaComponent sacc = null;
276  0 if (value instanceof String) {
277  0 sacc = new SearchAttributeCriteriaComponent(formKey,(String)value,savedKey);
278    } else {
279  0 sacc = new SearchAttributeCriteriaComponent(formKey,null,savedKey);
280  0 sacc.setValues(Arrays.asList((String[])value));
281    }
282  0 Field field = getFieldByFormKey(docType, formKey);
283  0 if (field != null) {
284  0 sacc.setSearchableAttributeValue(DocSearchUtils.getSearchableAttributeValueByDataTypeString(field.getFieldDataType()));
285  0 sacc.setRangeSearch(field.isMemberOfRange());
286  0 sacc.setCaseSensitive(!field.isUpperCase());
287  0 sacc.setSearchInclusive(field.isInclusive());
288  0 sacc.setSearchable(field.isIndexedForSearch());
289  0 sacc.setCanHoldMultipleValues(Field.MULTI_VALUE_FIELD_TYPES.contains(field.getFieldType()));
290    }
291  0 return sacc;
292    }
293   
294    /*
295    * A method that was copied from DocumentSearchTestBase.
296    */
 
297  0 toggle private Field getFieldByFormKey(DocumentType docType, String formKey) {
298  0 if (docType == null) {
299  0 return null;
300    }
301  0 for (SearchableAttribute searchableAttribute : docType.getSearchableAttributes()) {
302  0 for (Row row : searchableAttribute.getSearchingRows(DocSearchUtils.getDocumentSearchContext("", docType.getName(), ""))) {
303  0 for (org.kuali.rice.kns.web.ui.Field field : row.getFields()) {
304  0 if (field instanceof Field) {
305  0 if (field.getPropertyName().equals(formKey)) {
306  0 return (Field)field;
307    }
308    } else {
309  0 throw new RiceRuntimeException("Fields must be of type org.kuali.rice.kns.Field");
310    }
311    }
312    }
313    }
314  0 return null;
315    }
316    }