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.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
41
42
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
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
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
124 }
125
126 @Test
127
128
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
136 CountyBo county = KNSServiceLocator.getBusinessObjectService().findByPrimaryKey(CountyBo.class, primaryKeys);
137
138 primaryKeys.clear();
139 primaryKeys.put("countryCode","US");
140 primaryKeys.put("code","AZ");
141
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
150 county.refresh();
151
152
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 }