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