001/**
002 * Copyright 2005-2015 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.krad.util;
017
018import org.apache.commons.lang.StringUtils;
019import org.junit.Test;
020
021import java.util.Properties;
022
023import static org.junit.Assert.assertTrue;
024
025/**
026 * UrlFactoryTest tests the UrlFactory methods
027 */
028public class UrlFactoryTest {
029
030    /**
031     * Test that what is returned from url factory matches the url we expect.
032     */
033    @Test public void testFactoryMatch() throws Exception {
034        String basePath = "http://localhost:8080/";
035        String actionPath = "kr/lookup.do";
036        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";
037        testUrl = UrlFactory.encode(testUrl);
038
039        // construct lookup url
040        Properties parameters = new Properties();
041        parameters.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, "start");
042        parameters.put(KRADConstants.DOC_FORM_KEY, "903");
043        parameters.put(KRADConstants.LOOKUPABLE_IMPL_ATTRIBUTE_NAME, "accountLookupableImpl");
044        parameters.put(KRADConstants.RETURN_LOCATION_PARAMETER, basePath + "ib.do");
045
046        String returnedUrl = UrlFactory.parameterizeUrl(basePath + actionPath, parameters);
047
048        assertTrue("Returned url is empty", StringUtils.isNotBlank(returnedUrl));
049        assertTrue("Returned url has incorrect base", returnedUrl.startsWith(basePath + actionPath + "?"));
050        assertTrue("Returned url does not have correct # of &", StringUtils.countMatches(returnedUrl, "&") == 3);
051        assertTrue("Returned url missing parameter 1", StringUtils.contains(returnedUrl, KRADConstants.DISPATCH_REQUEST_PARAMETER + "=start"));
052        assertTrue("Returned url missing parameter 2", StringUtils.contains(returnedUrl, KRADConstants.DOC_FORM_KEY + "=903"));
053        assertTrue("Returned url missing parameter 3", StringUtils.contains(returnedUrl, KRADConstants.LOOKUPABLE_IMPL_ATTRIBUTE_NAME + "=accountLookupableImpl"));
054        // assertTrue("Returned url missing parameter 4",StringUtils.contains(returnedUrl,
055        // UrlFactory.encode(KRADConstants.RETURN_LOCATION_PARAMETER + "=" + basePath + "ib.do")));
056    }
057}