View Javadoc
1   /**
2    * Copyright 2005-2014 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.kns.lookup;
17  
18  import org.apache.commons.lang.exception.ExceptionUtils;
19  import org.junit.Assert;
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertTrue;
22  import static org.junit.Assert.fail;
23  
24  import java.util.HashMap;
25  import java.util.List;
26  import java.util.Map;
27  
28  import org.junit.Test;
29  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
30  import org.kuali.rice.kns.KNSTestCase;
31  import org.kuali.rice.kns.lookup.KualiLookupableImpl;
32  import org.kuali.rice.kns.lookup.LookupableHelperService;
33  import org.kuali.rice.kns.web.ui.Field;
34  import org.kuali.rice.kns.web.ui.Row;
35  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
36  import org.kuali.rice.krad.test.document.bo.Account;
37  import org.kuali.rice.krad.util.KRADConstants;
38  import org.kuali.rice.test.BaselineTestCase;
39  import org.kuali.rice.test.data.PerTestUnitTestData;
40  import org.kuali.rice.test.data.UnitTestData;
41  import org.kuali.rice.test.data.UnitTestFile;
42  import org.kuali.rice.test.data.UnitTestSql;
43  import org.kuali.rice.krad.test.KRADTestCase;
44  import org.kuali.rice.krad.test.KRADTestConstants.TestConstants;
45  
46  /**
47   * KualiLookupableTest tests {@link KualiLookupableImpl} methods
48   * 
49   * @deprecated KNS test class, convert to KRAD equivalent if applicable.
50   */
51  @Deprecated
52  @PerTestUnitTestData(
53          value = @UnitTestData(
54                  order = {UnitTestData.Type.SQL_STATEMENTS, UnitTestData.Type.SQL_FILES},
55                  sqlStatements = {
56                          @UnitTestSql("delete from trv_acct where acct_fo_id = '1'")
57                          ,@UnitTestSql("delete from trv_acct_type")
58  
59                  },
60                  sqlFiles = {
61                          @UnitTestFile(filename = "classpath:testAccountType.sql", delimiter = ";")
62                          ,@UnitTestFile(filename = "classpath:testAccounts.sql", delimiter = ";")
63                  }
64          ),
65          tearDown = @UnitTestData(
66                  sqlStatements = {
67                          @UnitTestSql("delete from trv_acct where acct_fo_id = '1'")
68                          ,@UnitTestSql("delete from trv_acct_type")
69                  }
70         )
71  )
72  @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.NONE)
73  public class KualiLookupableTest extends KNSTestCase {
74      private KualiLookupableImpl lookupableImpl;
75  
76      @Override
77      public void setUp() throws Exception {
78          super.setUp();
79          lookupableImpl = new KualiLookupableImpl();
80          lookupableImpl.setLookupableHelperService((LookupableHelperService) GlobalResourceLoader.getService(
81                  "lookupableHelperService"));
82          try {
83              lookupableImpl.setBusinessObjectClass(Account.class);
84          } catch (RuntimeException re) {
85              if (re.getMessage().contains("Lookup not defined for business object class org.kuali.rice.krad.test.document.bo.Account")) {
86                  fail("CI Failure Jira https://jira.kuali.org/browse/KULRICE-9287 " + re.getMessage() + " " + ExceptionUtils.getStackTrace(re));
87              }
88          }
89      }
90  
91      /**
92       * Tests generation of lookup form rows
93       *
94       * @throws Exception
95       */
96      @Test public void testGetRows() throws Exception {
97          // rows should have been populated by business object class initialization
98          List<? extends Row> rows = lookupableImpl.getRows();
99          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 }