View Javadoc
1   /**
2    * Copyright 2005-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Tests how refreshing works for Business Objects
38   *
39   * @author Kuali Rice Team (rice.collab@kuali.org)
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       * tests that {@link PersistableBusinessObjectBase#refreshReferenceObject(String)} works for a lazy loaded reference when the foreign key is changed
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       * tests that {@link PersistableBusinessObjectBase#refresh()} works for a lazy loaded reference when the foreign key is changed
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      * tests that {@link PersistableBusinessObjectBase#refresh()} works for an non lazy loaded reference when the foreign key is changed
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         //final CountyId countyId = new CountyId("COCONINO", "US", "AZ");
115         CountyBo county = KNSServiceLocator.getBusinessObjectService().findByPrimaryKey(CountyBo.class, primaryKeys);
116 
117         primaryKeys.clear();
118         primaryKeys.put("countryCode","US");
119         primaryKeys.put("code","AZ");
120         //final StateId arizonaStateId = new StateId("US", "AZ");
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         // NOTE: since county is an EBO, whether or not refresh() fetches references is an implementation choice in the LocationModuleService
131         county.refresh();
132 
133         //final StateId californiaStateId = new StateId("US", "CA");
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 }