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.krad.service; 017 018import org.junit.Test; 019import org.kuali.rice.kew.api.WorkflowDocument; 020import org.kuali.rice.krad.UserSession; 021import org.kuali.rice.krad.document.Document; 022import org.kuali.rice.krad.document.DocumentBase; 023import org.kuali.rice.krad.maintenance.MaintenanceDocument; 024import org.kuali.rice.krad.maintenance.MaintenanceDocumentBase; 025import org.kuali.rice.krad.rules.rule.event.SaveDocumentEvent; 026import org.kuali.rice.krad.test.KRADTestCase; 027import org.kuali.rice.krad.test.document.AccountRequestDocument; 028import org.kuali.rice.krad.test.document.RuleEventImpl; 029import org.kuali.rice.krad.test.document.bo.Account; 030import org.kuali.rice.krad.util.ErrorMessage; 031import org.kuali.rice.krad.util.GlobalVariables; 032import org.kuali.rice.krad.util.MessageMap; 033 034import java.util.Arrays; 035import java.util.List; 036 037import static org.junit.Assert.*; 038 039/** 040 * This class tests the DocumentService (currently only getNewDocument is tested). 041 * 042 * @author Kuali Rice Team (rice.collab@kuali.org) 043 * 044 */ 045public class DocumentServiceTest extends KRADTestCase { 046 047 public DocumentServiceTest() { 048 } 049 050 @Override 051 public void setUp() throws Exception { 052 super.setUp(); 053 GlobalVariables.setMessageMap(new MessageMap()); 054 GlobalVariables.setUserSession(new UserSession("quickstart")); 055 } 056 057 @Override 058 public void tearDown() throws Exception { 059 GlobalVariables.setMessageMap(new MessageMap()); 060 GlobalVariables.setUserSession(null); 061 super.tearDown(); 062 } 063 064 /** 065 * This method tests getNewDocument 066 * 067 * @throws Exception 068 */ 069 @Test public void testGetNewDocument() throws Exception { 070 AccountRequestDocument travelDocument = (AccountRequestDocument) KRADServiceLocatorWeb.getDocumentService().getNewDocument("AccountRequest"); 071 WorkflowDocument wd = travelDocument.getDocumentHeader().getWorkflowDocument(); 072 073 assertEquals("Initiator should be the current user", wd.getInitiatorPrincipalId(), 074 GlobalVariables.getUserSession().getPerson().getPrincipalId()); 075 } 076 077 /** 078 * This method tests getNewDocument but the initiator is not the current user 079 * 080 * @throws Exception 081 */ 082 @Test public void testGetNewDocumentDifferentInitiatorThanCurrentUser() throws Exception { 083 AccountRequestDocument travelDocument = (AccountRequestDocument) KRADServiceLocatorWeb.getDocumentService().getNewDocument("AccountRequest", "testuser1"); 084 WorkflowDocument wd = travelDocument.getDocumentHeader().getWorkflowDocument(); 085 086 assertEquals("Initiator should be testuser1", wd.getInitiatorPrincipalId(), "testuser1"); 087 } 088 089 /** 090 * This method tests getNewDocument but the initiator is invalid 091 * 092 * @throws Exception 093 */ 094 @Test public void testGetNewDocumentInvalidInitiator() throws Exception { 095 AccountRequestDocument travelDocument = (AccountRequestDocument) KRADServiceLocatorWeb.getDocumentService().getNewDocument("AccountRequest", "notValidUserAtAll"); 096 WorkflowDocument wd = travelDocument.getDocumentHeader().getWorkflowDocument(); 097 098 assertEquals("Initiator should be the current user", wd.getInitiatorPrincipalId(), GlobalVariables.getUserSession().getPerson().getPrincipalId()); 099 } 100 101 /** 102 * tests saveDocument, in particular, the save document rule event and the custom rule method 103 * invocation of the business rule associated with the document. 104 * 105 * @throws Exception 106 */ 107 @Test 108 public void testSaveDocument_DocumentEvent() throws Exception { 109 MaintenanceDocument maintenanceDocument = ( MaintenanceDocument ) KRADServiceLocatorWeb 110 .getDocumentService().getNewDocument( "AccountMaintenanceDocument" ); 111 112 Account account = ( Account ) maintenanceDocument.getNewMaintainableObject().getDataObject(); 113 114 SaveDocumentEvent documentEvent = new SaveDocumentEvent( maintenanceDocument ); 115 documentEvent.setName( "DocumentControllerBaseSaveDocumentRuleTest#testSave_SaveDocumentEvent()" ); 116 documentEvent.setRuleMethodName( "processEvent" ); 117 118 Document savedDocument = KRADServiceLocatorWeb.getDocumentService() 119 .saveDocument(maintenanceDocument, documentEvent); 120 121 assertNull( "New maintenance document should not have a version number yet.", 122 maintenanceDocument.getDocumentHeader().getVersionNumber() ); 123 assertNotNull( "Saved maintenance document must have a version number.", savedDocument.getDocumentHeader().getVersionNumber() ); 124 125 List<ErrorMessage> msgs = GlobalVariables.getMessageMap().getInfoMessagesForProperty( documentEvent.getName() ); 126 127 assertEquals( "There must be one entry added by the business rule method.", 1, msgs.size() ); 128 assertEquals( "The message set by the business rule must match the test message.", 129 documentEvent.getRuleMethodName() + "()", msgs.get(0).toString() ); 130 } 131 132 /** 133 * tests saveDocument, in particular, the save document rule event and the default rule method 134 * invocation of the business rule associated with the document. 135 * 136 * @throws Exception 137 */ 138 @Test 139 public void testSaveDocument_Default() throws Exception { 140 MaintenanceDocument maintenanceDocument = ( MaintenanceDocument ) KRADServiceLocatorWeb 141 .getDocumentService().getNewDocument( "AccountMaintenanceDocument" ); 142 143 Account account = ( Account ) maintenanceDocument.getNewMaintainableObject().getDataObject(); 144 145 RuleEventImpl documentEvent = new RuleEventImpl( maintenanceDocument ); 146 documentEvent.setName("DocumentControllerBaseSaveDocumentRuleTest#testSave_Default()"); 147 148 Document savedDocument = KRADServiceLocatorWeb.getDocumentService() 149 .saveDocument(maintenanceDocument, documentEvent); 150 151 assertNull( "New maintenance document should not have a version number yet.", 152 maintenanceDocument.getDocumentHeader().getVersionNumber() ); 153 assertNotNull( "Saved maintenance document must have a version number.", savedDocument.getDocumentHeader().getVersionNumber() ); 154 155 List<ErrorMessage> msgs = GlobalVariables.getMessageMap() 156 .getInfoMessagesForProperty( documentEvent.getClass().getName() ); 157 158 assertEquals( "There must be one entry added by the business rule method.", 1, msgs.size() ); 159 assertEquals( "The message set by the business rule must match the test message.", 160 "org.kuali.rice.krad.test.document.AccountRules" + "()", msgs.get( 0 ).toString() ); 161 } 162 163 /** 164 * Tests getting documents by a list of document numbers. 165 * 166 * @throws Exception for any test issues 167 */ 168 @Test 169 public void testGetDocumentsByListOfDocumentHeaderIds_Default() throws Exception { 170 Document document1 = KRADServiceLocatorWeb.getDocumentService().getNewDocument("AccountMaintenanceDocument"); 171 Document document2 = KRADServiceLocatorWeb.getDocumentService().getNewDocument("AccountMaintenanceDocument"); 172 173 RuleEventImpl documentEvent1 = new RuleEventImpl(document1); 174 documentEvent1.setName("saving document 1"); 175 RuleEventImpl documentEvent2 = new RuleEventImpl(document2); 176 documentEvent2.setName("saving document 2"); 177 178 Document savedDocument1 = KRADServiceLocatorWeb.getDocumentService().saveDocument(document1, documentEvent1); 179 Document savedDocument2 = KRADServiceLocatorWeb.getDocumentService().saveDocument(document2, documentEvent2); 180 181 String documentNumber1 = savedDocument1.getDocumentNumber(); 182 String documentNumber2 = savedDocument2.getDocumentNumber(); 183 184 List<Document> documents = KRADServiceLocatorWeb.getDocumentService().getDocumentsByListOfDocumentHeaderIds( 185 MaintenanceDocumentBase.class, Arrays.asList(documentNumber1, documentNumber2)); 186 187 assertEquals("wrong number of documents found", 2, documents.size()); 188 189 for (Document document : documents) { 190 assertNotNull("document was null", document); 191 } 192 } 193 194 /** 195 * Tests getting documents by a list of document numbers which don't exist. 196 * 197 * @throws Exception for any test issues 198 */ 199 @Test 200 public void testGetDocumentsByListOfDocumentHeaderIds_MissingDocuments() throws Exception { 201 String fakeDocumentNumber1 = "1234"; 202 String fakeDocumentNumber2 = "5678"; 203 204 List<Document> documents = KRADServiceLocatorWeb.getDocumentService().getDocumentsByListOfDocumentHeaderIds( 205 MaintenanceDocumentBase.class, Arrays.asList(fakeDocumentNumber1, fakeDocumentNumber2)); 206 207 assertTrue("documents found", documents.isEmpty()); 208 } 209 210}