1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.service;
17
18 import org.junit.Test;
19 import org.kuali.rice.kew.api.WorkflowDocument;
20 import org.kuali.rice.krad.UserSession;
21 import org.kuali.rice.krad.document.Document;
22 import org.kuali.rice.krad.maintenance.MaintenanceDocument;
23 import org.kuali.rice.krad.rules.rule.event.SaveDocumentEvent;
24 import org.kuali.rice.krad.test.KRADTestCase;
25 import org.kuali.rice.krad.test.document.AccountRequestDocument;
26 import org.kuali.rice.krad.test.document.RuleEventImpl;
27 import org.kuali.rice.krad.test.document.bo.Account;
28 import org.kuali.rice.krad.util.ErrorMessage;
29 import org.kuali.rice.krad.util.GlobalVariables;
30 import org.kuali.rice.krad.util.MessageMap;
31
32 import java.util.List;
33
34 import static org.junit.Assert.*;
35
36
37
38
39
40
41
42 public class DocumentServiceTest extends KRADTestCase {
43
44 public DocumentServiceTest() {
45 }
46
47 @Override
48 public void setUp() throws Exception {
49 super.setUp();
50 GlobalVariables.setMessageMap(new MessageMap());
51 GlobalVariables.setUserSession(new UserSession("quickstart"));
52 }
53
54 @Override
55 public void tearDown() throws Exception {
56 GlobalVariables.setMessageMap(new MessageMap());
57 GlobalVariables.setUserSession(null);
58 super.tearDown();
59 }
60
61
62
63
64
65
66 @Test public void testGetNewDocument() throws Exception {
67 AccountRequestDocument travelDocument = (AccountRequestDocument) KRADServiceLocatorWeb.getDocumentService().getNewDocument("AccountRequest");
68 WorkflowDocument wd = travelDocument.getDocumentHeader().getWorkflowDocument();
69
70 assertEquals("Initiator should be the current user", wd.getInitiatorPrincipalId(),
71 GlobalVariables.getUserSession().getPerson().getPrincipalId());
72 }
73
74
75
76
77
78
79 @Test public void testGetNewDocumentDifferentInitiatorThanCurrentUser() throws Exception {
80 AccountRequestDocument travelDocument = (AccountRequestDocument) KRADServiceLocatorWeb.getDocumentService().getNewDocument("AccountRequest", "testuser1");
81 WorkflowDocument wd = travelDocument.getDocumentHeader().getWorkflowDocument();
82
83 assertEquals("Initiator should be testuser1", wd.getInitiatorPrincipalId(), "testuser1");
84 }
85
86
87
88
89
90
91 @Test public void testGetNewDocumentInvalidInitiator() throws Exception {
92 AccountRequestDocument travelDocument = (AccountRequestDocument) KRADServiceLocatorWeb.getDocumentService().getNewDocument("AccountRequest", "notValidUserAtAll");
93 WorkflowDocument wd = travelDocument.getDocumentHeader().getWorkflowDocument();
94
95 assertEquals("Initiator should be the current user", wd.getInitiatorPrincipalId(), GlobalVariables.getUserSession().getPerson().getPrincipalId());
96 }
97
98
99
100
101
102
103
104 @Test
105 public void testSaveDocument_DocumentEvent() throws Exception {
106 MaintenanceDocument maintenanceDocument = ( MaintenanceDocument ) KRADServiceLocatorWeb
107 .getDocumentService().getNewDocument( "AccountMaintenanceDocument" );
108
109 Account account = ( Account ) maintenanceDocument.getNewMaintainableObject().getDataObject();
110
111 SaveDocumentEvent documentEvent = new SaveDocumentEvent( maintenanceDocument );
112 documentEvent.setName( "DocumentControllerBaseSaveDocumentRuleTest#testSave_SaveDocumentEvent()" );
113 documentEvent.setRuleMethodName( "processEvent" );
114
115 Document savedDocument = KRADServiceLocatorWeb.getDocumentService()
116 .saveDocument(maintenanceDocument, documentEvent);
117
118 assertNull( "New maintenance document should not have a version number yet.",
119 maintenanceDocument.getDocumentHeader().getVersionNumber() );
120 assertNotNull( "Saved maintenance document must have a version number.", savedDocument.getDocumentHeader().getVersionNumber() );
121
122 List<ErrorMessage> msgs = GlobalVariables.getMessageMap().getInfoMessagesForProperty( documentEvent.getName() );
123
124 assertEquals( "There must be one entry added by the business rule method.", 1, msgs.size() );
125 assertEquals( "The message set by the business rule must match the test message.",
126 documentEvent.getRuleMethodName() + "()", msgs.get(0).toString() );
127 }
128
129
130
131
132
133
134
135 @Test
136 public void testSaveDocument_Default() throws Exception {
137 MaintenanceDocument maintenanceDocument = ( MaintenanceDocument ) KRADServiceLocatorWeb
138 .getDocumentService().getNewDocument( "AccountMaintenanceDocument" );
139
140 Account account = ( Account ) maintenanceDocument.getNewMaintainableObject().getDataObject();
141
142 RuleEventImpl documentEvent = new RuleEventImpl( maintenanceDocument );
143 documentEvent.setName("DocumentControllerBaseSaveDocumentRuleTest#testSave_Default()");
144
145 Document savedDocument = KRADServiceLocatorWeb.getDocumentService()
146 .saveDocument(maintenanceDocument, documentEvent);
147
148 assertNull( "New maintenance document should not have a version number yet.",
149 maintenanceDocument.getDocumentHeader().getVersionNumber() );
150 assertNotNull( "Saved maintenance document must have a version number.", savedDocument.getDocumentHeader().getVersionNumber() );
151
152 List<ErrorMessage> msgs = GlobalVariables.getMessageMap()
153 .getInfoMessagesForProperty( documentEvent.getClass().getName() );
154
155 assertEquals( "There must be one entry added by the business rule method.", 1, msgs.size() );
156 assertEquals( "The message set by the business rule must match the test message.",
157 "org.kuali.rice.krad.test.document.AccountRules" + "()", msgs.get( 0 ).toString() );
158 }
159 }