001    /**
002     * Copyright 2005-2011 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.rice.kew.server;
017    
018    import org.apache.commons.lang.StringUtils;
019    import org.joda.time.DateTime;
020    import org.junit.Test;
021    import org.kuali.rice.core.api.delegation.DelegationType;
022    import org.kuali.rice.kew.actionitem.ActionItem;
023    import org.kuali.rice.kew.api.document.DocumentContent;
024    import org.kuali.rice.kew.api.document.DocumentContentUpdate;
025    import org.kuali.rice.kew.api.document.attribute.WorkflowAttributeDefinition;
026    import org.kuali.rice.kew.dto.DTOConverter;
027    import org.kuali.rice.kew.rule.TestRuleAttribute;
028    import org.kuali.rice.kew.test.KEWTestCase;
029    import org.kuali.rice.kew.api.KewApiConstants;
030    import org.kuali.rice.kim.api.KimConstants;
031    import org.kuali.rice.kim.api.group.Group;
032    import org.kuali.rice.kim.api.services.KimApiServiceLocator;
033    
034    import java.sql.Timestamp;
035    import java.util.Date;
036    
037    import static org.junit.Assert.*;
038    
039    public class BeanConverterTester extends KEWTestCase {
040    
041        private static final String DOCUMENT_CONTENT = KewApiConstants.DOCUMENT_CONTENT_ELEMENT;
042        private static final String ATTRIBUTE_CONTENT = KewApiConstants.ATTRIBUTE_CONTENT_ELEMENT;
043        private static final String SEARCHABLE_CONTENT = KewApiConstants.SEARCHABLE_CONTENT_ELEMENT;
044        private static final String APPLICATION_CONTENT = KewApiConstants.APPLICATION_CONTENT_ELEMENT;
045    
046        /**
047         * Tests the conversion of a String into a DocumentContentVO object which should split the
048         * String into it's 3 distinct components.
049         */
050        @Test public void testConvertDocumentContent() throws Exception {
051    
052            // test null content
053            String attributeContent = null;
054            String searchableContent = null;
055            String applicationContent = null;
056            String xmlContent = constructContent(attributeContent, searchableContent, applicationContent);
057            DocumentContent.Builder builder = DocumentContent.Builder.create("-1234");
058            builder.setApplicationContent(applicationContent);
059            builder.setAttributeContent(attributeContent);
060            builder.setSearchableContent(searchableContent);
061    
062            DocumentContent content = builder.build();
063            assertFalse("Content cannot be empty.", org.apache.commons.lang.StringUtils.isEmpty(content.getFullContent()));
064            assertEquals("Attribute content is invalid.", null, content.getAttributeContent());
065            assertEquals("Searchable content is invalid.", null, content.getSearchableContent());
066            assertEquals("Application content is invalid.", null, content.getApplicationContent());
067            assertEquals("Should have fake document id.", "-1234", content.getDocumentId());
068    
069            // test empty content
070            attributeContent = "";
071            searchableContent = "";
072            applicationContent = "";
073            builder = DocumentContent.Builder.create("testId");
074            builder.setApplicationContent(applicationContent);
075            builder.setAttributeContent(attributeContent);
076            builder.setSearchableContent(searchableContent);
077            content = builder.build();
078            assertContent(content, attributeContent, searchableContent, applicationContent);
079    
080            // test fancy dancy content
081            attributeContent = "<iEnjoyFlexContent><id>1234</id></iEnjoyFlexContent>";
082            searchableContent = "<thisIdBeWarrenG>Warren G</thisIdBeWarrenG><whatsMyName>Snoop</whatsMyName>";
083            applicationContent = "<thisIsTotallyRad><theCoolestContentInTheWorld qualify=\"iSaidSo\">it's marvelous!</theCoolestContentInTheWorld></thisIsTotallyRad>";
084            builder = DocumentContent.Builder.create("testId");
085            builder.setApplicationContent(applicationContent);
086            builder.setAttributeContent(attributeContent);
087            builder.setSearchableContent(searchableContent);
088            content = builder.build();
089            assertContent(content, attributeContent, searchableContent, applicationContent);
090    
091            /*attributeContent = "invalid<xml, I can't believe you would do such a thing<<<";
092            try {
093                builder = DocumentContent.Builder.create("testId");
094                builder.setApplicationContent(applicationContent);
095                builder.setAttributeContent(attributeContent);
096                builder.setSearchableContent(searchableContent);
097                content = builder.build();
098                fail("Parsing bad xml should have thrown an XmlException.");
099            } 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().getGroupByNameAndNamespaceCode(
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    }