View Javadoc

1   /**
2    * Copyright 2005-2011 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.krad.bo;
17  
18  import org.junit.Assert;
19  import org.junit.Ignore;
20  import org.junit.Test;
21  import org.kuali.rice.krad.service.KRADServiceLocator;
22  import org.kuali.rice.krad.test.document.bo.Account;
23  import org.kuali.rice.krad.test.document.bo.AccountManager;
24  import org.kuali.rice.location.impl.county.CountyBo;
25  import org.kuali.rice.location.impl.county.CountyId;
26  import org.kuali.rice.location.impl.state.StateBo;
27  import org.kuali.rice.location.impl.state.StateId;
28  import org.kuali.rice.test.BaselineTestCase;
29  import org.kuali.rice.test.data.PerTestUnitTestData;
30  import org.kuali.rice.test.data.UnitTestData;
31  import org.kuali.rice.test.data.UnitTestFile;
32  import org.kuali.rice.test.data.UnitTestSql;
33  import org.kuali.test.KRADTestCase;
34  
35  /**
36   * Tests how refreshing works for Business Objects 
37   * 
38   * @author Kuali Rice Team (rice.collab@kuali.org)
39   *
40   */
41  @PerTestUnitTestData(
42          value = @UnitTestData(
43                  order = {UnitTestData.Type.SQL_STATEMENTS, UnitTestData.Type.SQL_FILES},
44                  sqlStatements = {
45                          @UnitTestSql("delete from trv_acct where acct_fo_id between 101 and 301")
46                          ,@UnitTestSql("delete from trv_acct_fo where acct_fo_id between 101 and 301")
47                  },
48                  sqlFiles = {
49                          @UnitTestFile(filename = "classpath:testAccountManagers.sql", delimiter = ";")
50                          , @UnitTestFile(filename = "classpath:testAccounts.sql", delimiter = ";")
51                  }
52          ),
53          tearDown = @UnitTestData(
54                  sqlStatements = {
55                          @UnitTestSql("delete from trv_acct where acct_fo_id between 101 and 301")
56                          ,@UnitTestSql("delete from trv_acct_fo where acct_fo_id between 101 and 301")
57                  }
58         )
59  )
60  @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.NONE)
61  public class BusinessObjectRefreshTest extends KRADTestCase {
62  
63  	@Test
64  	public void testLazyRefreshField() {
65  		final String accountNumber = "b101";
66  		Account account = KRADServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(Account.class, accountNumber);
67  		
68  		Assert.assertEquals("Retrieved account should have name b101", "b101", account.getName());
69  		Assert.assertEquals("Retrieved account should have a account manager with user name fo-101", "fo-101", account.getAccountManager().getUserName());
70  		
71  		account.setAmId(102L);
72  		account.refreshReferenceObject("accountManager");
73  		
74  		Assert.assertEquals("Account Manager should now have user name of fo-102", "fo-102", account.getAccountManager().getUserName());
75  		
76  	}
77  	
78  	@Test
79  	public void testLazyRefreshWholeObject() {
80  		final String accountNumber = "b101";
81  		Account account = KRADServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(Account.class, accountNumber);
82  		
83  		Assert.assertEquals("Retrieved account should have name b101", "b101", account.getName());
84  		Assert.assertEquals("Retrieved account should have a account manager with user name fo-101", "fo-101", account.getAccountManager().getUserName());
85  		
86  		account.setAmId(102L);
87  		account.refresh();
88  		
89  		Assert.assertEquals("Account Manager should now have user name of fo-102", "fo-102", account.getAccountManager().getUserName());
90  	}
91  	
92  	@Ignore // until BO extensions work with JPA
93  	@Test
94  	public void testLazyCollectionRefresh() {
95  		final Long fredManagerId = 101L;
96  		AccountManager manager = KRADServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(AccountManager.class, new Long(fredManagerId));
97  		
98  		Assert.assertEquals("Retrieve manager should have a name 'fo-101'", "fo-101", manager.getUserName());
99  		Assert.assertEquals("Manager should have one account", new Integer(101), new Integer(manager.getAccounts().size()));
100 		
101 		final String accountNumber = "b102";
102 		Account account = KRADServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(Account.class, accountNumber);
103 
104 		account.setAmId(101L);
105 		account = (Account) KRADServiceLocator.getBusinessObjectService().save(account);
106 		
107 		manager.refreshReferenceObject("accounts");
108 		Assert.assertEquals("Manager should have one account", new Integer(2), new Integer(manager.getAccounts().size()));
109 	}
110 	
111 	@Test
112 	public void testEagerRefreshField() {
113 		final CountyId countyId = new CountyId("COCONINO", "US", "AZ");
114 		CountyBo county = KRADServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(CountyBo.class, countyId);
115 		
116 		final StateId arizonaStateId = new StateId("US", "AZ");
117 		final StateBo arizonaState = KRADServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(StateBo.class, arizonaStateId);
118 		
119 		Assert.assertEquals("On retrieval from database, state code should be AZ", arizonaState.getCode(), county.getState().getCode());
120 		Assert.assertEquals("On retrieval from database, state name should be ARIZONA", arizonaState.getName(), county.getState().getName());
121 		
122 		county.setStateCode("CA");
123 		county.setCode("VENTURA");
124 		county.refresh();
125 		
126 		final StateId californiaStateId = new StateId("US", "CA");
127 		final StateBo californiaState = KRADServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(StateBo.class, californiaStateId);
128 		
129 		Assert.assertEquals("Does eager fetching automatically refresh?", californiaState.getCode(), county.getState().getCode());
130 		Assert.assertEquals("On refresh, state name should be CALIFORNIA", californiaState.getName(), county.getState().getName());
131 	}
132 }