001 /**
002 * Copyright 2005-2013 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.krad.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.rice.krad.test.KRADTestCase;
030
031 import java.util.ArrayList;
032 import java.util.List;
033
034 import static org.junit.Assert.assertEquals;
035
036 /**
037 * PersistableBusinessObjectValuesFinderTest tests the {@link PersistableBusinessObjectValuesFinder}
038 *
039 * @author Kuali Rice Team (rice.collab@kuali.org)
040 */
041 @PerTestUnitTestData(
042 value = @UnitTestData(
043 order = {UnitTestData.Type.SQL_STATEMENTS, UnitTestData.Type.SQL_FILES},
044 sqlStatements = {
045 @UnitTestSql("delete from trv_acct_type")
046 ,@UnitTestSql("delete from trv_acct_fo where acct_fo_id between 101 and 301")
047 },
048 sqlFiles = {
049 @UnitTestFile(filename = "classpath:testAccountManagers.sql", delimiter = ";")
050 , @UnitTestFile(filename = "classpath:testAccountType.sql", delimiter = ";")
051 }
052 ),
053 tearDown = @UnitTestData(
054 sqlStatements = {
055 @UnitTestSql("delete from trv_acct_type")
056 ,@UnitTestSql("delete from trv_acct_fo where acct_fo_id between 101 and 301")
057 }
058 )
059 )
060 @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.NONE)
061 public class PersistableBusinessObjectValuesFinderTest extends KRADTestCase {
062
063 private List<KeyValue> testKeyValues = new ArrayList<KeyValue>();
064 private List<KeyValue> testKeyValuesKeyInLabel = new ArrayList<KeyValue>();
065 private List<KeyValue> testKeyValuesLongKey = new ArrayList<KeyValue>();
066 private List<KeyValue> testKeyValuesKeyInLabelLongKey = new ArrayList<KeyValue>();
067
068 /**
069 * Default Constructor builds KeyValue Lists used for tests.
070 *
071 */
072 public PersistableBusinessObjectValuesFinderTest() {
073 testKeyValues.add(new ConcreteKeyValue("CAT", "Clearing Account Type"));
074 testKeyValues.add(new ConcreteKeyValue("EAT", "Expense Account Type"));
075 testKeyValues.add(new ConcreteKeyValue("IAT", "Income Account Type"));
076
077 testKeyValuesKeyInLabel.add(new ConcreteKeyValue("CAT", "CAT - Clearing Account Type"));
078 testKeyValuesKeyInLabel.add(new ConcreteKeyValue("EAT", "EAT - Expense Account Type"));
079 testKeyValuesKeyInLabel.add(new ConcreteKeyValue("IAT", "IAT - Income Account Type"));
080
081
082 for (int x=101;x<302;x++) {
083 testKeyValuesLongKey.add(new ConcreteKeyValue(new Long(x).toString(), "fo-" + x));
084 testKeyValuesKeyInLabelLongKey.add(new ConcreteKeyValue(new Long(x).toString(), x + " - fo-" + x));
085 }
086 }
087
088 /**
089 * tests to make sure the <code>PersistableBusinessObjectValuesFinder</code> works
090 * as expected for the TravelAccountType BO
091 *
092 * @throws Exception
093 */
094 @Test public void testGetKeyValues() throws Exception {
095 PersistableBusinessObjectValuesFinder valuesFinder = new PersistableBusinessObjectValuesFinder();
096 valuesFinder.setBusinessObjectClass(AccountType.class);
097 valuesFinder.setKeyAttributeName("accountTypeCode");
098 valuesFinder.setLabelAttributeName("name");
099 valuesFinder.setIncludeKeyInDescription(false);
100 List<KeyValue> keyValues = valuesFinder.getKeyValues();
101 assertEquals(testKeyValues.size(), keyValues.size());
102 for (KeyValue testKeyValue: testKeyValues) {
103 assertEquals(testKeyValue.getValue(), valuesFinder.getKeyLabel(testKeyValue.getKey()));
104 }
105 }
106
107 /**
108 * tests to make sure the <code>PersistableBusinessObjectValuesFinder</code> works
109 * as expected for the TravelAccountType BO with the key included in the label
110 *
111 * @throws Exception
112 */
113 @Test public void testGetKeyValuesKeyInLabel() throws Exception {
114 PersistableBusinessObjectValuesFinder valuesFinder = new PersistableBusinessObjectValuesFinder();
115 valuesFinder.setBusinessObjectClass(AccountType.class);
116 valuesFinder.setKeyAttributeName("accountTypeCode");
117 valuesFinder.setLabelAttributeName("name");
118 valuesFinder.setIncludeKeyInDescription(true);
119 List<KeyValue> keyValues = valuesFinder.getKeyValues();
120 assertEquals(testKeyValuesKeyInLabel.size(), keyValues.size());
121 for (KeyValue testKeyValue: testKeyValuesKeyInLabel) {
122 assertEquals(testKeyValue.getValue(), valuesFinder.getKeyLabel(testKeyValue.getKey()));
123 }
124 }
125
126 /**
127 * tests to make sure the <code>PersistableBusinessObjectValuesFinder</code> works
128 * as expected for the FiscalOfficer BO.
129 *
130 * @throws Exception
131 */
132 @Test public void testGetKeyValuesLongKey() throws Exception {
133 PersistableBusinessObjectValuesFinder valuesFinder = new PersistableBusinessObjectValuesFinder();
134 valuesFinder.setBusinessObjectClass(AccountManager.class);
135 valuesFinder.setKeyAttributeName("amId");
136 valuesFinder.setLabelAttributeName("userName");
137 valuesFinder.setIncludeKeyInDescription(false);
138 List<KeyValue> keyValues = valuesFinder.getKeyValues();
139 assertEquals(testKeyValuesLongKey.size(), keyValues.size());
140 for (KeyValue testKeyValue: testKeyValuesLongKey) {
141 assertEquals(testKeyValue.getValue(), valuesFinder.getKeyLabel(testKeyValue.getKey()));
142 }
143 }
144
145 /**
146 * tests that <code>PersistableBusinessObjectValuesFinder</code> works
147 * as expected for the FiscalOfficer BO with the key included in the label
148 *
149 * @throws Exception
150 */
151 @Test public void testGetKeyValuesKeyInLabelLongKey() throws Exception {
152 PersistableBusinessObjectValuesFinder valuesFinder = new PersistableBusinessObjectValuesFinder();
153 valuesFinder.setBusinessObjectClass(AccountManager.class);
154 valuesFinder.setKeyAttributeName("amId");
155 valuesFinder.setLabelAttributeName("userName");
156 valuesFinder.setIncludeKeyInDescription(true);
157 List<KeyValue> keyValues = valuesFinder.getKeyValues();
158 assertEquals(testKeyValuesKeyInLabelLongKey.size(), keyValues.size());
159 for (KeyValue testKeyValue: testKeyValuesKeyInLabelLongKey) {
160 assertEquals(testKeyValue.getValue(), valuesFinder.getKeyLabel(testKeyValue.getKey()));
161 }
162 }
163
164 }