View Javadoc
1   /**
2    * Copyright 2005-2014 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.KewApiConstants;
24  import org.kuali.rice.kew.api.document.DocumentContent;
25  import org.kuali.rice.kew.api.document.DocumentContentUpdate;
26  import org.kuali.rice.kew.api.document.attribute.WorkflowAttributeDefinition;
27  import org.kuali.rice.kew.dto.DTOConverter;
28  import org.kuali.rice.kew.rule.TestRuleAttribute;
29  import org.kuali.rice.kew.test.KEWTestCase;
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 DTOConverterTest 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  
92      private void assertContent(DocumentContent contentVO, String attributeContent, String searchableContent, String applicationContent) throws Exception{
93          assertFalse("Content cannot be empty.", org.apache.commons.lang.StringUtils.isEmpty(contentVO.getFullContent()));
94          assertEquals("Attribute content is invalid.", attributeContent.replaceAll("\n", ""),
95                  contentVO.getAttributeContent().replaceAll("\n", ""));
96          assertEquals("Searchable content is invalid.", searchableContent.replaceAll("\n", ""), contentVO.getSearchableContent().replaceAll(
97                  "\n", ""));
98          assertEquals("Application content is invalid.", applicationContent.replaceAll("\n", ""), contentVO.getApplicationContent().replaceAll(
99                  "\n", ""));
100     }
101 
102     /**
103      * Tests the conversion of a DocumentContentVO object into an XML String.  Includes generating content
104      * for any attributes which are on the DocumentContentVO object.
105      *
106      * TODO there is some crossover between this test and the DocumentContentTest, do we really need both of them???
107      */
108     @Test public void testBuildUpdatedDocumentContent() throws Exception {
109         String startContent = "<"+DOCUMENT_CONTENT+">";
110         String endContent = "</"+DOCUMENT_CONTENT+">";
111 
112         /*
113          * 	// test no content, this should return null which indicates an unchanged document content VO
114          * //RouteHeaderVO routeHeaderVO = new RouteHeaderVO();
115          */
116 
117         // test no content, this should return empty document content
118         DocumentContentUpdate contentUpdate = DocumentContentUpdate.Builder.create().build();
119         //routeHeaderVO.setDocumentContent(contentVO);
120         String content = DTOConverter.buildUpdatedDocumentContent(KewApiConstants.DEFAULT_DOCUMENT_CONTENT, contentUpdate,
121                 null);
122         assertEquals("Invalid content conversion.", StringUtils.deleteWhitespace(KewApiConstants.DEFAULT_DOCUMENT_CONTENT), StringUtils.deleteWhitespace(content));
123 
124         // test simple case, no attributes
125         String attributeContent = "<attribute1><id value=\"3\"/></attribute1>";
126         String searchableContent = "<searchable1><data>hello</data></searchable1>";
127         DocumentContentUpdate.Builder contentUpdateBuilder = DocumentContentUpdate.Builder.create();
128         contentUpdateBuilder.setAttributeContent(constructContent(ATTRIBUTE_CONTENT, attributeContent));
129         contentUpdateBuilder.setSearchableContent(constructContent(SEARCHABLE_CONTENT, searchableContent));
130         content = DTOConverter.buildUpdatedDocumentContent(KewApiConstants.DEFAULT_DOCUMENT_CONTENT, contentUpdateBuilder.build(), null);
131         String fullContent = startContent+"\n"+constructContent(ATTRIBUTE_CONTENT, attributeContent)+"\n"+constructContent(SEARCHABLE_CONTENT, searchableContent)+"\n"+endContent;
132         assertEquals("Invalid content conversion.", StringUtils.deleteWhitespace(fullContent), StringUtils.deleteWhitespace(content));
133 
134         // now, add an attribute
135         String testAttributeContent = new TestRuleAttribute().getDocContent();
136         WorkflowAttributeDefinition attributeDefinition = WorkflowAttributeDefinition.Builder.create("TestRuleAttribute").build();
137         contentUpdateBuilder.getAttributeDefinitions().add(attributeDefinition);
138         content = DTOConverter.buildUpdatedDocumentContent(KewApiConstants.DEFAULT_DOCUMENT_CONTENT, contentUpdateBuilder.build(), null);
139         fullContent = startContent+
140             constructContent(ATTRIBUTE_CONTENT, attributeContent+testAttributeContent)+
141             constructContent(SEARCHABLE_CONTENT, searchableContent)+
142             endContent;
143         assertEquals("Invalid content conversion.", StringUtils.deleteWhitespace(fullContent), StringUtils.deleteWhitespace(content));
144     }
145 
146     private String constructContent(String type, String content) {
147         if (org.apache.commons.lang.StringUtils.isEmpty(content)) {
148             return "";
149         }
150         return "<"+type+">"+content+"</"+type+">";
151     }
152 
153     private String constructContent(String attributeContent, String searchableContent, String applicationContent) {
154         return "<"+DOCUMENT_CONTENT+">"+
155             constructContent(ATTRIBUTE_CONTENT, attributeContent)+
156             constructContent(SEARCHABLE_CONTENT, searchableContent)+
157             constructContent(APPLICATION_CONTENT, applicationContent)+
158             "</"+DOCUMENT_CONTENT+">";
159     }
160 
161     @Test public void testConvertActionItem() throws Exception {
162         // get test data
163         String testWorkgroupName = "TestWorkgroup";
164         Group testWorkgroup = KimApiServiceLocator.getGroupService().getGroupByNamespaceCodeAndName(
165                 KimConstants.KIM_GROUP_WORKFLOW_NAMESPACE_CODE, testWorkgroupName);
166         String testWorkgroupId = testWorkgroup.getId();
167         assertTrue("Test workgroup '" + testWorkgroupName + "' should have at least one user", KimApiServiceLocator.getGroupService().getDirectMemberPrincipalIds(
168                 testWorkgroup.getId()).size() > 0);
169         String workflowId = KimApiServiceLocator.getGroupService().getDirectMemberPrincipalIds(testWorkgroup.getId()).get(0);
170         assertNotNull("User from workgroup should not be null", workflowId);
171         String actionRequestCd = KewApiConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ;
172         String actionRequestId = "4";
173         String docName = "dummy";
174         String roleName = "fakeRole";
175         String documentId = "23";
176         Timestamp dateAssigned = new Timestamp(new Date().getTime());
177         String docHandlerUrl = "http://this.is.not.us";
178         String docTypeLabel = "Label Me";
179         String docTitle = "Title me";
180         String responsibilityId = "35";
181         DelegationType delegationType = DelegationType.PRIMARY;
182 
183         // create fake action item
184         ActionItem actionItem = new ActionItem();
185         actionItem.setActionRequestCd(actionRequestCd);
186         actionItem.setActionRequestId(actionRequestId);
187         actionItem.setDocName(docName);
188         actionItem.setRoleName(roleName);
189         actionItem.setPrincipalId(workflowId);
190         actionItem.setDocumentId(documentId);
191         actionItem.setDateAssigned(dateAssigned);
192         actionItem.setDocHandlerURL(docHandlerUrl);
193         actionItem.setDocLabel(docTypeLabel);
194         actionItem.setDocTitle(docTitle);
195         actionItem.setGroupId(testWorkgroupId);
196         actionItem.setResponsibilityId(responsibilityId);
197         actionItem.setDelegationType(delegationType);
198         actionItem.setDelegatorPrincipalId(workflowId);
199         actionItem.setDelegatorGroupId(testWorkgroupId);
200 
201         // convert to action item vo object and verify
202         org.kuali.rice.kew.api.action.ActionItem actionItemVO = ActionItem.to(actionItem);
203         assertEquals("Action Item VO object has incorrect value", actionRequestCd, actionItemVO.getActionRequestCd());
204         assertEquals("Action Item VO object has incorrect value", actionRequestId, actionItemVO.getActionRequestId());
205         assertEquals("Action Item VO object has incorrect value", docName, actionItemVO.getDocName());
206         assertEquals("Action Item VO object has incorrect value", roleName, actionItemVO.getRoleName());
207         assertEquals("Action Item VO object has incorrect value", workflowId, actionItemVO.getPrincipalId());
208         assertEquals("Action Item VO object has incorrect value", documentId, actionItemVO.getDocumentId());
209         assertEquals("Action Item VO object has incorrect value", new DateTime(dateAssigned.getTime()), actionItemVO.getDateTimeAssigned());
210         assertEquals("Action Item VO object has incorrect value", docHandlerUrl, actionItemVO.getDocHandlerURL());
211         assertEquals("Action Item VO object has incorrect value", docTypeLabel, actionItemVO.getDocLabel());
212         assertEquals("Action Item VO object has incorrect value", docTitle, actionItemVO.getDocTitle());
213         assertEquals("Action Item VO object has incorrect value", "" + testWorkgroupId, actionItemVO.getGroupId());
214         assertEquals("Action Item VO object has incorrect value", responsibilityId, actionItemVO.getResponsibilityId());
215         assertEquals("Action Item VO object has incorrect value", delegationType, actionItemVO.getDelegationType());
216         assertEquals("Action Item VO object has incorrect value", workflowId, actionItemVO.getDelegatorPrincipalId());
217         assertEquals("Action Item VO object has incorrect value", testWorkgroupId, actionItemVO.getDelegatorGroupId());
218     }
219 
220 }