1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
40
41
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
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 Map<String, String> primaryKeys = new HashMap<String, String>();
117 primaryKeys.put("code", "COCONINO");
118 primaryKeys.put("countryCode", "US");
119 primaryKeys.put("stateCode","AZ");
120
121 CountyBo county = KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(CountyBo.class, primaryKeys);
122
123 primaryKeys.clear();
124 primaryKeys.put("countryCode","US");
125 primaryKeys.put("code","AZ");
126
127 final StateBo arizonaState = KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(StateBo.class, primaryKeys);
128
129 Assert.assertEquals("On retrieval from database, state code should be AZ", arizonaState.getCode(), county.getState().getCode());
130 Assert.assertEquals("On retrieval from database, state name should be ARIZONA", arizonaState.getName(), county.getState().getName());
131
132 county.setStateCode("CA");
133 county.setCode("VENTURA");
134 county.refresh();
135
136
137 primaryKeys.clear();
138 primaryKeys.put("countryCode","US");
139 primaryKeys.put("code","CA");
140 final StateBo californiaState = KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(StateBo.class, primaryKeys);
141
142 Assert.assertEquals("Does eager fetching automatically refresh?", californiaState.getCode(), county.getState().getCode());
143 Assert.assertEquals("On refresh, state name should be CALIFORNIA", californiaState.getName(), county.getState().getName());
144 }
145 }