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 static org.junit.Assert.assertEquals;
19 import static org.junit.Assert.assertNotNull;
20
21 import java.util.HashMap;
22 import java.util.Map;
23
24 import org.junit.Test;
25 import org.kuali.rice.kns.service.KNSServiceLocator;
26 import org.kuali.rice.krad.test.KRADTestCase;
27 import org.kuali.rice.krad.test.document.bo.AccountExtension;
28 import org.kuali.rice.location.impl.county.CountyBo;
29 import org.kuali.rice.location.impl.state.StateBo;
30 import org.kuali.rice.test.BaselineTestCase;
31 import org.kuali.rice.test.data.PerTestUnitTestData;
32 import org.kuali.rice.test.data.UnitTestData;
33 import org.kuali.rice.test.data.UnitTestFile;
34 import org.kuali.rice.test.data.UnitTestSql;
35
36
37
38
39
40
41
42 @PerTestUnitTestData(
43 value = @UnitTestData(
44 order = {UnitTestData.Type.SQL_STATEMENTS, UnitTestData.Type.SQL_FILES},
45 sqlStatements = {
46 @UnitTestSql("delete from trv_acct_ext")
47 ,@UnitTestSql("delete from trv_acct_type")
48 },
49 sqlFiles = {
50 @UnitTestFile(filename = "classpath:testAccountType.sql", delimiter = ";"),
51 @UnitTestFile(filename = "classpath:testAccountExtensions.sql", delimiter = ";")
52 }
53 ),
54 tearDown = @UnitTestData(
55 sqlStatements = {
56 @UnitTestSql("delete from trv_acct_ext")
57 ,@UnitTestSql("delete from trv_acct_type")
58 }
59 )
60 )
61 @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.NONE)
62 @KRADTestCase.Legacy
63 public class BusinessObjectRefreshTest extends KRADTestCase {
64
65 @Test
66
67
68
69 public void testLazyRefreshField() {
70 final String number = "a1";
71 AccountExtension accountExtension = KNSServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(AccountExtension.class, number);
72
73 assertNotNull( "Unable to retrieve account extension a1", accountExtension );
74 assertEquals("Retrieved account extension should have an account type of IAT", "IAT",
75 accountExtension.getAccountType().getAccountTypeCode());
76 assertEquals("Retrieved account extension should have an account type name of Income Account Type",
77 "Income Account Type", accountExtension.getAccountType().getName());
78 accountExtension.setAccountTypeCode("CAT");
79 accountExtension.refreshReferenceObject("accountType");
80 assertEquals("Account extension should now have an account type name of Clearing Account Type",
81 "Clearing Account Type", accountExtension.getAccountType().getName());
82 }
83
84 @Test
85
86
87
88 public void testLazyRefreshWholeObject() {
89 final String number = "a1";
90 AccountExtension accountExtension = KNSServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(AccountExtension.class, number);
91
92 assertNotNull( "Unable to retrieve account extension a1", accountExtension );
93 assertEquals("Retrieved account extension should have account type IAT", "IAT",
94 accountExtension.getAccountType().getAccountTypeCode());
95 assertEquals("Retrieved account extension should have an account type name of Income Account Type",
96 "Income Account Type", accountExtension.getAccountType().getName());
97
98 accountExtension.setAccountTypeCode("CAT");
99 accountExtension.refresh();
100
101 assertEquals("Account extension should now have an account type name of Clearing Account Type",
102 "Clearing Account Type", accountExtension.getAccountType().getName());
103 }
104
105 @Test
106
107
108
109 public void testEagerRefreshEboField() {
110 Map<String, String> primaryKeys = new HashMap<String, String>();
111 primaryKeys.put("code", "COCONINO");
112 primaryKeys.put("countryCode", "US");
113 primaryKeys.put("stateCode","AZ");
114
115 CountyBo county = KNSServiceLocator.getBusinessObjectService().findByPrimaryKey(CountyBo.class, primaryKeys);
116
117 primaryKeys.clear();
118 primaryKeys.put("countryCode","US");
119 primaryKeys.put("code","AZ");
120
121 final StateBo arizonaState = KNSServiceLocator.getBusinessObjectService().findByPrimaryKey(StateBo.class, primaryKeys);
122
123 assertEquals("On retrieval from database, state code should be AZ", arizonaState.getCode(),
124 county.getState().getCode());
125 assertEquals("On retrieval from database, state name should be ARIZONA", arizonaState.getName(),
126 county.getState().getName());
127
128 county.setStateCode("CA");
129 county.setCode("VENTURA");
130
131 county.refresh();
132
133
134 primaryKeys.clear();
135 primaryKeys.put("countryCode","US");
136 primaryKeys.put("code","CA");
137 final StateBo californiaState = KNSServiceLocator.getBusinessObjectService().findByPrimaryKey(StateBo.class, primaryKeys);
138
139 assertEquals("Does eager fetching automatically refresh?", californiaState.getCode(),
140 county.getState().getCode());
141 assertEquals("On refresh, state name should be CALIFORNIA", californiaState.getName(),
142 county.getState().getName());
143 }
144 }