001    /**
002     * Copyright 2005-2011 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.rice.krad.workflow;
017    
018    import org.junit.Test;
019    import org.kuali.rice.core.api.uif.RemotableAttributeField;
020    import org.kuali.rice.kew.api.document.search.DocumentSearchCriteria;
021    import org.kuali.rice.kew.api.document.search.DocumentSearchResults;
022    import org.kuali.rice.kew.docsearch.service.DocumentSearchService;
023    import org.kuali.rice.kew.doctype.bo.DocumentType;
024    import org.kuali.rice.kew.engine.RouteContext;
025    import org.kuali.rice.kew.framework.document.attribute.SearchableAttribute;
026    import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
027    import org.kuali.rice.kew.service.KEWServiceLocator;
028    import org.kuali.rice.kim.api.services.KimApiServiceLocator;
029    import org.kuali.rice.krad.UserSession;
030    import org.kuali.rice.krad.service.DocumentService;
031    import org.kuali.rice.krad.service.KRADServiceLocator;
032    import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
033    import org.kuali.rice.krad.test.document.SearchAttributeIndexTestDocument;
034    import org.kuali.rice.krad.util.GlobalVariables;
035    import org.kuali.test.KRADTestCase;
036    
037    import static org.junit.Assert.assertEquals;
038    import static org.junit.Assert.fail;
039    
040    /**
041     * This is a description of what this class does - jksmith don't forget to fill this in. 
042     * 
043     * @author Kuali Rice Team (rice.collab@kuali.org)
044     *
045     */
046    public class SearchAttributeIndexRequestTest extends KRADTestCase {
047            static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(SearchAttributeIndexRequestTest.class);
048            final static String SEARCH_ATTRIBUTE_INDEX_DOCUMENT_TEST_DOC_TYPE = "SearchAttributeIndexTestDocument";
049            
050            enum DOCUMENT_FIXTURE {
051                    NORMAL_DOCUMENT("hippo","routing");
052                    
053                    private String constantString;
054                    private String routingString;
055                    private DOCUMENT_FIXTURE(String constantString, String routingString) {
056                            this.constantString = constantString;
057                            this.routingString = routingString;
058                    }
059                    
060                    public SearchAttributeIndexTestDocument getDocument(DocumentService documentService) throws Exception {
061                            SearchAttributeIndexTestDocument doc = (SearchAttributeIndexTestDocument)documentService.getNewDocument(SearchAttributeIndexRequestTest.SEARCH_ATTRIBUTE_INDEX_DOCUMENT_TEST_DOC_TYPE);
062                            doc.initialize(constantString, routingString);
063                            return doc;
064                    }
065            }
066            
067            /**
068             * Tests that a document, which goes through a regular approval process, is indexed correctly
069             */
070            @Test
071            public void regularApproveTest() throws Exception {
072                    LOG.warn("message.delivery state: "+ KRADServiceLocator.getKualiConfigurationService().getPropertyValueAsString(
073                    "message.delivery"));
074                    
075                    final DocumentService documentService = KRADServiceLocatorWeb.getDocumentService();
076                    final String principalName = "quickstart";
077            final String principalId = KimApiServiceLocator.getPersonService().getPersonByPrincipalName(principalName).getPrincipalId();
078            GlobalVariables.setUserSession(new UserSession(principalName));
079            RouteContext.clearCurrentRouteContext();
080    
081                    SearchAttributeIndexTestDocument document = DOCUMENT_FIXTURE.NORMAL_DOCUMENT.getDocument(documentService);
082                    document.getDocumentHeader().setDocumentDescription("Routed SAIndexTestDoc");
083                    final String documentNumber = document.getDocumentNumber();
084                    final DocumentType docType = KEWServiceLocator.getDocumentTypeService().findByName(SearchAttributeIndexRequestTest.SEARCH_ATTRIBUTE_INDEX_DOCUMENT_TEST_DOC_TYPE);
085                    
086                    documentService.routeDocument(document, "Routed SearchAttributeIndexTestDocument", null);
087                    
088                    document = (SearchAttributeIndexTestDocument)documentService.getByDocumentHeaderId(documentNumber);
089                    DocumentRouteHeaderValue routeHeader = KEWServiceLocator.getRouteHeaderService().getRouteHeader(documentNumber);
090                                                    
091                    assertDDSearchableAttributesWork(docType,principalId,"routeLevelCount",
092                                    new String[] {"1","0","2","7"},
093                                    new int[] {1, 0, 0, 0}
094                    );
095                    
096                    assertDDSearchableAttributesWork(docType,principalId,"constantString",
097                                    new String[] {"hippo","monkey"},
098                                    new int[] {1, 0}
099                    );
100                    
101                    assertDDSearchableAttributesWork(docType,principalId,"routedString",
102                                    new String[] {"routing","hippo"},
103                                    new int[] {1, 0}
104                    );
105                    
106                    GlobalVariables.setUserSession(new UserSession("user1"));
107                    document = (SearchAttributeIndexTestDocument)documentService.getByDocumentHeaderId(documentNumber);
108                    documentService.approveDocument(document, "User1 approved document", null);
109                    
110                    routeHeader = KEWServiceLocator.getRouteHeaderService().getRouteHeader(documentNumber);
111                                                    
112                    assertDDSearchableAttributesWork(docType,principalId,"routeLevelCount",
113                                    new String[] {"1","0","2","7"},
114                                    new int[] {0, 0, 1, 0}
115                    );
116                    
117                    assertDDSearchableAttributesWork(docType,principalId,"constantString",
118                                    new String[] {"hippo","monkey"},
119                                    new int[] {1, 0}
120                    );
121                    
122                    assertDDSearchableAttributesWork(docType,principalId,"routedString",
123                                    new String[] {"routing","hippo"},
124                                    new int[] {1, 0}
125                    );
126                    
127                    LOG.info("Read Access Count not at expected value: "+document.getReadAccessCount());
128                    
129                    GlobalVariables.setUserSession(new UserSession("user2"));
130                    document = (SearchAttributeIndexTestDocument)documentService.getByDocumentHeaderId(documentNumber);
131                    documentService.approveDocument(document, "User1 approved document", null);
132                    
133                    routeHeader = KEWServiceLocator.getRouteHeaderService().getRouteHeader(documentNumber);
134                                    
135                    assertDDSearchableAttributesWork(docType,principalId,"routeLevelCount",
136                                    new String[] {"1","0","2","3","4","7"},
137                                    new int[] {0, 0, 0, 1, 0, 0}
138                    );
139                    
140                    assertDDSearchableAttributesWork(docType,principalId,"constantString",
141                                    new String[] {"hippo","monkey"},
142                                    new int[] {1, 0}
143                    );
144                    
145                    assertDDSearchableAttributesWork(docType,principalId,"routedString",
146                                    new String[] {"routing","hippo"},
147                                    new int[] {1, 0}
148                    );
149                    
150                    LOG.info("Read Access Count not at expected value: "+document.getReadAccessCount());
151                    
152                    GlobalVariables.setUserSession(new UserSession("user3"));
153                    document = (SearchAttributeIndexTestDocument)documentService.getByDocumentHeaderId(documentNumber);
154                    documentService.approveDocument(document, "User3 approved document", null);
155                    
156                    routeHeader = KEWServiceLocator.getRouteHeaderService().getRouteHeader(documentNumber);
157                                                    
158                    assertDDSearchableAttributesWork(docType,principalId,"routeLevelCount",
159                                    new String[] {"1","0","2","3","4","7"},
160                                    new int[] {0, 0, 0, 1, 0, 0}
161                    );
162                    
163                    assertDDSearchableAttributesWork(docType,principalId,"constantString",
164                                    new String[] {"hippo","monkey"},
165                                    new int[] {1, 0}
166                    );
167                    
168                    assertDDSearchableAttributesWork(docType,principalId,"routedString",
169                                    new String[] {"routing","hippo"},
170                                    new int[] {1, 0}
171                    );
172                    
173                    LOG.info("Read Access Count not at expected value: "+document.getReadAccessCount());
174                    
175                    GlobalVariables.setUserSession(null);
176            }
177            
178            /**
179             * Tests that a blanket approved document is indexed correctly
180             */
181            @Test
182            public void blanketApproveTest() throws Exception {
183                    LOG.warn("message.delivery state: "+ KRADServiceLocator.getKualiConfigurationService().getPropertyValueAsString(
184                    "message.delivery"));
185                    
186                    final DocumentService documentService = KRADServiceLocatorWeb.getDocumentService();
187                    final String principalName = "admin";
188            final String principalId = KimApiServiceLocator.getPersonService().getPersonByPrincipalName(principalName).getPrincipalId();
189            GlobalVariables.setUserSession(new UserSession(principalName));
190    
191                    SearchAttributeIndexTestDocument document = DOCUMENT_FIXTURE.NORMAL_DOCUMENT.getDocument(documentService);
192                    document.getDocumentHeader().setDocumentDescription("Blanket Approved SAIndexTestDoc");
193                    final String documentNumber = document.getDocumentNumber();
194                    final DocumentType docType = KEWServiceLocator.getDocumentTypeService().findByName(SearchAttributeIndexRequestTest.SEARCH_ATTRIBUTE_INDEX_DOCUMENT_TEST_DOC_TYPE);
195                                    
196                    documentService.blanketApproveDocument(document, "Blanket Approved SearchAttributeIndexTestDocument", null);
197                    
198                    document = (SearchAttributeIndexTestDocument)documentService.getByDocumentHeaderId(documentNumber);
199                    DocumentRouteHeaderValue routeHeader = KEWServiceLocator.getRouteHeaderService().getRouteHeader(documentNumber);
200                                                    
201                    assertDDSearchableAttributesWork(docType,principalId,"routeLevelCount",
202                                    new String[] {"1","0","2","3","7"},
203                                    new int[] {0, 0, 0, 1, 0}
204                    );
205                    
206                    assertDDSearchableAttributesWork(docType,principalId,"constantString",
207                                    new String[] {"hippo","monkey"},
208                                    new int[] {1, 0}
209                    );
210                    
211                    assertDDSearchableAttributesWork(docType,principalId,"routedString",
212                                    new String[] {"routing","hippo"},
213                                    new int[] {1, 0}
214                    );
215                    
216                    LOG.info("Read Access Count not at expected value: "+document.getReadAccessCount());
217                    
218                    GlobalVariables.setUserSession(null);
219            }
220            
221            /**
222         * A convenience method for testing wildcards on data dictionary searchable attributes.
223         *
224         * @param docType The document type containing the attributes.
225         * @param principalId The ID of the user performing the search.
226         * @param fieldName The name of the field on the test document.
227         * @param searchValues The search expressions to test. Has to be a String array (for regular fields) or a String[] array (for multi-select fields).
228         * @param resultSizes The number of expected documents to be returned by the search; use -1 to indicate that an error should have occurred.
229         * @throws Exception
230         */
231        private void assertDDSearchableAttributesWork(DocumentType docType, String principalId, String fieldName, Object[] searchValues,
232                    int[] resultSizes) throws Exception {
233            if (!(searchValues instanceof String[]) && !(searchValues instanceof String[][])) {
234                    throw new IllegalArgumentException("'searchValues' parameter has to be either a String[] or a String[][]");
235            }
236            DocumentSearchCriteria.Builder criteria = null;
237            DocumentSearchResults results = null;
238            DocumentSearchService docSearchService = KEWServiceLocator.getDocumentSearchService();
239            for (int i = 0; i < resultSizes.length; i++) {
240                    criteria = DocumentSearchCriteria.Builder.create();
241                    criteria.setDocumentTypeName(docType.getName());
242                    criteria.addDocumentAttributeValue(fieldName, searchValues[i].toString());
243                    try {
244                            results = docSearchService.lookupDocuments(principalId, criteria.build());
245                            if (resultSizes[i] < 0) {
246                                    fail(fieldName + "'s search at loop index " + i + " should have thrown an exception");
247                            }
248                            if(resultSizes[i] != results.getSearchResults().size()){
249                                    assertEquals(fieldName + "'s search results at loop index " + i + " returned the wrong number of documents.", resultSizes[i], results.getSearchResults().size());
250                            }
251                    } catch (Exception ex) {
252                            if (resultSizes[i] >= 0) {
253                                    fail(fieldName + "'s search at loop index " + i + " should not have thrown an exception");
254                            }
255                    }
256                    GlobalVariables.clear();
257            }
258        }
259    }