001 /**
002 * Copyright 2005-2011 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.rice.lookup.keyvalues;
017
018 import org.junit.Test;
019 import org.kuali.rice.core.api.util.ConcreteKeyValue;
020 import org.kuali.rice.core.api.util.KeyValue;
021 import org.kuali.rice.krad.keyvalues.PersistableBusinessObjectValuesFinder;
022 import org.kuali.rice.krad.test.document.bo.AccountManager;
023 import org.kuali.rice.krad.test.document.bo.AccountType;
024 import org.kuali.rice.test.BaselineTestCase;
025 import org.kuali.rice.test.data.PerTestUnitTestData;
026 import org.kuali.rice.test.data.UnitTestData;
027 import org.kuali.rice.test.data.UnitTestFile;
028 import org.kuali.rice.test.data.UnitTestSql;
029 import org.kuali.test.KRADTestCase;
030
031 import java.util.ArrayList;
032 import java.util.List;
033
034 import static org.junit.Assert.assertEquals;
035
036 /**
037 * This class tests the PersistableBusinessObjectValuesFinder.
038 *
039 * @author Kuali Rice Team (rice.collab@kuali.org)
040 *
041 */
042 @PerTestUnitTestData(
043 value = @UnitTestData(
044 order = {UnitTestData.Type.SQL_STATEMENTS, UnitTestData.Type.SQL_FILES},
045 sqlStatements = {
046 @UnitTestSql("delete from trv_acct_type")
047 ,@UnitTestSql("delete from trv_acct_fo where acct_fo_id between 101 and 301")
048 },
049 sqlFiles = {
050 @UnitTestFile(filename = "classpath:testAccountManagers.sql", delimiter = ";")
051 , @UnitTestFile(filename = "classpath:testAccountType.sql", delimiter = ";")
052 }
053 ),
054 tearDown = @UnitTestData(
055 sqlStatements = {
056 @UnitTestSql("delete from trv_acct_type")
057 ,@UnitTestSql("delete from trv_acct_fo where acct_fo_id between 101 and 301")
058 }
059 )
060 )
061 @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.NONE)
062 public class PersistableBusinessObjectValuesFinderTest extends KRADTestCase {
063
064 private List<KeyValue> testKeyValues = new ArrayList<KeyValue>();
065 private List<KeyValue> testKeyValuesKeyInLabel = new ArrayList<KeyValue>();
066 private List<KeyValue> testKeyValuesLongKey = new ArrayList<KeyValue>();
067 private List<KeyValue> testKeyValuesKeyInLabelLongKey = new ArrayList<KeyValue>();
068
069 /**
070 * Default Constructor builds KeyValue Lists used for tests.
071 *
072 */
073 public PersistableBusinessObjectValuesFinderTest() {
074 testKeyValues.add(new ConcreteKeyValue("CAT", "Clearing Account Type"));
075 testKeyValues.add(new ConcreteKeyValue("EAT", "Expense Account Type"));
076 testKeyValues.add(new ConcreteKeyValue("IAT", "Income Account Type"));
077
078 testKeyValuesKeyInLabel.add(new ConcreteKeyValue("CAT", "CAT - Clearing Account Type"));
079 testKeyValuesKeyInLabel.add(new ConcreteKeyValue("EAT", "EAT - Expense Account Type"));
080 testKeyValuesKeyInLabel.add(new ConcreteKeyValue("IAT", "IAT - Income Account Type"));
081
082
083 for (int x=101;x<302;x++) {
084 testKeyValuesLongKey.add(new ConcreteKeyValue(new Long(x).toString(), "fo-" + x));
085 testKeyValuesKeyInLabelLongKey.add(new ConcreteKeyValue(new Long(x).toString(), x + " - fo-" + x));
086 }
087 }
088
089 /**
090 * This method tests to make sure teh PersistableBusinessObjectValuesFinder works
091 * as expected for the TravelAccountType BO.
092 *
093 * @throws Exception
094 */
095 @Test public void testGetKeyValues() throws Exception {
096 PersistableBusinessObjectValuesFinder valuesFinder = new PersistableBusinessObjectValuesFinder();
097 valuesFinder.setBusinessObjectClass(AccountType.class);
098 valuesFinder.setKeyAttributeName("accountTypeCode");
099 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 }