View Javadoc
1   /**
2    * Copyright 2005-2016 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.util;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.junit.Test;
20  
21  import java.util.Properties;
22  
23  import static org.junit.Assert.assertTrue;
24  
25  /**
26   * UrlFactoryTest tests the UrlFactory methods
27   */
28  public class UrlFactoryTest {
29  
30      /**
31       * Test that what is returned from url factory matches the url we expect.
32       */
33      @Test public void testFactoryMatch() throws Exception {
34          String basePath = "http://localhost:8080/";
35          String actionPath = "kr/lookup.do";
36          String testUrl = basePath + actionPath + "?" + KRADConstants.DISPATCH_REQUEST_PARAMETER + "=start" + "&" + KRADConstants.DOC_FORM_KEY + "=903" + KRADConstants.LOOKUPABLE_IMPL_ATTRIBUTE_NAME + "=accountLookupableImpl" + KRADConstants.RETURN_LOCATION_PARAMETER + "=" + basePath + "ib.do";
37          testUrl = UrlFactory.encode(testUrl);
38  
39          // construct lookup url
40          Properties parameters = new Properties();
41          parameters.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, "start");
42          parameters.put(KRADConstants.DOC_FORM_KEY, "903");
43          parameters.put(KRADConstants.LOOKUPABLE_IMPL_ATTRIBUTE_NAME, "accountLookupableImpl");
44          parameters.put(KRADConstants.RETURN_LOCATION_PARAMETER, basePath + "ib.do");
45  
46          String returnedUrl = UrlFactory.parameterizeUrl(basePath + actionPath, parameters);
47  
48          assertTrue("Returned url is empty", StringUtils.isNotBlank(returnedUrl));
49          assertTrue("Returned url has incorrect base", returnedUrl.startsWith(basePath + actionPath + "?"));
50          assertTrue("Returned url does not have correct # of &", StringUtils.countMatches(returnedUrl, "&") == 3);
51          assertTrue("Returned url missing parameter 1", StringUtils.contains(returnedUrl, KRADConstants.DISPATCH_REQUEST_PARAMETER + "=start"));
52          assertTrue("Returned url missing parameter 2", StringUtils.contains(returnedUrl, KRADConstants.DOC_FORM_KEY + "=903"));
53          assertTrue("Returned url missing parameter 3", StringUtils.contains(returnedUrl, KRADConstants.LOOKUPABLE_IMPL_ATTRIBUTE_NAME + "=accountLookupableImpl"));
54          // assertTrue("Returned url missing parameter 4",StringUtils.contains(returnedUrl,
55          // UrlFactory.encode(KRADConstants.RETURN_LOCATION_PARAMETER + "=" + basePath + "ib.do")));
56      }
57  }