001/** 002 * Copyright 2005-2014 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 * @deprecated KNS test class, convert to KRAD equivalent if applicable. 050 */ 051@Deprecated 052@PerTestUnitTestData( 053 value = @UnitTestData( 054 order = {UnitTestData.Type.SQL_STATEMENTS, UnitTestData.Type.SQL_FILES}, 055 sqlStatements = { 056 @UnitTestSql("delete from trv_acct where acct_fo_id = '1'") 057 ,@UnitTestSql("delete from trv_acct_type") 058 059 }, 060 sqlFiles = { 061 @UnitTestFile(filename = "classpath:testAccountType.sql", delimiter = ";") 062 ,@UnitTestFile(filename = "classpath:testAccounts.sql", delimiter = ";") 063 } 064 ), 065 tearDown = @UnitTestData( 066 sqlStatements = { 067 @UnitTestSql("delete from trv_acct where acct_fo_id = '1'") 068 ,@UnitTestSql("delete from trv_acct_type") 069 } 070 ) 071) 072@BaselineTestCase.BaselineMode(BaselineTestCase.Mode.NONE) 073public class KualiLookupableTest extends KNSTestCase { 074 private KualiLookupableImpl lookupableImpl; 075 076 @Override 077 public void setUp() throws Exception { 078 super.setUp(); 079 lookupableImpl = new KualiLookupableImpl(); 080 lookupableImpl.setLookupableHelperService((LookupableHelperService) GlobalResourceLoader.getService( 081 "lookupableHelperService")); 082 try { 083 lookupableImpl.setBusinessObjectClass(Account.class); 084 } catch (RuntimeException re) { 085 if (re.getMessage().contains("Lookup not defined for business object class org.kuali.rice.krad.test.document.bo.Account")) { 086 fail("CI Failure Jira https://jira.kuali.org/browse/KULRICE-9287 " + re.getMessage() + " " + ExceptionUtils.getStackTrace(re)); 087 } 088 } 089 } 090 091 /** 092 * Tests generation of lookup form rows 093 * 094 * @throws Exception 095 */ 096 @Test public void testGetRows() throws Exception { 097 // rows should have been populated by business object class initialization 098 List<? extends Row> rows = lookupableImpl.getRows(); 099 Assert.assertEquals(4, rows.size()); 100 101 Field f = rows.get(0).getField(0); 102 Assert.assertEquals("number", f.getPropertyName()); 103 Assert.assertEquals("Account Number", f.getFieldLabel()); 104 Assert.assertEquals("text", f.getFieldType()); 105 106 f = rows.get(1).getField(0); 107 Assert.assertEquals("name", f.getPropertyName()); 108 Assert.assertEquals("Account Name", f.getFieldLabel()); 109 Assert.assertEquals("text", f.getFieldType()); 110 111 f = rows.get(2).getField(0); 112 Assert.assertEquals("extension.accountTypeCode", f.getPropertyName()); 113 Assert.assertEquals("Account Type Code", f.getFieldLabel()); 114 Assert.assertEquals("dropdown", f.getFieldType()); 115 116 f = rows.get(3).getField(0); 117 Assert.assertEquals("amId", f.getPropertyName()); 118 Assert.assertEquals("Account Manager", f.getFieldLabel()); 119 Assert.assertEquals("text", f.getFieldType()); 120 } 121 122 /** 123 * Test that the return url for a business object is getting set correctly based on the defined return fields 124 * 125 * @throws Exception 126 */ 127 @Test public void testReturnUrl() throws Exception { 128 Map<String, String> lookupProps = new HashMap<String, String>(); 129 lookupProps.put("number", "b101"); 130 lookupProps.put("name", "b101"); 131 132 List<Account> accounts = (List<Account>) KRADServiceLocatorWeb.getLookupService().findCollectionBySearch( 133 Account.class, lookupProps); 134 Assert.assertTrue(accounts.size() == 1); 135 Account account = accounts.get(0); 136 137 Map fieldConversions = new HashMap(); 138 lookupableImpl.setDocFormKey("8888888"); 139 lookupableImpl.setBackLocation(TestConstants.BASE_PATH + "ib.do"); 140 141 String returnUrl = lookupableImpl.getReturnUrl(account, fieldConversions, "kualiLookupable", null).constructCompleteHtmlTag(); 142 143 // check url has our doc form key 144 checkURLContains("Lookup return url does not contain docFormKey", KRADConstants.DOC_FORM_KEY + "=8888888", returnUrl); 145 146 // check url goes back to our back location 147 checkURLContains("Lookup return url does not go back to back location", TestConstants.BASE_PATH + "ib.do", returnUrl); 148 149 Assert.assertEquals(returnUrl, 150 "<a title=\"return valueAccount Number=b101 \" href=\"http://localhost:8080/ib.do?refreshCaller=kualiLookupable&number=b101&methodToCall=refresh&docFormKey=8888888\" >return value</a>"); 151 152 // check that field conversions are working correctly for keys 153 fieldConversions.put("number", "myAccount[0].chartCode"); 154 155 returnUrl = lookupableImpl.getReturnUrl(account, fieldConversions, "kualiLookupable", null).constructCompleteHtmlTag(); 156 157 // check keys have been mapped properly 158 checkURLContains("Lookup return url does not map key", "myAccount[0].chartCode=b101", returnUrl); 159 } 160 161 162 /** 163 * Checks the url string contains a substring 164 * 165 * @param message - an informational message to show if the test fails 166 * @param containString - the string to check for in the url 167 * @param url - a url to look for a string in 168 */ 169 private void checkURLContains(String message, String containString, String url) { 170 Assert.assertTrue(message, url.indexOf(containString) > 0); 171 } 172}