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.List;
23  import java.util.Map;
24  
25  import org.junit.Test;
26  import org.kuali.rice.kns.lookup.KualiLookupableImpl;
27  import org.kuali.rice.kns.lookup.LookupableHelperService;
28  import org.kuali.rice.kns.web.ui.Field;
29  import org.kuali.rice.kns.web.ui.Row;
30  import org.kuali.rice.krad.service.KRADServiceLocatorInternal;
31  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
32  import org.kuali.rice.krad.test.document.bo.Account;
33  import org.kuali.rice.krad.util.KRADConstants;
34  import org.kuali.rice.test.BaselineTestCase;
35  import org.kuali.rice.test.data.PerTestUnitTestData;
36  import org.kuali.rice.test.data.UnitTestData;
37  import org.kuali.rice.test.data.UnitTestFile;
38  import org.kuali.rice.test.data.UnitTestSql;
39  import org.kuali.test.KRADTestCase;
40  import org.kuali.test.KRADTestConstants.TestConstants;
41  
42  /**
43   * KualiLookupableTest tests {@link KualiLookupableImpl} methods
44   * 
45   * 
46   */
47  @PerTestUnitTestData(
48          value = @UnitTestData(
49                  order = {UnitTestData.Type.SQL_STATEMENTS, UnitTestData.Type.SQL_FILES},
50                  sqlStatements = {
51                          @UnitTestSql("delete from trv_acct where acct_fo_id between 101 and 301")
52                          ,@UnitTestSql("delete from trv_acct_fo where acct_fo_id between 101 and 301")
53                  },
54                  sqlFiles = {
55                          @UnitTestFile(filename = "classpath:testAccountManagers.sql", delimiter = ";")
56                          , @UnitTestFile(filename = "classpath:testAccounts.sql", delimiter = ";")
57                  }
58          ),
59          tearDown = @UnitTestData(
60                  sqlStatements = {
61                          @UnitTestSql("delete from trv_acct where acct_fo_id between 101 and 301")
62                          ,@UnitTestSql("delete from trv_acct_fo where acct_fo_id between 101 and 301")
63                  }
64         )
65  )
66  @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.NONE)
67  public class KualiLookupableTest extends KRADTestCase {
68      private KualiLookupableImpl lookupableImpl;
69  
70      @Override
71      public void setUp() throws Exception {
72          super.setUp();
73          lookupableImpl = new KualiLookupableImpl();
74          lookupableImpl.setLookupableHelperService((LookupableHelperService) KRADServiceLocatorInternal.getService("lookupableHelperService"));
75          lookupableImpl.setBusinessObjectClass(Account.class);
76      }
77  
78      /**
79       * Tests generation of lookup form rows
80       *
81       * @throws Exception
82       */
83      @Test public void testGetRows() throws Exception {
84          // rows should have been populated by business object class initialization
85          List<? extends Row> rows = lookupableImpl.getRows();
86          assertEquals(4, rows.size());
87  
88          Field f = rows.get(0).getField(0);
89          assertEquals("number", f.getPropertyName());
90          assertEquals("Account Number", f.getFieldLabel());
91          assertEquals("text", f.getFieldType());
92  
93          f = rows.get(1).getField(0);
94          assertEquals("name", f.getPropertyName());
95          assertEquals("Account Name", f.getFieldLabel());
96          assertEquals("text", f.getFieldType());
97  
98          f = rows.get(2).getField(0);
99          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 }