View Javadoc

1   /**
2    * Copyright 2005-2013 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.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.rice.krad.test.KRADTestCase;
30  
31  import java.util.ArrayList;
32  import java.util.List;
33  
34  import static org.junit.Assert.assertEquals;
35  
36  /**
37   * PersistableBusinessObjectValuesFinderTest tests the {@link PersistableBusinessObjectValuesFinder}
38   *
39   * @author Kuali Rice Team (rice.collab@kuali.org)
40   */
41  @PerTestUnitTestData(
42          value = @UnitTestData(
43                  order = {UnitTestData.Type.SQL_STATEMENTS, UnitTestData.Type.SQL_FILES},
44                  sqlStatements = {
45                          @UnitTestSql("delete from trv_acct_type")
46                          ,@UnitTestSql("delete from trv_acct_fo where acct_fo_id between 101 and 301")
47                  },
48                  sqlFiles = {
49                          @UnitTestFile(filename = "classpath:testAccountManagers.sql", delimiter = ";")
50                          , @UnitTestFile(filename = "classpath:testAccountType.sql", delimiter = ";")
51                  }
52          ),
53          tearDown = @UnitTestData(
54                  sqlStatements = {
55                          @UnitTestSql("delete from trv_acct_type")
56                          ,@UnitTestSql("delete from trv_acct_fo where acct_fo_id between 101 and 301")
57                  }
58         )
59  )
60  @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.NONE)
61  public class PersistableBusinessObjectValuesFinderTest extends KRADTestCase {
62  
63      private List<KeyValue> testKeyValues = new ArrayList<KeyValue>();
64      private List<KeyValue> testKeyValuesKeyInLabel = new ArrayList<KeyValue>();
65      private List<KeyValue> testKeyValuesLongKey = new ArrayList<KeyValue>();
66      private List<KeyValue> testKeyValuesKeyInLabelLongKey = new ArrayList<KeyValue>();
67  
68      /**
69       * Default Constructor builds KeyValue Lists used for tests.
70       *
71       */
72      public PersistableBusinessObjectValuesFinderTest() {
73      	testKeyValues.add(new ConcreteKeyValue("CAT", "Clearing Account Type"));
74      	testKeyValues.add(new ConcreteKeyValue("EAT", "Expense Account Type"));
75      	testKeyValues.add(new ConcreteKeyValue("IAT", "Income Account Type"));
76      
77      	testKeyValuesKeyInLabel.add(new ConcreteKeyValue("CAT", "CAT - Clearing Account Type"));
78      	testKeyValuesKeyInLabel.add(new ConcreteKeyValue("EAT", "EAT - Expense Account Type"));
79      	testKeyValuesKeyInLabel.add(new ConcreteKeyValue("IAT", "IAT - Income Account Type"));
80       
81      	
82      	for (int x=101;x<302;x++) {
83      	    testKeyValuesLongKey.add(new ConcreteKeyValue(new Long(x).toString(), "fo-" + x));
84      	    testKeyValuesKeyInLabelLongKey.add(new ConcreteKeyValue(new Long(x).toString(), x + " - fo-" + x));
85      	}
86      }
87  
88      /**
89       * tests to make sure the <code>PersistableBusinessObjectValuesFinder</code> works
90       * as expected for the TravelAccountType BO
91       *
92       * @throws Exception
93       */
94      @Test public void testGetKeyValues() throws Exception {
95      	PersistableBusinessObjectValuesFinder valuesFinder = new PersistableBusinessObjectValuesFinder();
96      	valuesFinder.setBusinessObjectClass(AccountType.class);
97      	valuesFinder.setKeyAttributeName("accountTypeCode");
98      	valuesFinder.setLabelAttributeName("name");
99      	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 }