001 /**
002 * Copyright 2005-2011 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 */
016 package org.kuali.rice.krad.bo;
017
018 import org.junit.Assert;
019 import org.junit.Ignore;
020 import org.junit.Test;
021 import org.kuali.rice.krad.service.KRADServiceLocator;
022 import org.kuali.rice.krad.test.document.bo.Account;
023 import org.kuali.rice.krad.test.document.bo.AccountManager;
024 import org.kuali.rice.location.impl.county.CountyBo;
025 import org.kuali.rice.location.impl.county.CountyId;
026 import org.kuali.rice.location.impl.state.StateBo;
027 import org.kuali.rice.location.impl.state.StateId;
028 import org.kuali.rice.test.BaselineTestCase;
029 import org.kuali.rice.test.data.PerTestUnitTestData;
030 import org.kuali.rice.test.data.UnitTestData;
031 import org.kuali.rice.test.data.UnitTestFile;
032 import org.kuali.rice.test.data.UnitTestSql;
033 import org.kuali.test.KRADTestCase;
034
035 /**
036 * Tests how refreshing works for Business Objects
037 *
038 * @author Kuali Rice Team (rice.collab@kuali.org)
039 *
040 */
041 @PerTestUnitTestData(
042 value = @UnitTestData(
043 order = {UnitTestData.Type.SQL_STATEMENTS, UnitTestData.Type.SQL_FILES},
044 sqlStatements = {
045 @UnitTestSql("delete from trv_acct where acct_fo_id between 101 and 301")
046 ,@UnitTestSql("delete from trv_acct_fo where acct_fo_id between 101 and 301")
047 },
048 sqlFiles = {
049 @UnitTestFile(filename = "classpath:testAccountManagers.sql", delimiter = ";")
050 , @UnitTestFile(filename = "classpath:testAccounts.sql", delimiter = ";")
051 }
052 ),
053 tearDown = @UnitTestData(
054 sqlStatements = {
055 @UnitTestSql("delete from trv_acct where acct_fo_id between 101 and 301")
056 ,@UnitTestSql("delete from trv_acct_fo where acct_fo_id between 101 and 301")
057 }
058 )
059 )
060 @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.NONE)
061 public class BusinessObjectRefreshTest extends KRADTestCase {
062
063 @Test
064 public void testLazyRefreshField() {
065 final String accountNumber = "b101";
066 Account account = KRADServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(Account.class, accountNumber);
067
068 Assert.assertEquals("Retrieved account should have name b101", "b101", account.getName());
069 Assert.assertEquals("Retrieved account should have a account manager with user name fo-101", "fo-101", account.getAccountManager().getUserName());
070
071 account.setAmId(102L);
072 account.refreshReferenceObject("accountManager");
073
074 Assert.assertEquals("Account Manager should now have user name of fo-102", "fo-102", account.getAccountManager().getUserName());
075
076 }
077
078 @Test
079 public void testLazyRefreshWholeObject() {
080 final String accountNumber = "b101";
081 Account account = KRADServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(Account.class, accountNumber);
082
083 Assert.assertEquals("Retrieved account should have name b101", "b101", account.getName());
084 Assert.assertEquals("Retrieved account should have a account manager with user name fo-101", "fo-101", account.getAccountManager().getUserName());
085
086 account.setAmId(102L);
087 account.refresh();
088
089 Assert.assertEquals("Account Manager should now have user name of fo-102", "fo-102", account.getAccountManager().getUserName());
090 }
091
092 @Ignore // until BO extensions work with JPA
093 @Test
094 public void testLazyCollectionRefresh() {
095 final Long fredManagerId = 101L;
096 AccountManager manager = KRADServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(AccountManager.class, new Long(fredManagerId));
097
098 Assert.assertEquals("Retrieve manager should have a name 'fo-101'", "fo-101", manager.getUserName());
099 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 }