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  import java.util.HashMap;
36  import java.util.Map;
37  
38  /**
39   * Tests how refreshing works for Business Objects 
40   * 
41   * @author Kuali Rice Team (rice.collab@kuali.org)
42   *
43   */
44  @PerTestUnitTestData(
45          value = @UnitTestData(
46                  order = {UnitTestData.Type.SQL_STATEMENTS, UnitTestData.Type.SQL_FILES},
47                  sqlStatements = {
48                          @UnitTestSql("delete from trv_acct where acct_fo_id between 101 and 301")
49                          ,@UnitTestSql("delete from trv_acct_fo where acct_fo_id between 101 and 301")
50                  },
51                  sqlFiles = {
52                          @UnitTestFile(filename = "classpath:testAccountManagers.sql", delimiter = ";")
53                          , @UnitTestFile(filename = "classpath:testAccounts.sql", delimiter = ";")
54                  }
55          ),
56          tearDown = @UnitTestData(
57                  sqlStatements = {
58                          @UnitTestSql("delete from trv_acct where acct_fo_id between 101 and 301")
59                          ,@UnitTestSql("delete from trv_acct_fo where acct_fo_id between 101 and 301")
60                  }
61         )
62  )
63  @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.NONE)
64  public class BusinessObjectRefreshTest extends KRADTestCase {
65  
66  	@Test
67      /**
68       * tests that {@link PersistableBusinessObjectBase#refreshReferenceObject(String)} works for a lazy loaded reference when the foreign key is changed
69       */
70  	public void testLazyRefreshField() {
71  		final String accountNumber = "b101";
72  		Account account = KRADServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(Account.class, accountNumber);
73  		
74  		Assert.assertEquals("Retrieved account should have name b101", "b101", account.getName());
75  		Assert.assertEquals("Retrieved account should have a account manager with user name fo-101", "fo-101", account.getAccountManager().getUserName());
76  		
77  		account.setAmId(102L);
78  		account.refreshReferenceObject("accountManager");
79  		
80  		Assert.assertEquals("Account Manager should now have user name of fo-102", "fo-102", account.getAccountManager().getUserName());
81  		
82  	}
83  	
84  	@Test
85      /**
86       * tests that {@link PersistableBusinessObjectBase#refresh()} works for a lazy loaded reference when the foreign key is changed
87       */
88  	public void testLazyRefreshWholeObject() {
89  		final String accountNumber = "b101";
90  		Account account = KRADServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(Account.class, accountNumber);
91  		
92  		Assert.assertEquals("Retrieved account should have name b101", "b101", account.getName());
93  		Assert.assertEquals("Retrieved account should have a account manager with user name fo-101", "fo-101", account.getAccountManager().getUserName());
94  		
95  		account.setAmId(102L);
96  		account.refresh();
97  		
98  		Assert.assertEquals("Account Manager should now have user name of fo-102", "fo-102", account.getAccountManager().getUserName());
99  	}
100 	
101 	@Ignore("until BO extensions work with JPA")
102 	@Test
103 	public void testLazyCollectionRefresh() {
104 		final Long fredManagerId = 101L;
105 		AccountManager manager = KRADServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(AccountManager.class, new Long(fredManagerId));
106 		
107 		Assert.assertEquals("Retrieve manager should have a name 'fo-101'", "fo-101", manager.getUserName());
108 		Assert.assertEquals("Manager should have one account", new Integer(101), new Integer(manager.getAccounts().size()));
109 		
110 		final String accountNumber = "b102";
111 		Account account = KRADServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(Account.class, accountNumber);
112 
113 		account.setAmId(101L);
114 		account = (Account) KRADServiceLocator.getBusinessObjectService().save(account);
115 		
116 		manager.refreshReferenceObject("accounts");
117 		Assert.assertEquals("Manager should have one account", new Integer(2), new Integer(manager.getAccounts().size()));
118 	}
119 
120     @Test
121     public void testEagerRefreshField() {
122         // TODO: implement -- see testEagerRefreshEboField, but do without using EBOs
123     }
124 
125 	@Test
126     /**
127      * tests that {@link PersistableBusinessObjectBase#refresh()} works for an non lazy loaded reference when the foreign key is changed
128      */
129 	public void testEagerRefreshEboField() {
130         Map<String, String> primaryKeys = new HashMap<String, String>();
131         primaryKeys.put("code", "COCONINO");
132         primaryKeys.put("countryCode", "US");
133         primaryKeys.put("stateCode","AZ");
134 		//final CountyId countyId = new CountyId("COCONINO", "US", "AZ");
135 		CountyBo county = KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(CountyBo.class, primaryKeys);
136 
137         primaryKeys.clear();
138         primaryKeys.put("countryCode","US");
139         primaryKeys.put("code","AZ");
140 		//final StateId arizonaStateId = new StateId("US", "AZ");
141 		final StateBo arizonaState = KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(StateBo.class, primaryKeys);
142 		
143 		Assert.assertEquals("On retrieval from database, state code should be AZ", arizonaState.getCode(), county.getState().getCode());
144 		Assert.assertEquals("On retrieval from database, state name should be ARIZONA", arizonaState.getName(), county.getState().getName());
145 		
146 		county.setStateCode("CA");
147 		county.setCode("VENTURA");
148         // NOTE: since county is an EBO, whether or not refresh() fetches references is an implementation choice in the LocationModuleService
149 		county.refresh();
150 		
151 		//final StateId californiaStateId = new StateId("US", "CA");
152         primaryKeys.clear();
153         primaryKeys.put("countryCode","US");
154         primaryKeys.put("code","CA");
155 		final StateBo californiaState = KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(StateBo.class, primaryKeys);
156 		
157 		Assert.assertEquals("Does eager fetching automatically refresh?", californiaState.getCode(), county.getState().getCode());
158 		Assert.assertEquals("On refresh, state name should be CALIFORNIA", californiaState.getName(), county.getState().getName());
159 	}
160 }