    001package org.apache.turbine.services.intake;
    002
    003/*
    004 * Licensed to the Apache Software Foundation (ASF) under one
    005 * or more contributor license agreements.  See the NOTICE file
    006 * distributed with this work for additional information
    007 * regarding copyright ownership.  The ASF licenses this file
    008 * to you under the Apache License, Version 2.0 (the
    009 * "License"); you may not use this file except in compliance
    010 * with the License.  You may obtain a copy of the License at
    011 *
    012 *   http://www.apache.org/licenses/LICENSE-2.0
    013 *
    014 * Unless required by applicable law or agreed to in writing,
    015 * software distributed under the License is distributed on an
    016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    017 * KIND, either express or implied.  See the License for the
    018 * specific language governing permissions and limitations
    019 * under the License.
    020 */
    021
    022import static org.junit.Assert.assertEquals;
    023import static org.junit.Assert.assertNotNull;
    024import static org.junit.Assert.assertTrue;
    025
    026import java.io.File;
    027import java.util.Vector;
    028
    029import javax.servlet.ServletConfig;
    030import javax.servlet.http.HttpServletResponse;
    031
    032import org.apache.fulcrum.intake.IntakeService;
    033import org.apache.fulcrum.intake.model.Group;
    034import org.apache.fulcrum.parser.DefaultParameterParser;
    035import org.apache.turbine.om.security.User;
    036import org.apache.turbine.services.TurbineServices;
    037import org.apache.turbine.services.rundata.RunDataService;
    038import org.apache.turbine.test.BaseTestCase;
    039import org.apache.turbine.test.EnhancedMockHttpServletRequest;
    040import org.apache.turbine.util.RunData;
    041import org.apache.turbine.util.TurbineConfig;
    042import org.junit.AfterClass;
    043import org.junit.BeforeClass;
    044import org.junit.Test;
    045
    046import com.mockobjects.servlet.MockHttpServletResponse;
    047import com.mockobjects.servlet.MockHttpSession;
    048import com.mockobjects.servlet.MockServletConfig;
    049
    050/**
    051 * Unit test for Intake Tool, wrapping the Fulcrum Intake service.
    052 *
    053 * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
    054 * @version $Id: IntakeToolTest.java 1606111 2014-06-27 14:46:47Z gk $
    055 */
    056public class IntakeToolTest extends BaseTestCase
    057{
    058    private static TurbineConfig tc = null;
    059
    060    @Test public void testGet() throws Exception
    061    {
    062        IntakeTool intakeTool = new IntakeTool();
    063        intakeTool.init(getRunData());
    064        File file = new File("./target/appData.ser");
    065        assertTrue(
    066            "Make sure serialized data file exists:" + file,
    067            file.exists());
    068        Group group = intakeTool.get("LoginGroup","loginGroupKey");
    069        assertNotNull(group);
    070        assertEquals("loginGroupKey", group.getGID());
    071        assertEquals("LoginGroup", group.getIntakeGroupName());
    072    }
    073
    074
    075    /**
    076     * Make sure refresh DOESN'T do anything
    077     * @throws Exception
    078     */
    079    @Test public void testRefresh() throws Exception
    080    {
    081        IntakeTool intakeTool = new IntakeTool();
    082        intakeTool.init(getRunData());
    083        int numberOfGroups = intakeTool.getGroups().size();
    084        intakeTool.refresh();
    085        assertEquals(numberOfGroups,intakeTool.getGroups().size());
    086    }
    087    private RunData getRunData() throws Exception
    088    {
    089        RunDataService rds =
    090            (RunDataService) TurbineServices.getInstance().getService(
    091                RunDataService.SERVICE_NAME);
    092        EnhancedMockHttpServletRequest request =
    093            new EnhancedMockHttpServletRequest();
    094        request.setupServerName("bob");
    095        request.setupGetProtocol("http");
    096        request.setupScheme("scheme");
    097        request.setupPathInfo("damn");
    098        request.setupGetServletPath("damn2");
    099        request.setupGetContextPath("wow");
    100        request.setupGetContentType("html/text");
    101        request.setupAddHeader("Content-type", "html/text");
    102        request.setupAddHeader("Accept-Language", "en-US");
    103        Vector<String> v = new Vector<String>();
    104        request.setupGetParameterNames(v.elements());
    105        MockHttpSession session = new MockHttpSession();
    106        session.setupGetAttribute(User.SESSION_KEY, null);
    107        request.setSession(session);
    108        HttpServletResponse response = new MockHttpServletResponse();
    109        ServletConfig config = new MockServletConfig();
    110        RunData runData = rds.getRunData(request, response, config);
    111        assertEquals("Verify we are using Fulcrum parameter parser",DefaultParameterParser.class,runData.getParameters().getClass());
    112        return runData;
    113    }
    114
    115    @BeforeClass
    116    public static void setUp() throws Exception
    117    {
    118        tc = new TurbineConfig(".", "/conf/test/TestFulcrumComponents.properties");
    119        tc.initialize();
    120        TurbineServices.getInstance().getService(IntakeService.class.getName());
    121    }
    122
    123    @AfterClass
    124    public static void tearDown() throws Exception
    125    {
    126        if (tc != null)
    127        {
    128            tc.dispose();
    129        }
    130    }
    131}