View Javadoc

1   /**
2    * Copyright 2005-2011 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.lookup.keyvalues;
17  
18  import org.junit.Test;
19  import org.kuali.rice.core.api.util.ConcreteKeyValue;
20  import org.kuali.rice.core.api.util.KeyValue;
21  import org.kuali.rice.krad.keyvalues.PersistableBusinessObjectValuesFinder;
22  import org.kuali.rice.krad.test.document.bo.AccountManager;
23  import org.kuali.rice.krad.test.document.bo.AccountType;
24  import org.kuali.rice.test.BaselineTestCase;
25  import org.kuali.rice.test.data.PerTestUnitTestData;
26  import org.kuali.rice.test.data.UnitTestData;
27  import org.kuali.rice.test.data.UnitTestFile;
28  import org.kuali.rice.test.data.UnitTestSql;
29  import org.kuali.test.KRADTestCase;
30  
31  import java.util.ArrayList;
32  import java.util.List;
33  
34  import static org.junit.Assert.assertEquals;
35  
36  /**
37   * This class tests the PersistableBusinessObjectValuesFinder.
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_type")
47                          ,@UnitTestSql("delete from trv_acct_fo where acct_fo_id between 101 and 301")
48                  },
49                  sqlFiles = {
50                          @UnitTestFile(filename = "classpath:testAccountManagers.sql", delimiter = ";")
51                          , @UnitTestFile(filename = "classpath:testAccountType.sql", delimiter = ";")
52                  }
53          ),
54          tearDown = @UnitTestData(
55                  sqlStatements = {
56                          @UnitTestSql("delete from trv_acct_type")
57                          ,@UnitTestSql("delete from trv_acct_fo where acct_fo_id between 101 and 301")
58                  }
59         )
60  )
61  @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.NONE)
62  public class PersistableBusinessObjectValuesFinderTest extends KRADTestCase {
63  
64      private List<KeyValue> testKeyValues = new ArrayList<KeyValue>();
65      private List<KeyValue> testKeyValuesKeyInLabel = new ArrayList<KeyValue>();
66      private List<KeyValue> testKeyValuesLongKey = new ArrayList<KeyValue>();
67      private List<KeyValue> testKeyValuesKeyInLabelLongKey = new ArrayList<KeyValue>();
68  
69      /**
70       * Default Constructor builds KeyValue Lists used for tests.
71       *
72       */
73      public PersistableBusinessObjectValuesFinderTest() {
74      	testKeyValues.add(new ConcreteKeyValue("CAT", "Clearing Account Type"));
75      	testKeyValues.add(new ConcreteKeyValue("EAT", "Expense Account Type"));
76      	testKeyValues.add(new ConcreteKeyValue("IAT", "Income Account Type"));
77      
78      	testKeyValuesKeyInLabel.add(new ConcreteKeyValue("CAT", "CAT - Clearing Account Type"));
79      	testKeyValuesKeyInLabel.add(new ConcreteKeyValue("EAT", "EAT - Expense Account Type"));
80      	testKeyValuesKeyInLabel.add(new ConcreteKeyValue("IAT", "IAT - Income Account Type"));
81       
82      	
83      	for (int x=101;x<302;x++) {
84      	    testKeyValuesLongKey.add(new ConcreteKeyValue(new Long(x).toString(), "fo-" + x));
85      	    testKeyValuesKeyInLabelLongKey.add(new ConcreteKeyValue(new Long(x).toString(), x + " - fo-" + x));
86      	}
87      }
88  
89      /**
90       * This method tests to make sure teh PersistableBusinessObjectValuesFinder works
91       * as expected for the TravelAccountType BO.
92       *
93       * @throws Exception
94       */
95      @Test public void testGetKeyValues() throws Exception {
96      	PersistableBusinessObjectValuesFinder valuesFinder = new PersistableBusinessObjectValuesFinder();
97      	valuesFinder.setBusinessObjectClass(AccountType.class);
98      	valuesFinder.setKeyAttributeName("accountTypeCode");
99      	valuesFinder.setLabelAttributeName("name");
100     	valuesFinder.setIncludeKeyInDescription(false);
101     	List<KeyValue> keyValues = valuesFinder.getKeyValues();
102     	assertEquals(testKeyValues.size(), keyValues.size());
103     	for (KeyValue testKeyValue: testKeyValues) {
104                 assertEquals(testKeyValue.getValue(), valuesFinder.getKeyLabel(testKeyValue.getKey()));
105     	}
106     }
107 
108     /**
109      * This method tests to make sure teh PersistableBusinessObjectValuesFinder works
110      * as expected for the TravelAccountType BO with the key included in the label.
111      *
112      * @throws Exception
113      */
114     @Test public void testGetKeyValuesKeyInLabel() throws Exception {
115     	PersistableBusinessObjectValuesFinder valuesFinder = new PersistableBusinessObjectValuesFinder();
116     	valuesFinder.setBusinessObjectClass(AccountType.class);
117     	valuesFinder.setKeyAttributeName("accountTypeCode");
118     	valuesFinder.setLabelAttributeName("name");
119     	valuesFinder.setIncludeKeyInDescription(true);
120     	List<KeyValue> keyValues = valuesFinder.getKeyValues();
121     	assertEquals(testKeyValuesKeyInLabel.size(), keyValues.size());
122     	for (KeyValue testKeyValue: testKeyValuesKeyInLabel) {
123                 assertEquals(testKeyValue.getValue(), valuesFinder.getKeyLabel(testKeyValue.getKey()));
124     	}
125     }
126 
127     /**
128      * This method tests to make sure teh PersistableBusinessObjectValuesFinder works
129      * as expected for the FiscalOfficer BO.
130      *
131      * @throws Exception
132      */
133     @Test public void testGetKeyValuesLongKey() throws Exception {
134     	PersistableBusinessObjectValuesFinder valuesFinder = new PersistableBusinessObjectValuesFinder();
135     	valuesFinder.setBusinessObjectClass(AccountManager.class);
136     	valuesFinder.setKeyAttributeName("amId");
137     	valuesFinder.setLabelAttributeName("userName");
138     	valuesFinder.setIncludeKeyInDescription(false);
139     	List<KeyValue> keyValues = valuesFinder.getKeyValues();
140     	assertEquals(testKeyValuesLongKey.size(), keyValues.size());
141     	for (KeyValue testKeyValue: testKeyValuesLongKey) {
142                 assertEquals(testKeyValue.getValue(), valuesFinder.getKeyLabel(testKeyValue.getKey()));
143     	}
144     }
145 
146     /**
147      * This method tests to make sure teh PersistableBusinessObjectValuesFinder works
148      * as expected for the FiscalOfficer BO with the key included in the label.
149      *
150      * @throws Exception
151      */
152     @Test public void testGetKeyValuesKeyInLabelLongKey() throws Exception {
153     	PersistableBusinessObjectValuesFinder valuesFinder = new PersistableBusinessObjectValuesFinder();
154     	valuesFinder.setBusinessObjectClass(AccountManager.class);
155     	valuesFinder.setKeyAttributeName("amId");
156     	valuesFinder.setLabelAttributeName("userName");
157     	valuesFinder.setIncludeKeyInDescription(true);
158     	List<KeyValue> keyValues = valuesFinder.getKeyValues();
159     	assertEquals(testKeyValuesKeyInLabelLongKey.size(), keyValues.size());
160     	for (KeyValue testKeyValue: testKeyValuesKeyInLabelLongKey) {
161                 assertEquals(testKeyValue.getValue(), valuesFinder.getKeyLabel(testKeyValue.getKey()));
162     	}
163     }
164 
165 }