View Javadoc

1   /**
2    * Copyright 2005-2013 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   * 
50   */
51  @PerTestUnitTestData(
52          value = @UnitTestData(
53                  order = {UnitTestData.Type.SQL_STATEMENTS, UnitTestData.Type.SQL_FILES},
54                  sqlStatements = {
55                          @UnitTestSql("delete from trv_acct where acct_fo_id between 101 and 301")
56                          ,@UnitTestSql("delete from trv_acct_fo where acct_fo_id between 101 and 301")
57                  },
58                  sqlFiles = {
59                          @UnitTestFile(filename = "classpath:testAccountManagers.sql", delimiter = ";")
60                          , @UnitTestFile(filename = "classpath:testAccounts.sql", delimiter = ";")
61                  }
62          ),
63          tearDown = @UnitTestData(
64                  sqlStatements = {
65                          @UnitTestSql("delete from trv_acct where acct_fo_id between 101 and 301")
66                          ,@UnitTestSql("delete from trv_acct_fo where acct_fo_id between 101 and 301")
67                  }
68         )
69  )
70  @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.NONE)
71  public class KualiLookupableTest extends KNSTestCase {
72      private KualiLookupableImpl lookupableImpl;
73  
74      @Override
75      public void setUp() throws Exception {
76          super.setUp();
77          lookupableImpl = new KualiLookupableImpl();
78          lookupableImpl.setLookupableHelperService((LookupableHelperService) GlobalResourceLoader.getService(
79                  "lookupableHelperService"));
80          try {
81              lookupableImpl.setBusinessObjectClass(Account.class);
82          } catch (RuntimeException re) {
83              if (re.getMessage().contains("Lookup not defined for business object class org.kuali.rice.krad.test.document.bo.Account")) {
84                  fail("CI Failure Jira https://jira.kuali.org/browse/KULRICE-9287 " + re.getMessage() + " " + ExceptionUtils.getStackTrace(re));
85              }
86          }
87      }
88  
89      /**
90       * Tests generation of lookup form rows
91       *
92       * @throws Exception
93       */
94      @Test public void testGetRows() throws Exception {
95          // rows should have been populated by business object class initialization
96          List<? extends Row> rows = lookupableImpl.getRows();
97          Assert.assertEquals(4, rows.size());
98  
99          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 }