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