001: /*
002: * Copyright 2005-2007 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.opensource.org/licenses/ecl1.php
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package edu.iu.uis.eden.server;
018:
019: import java.util.HashMap;
020: import java.util.List;
021:
022: import org.apache.commons.lang.StringUtils;
023: import org.junit.Test;
024: import org.kuali.workflow.test.WorkflowTestCase;
025:
026: import edu.iu.uis.eden.EdenConstants;
027: import edu.iu.uis.eden.KEWServiceLocator;
028: import edu.iu.uis.eden.clientapp.vo.DocumentContentVO;
029: import edu.iu.uis.eden.clientapp.vo.WorkflowAttributeDefinitionVO;
030: import edu.iu.uis.eden.clientapp.vo.WorkflowGroupIdVO;
031: import edu.iu.uis.eden.exception.InvalidXmlException;
032: import edu.iu.uis.eden.routetemplate.TestRuleAttribute;
033: import edu.iu.uis.eden.util.Utilities;
034: import edu.iu.uis.eden.workgroup.BaseWorkgroup;
035: import edu.iu.uis.eden.workgroup.GroupId;
036: import edu.iu.uis.eden.workgroup.GroupNameId;
037: import edu.iu.uis.eden.workgroup.Workgroup;
038:
039: public class BeanConverterTester extends WorkflowTestCase {
040:
041: private static final String DOCUMENT_CONTENT = EdenConstants.DOCUMENT_CONTENT_ELEMENT;
042: private static final String ATTRIBUTE_CONTENT = EdenConstants.ATTRIBUTE_CONTENT_ELEMENT;
043: private static final String SEARCHABLE_CONTENT = EdenConstants.SEARCHABLE_CONTENT_ELEMENT;
044: private static final String APPLICATION_CONTENT = EdenConstants.APPLICATION_CONTENT_ELEMENT;
045:
046: @Test
047: public void testConvertWorkflowGroupId() {
048: BaseWorkgroup prototype = new BaseWorkgroup();
049: prototype.setGroupNameId(new GroupNameId("TestWorkgroup"));
050: //prototype.setDisplayName("TestWorkgroup");
051: //prototype.setWorkgroupType("W");
052: //assertNull(prototype.getDisplayName());
053: //assertNull(prototype.getWorkgroupType());
054: List list = KEWServiceLocator.getWorkgroupService().search(
055: prototype, new HashMap<String, String>(), true);
056: assertNotNull(list);
057: assertTrue(list.size() > 0);
058: Workgroup group = (Workgroup) list.get(0);
059: assertNotNull(group);
060: WorkflowGroupIdVO vo = new WorkflowGroupIdVO(group
061: .getWorkflowGroupId().getGroupId());
062: GroupId id = BeanConverter.convertWorkgroupIdVO(vo);
063: assertNotNull(id);
064: }
065:
066: /**
067: * Tests the conversion of a String into a DocumentContentVO object which should split the
068: * String into it's 3 distinct components.
069: */
070: @Test
071: public void testConvertDocumentContent() throws Exception {
072:
073: // test null content
074: String attributeContent = null;
075: String searchableContent = null;
076: String applicationContent = null;
077: String xmlContent = constructContent(attributeContent,
078: searchableContent, applicationContent);
079: DocumentContentVO contentVO = BeanConverter
080: .convertDocumentContent(xmlContent, new Long(-1234));
081: assertFalse("Content cannot be empty.", Utilities
082: .isEmpty(contentVO.getFullContent()));
083: assertEquals("Attribute content is invalid.", "", contentVO
084: .getAttributeContent());
085: assertEquals("Searchable content is invalid.", "", contentVO
086: .getSearchableContent());
087: assertEquals("Application content is invalid.", "", contentVO
088: .getApplicationContent());
089: assertEquals("Should have fake document id.", new Long(-1234),
090: contentVO.getRouteHeaderId());
091:
092: // test empty content
093: attributeContent = "";
094: searchableContent = "";
095: applicationContent = "";
096: contentVO = BeanConverter.convertDocumentContent(
097: constructContent(attributeContent, searchableContent,
098: applicationContent), null);
099: assertContent(contentVO, attributeContent, searchableContent,
100: applicationContent);
101:
102: // test fancy dancy content
103: attributeContent = "<iEnjoyFlexContent><id>1234</id></iEnjoyFlexContent>";
104: searchableContent = "<thisIdBeWarrenG>Warren G</thisIdBeWarrenG><whatsMyName>Snoop</whatsMyName>";
105: applicationContent = "<thisIsTotallyRad><theCoolestContentInTheWorld qualify=\"iSaidSo\">it's marvelous!</theCoolestContentInTheWorld></thisIsTotallyRad>";
106: contentVO = BeanConverter.convertDocumentContent(
107: constructContent(attributeContent, searchableContent,
108: applicationContent), null);
109: assertContent(contentVO, attributeContent, searchableContent,
110: applicationContent);
111:
112: attributeContent = "invalid<xml, I can't believe you would do such a thing<<<";
113: try {
114: contentVO = BeanConverter.convertDocumentContent(
115: constructContent(attributeContent,
116: searchableContent, applicationContent),
117: null);
118: fail("Parsing bad xml should have thrown an InvalidXmlException.");
119: } catch (InvalidXmlException e) {
120: log.info("Expected InvalidXmlException was thrown.");
121: // if we got the exception we are good to go
122: }
123:
124: // test an older style document
125: String appSpecificXml = "<iAmAnOldSchoolApp><myDocContent type=\"custom\">is totally app specific</myDocContent><howIroll>old school, that's how I roll</howIroll></iAmAnOldSchoolApp>";
126: contentVO = BeanConverter.convertDocumentContent(
127: appSpecificXml, null);
128: assertContent(contentVO, "", "", appSpecificXml);
129:
130: // test the old school (Workflow 1.6) flex document XML
131: String fleXml = "<flexdoc><meinAttribute>nein</meinAttribute></flexdoc>";
132: contentVO = BeanConverter.convertDocumentContent(fleXml, null);
133: assertFalse("Content cannot be empty.", Utilities
134: .isEmpty(contentVO.getFullContent()));
135: assertEquals("Attribute content is invalid.", fleXml, contentVO
136: .getAttributeContent());
137: assertEquals("Searchable content is invalid.", "", contentVO
138: .getSearchableContent());
139: assertEquals("Application content is invalid.", "", contentVO
140: .getApplicationContent());
141: }
142:
143: /**
144: * Tests the conversion of a DocumentContentVO object into an XML String. Includes generating content
145: * for any attributes which are on the DocumentContentVO object.
146: *
147: * TODO there is some crossover between this test and the DocumentContentTest, do we really need both of them???
148: */
149: @Test
150: public void testBuildUpdatedDocumentContent() throws Exception {
151: String startContent = "<" + DOCUMENT_CONTENT + ">";
152: String endContent = "</" + DOCUMENT_CONTENT + ">";
153:
154: /*
155: * // test no content, this should return null which indicates an unchanged document content VO
156: * //RouteHeaderVO routeHeaderVO = new RouteHeaderVO();
157: */
158:
159: // test no content, this should return empty document content
160: DocumentContentVO contentVO = new DocumentContentVO();
161: //routeHeaderVO.setDocumentContent(contentVO);
162: String content = BeanConverter
163: .buildUpdatedDocumentContent(contentVO);
164: assertEquals("Invalid content conversion.",
165: EdenConstants.DEFAULT_DOCUMENT_CONTENT, content);
166:
167: // test simple case, no attributes
168: String attributeContent = "<attribute1><id value=\"3\"/></attribute1>";
169: String searchableContent = "<searchable1><data>hello</data></searchable1>";
170: contentVO = new DocumentContentVO();
171: contentVO.setAttributeContent(constructContent(
172: ATTRIBUTE_CONTENT, attributeContent));
173: contentVO.setSearchableContent(constructContent(
174: SEARCHABLE_CONTENT, searchableContent));
175: content = BeanConverter.buildUpdatedDocumentContent(contentVO);
176: String fullContent = startContent
177: + constructContent(ATTRIBUTE_CONTENT, attributeContent)
178: + constructContent(SEARCHABLE_CONTENT,
179: searchableContent) + endContent;
180: assertEquals("Invalid content conversion.", StringUtils
181: .deleteWhitespace(fullContent), StringUtils
182: .deleteWhitespace(content));
183:
184: // now, add an attribute
185: String testAttributeContent = new TestRuleAttribute()
186: .getDocContent();
187: WorkflowAttributeDefinitionVO attributeDefinition = new WorkflowAttributeDefinitionVO(
188: TestRuleAttribute.class.getName());
189: contentVO.addAttributeDefinition(attributeDefinition);
190: content = BeanConverter.buildUpdatedDocumentContent(contentVO);
191: fullContent = startContent
192: + constructContent(ATTRIBUTE_CONTENT, attributeContent
193: + testAttributeContent)
194: + constructContent(SEARCHABLE_CONTENT,
195: searchableContent) + endContent;
196: assertEquals("Invalid content conversion.", StringUtils
197: .deleteWhitespace(fullContent), StringUtils
198: .deleteWhitespace(content));
199: }
200:
201: private String constructContent(String type, String content) {
202: if (Utilities.isEmpty(content)) {
203: return "";
204: }
205: return "<" + type + ">" + content + "</" + type + ">";
206: }
207:
208: private String constructContent(String attributeContent,
209: String searchableContent, String applicationContent) {
210: return "<"
211: + DOCUMENT_CONTENT
212: + ">"
213: + constructContent(ATTRIBUTE_CONTENT, attributeContent)
214: + constructContent(SEARCHABLE_CONTENT,
215: searchableContent)
216: + constructContent(APPLICATION_CONTENT,
217: applicationContent) + "</" + DOCUMENT_CONTENT
218: + ">";
219: }
220:
221: private void assertContent(DocumentContentVO contentVO,
222: String attributeContent, String searchableContent,
223: String applicationContent) {
224: if (Utilities.isEmpty(attributeContent)) {
225: attributeContent = "";
226: } else {
227: attributeContent = "<" + ATTRIBUTE_CONTENT + ">"
228: + attributeContent + "</" + ATTRIBUTE_CONTENT + ">";
229: }
230: if (Utilities.isEmpty(searchableContent)) {
231: searchableContent = "";
232: } else {
233: searchableContent = "<" + SEARCHABLE_CONTENT + ">"
234: + searchableContent + "</" + SEARCHABLE_CONTENT
235: + ">";
236: }
237: assertFalse("Content cannot be empty.", Utilities
238: .isEmpty(contentVO.getFullContent()));
239: assertEquals("Attribute content is invalid.", attributeContent,
240: contentVO.getAttributeContent());
241: assertEquals("Searchable content is invalid.",
242: searchableContent, contentVO.getSearchableContent());
243: assertEquals("Application content is invalid.",
244: applicationContent, contentVO.getApplicationContent());
245: assertEquals("Incorrect number of attribute definitions.", 0,
246: contentVO.getAttributeDefinitions().length);
247: assertEquals(
248: "Incorrect number of searchable attribute definitions.",
249: 0, contentVO.getSearchableDefinitions().length);
250: }
251:
252: }
|