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.krad.lookup;
017
018 import static org.junit.Assert.assertEquals;
019 import static org.junit.Assert.assertTrue;
020
021 import java.util.HashMap;
022 import java.util.List;
023 import java.util.Map;
024
025 import org.junit.Test;
026 import org.kuali.rice.kns.lookup.KualiLookupableImpl;
027 import org.kuali.rice.kns.lookup.LookupableHelperService;
028 import org.kuali.rice.kns.web.ui.Field;
029 import org.kuali.rice.kns.web.ui.Row;
030 import org.kuali.rice.krad.service.KRADServiceLocatorInternal;
031 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
032 import org.kuali.rice.krad.test.document.bo.Account;
033 import org.kuali.rice.krad.util.KRADConstants;
034 import org.kuali.rice.test.BaselineTestCase;
035 import org.kuali.rice.test.data.PerTestUnitTestData;
036 import org.kuali.rice.test.data.UnitTestData;
037 import org.kuali.rice.test.data.UnitTestFile;
038 import org.kuali.rice.test.data.UnitTestSql;
039 import org.kuali.test.KRADTestCase;
040 import org.kuali.test.KRADTestConstants.TestConstants;
041
042 /**
043 * KualiLookupableTest tests {@link KualiLookupableImpl} methods
044 *
045 *
046 */
047 @PerTestUnitTestData(
048 value = @UnitTestData(
049 order = {UnitTestData.Type.SQL_STATEMENTS, UnitTestData.Type.SQL_FILES},
050 sqlStatements = {
051 @UnitTestSql("delete from trv_acct where acct_fo_id between 101 and 301")
052 ,@UnitTestSql("delete from trv_acct_fo where acct_fo_id between 101 and 301")
053 },
054 sqlFiles = {
055 @UnitTestFile(filename = "classpath:testAccountManagers.sql", delimiter = ";")
056 , @UnitTestFile(filename = "classpath:testAccounts.sql", delimiter = ";")
057 }
058 ),
059 tearDown = @UnitTestData(
060 sqlStatements = {
061 @UnitTestSql("delete from trv_acct where acct_fo_id between 101 and 301")
062 ,@UnitTestSql("delete from trv_acct_fo where acct_fo_id between 101 and 301")
063 }
064 )
065 )
066 @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.NONE)
067 public class KualiLookupableTest extends KRADTestCase {
068 private KualiLookupableImpl lookupableImpl;
069
070 @Override
071 public void setUp() throws Exception {
072 super.setUp();
073 lookupableImpl = new KualiLookupableImpl();
074 lookupableImpl.setLookupableHelperService((LookupableHelperService) KRADServiceLocatorInternal.getService("lookupableHelperService"));
075 lookupableImpl.setBusinessObjectClass(Account.class);
076 }
077
078 /**
079 * Tests generation of lookup form rows
080 *
081 * @throws Exception
082 */
083 @Test public void testGetRows() throws Exception {
084 // rows should have been populated by business object class initialization
085 List<? extends Row> rows = lookupableImpl.getRows();
086 assertEquals(4, rows.size());
087
088 Field f = rows.get(0).getField(0);
089 assertEquals("number", f.getPropertyName());
090 assertEquals("Account Number", f.getFieldLabel());
091 assertEquals("text", f.getFieldType());
092
093 f = rows.get(1).getField(0);
094 assertEquals("name", f.getPropertyName());
095 assertEquals("Account Name", f.getFieldLabel());
096 assertEquals("text", f.getFieldType());
097
098 f = rows.get(2).getField(0);
099 assertEquals("extension.accountTypeCode", f.getPropertyName());
100 assertEquals("Account Type Code", f.getFieldLabel());
101 assertEquals("dropdown", f.getFieldType());
102
103 f = rows.get(3).getField(0);
104 assertEquals("amId", f.getPropertyName());
105 assertEquals("Account Manager Id", f.getFieldLabel());
106 assertEquals("text", f.getFieldType());
107 }
108
109 /**
110 * Test that the return url for a business object is getting set correctly based on the defined return fields
111 *
112 * @throws Exception
113 */
114 @Test public void testReturnUrl() throws Exception {
115 Map<String, String> lookupProps = new HashMap<String, String>();
116 lookupProps.put("number", "b101");
117 lookupProps.put("name", "b101");
118
119 Account account = (Account) KRADServiceLocatorWeb.getLookupService().findObjectBySearch(Account.class, lookupProps);
120 // ObjectCode objCode = getObjectCodeService().getCountry(TestConstants.Data1.UNIVERSITY_FISCAL_YEAR, TestConstants.Data1.CHART_OF_ACCOUNTS_CODE, TestConstants.Data1.OBJECT_CODE);
121
122 Map fieldConversions = new HashMap();
123 lookupableImpl.setDocFormKey("8888888");
124 lookupableImpl.setBackLocation(TestConstants.BASE_PATH + "ib.do");
125
126 String returnUrl = lookupableImpl.getReturnUrl(account, fieldConversions, "kualiLookupable", null).constructCompleteHtmlTag();
127
128 // check url has our doc form key
129 checkURLContains("Lookup return url does not contain docFormKey", KRADConstants.DOC_FORM_KEY + "=8888888", returnUrl);
130
131 // check url goes back to our back location
132 checkURLContains("Lookup return url does not go back to back location", TestConstants.BASE_PATH + "ib.do", returnUrl);
133
134 assertEquals(returnUrl, "<a title=\"return valueAccount Number=b101 \" href=\"http://localhost:8080/ib.do?refreshCaller=kualiLookupable&number=b101&methodToCall=refresh&docFormKey=8888888\" >return value</a>");
135
136 // check that field conversions are working correctly for keys
137 fieldConversions.put("number", "myAccount[0].chartCode");
138
139 returnUrl = lookupableImpl.getReturnUrl(account, fieldConversions, "kualiLookupable", null).constructCompleteHtmlTag();
140
141 // check keys have been mapped properly
142 checkURLContains("Lookup return url does not map key", "myAccount[0].chartCode=b101", returnUrl);
143 }
144
145
146 /**
147 * Checks the url string contains a substring
148 *
149 * @param message - an informational message to show if the test fails
150 * @param containString - the string to check for in the url
151 * @param url - a url to look for a string in
152 */
153 private void checkURLContains(String message, String containString, String url) {
154 assertTrue(message, url.indexOf(containString) > 0);
155 }
156 }