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 * This class tests the KualiLookupable 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 * @throws Exception
081 */
082 @Test public void testGetRows() throws Exception {
083 // rows should have been populated by business object class initialization
084 List<? extends Row> rows = lookupableImpl.getRows();
085 assertEquals(4, rows.size());
086
087 Field f = rows.get(0).getField(0);
088 assertEquals("number", f.getPropertyName());
089 assertEquals("Account Number", f.getFieldLabel());
090 assertEquals("text", f.getFieldType());
091
092 f = rows.get(1).getField(0);
093 assertEquals("name", f.getPropertyName());
094 assertEquals("Account Name", f.getFieldLabel());
095 assertEquals("text", f.getFieldType());
096
097 f = rows.get(2).getField(0);
098 assertEquals("extension.accountTypeCode", f.getPropertyName());
099 assertEquals("Account Type Code", f.getFieldLabel());
100 assertEquals("dropdown", f.getFieldType());
101
102 f = rows.get(3).getField(0);
103 assertEquals("amId", f.getPropertyName());
104 assertEquals("Account Manager Id", f.getFieldLabel());
105 assertEquals("text", f.getFieldType());
106 }
107
108 /**
109 * Test that the return url for a business object is getting set correctly based on the defined return fields.
110 *
111 * @throws Exception
112 */
113 @Test public void testReturnUrl() throws Exception {
114 Map<String, String> lookupProps = new HashMap<String, String>();
115 lookupProps.put("number", "b101");
116 lookupProps.put("name", "b101");
117
118 Account account = (Account) KRADServiceLocatorWeb.getLookupService().findObjectBySearch(Account.class, lookupProps);
119 // ObjectCode objCode = getObjectCodeService().getCountry(TestConstants.Data1.UNIVERSITY_FISCAL_YEAR, TestConstants.Data1.CHART_OF_ACCOUNTS_CODE, TestConstants.Data1.OBJECT_CODE);
120
121 Map fieldConversions = new HashMap();
122 lookupableImpl.setDocFormKey("8888888");
123 lookupableImpl.setBackLocation(TestConstants.BASE_PATH + "ib.do");
124
125 String returnUrl = lookupableImpl.getReturnUrl(account, fieldConversions, "kualiLookupable", null).constructCompleteHtmlTag();
126
127 // check url has our doc form key
128 checkURLContains("Lookup return url does not contain docFormKey", KRADConstants.DOC_FORM_KEY + "=8888888", returnUrl);
129
130 // check url goes back to our back location
131 checkURLContains("Lookup return url does not go back to back location", TestConstants.BASE_PATH + "ib.do", returnUrl);
132
133 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>");
134
135 // check that field conversions are working correctly for keys
136 fieldConversions.put("number", "myAccount[0].chartCode");
137
138 returnUrl = lookupableImpl.getReturnUrl(account, fieldConversions, "kualiLookupable", null).constructCompleteHtmlTag();
139
140 // check keys have been mapped properly
141 checkURLContains("Lookup return url does not map key", "myAccount[0].chartCode=b101", returnUrl);
142 }
143
144
145 /**
146 * Checks the url string contains a substring.
147 *
148 * @param message
149 * @param containString
150 * @param url
151 */
152 private void checkURLContains(String message, String containString, String url) {
153 assertTrue(message, url.indexOf(containString) > 0);
154 }
155 }