View Javadoc

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