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
68
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
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
123 }
124
125 @Test
126
127
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
135 CountyBo county = KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(CountyBo.class, primaryKeys);
136
137 primaryKeys.clear();
138 primaryKeys.put("countryCode","US");
139 primaryKeys.put("code","AZ");
140
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
149 county.refresh();
150
151
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 }