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.DocumentContentUpdate;
024    import org.kuali.rice.kew.api.document.attribute.WorkflowAttributeDefinition;
025    import org.kuali.rice.kew.dto.DTOConverter;
026    import org.kuali.rice.kew.rule.TestRuleAttribute;
027    import org.kuali.rice.kew.test.KEWTestCase;
028    import org.kuali.rice.kew.api.KewApiConstants;
029    import org.kuali.rice.kim.api.KimConstants;
030    import org.kuali.rice.kim.api.group.Group;
031    import org.kuali.rice.kim.api.services.KimApiServiceLocator;
032    
033    import java.sql.Timestamp;
034    import java.util.Date;
035    
036    import static org.junit.Assert.*;
037    
038    public class DTOConverterTest extends KEWTestCase {
039    
040        private static final String DOCUMENT_CONTENT = KewApiConstants.DOCUMENT_CONTENT_ELEMENT;
041        private static final String ATTRIBUTE_CONTENT = KewApiConstants.ATTRIBUTE_CONTENT_ELEMENT;
042        private static final String SEARCHABLE_CONTENT = KewApiConstants.SEARCHABLE_CONTENT_ELEMENT;
043        private static final String APPLICATION_CONTENT = KewApiConstants.APPLICATION_CONTENT_ELEMENT;
044    
045        /**
046         * Tests the conversion of a String into a DocumentContentVO object which should split the
047         * String into it's 3 distinct components.
048         */
049    /*    @Test public void testConvertDocumentContent() throws Exception {
050    
051            // test null content
052            String attributeContent = null;
053            String searchableContent = null;
054            String applicationContent = null;
055            String xmlContent = constructContent(attributeContent, searchableContent, applicationContent);
056            DocumentContentDTO contentVO = DTOConverter.convertDocumentContent(xmlContent, "-1234");
057            assertFalse("Content cannot be empty.", org.apache.commons.lang.StringUtils.isEmpty(contentVO.getFullContent()));
058            assertEquals("Attribute content is invalid.", "", contentVO.getAttributeContent());
059            assertEquals("Searchable content is invalid.", "", contentVO.getSearchableContent());
060            assertEquals("Application content is invalid.", "", contentVO.getApplicationContent());
061            assertEquals("Should have fake document id.", "-1234", contentVO.getDocumentId());
062    
063            // test empty content
064            attributeContent = "";
065            searchableContent = "";
066            applicationContent = "";
067            contentVO = DTOConverter.convertDocumentContent(constructContent(attributeContent, searchableContent, applicationContent), null);
068            assertContent(contentVO, attributeContent, searchableContent, applicationContent);
069    
070            // test fancy dancy content
071            attributeContent = "<iEnjoyFlexContent><id>1234</id></iEnjoyFlexContent>";
072            searchableContent = "<thisIdBeWarrenG>Warren G</thisIdBeWarrenG><whatsMyName>Snoop</whatsMyName>";
073            applicationContent = "<thisIsTotallyRad><theCoolestContentInTheWorld qualify=\"iSaidSo\">it's marvelous!</theCoolestContentInTheWorld></thisIsTotallyRad>";
074            contentVO = DTOConverter.convertDocumentContent(constructContent(attributeContent, searchableContent, applicationContent), null);
075            assertContent(contentVO, attributeContent, searchableContent, applicationContent);
076    
077            attributeContent = "invalid<xml, I can't believe you would do such a thing<<<";
078            try {
079                contentVO = DTOConverter.convertDocumentContent(constructContent(attributeContent, searchableContent, applicationContent), null);
080                fail("Parsing bad xml should have thrown an XmlException.");
081            } catch (InvalidDocumentContentException e) {
082                log.info("Expected XmlException was thrown.");
083                // if we got the exception we are good to go
084            }
085    
086            // test an older style document
087            String appSpecificXml = "<iAmAnOldSchoolApp><myDocContent type=\"custom\">is totally app specific</myDocContent><howIroll>old school, that's how I roll</howIroll></iAmAnOldSchoolApp>";
088            contentVO = DTOConverter.convertDocumentContent(appSpecificXml, null);
089            assertContent(contentVO, "", "", appSpecificXml);
090    
091            // test the old school (Workflow 1.6) flex document XML
092            String fleXml = "<flexdoc><meinAttribute>nein</meinAttribute></flexdoc>";
093            contentVO = DTOConverter.convertDocumentContent(fleXml, null);
094            assertFalse("Content cannot be empty.", org.apache.commons.lang.StringUtils.isEmpty(contentVO.getFullContent()));
095            assertEquals("Attribute content is invalid.", StringUtils.deleteWhitespace(fleXml), StringUtils.deleteWhitespace(contentVO.getAttributeContent()));
096            assertEquals("Searchable content is invalid.", "", StringUtils.deleteWhitespace(contentVO.getSearchableContent()));
097            assertEquals("Application content is invalid.", "", StringUtils.deleteWhitespace(contentVO.getApplicationContent()));
098        }*/
099    
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    }