View Javadoc

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