View Javadoc

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