View Javadoc

1   /*
2    * Copyright 2005-2009 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.kew.server;
17  
18  
19  import java.sql.Timestamp;
20  import java.util.Date;
21  
22  import org.apache.commons.lang.StringUtils;
23  import org.junit.Test;
24  import org.kuali.rice.kew.actionitem.ActionItem;
25  import org.kuali.rice.kew.actionrequest.ActionRequestValue;
26  import org.kuali.rice.kew.actiontaken.ActionTakenValue;
27  import org.kuali.rice.kew.dto.ActionItemDTO;
28  import org.kuali.rice.kew.dto.ActionRequestDTO;
29  import org.kuali.rice.kew.dto.ActionTakenDTO;
30  import org.kuali.rice.kew.dto.DTOConverter;
31  import org.kuali.rice.kew.dto.DocumentContentDTO;
32  import org.kuali.rice.kew.dto.WorkflowAttributeDefinitionDTO;
33  import org.kuali.rice.kew.exception.InvalidXmlException;
34  import org.kuali.rice.kew.rule.TestRuleAttribute;
35  import org.kuali.rice.kew.test.KEWTestCase;
36  import org.kuali.rice.kew.util.KEWConstants;
37  import org.kuali.rice.kew.util.Utilities;
38  import org.kuali.rice.kim.bo.Group;
39  import org.kuali.rice.kim.service.KIMServiceLocator;
40  import org.kuali.rice.kim.util.KimConstants;
41  
42  
43  public class DTOConverterTest extends KEWTestCase {
44  
45      private static final String DOCUMENT_CONTENT = KEWConstants.DOCUMENT_CONTENT_ELEMENT;
46      private static final String ATTRIBUTE_CONTENT = KEWConstants.ATTRIBUTE_CONTENT_ELEMENT;
47      private static final String SEARCHABLE_CONTENT = KEWConstants.SEARCHABLE_CONTENT_ELEMENT;
48      private static final String APPLICATION_CONTENT = KEWConstants.APPLICATION_CONTENT_ELEMENT;
49  
50      /**
51       * Tests the conversion of a String into a DocumentContentVO object which should split the
52       * String into it's 3 distinct components.
53       */
54      @Test public void testConvertDocumentContent() throws Exception {
55  
56          // test null content
57          String attributeContent = null;
58          String searchableContent = null;
59          String applicationContent = null;
60          String xmlContent = constructContent(attributeContent, searchableContent, applicationContent);
61          DocumentContentDTO contentVO = DTOConverter.convertDocumentContent(xmlContent, new Long(-1234));
62          assertFalse("Content cannot be empty.", Utilities.isEmpty(contentVO.getFullContent()));
63          assertEquals("Attribute content is invalid.", "", contentVO.getAttributeContent());
64          assertEquals("Searchable content is invalid.", "", contentVO.getSearchableContent());
65          assertEquals("Application content is invalid.", "", contentVO.getApplicationContent());
66          assertEquals("Should have fake document id.", new Long(-1234), contentVO.getRouteHeaderId());
67  
68          // test empty content
69          attributeContent = "";
70          searchableContent = "";
71          applicationContent = "";
72          contentVO = DTOConverter.convertDocumentContent(constructContent(attributeContent, searchableContent, applicationContent), null);
73          assertContent(contentVO, attributeContent, searchableContent, applicationContent);
74  
75          // test fancy dancy content
76          attributeContent = "<iEnjoyFlexContent><id>1234</id></iEnjoyFlexContent>";
77          searchableContent = "<thisIdBeWarrenG>Warren G</thisIdBeWarrenG><whatsMyName>Snoop</whatsMyName>";
78          applicationContent = "<thisIsTotallyRad><theCoolestContentInTheWorld qualify=\"iSaidSo\">it's marvelous!</theCoolestContentInTheWorld></thisIsTotallyRad>";
79          contentVO = DTOConverter.convertDocumentContent(constructContent(attributeContent, searchableContent, applicationContent), null);
80          assertContent(contentVO, attributeContent, searchableContent, applicationContent);
81  
82          attributeContent = "invalid<xml, I can't believe you would do such a thing<<<";
83          try {
84              contentVO = DTOConverter.convertDocumentContent(constructContent(attributeContent, searchableContent, applicationContent), null);
85              fail("Parsing bad xml should have thrown an InvalidXmlException.");
86          } catch (InvalidXmlException e) {
87              log.info("Expected InvalidXmlException was thrown.");
88              // if we got the exception we are good to go
89          }
90  
91          // test an older style document
92          String appSpecificXml = "<iAmAnOldSchoolApp><myDocContent type=\"custom\">is totally app specific</myDocContent><howIroll>old school, that's how I roll</howIroll></iAmAnOldSchoolApp>";
93          contentVO = DTOConverter.convertDocumentContent(appSpecificXml, null);
94          assertContent(contentVO, "", "", appSpecificXml);
95  
96          // test the old school (Workflow 1.6) flex document XML
97          String fleXml = "<flexdoc><meinAttribute>nein</meinAttribute></flexdoc>";
98          contentVO = DTOConverter.convertDocumentContent(fleXml, null);
99          assertFalse("Content cannot be empty.", Utilities.isEmpty(contentVO.getFullContent()));
100         assertEquals("Attribute content is invalid.", fleXml, contentVO.getAttributeContent());
101         assertEquals("Searchable content is invalid.", "", contentVO.getSearchableContent());
102         assertEquals("Application content is invalid.", "", contentVO.getApplicationContent());
103     }
104 
105     /**
106      * Tests the conversion of a DocumentContentVO object into an XML String.  Includes generating content
107      * for any attributes which are on the DocumentContentVO object.
108      *
109      * TODO there is some crossover between this test and the DocumentContentTest, do we really need both of them???
110      */
111     @Test public void testBuildUpdatedDocumentContent() throws Exception {
112         String startContent = "<"+DOCUMENT_CONTENT+">";
113         String endContent = "</"+DOCUMENT_CONTENT+">";
114 
115         /*
116          * 	// test no content, this should return null which indicates an unchanged document content VO
117          * //RouteHeaderVO routeHeaderVO = new RouteHeaderVO();
118          */
119 
120         // test no content, this should return empty document content
121         DocumentContentDTO contentVO = new DocumentContentDTO();
122         //routeHeaderVO.setDocumentContent(contentVO);
123         String content = DTOConverter.buildUpdatedDocumentContent(contentVO);
124         assertEquals("Invalid content conversion.", KEWConstants.DEFAULT_DOCUMENT_CONTENT, content);
125 
126         // test simple case, no attributes
127         String attributeContent = "<attribute1><id value=\"3\"/></attribute1>";
128         String searchableContent = "<searchable1><data>hello</data></searchable1>";
129         contentVO = new DocumentContentDTO();
130         contentVO.setAttributeContent(constructContent(ATTRIBUTE_CONTENT, attributeContent));
131         contentVO.setSearchableContent(constructContent(SEARCHABLE_CONTENT, searchableContent));
132         content = DTOConverter.buildUpdatedDocumentContent(contentVO);
133         String fullContent = startContent+constructContent(ATTRIBUTE_CONTENT, attributeContent)+constructContent(SEARCHABLE_CONTENT, searchableContent)+endContent;
134         assertEquals("Invalid content conversion.", StringUtils.deleteWhitespace(fullContent), StringUtils.deleteWhitespace(content));
135 
136         // now, add an attribute
137         String testAttributeContent = new TestRuleAttribute().getDocContent();
138         WorkflowAttributeDefinitionDTO attributeDefinition = new WorkflowAttributeDefinitionDTO(TestRuleAttribute.class.getName());
139         contentVO.addAttributeDefinition(attributeDefinition);
140         content = DTOConverter.buildUpdatedDocumentContent(contentVO);
141         fullContent = startContent+
142             constructContent(ATTRIBUTE_CONTENT, attributeContent+testAttributeContent)+
143             constructContent(SEARCHABLE_CONTENT, searchableContent)+
144             endContent;
145         assertEquals("Invalid content conversion.", StringUtils.deleteWhitespace(fullContent), StringUtils.deleteWhitespace(content));
146     }
147 
148     private String constructContent(String type, String content) {
149         if (Utilities.isEmpty(content)) {
150             return "";
151         }
152         return "<"+type+">"+content+"</"+type+">";
153     }
154 
155     private String constructContent(String attributeContent, String searchableContent, String applicationContent) {
156         return "<"+DOCUMENT_CONTENT+">"+
157             constructContent(ATTRIBUTE_CONTENT, attributeContent)+
158             constructContent(SEARCHABLE_CONTENT, searchableContent)+
159             constructContent(APPLICATION_CONTENT, applicationContent)+
160             "</"+DOCUMENT_CONTENT+">";
161     }
162 
163     private void assertContent(DocumentContentDTO contentVO, String attributeContent, String searchableContent, String applicationContent) {
164         if (Utilities.isEmpty(attributeContent)) {
165         	attributeContent = "";
166         } else {
167             attributeContent = "<"+ATTRIBUTE_CONTENT+">"+attributeContent+"</"+ATTRIBUTE_CONTENT+">";
168         }
169         if (Utilities.isEmpty(searchableContent)) {
170         	searchableContent = "";
171         } else {
172             searchableContent = "<"+SEARCHABLE_CONTENT+">"+searchableContent+"</"+SEARCHABLE_CONTENT+">";
173         }
174         assertFalse("Content cannot be empty.", Utilities.isEmpty(contentVO.getFullContent()));
175         assertEquals("Attribute content is invalid.", attributeContent, contentVO.getAttributeContent());
176         assertEquals("Searchable content is invalid.", searchableContent, contentVO.getSearchableContent());
177         assertEquals("Application content is invalid.", applicationContent, contentVO.getApplicationContent());
178         assertEquals("Incorrect number of attribute definitions.", 0, contentVO.getAttributeDefinitions().length);
179         assertEquals("Incorrect number of searchable attribute definitions.", 0, contentVO.getSearchableDefinitions().length);
180     }
181 
182     @Test public void testConvertActionItem() throws Exception {
183         // get test data
184         String testWorkgroupName = "TestWorkgroup";
185         Group testWorkgroup = KIMServiceLocator.getIdentityManagementService().getGroupByName(KimConstants.KIM_GROUP_WORKFLOW_NAMESPACE_CODE, testWorkgroupName);
186         String testWorkgroupId = testWorkgroup.getGroupId();
187         assertTrue("Test workgroup '" + testWorkgroupName + "' should have at least one user", KIMServiceLocator.getIdentityManagementService().getDirectGroupMemberPrincipalIds(testWorkgroup.getGroupId()).size() > 0);
188         String workflowId = KIMServiceLocator.getIdentityManagementService().getDirectGroupMemberPrincipalIds(testWorkgroup.getGroupId()).get(0);
189         assertNotNull("User from workgroup should not be null", workflowId);
190         String actionRequestCd = KEWConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ;
191         Long actionRequestId = Long.valueOf(4);
192         String docName = "dummy";
193         String roleName = "fakeRole";
194         Long routeHeaderId = Long.valueOf(23);
195         Timestamp dateAssigned = new Timestamp(new Date().getTime());
196         String docHandlerUrl = "http://this.is.not.us";
197         String docTypeLabel = "Label Me";
198         String docTitle = "Title me";
199         Long responsibilityId = Long.valueOf(35);
200         String delegationType = KEWConstants.DELEGATION_PRIMARY;
201 
202         // create fake action item
203         ActionItem actionItem = new ActionItem();
204         actionItem.setActionRequestCd(actionRequestCd);
205         actionItem.setActionRequestId(actionRequestId);
206         actionItem.setDocName(docName);
207         actionItem.setRoleName(roleName);
208         actionItem.setPrincipalId(workflowId);
209         actionItem.setRouteHeaderId(routeHeaderId);
210         actionItem.setDateAssigned(dateAssigned);
211         actionItem.setDocHandlerURL(docHandlerUrl);
212         actionItem.setDocLabel(docTypeLabel);
213         actionItem.setDocTitle(docTitle);
214         actionItem.setGroupId(testWorkgroupId);
215         actionItem.setResponsibilityId(responsibilityId);
216         actionItem.setDelegationType(delegationType);
217         actionItem.setDelegatorWorkflowId(workflowId);
218         actionItem.setDelegatorGroupId(testWorkgroupId);
219 
220         // convert to action item vo object and verify
221         ActionItemDTO actionItemVO = DTOConverter.convertActionItem(actionItem);
222         assertEquals("Action Item VO object has incorrect value", actionRequestCd, actionItemVO.getActionRequestCd());
223         assertEquals("Action Item VO object has incorrect value", actionRequestId, actionItemVO.getActionRequestId());
224         assertEquals("Action Item VO object has incorrect value", docName, actionItemVO.getDocName());
225         assertEquals("Action Item VO object has incorrect value", roleName, actionItemVO.getRoleName());
226         assertEquals("Action Item VO object has incorrect value", workflowId, actionItemVO.getPrincipalId());
227         assertEquals("Action Item VO object has incorrect value", routeHeaderId, actionItemVO.getRouteHeaderId());
228         assertEquals("Action Item VO object has incorrect value", dateAssigned, actionItemVO.getDateAssigned());
229         assertEquals("Action Item VO object has incorrect value", docHandlerUrl, actionItemVO.getDocHandlerURL());
230         assertEquals("Action Item VO object has incorrect value", docTypeLabel, actionItemVO.getDocLabel());
231         assertEquals("Action Item VO object has incorrect value", docTitle, actionItemVO.getDocTitle());
232         assertEquals("Action Item VO object has incorrect value", "" + testWorkgroupId, actionItemVO.getGroupId());
233         assertEquals("Action Item VO object has incorrect value", responsibilityId, actionItemVO.getResponsibilityId());
234         assertEquals("Action Item VO object has incorrect value", delegationType, actionItemVO.getDelegationType());
235         assertEquals("Action Item VO object has incorrect value", workflowId, actionItemVO.getDelegatorPrincipalId());
236         assertEquals("Action Item VO object has incorrect value", testWorkgroupId, actionItemVO.getDelegatorGroupId());
237     }
238 
239     @Test public void testConvertActionRequest() throws Exception {
240     	ActionRequestValue actionRequest = new ActionRequestValue();
241     	ActionTakenValue actionTaken = new ActionTakenValue();
242     	actionRequest.setActionTaken(actionTaken);
243     	actionTaken.getActionRequests().add(actionRequest);
244     	
245     	ActionRequestDTO convertedActionRequest = DTOConverter.convertActionRequest(actionRequest);
246     	assertNotNull("ActionTakenDTO object should be valid", convertedActionRequest);
247 
248     }
249 
250     @Test public void testConvertActionTaken() throws Exception {
251     	ActionRequestValue actionRequest = new ActionRequestValue();
252     	ActionTakenValue actionTaken = new ActionTakenValue();
253     	actionRequest.setActionTaken(actionTaken);
254     	actionTaken.getActionRequests().add(actionRequest);
255     	
256     	ActionTakenDTO convertedActionTaken = DTOConverter.convertActionTaken(actionTaken);
257     	assertNotNull("ActionTakenDTO object should be valid", convertedActionTaken);
258     	convertedActionTaken = DTOConverter.convertActionTakenWithActionRequests(actionTaken);
259     	assertNotNull("ActionTakenDTO object should be valid", convertedActionTaken);
260     }
261 }