001/**
002 * Copyright 2005-2014 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 */
016package org.kuali.rice.kew.server;
017
018import org.apache.commons.lang.StringUtils;
019import org.joda.time.DateTime;
020import org.junit.Test;
021import org.kuali.rice.core.api.delegation.DelegationType;
022import org.kuali.rice.kew.actionitem.ActionItem;
023import org.kuali.rice.kew.api.KewApiConstants;
024import org.kuali.rice.kew.api.document.DocumentContent;
025import org.kuali.rice.kew.api.document.DocumentContentUpdate;
026import org.kuali.rice.kew.api.document.attribute.WorkflowAttributeDefinition;
027import org.kuali.rice.kew.dto.DTOConverter;
028import org.kuali.rice.kew.rule.TestRuleAttribute;
029import org.kuali.rice.kew.test.KEWTestCase;
030import org.kuali.rice.kim.api.KimConstants;
031import org.kuali.rice.kim.api.group.Group;
032import org.kuali.rice.kim.api.services.KimApiServiceLocator;
033
034import java.sql.Timestamp;
035import java.util.Date;
036
037import static org.junit.Assert.*;
038
039public class DTOConverterTest 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
092    private void assertContent(DocumentContent contentVO, String attributeContent, String searchableContent, String applicationContent) throws Exception{
093        assertFalse("Content cannot be empty.", org.apache.commons.lang.StringUtils.isEmpty(contentVO.getFullContent()));
094        assertEquals("Attribute content is invalid.", attributeContent.replaceAll("\n", ""),
095                contentVO.getAttributeContent().replaceAll("\n", ""));
096        assertEquals("Searchable content is invalid.", searchableContent.replaceAll("\n", ""), contentVO.getSearchableContent().replaceAll(
097                "\n", ""));
098        assertEquals("Application content is invalid.", applicationContent.replaceAll("\n", ""), contentVO.getApplicationContent().replaceAll(
099                "\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}