View Javadoc

1   /**
2    * Copyright 2005-2011 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.lookup;
17  
18  import static org.junit.Assert.assertEquals;
19  import static org.junit.Assert.assertTrue;
20  
21  import java.util.HashMap;
22  import java.util.Map;
23  
24  import org.junit.Test;
25  import org.kuali.rice.kns.lookup.KualiLookupableImpl;
26  import org.kuali.rice.kns.lookup.LookupableHelperService;
27  import org.kuali.rice.krad.service.KRADServiceLocatorInternal;
28  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
29  import org.kuali.rice.krad.test.document.bo.Account;
30  import org.kuali.rice.krad.util.KRADConstants;
31  import org.kuali.rice.test.BaselineTestCase;
32  import org.kuali.rice.test.data.PerTestUnitTestData;
33  import org.kuali.rice.test.data.UnitTestData;
34  import org.kuali.rice.test.data.UnitTestFile;
35  import org.kuali.rice.test.data.UnitTestSql;
36  import org.kuali.test.KRADTestCase;
37  import org.kuali.test.KRADTestConstants.TestConstants;
38  
39  /**
40   * This class tests the KualiLookupable methods.
41   * 
42   * 
43   */
44  @PerTestUnitTestData(
45          value = @UnitTestData(
46                  order = {UnitTestData.Type.SQL_STATEMENTS, UnitTestData.Type.SQL_FILES},
47                  sqlStatements = {
48                          @UnitTestSql("delete from trv_acct where acct_fo_id between 101 and 301")
49                          ,@UnitTestSql("delete from trv_acct_fo where acct_fo_id between 101 and 301")
50                  },
51                  sqlFiles = {
52                          @UnitTestFile(filename = "classpath:testAccountManagers.sql", delimiter = ";")
53                          , @UnitTestFile(filename = "classpath:testAccounts.sql", delimiter = ";")
54                  }
55          ),
56          tearDown = @UnitTestData(
57                  sqlStatements = {
58                          @UnitTestSql("delete from trv_acct where acct_fo_id between 101 and 301")
59                          ,@UnitTestSql("delete from trv_acct_fo where acct_fo_id between 101 and 301")
60                  }
61         )
62  )
63  @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.NONE)
64  public class KualiLookupableTest extends KRADTestCase {
65      private KualiLookupableImpl lookupableImpl;
66  
67      @Override
68      public void setUp() throws Exception {
69          super.setUp();
70          lookupableImpl = new KualiLookupableImpl();
71          lookupableImpl.setLookupableHelperService((LookupableHelperService) KRADServiceLocatorInternal.getService("lookupableHelperService"));
72          lookupableImpl.setBusinessObjectClass(Account.class);
73      }
74  
75      /**
76       * Test that the return url for a business object is getting set correctly based on the defined return fields.
77       * 
78       * @throws Exception
79       */
80      @Test public void testReturnUrl() throws Exception {
81      	Map<String, String> lookupProps = new HashMap<String, String>();
82      	lookupProps.put("number", "b101");
83      	lookupProps.put("name", "b101");
84      	
85      	Account account = (Account) KRADServiceLocatorWeb.getLookupService().findObjectBySearch(Account.class, lookupProps);
86  //        ObjectCode objCode = getObjectCodeService().getCountry(TestConstants.Data1.UNIVERSITY_FISCAL_YEAR, TestConstants.Data1.CHART_OF_ACCOUNTS_CODE, TestConstants.Data1.OBJECT_CODE);
87  
88          Map fieldConversions = new HashMap();
89          lookupableImpl.setDocFormKey("8888888");
90          lookupableImpl.setBackLocation(TestConstants.BASE_PATH + "ib.do");
91  
92          String returnUrl = lookupableImpl.getReturnUrl(account, fieldConversions, "kualiLookupable", null).constructCompleteHtmlTag();
93  
94          // check url has our doc form key
95          checkURLContains("Lookup return url does not contain docFormKey", KRADConstants.DOC_FORM_KEY + "=8888888", returnUrl);
96  
97          // check url goes back to our back location
98          checkURLContains("Lookup return url does not go back to back location", TestConstants.BASE_PATH + "ib.do", returnUrl);
99  
100         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>");
101 
102         // check that field conversions are working correctly for keys
103         fieldConversions.put("number", "myAccount[0].chartCode");
104 
105         returnUrl = lookupableImpl.getReturnUrl(account, fieldConversions, "kualiLookupable", null).constructCompleteHtmlTag();
106 
107         // check keys have been mapped properly
108         checkURLContains("Lookup return url does not map key", "myAccount[0].chartCode=b101", returnUrl);
109     }
110 
111 
112     /**
113      * Checks the url string contains a substring.
114      * 
115      * @param message
116      * @param containString
117      * @param url
118      */
119     private void checkURLContains(String message, String containString, String url) {
120         assertTrue(message, url.indexOf(containString) > 0);
121     }
122 }