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  	public void testLazyRefreshField() {
68  		final String accountNumber = "b101";
69  		Account account = KRADServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(Account.class, accountNumber);
70  		
71  		Assert.assertEquals("Retrieved account should have name b101", "b101", account.getName());
72  		Assert.assertEquals("Retrieved account should have a account manager with user name fo-101", "fo-101", account.getAccountManager().getUserName());
73  		
74  		account.setAmId(102L);
75  		account.refreshReferenceObject("accountManager");
76  		
77  		Assert.assertEquals("Account Manager should now have user name of fo-102", "fo-102", account.getAccountManager().getUserName());
78  		
79  	}
80  	
81  	@Test
82  	public void testLazyRefreshWholeObject() {
83  		final String accountNumber = "b101";
84  		Account account = KRADServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(Account.class, accountNumber);
85  		
86  		Assert.assertEquals("Retrieved account should have name b101", "b101", account.getName());
87  		Assert.assertEquals("Retrieved account should have a account manager with user name fo-101", "fo-101", account.getAccountManager().getUserName());
88  		
89  		account.setAmId(102L);
90  		account.refresh();
91  		
92  		Assert.assertEquals("Account Manager should now have user name of fo-102", "fo-102", account.getAccountManager().getUserName());
93  	}
94  	
95  	@Ignore // until BO extensions work with JPA
96  	@Test
97  	public void testLazyCollectionRefresh() {
98  		final Long fredManagerId = 101L;
99  		AccountManager manager = KRADServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(AccountManager.class, new Long(fredManagerId));
100 		
101 		Assert.assertEquals("Retrieve manager should have a name 'fo-101'", "fo-101", manager.getUserName());
102 		Assert.assertEquals("Manager should have one account", new Integer(101), new Integer(manager.getAccounts().size()));
103 		
104 		final String accountNumber = "b102";
105 		Account account = KRADServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(Account.class, accountNumber);
106 
107 		account.setAmId(101L);
108 		account = (Account) KRADServiceLocator.getBusinessObjectService().save(account);
109 		
110 		manager.refreshReferenceObject("accounts");
111 		Assert.assertEquals("Manager should have one account", new Integer(2), new Integer(manager.getAccounts().size()));
112 	}
113 
114     @Test
115     public void testEagerRefreshField() {
116         // TODO: implement -- see testEagerRefreshEboField, but do without using EBOs
117     }
118 
119 	@Test
120 	public void testEagerRefreshEboField() {
121 
122         Map<String, String> primaryKeys = new HashMap<String, String>();
123         primaryKeys.put("code", "COCONINO");
124         primaryKeys.put("countryCode", "US");
125         primaryKeys.put("stateCode","AZ");
126 		//final CountyId countyId = new CountyId("COCONINO", "US", "AZ");
127 		CountyBo county = KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(CountyBo.class, primaryKeys);
128 
129         primaryKeys.clear();
130         primaryKeys.put("countryCode","US");
131         primaryKeys.put("code","AZ");
132 		//final StateId arizonaStateId = new StateId("US", "AZ");
133 		final StateBo arizonaState = KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(StateBo.class, primaryKeys);
134 		
135 		Assert.assertEquals("On retrieval from database, state code should be AZ", arizonaState.getCode(), county.getState().getCode());
136 		Assert.assertEquals("On retrieval from database, state name should be ARIZONA", arizonaState.getName(), county.getState().getName());
137 		
138 		county.setStateCode("CA");
139 		county.setCode("VENTURA");
140         // NOTE: since county is an EBO, whether or not refresh() fetches references is an implementation choice in the LocationModuleService
141 		county.refresh();
142 		
143 		//final StateId californiaStateId = new StateId("US", "CA");
144         primaryKeys.clear();
145         primaryKeys.put("countryCode","US");
146         primaryKeys.put("code","CA");
147 		final StateBo californiaState = KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(StateBo.class, primaryKeys);
148 		
149 		Assert.assertEquals("Does eager fetching automatically refresh?", californiaState.getCode(), county.getState().getCode());
150 		Assert.assertEquals("On refresh, state name should be CALIFORNIA", californiaState.getName(), county.getState().getName());
151 	}
152 }