1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kew.docsearch;
17
18 import java.util.Arrays;
19 import java.util.HashMap;
20 import java.util.Iterator;
21 import java.util.List;
22 import java.util.Map;
23
24 import org.junit.Ignore;
25 import org.junit.Test;
26 import org.kuali.rice.kew.KEWHtmlUnitTestCase;
27 import org.kuali.rice.kew.util.Utilities;
28
29 import com.gargoylesoftware.htmlunit.ElementNotFoundException;
30 import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
31 import com.gargoylesoftware.htmlunit.html.HtmlInput;
32 import com.gargoylesoftware.htmlunit.html.HtmlPage;
33
34
35
36
37
38
39
40 @Ignore
41 public class DocumentSearchActionTest extends KEWHtmlUnitTestCase {
42
43 private static final String DOCUMENT_SEARCH_FORM_NAME = "DocumentSearchForm";
44 private static final String DOCUMENT_TYPE_NAME_HIDE_SPECIFIC = "DocumentSearchActionTest_HideSpecific";
45 private static final String DOCUMENT_TYPE_NAME_HIDE_ALL = "DocumentSearchActionTest_HideAll";
46 private static final String DOCUMENT_TYPE_NAME_NO_PROCESSOR = "DocumentSearchActionTest_NoProcessor";
47 private static final String SEARCH_ATTRIBUTE_FORM_FIELD_PROPERTY_NAME = "propertyField[0].value";
48
49 protected void loadTestData() throws Exception {
50 loadXmlFile("DocumentSearchWebConfig.xml");
51 }
52
53 @Test public void testCustomSearchCriteriaProcessor_HideSpecificFields() throws Exception {
54 Map<String,String> expectedValues = new HashMap<String,String>();
55 expectedValues.put("criteria.docTypeFullName", DOCUMENT_TYPE_NAME_HIDE_SPECIFIC);
56
57 HtmlPage basicSearchPage = performLogin(ADMIN_USER_NETWORK_ID, "/kew/DocumentSearch.do?criteria.docTypeFullName=" + DOCUMENT_TYPE_NAME_HIDE_SPECIFIC);
58 checkStandardCriteriaFields(basicSearchPage, expectedValues, Arrays.asList(new String[]{"criteria.docTypeFullName","criteria.initiator","fromDateCreated"}), Arrays.asList(new String[]{}));
59 verifySearchableAttribute(true,basicSearchPage, SEARCH_ATTRIBUTE_FORM_FIELD_PROPERTY_NAME, expectedValues.get(SEARCH_ATTRIBUTE_FORM_FIELD_PROPERTY_NAME), true);
60 HtmlPage basicSearchResultPage = (HtmlPage) basicSearchPage.getFormByName(DOCUMENT_SEARCH_FORM_NAME).getInputByName("methodToCall.doDocSearch").click();
61 checkStandardCriteriaFields(basicSearchResultPage, expectedValues, Arrays.asList(new String[]{"criteria.docTypeFullName","criteria.initiator","fromDateCreated"}), Arrays.asList(new String[]{}));
62 verifySearchableAttribute(true,basicSearchResultPage, SEARCH_ATTRIBUTE_FORM_FIELD_PROPERTY_NAME, expectedValues.get(SEARCH_ATTRIBUTE_FORM_FIELD_PROPERTY_NAME), true);
63
64
65 HtmlPage advancedSearchPage = (HtmlPage)((HtmlAnchor) basicSearchResultPage.getHtmlElementById("searchType")).click();
66 checkStandardCriteriaFields(advancedSearchPage, expectedValues, Arrays.asList(new String[]{"criteria.docTypeFullName","criteria.initiator","criteria.docTitle"}), Arrays.asList(new String[]{}));
67 verifySearchableAttribute(true,advancedSearchPage, SEARCH_ATTRIBUTE_FORM_FIELD_PROPERTY_NAME, expectedValues.get(SEARCH_ATTRIBUTE_FORM_FIELD_PROPERTY_NAME), false);
68 HtmlPage advancedSearchResultPage = (HtmlPage) advancedSearchPage.getFormByName(DOCUMENT_SEARCH_FORM_NAME).getInputByName("methodToCall.doDocSearch").click();
69 checkStandardCriteriaFields(advancedSearchResultPage, expectedValues, Arrays.asList(new String[]{"criteria.docTypeFullName","criteria.initiator","criteria.docTitle"}), Arrays.asList(new String[]{}));
70 verifySearchableAttribute(true,advancedSearchResultPage, SEARCH_ATTRIBUTE_FORM_FIELD_PROPERTY_NAME, expectedValues.get(SEARCH_ATTRIBUTE_FORM_FIELD_PROPERTY_NAME), false);
71
72
73 HtmlPage basicSearchPage2 = (HtmlPage)((HtmlAnchor) advancedSearchResultPage.getHtmlElementById("searchType")).click();
74 checkStandardCriteriaFields(basicSearchPage2, expectedValues, Arrays.asList(new String[]{"criteria.docTypeFullName","criteria.initiator","fromDateCreated"}), Arrays.asList(new String[]{}));
75 verifySearchableAttribute(true,basicSearchPage2, SEARCH_ATTRIBUTE_FORM_FIELD_PROPERTY_NAME, expectedValues.get(SEARCH_ATTRIBUTE_FORM_FIELD_PROPERTY_NAME), true);
76 HtmlPage basicSearchResultPage2 = (HtmlPage) basicSearchPage2.getFormByName(DOCUMENT_SEARCH_FORM_NAME).getInputByName("methodToCall.doDocSearch").click();
77 checkStandardCriteriaFields(basicSearchResultPage2, expectedValues, Arrays.asList(new String[]{"criteria.docTypeFullName","criteria.initiator","fromDateCreated"}), Arrays.asList(new String[]{}));
78 verifySearchableAttribute(true,basicSearchResultPage2, SEARCH_ATTRIBUTE_FORM_FIELD_PROPERTY_NAME, expectedValues.get(SEARCH_ATTRIBUTE_FORM_FIELD_PROPERTY_NAME), true);
79 }
80
81 @Test public void testCustomSearchCriteriaProcessor_HideSpecificFields_Clear() throws Exception {
82 Map<String,String> expectedValues = new HashMap<String,String>();
83 expectedValues.put("criteria.docTypeFullName", DOCUMENT_TYPE_NAME_HIDE_SPECIFIC);
84 Map<String,String> expectedValuesAfterClear = new HashMap<String,String>();
85 expectedValuesAfterClear.put("criteria.docTypeFullName", "");
86
87
88 HtmlPage basicSearchPage = performLogin(ADMIN_USER_NETWORK_ID, "DocumentSearch.do?criteria.docTypeFullName=" + DOCUMENT_TYPE_NAME_HIDE_SPECIFIC);
89 checkStandardCriteriaFields(basicSearchPage, expectedValues, Arrays.asList(new String[]{"criteria.docTypeFullName","criteria.initiator","fromDateCreated"}), Arrays.asList(new String[]{}));
90 verifySearchableAttribute(true,basicSearchPage, SEARCH_ATTRIBUTE_FORM_FIELD_PROPERTY_NAME, expectedValues.get(SEARCH_ATTRIBUTE_FORM_FIELD_PROPERTY_NAME), true);
91
92 HtmlPage basicSearchClearedPage = (HtmlPage) basicSearchPage.getFormByName(DOCUMENT_SEARCH_FORM_NAME).getInputByName("methodToCall.clear").click();
93 checkStandardCriteriaFields(basicSearchClearedPage, expectedValuesAfterClear, Arrays.asList(new String[]{"criteria.docTypeFullName"}), Arrays.asList(new String[]{"criteria.initiator","fromDateCreated"}));
94 verifySearchableAttribute(false,basicSearchClearedPage, SEARCH_ATTRIBUTE_FORM_FIELD_PROPERTY_NAME, expectedValues.get(SEARCH_ATTRIBUTE_FORM_FIELD_PROPERTY_NAME), true);
95
96
97 HtmlPage advancedSearchPage = getPage("DocumentSearch.do?isAdvancedSearch=" + DocSearchCriteriaDTO.ADVANCED_SEARCH_INDICATOR_STRING + "&criteria.docTypeFullName=" + DOCUMENT_TYPE_NAME_HIDE_SPECIFIC);
98 checkStandardCriteriaFields(advancedSearchPage, expectedValues, Arrays.asList(new String[]{"criteria.docTypeFullName","criteria.initiator","criteria.docTitle"}), Arrays.asList(new String[]{}));
99 verifySearchableAttribute(true,advancedSearchPage, SEARCH_ATTRIBUTE_FORM_FIELD_PROPERTY_NAME, expectedValues.get(SEARCH_ATTRIBUTE_FORM_FIELD_PROPERTY_NAME), false);
100
101 HtmlPage advancedSearchClearedPage = (HtmlPage) advancedSearchPage.getFormByName(DOCUMENT_SEARCH_FORM_NAME).getInputByName("methodToCall.clear").click();
102 checkStandardCriteriaFields(advancedSearchClearedPage, expectedValuesAfterClear, Arrays.asList(new String[]{"criteria.docTypeFullName"}), Arrays.asList(new String[]{"criteria.initiator","fromDateCreated"}));
103 verifySearchableAttribute(false,advancedSearchClearedPage, SEARCH_ATTRIBUTE_FORM_FIELD_PROPERTY_NAME, expectedValues.get(SEARCH_ATTRIBUTE_FORM_FIELD_PROPERTY_NAME), true);
104 }
105
106 @Test public void testCustomSearchCriteriaProcessor_HideAllFieldsByProcessor() throws Exception {
107 verifyHideAllFieldsSearch(DOCUMENT_TYPE_NAME_HIDE_ALL,"");
108 }
109
110 @Test public void testCustomSearchCriteriaProcessor_HideAllFieldsByUrlParams() throws Exception {
111 verifyHideAllFieldsSearch(DOCUMENT_TYPE_NAME_NO_PROCESSOR,"searchCriteriaEnabled=false&headerBarEnabled=false");
112 }
113
114 private void verifyHideAllFieldsSearch(String documentTypeName, String urlParameters) throws Exception {
115 Map<String,String> expectedValues = new HashMap<String,String>();
116 expectedValues.put("criteria.docTypeFullName", documentTypeName);
117 expectedValues.put("criteria.initiator", "delyea");
118 String idType = "principalName";
119 String searchAttributeUrlValue = "CURRENT_USER." + idType;
120 expectedValues.put(SEARCH_ATTRIBUTE_FORM_FIELD_PROPERTY_NAME, ADMIN_USER_NETWORK_ID);
121
122
123 HtmlPage basicSearchPage = performLogin(ADMIN_USER_NETWORK_ID, "DocumentSearch.do?criteria.initiator=delyea&criteria.docTypeFullName=" + documentTypeName + "&searchableAttributes=givenname:" + searchAttributeUrlValue + ((Utilities.isEmpty(urlParameters)) ? "" : "&" + urlParameters));
124 checkStandardCriteriaFields(basicSearchPage, expectedValues, Arrays.asList(new String[]{"criteria.docTypeFullName","criteria.initiator","fromDateCreated"}), Arrays.asList(new String[]{}));
125 verifySearchableAttribute(true,basicSearchPage, SEARCH_ATTRIBUTE_FORM_FIELD_PROPERTY_NAME, expectedValues.get(SEARCH_ATTRIBUTE_FORM_FIELD_PROPERTY_NAME), false);
126
127 try {
128 basicSearchPage.getHtmlElementById("headerTable");
129 fail("Header Table should not exist on basic search page if criteria processor is working properly");
130 } catch (ElementNotFoundException e) {
131
132 }
133
134
135 HtmlPage advancedSearchPage = getPage("DocumentSearch.do?criteria.initiator=delyea&isAdvancedSearch=" + DocSearchCriteriaDTO.ADVANCED_SEARCH_INDICATOR_STRING + "&criteria.docTypeFullName=" + documentTypeName + "&searchableAttributes=givenname:" + searchAttributeUrlValue + ((Utilities.isEmpty(urlParameters)) ? "" : "&" + urlParameters));
136 checkStandardCriteriaFields(advancedSearchPage, expectedValues, Arrays.asList(new String[]{"criteria.docTypeFullName","criteria.initiator","fromDateCreated","criteria.appDocId","criteria.docTitle"}), Arrays.asList(new String[]{}));
137 verifySearchableAttribute(true,advancedSearchPage, SEARCH_ATTRIBUTE_FORM_FIELD_PROPERTY_NAME, expectedValues.get(SEARCH_ATTRIBUTE_FORM_FIELD_PROPERTY_NAME), false);
138
139 try {
140 advancedSearchPage.getHtmlElementById("headerTable");
141 fail("Header Table should not exist on advanced search page if criteria processor is working properly");
142 } catch (ElementNotFoundException e) {
143
144 }
145 }
146
147
148
149
150 private void checkStandardCriteriaFields(HtmlPage searchPage, Map<String,String> expectedFieldValuesByKey, List<String> hiddenFieldAttributeNames, List<String> shownFieldAttributeNames) {
151 assertEquals("Should be one form.", 1, searchPage.getForms().size());
152 for (Iterator iterator = shownFieldAttributeNames.iterator(); iterator.hasNext();) {
153 String fieldAttributeName = (String) iterator.next();
154 HtmlInput input = searchPage.getFormByName(DOCUMENT_SEARCH_FORM_NAME).getInputByName(fieldAttributeName);
155 assertFalse("Field with attribute name '" + fieldAttributeName + "' should not be hidden","hidden".equals(input.getTypeAttribute()));
156 if (expectedFieldValuesByKey.containsKey(fieldAttributeName)) {
157 assertEquals("Field with attribute name '" + fieldAttributeName + "' has incorrect value", expectedFieldValuesByKey.get(fieldAttributeName), input.getValueAttribute());
158 }
159 }
160 for (Iterator iter = hiddenFieldAttributeNames.iterator(); iter.hasNext();) {
161 String fieldAttributeName = (String) iter.next();
162 HtmlInput input = searchPage.getFormByName(DOCUMENT_SEARCH_FORM_NAME).getInputByName(fieldAttributeName);
163 assertTrue("Field with attribute name '" + fieldAttributeName + "' should be hidden","hidden".equals(input.getTypeAttribute()));
164 if (expectedFieldValuesByKey.containsKey(fieldAttributeName)) {
165 assertEquals("Field with attribute name '" + fieldAttributeName + "' has incorrect value", expectedFieldValuesByKey.get(fieldAttributeName), input.getValueAttribute());
166 }
167 }
168 }
169
170 private void verifySearchableAttribute(boolean elementShouldExist, HtmlPage searchPage, String propertyName, String expectedValue, boolean shouldHaveTextField) {
171 List searchAttributeInputs = searchPage.getFormByName(DOCUMENT_SEARCH_FORM_NAME).getInputsByName(propertyName);
172 if (!elementShouldExist) {
173
174 if (!searchAttributeInputs.isEmpty()) {
175 fail("Element with propertyName '" + propertyName + "' should not exist on form but does");
176 }
177 return;
178 } else if (elementShouldExist && searchAttributeInputs.isEmpty()) {
179 fail("Element with propertyName '" + propertyName + "' should exist on form but does not");
180 }
181 boolean foundDisplayedTextField = false;
182 for (Iterator iterator = searchAttributeInputs.iterator(); iterator.hasNext();) {
183 HtmlInput searchAttribute = (HtmlInput) iterator.next();
184 if (expectedValue != null) {
185 assertEquals("Searchable Attribute with propertyName '" + propertyName + "' should have been on search page",expectedValue,searchAttribute.getValueAttribute());
186 }
187 if (!"hidden".equals(searchAttribute.getTypeAttribute())) {
188 foundDisplayedTextField = true;
189 }
190 }
191 if (shouldHaveTextField) {
192 assertTrue("Searchable Attribute with propertyName '" + propertyName + "' should have had at least one field on the form that was displayed and not hidden",foundDisplayedTextField);
193 } else {
194 assertFalse("Searchable Attribute with propertyName '" + propertyName + "' should not have had any non-hidden field types on form",foundDisplayedTextField);
195 }
196 }
197
198 }