/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.commons.chain2.web.servlet;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import java.util.Locale;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.chain2.Processing;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;


/**
 * Test case for org.apache.commons.chain2.web.servlet.ServletGetLocaleCommand
 */
public final class ServletGetLocaleCommandTestCase {


    // ----------------------------------------------------- Instance Variables

    /** Locale */
    private Locale locale = null;

    // Servlet API Objects
    /** ServletContext */
    private ServletContext scontext = null;
    /** HttpServletRequest */
    private HttpServletRequest request = null;
    /** HttpServletResponse */
    private HttpServletResponse response = null;
    /** HttpSession */
    private HttpSession session = null;

    // Chain API Objects
    /** ServletWebContext */
    private ServletWebContext<String, Object> context = null;
    /** ServletGetLocaleCommand */
    private ServletGetLocaleCommand command = null;


    // -------------------------------------------------- Overall Test Methods


    /**
     * Set up instance variables required by this test case.
     */
    @Before
    public void setUp() {

        this.locale = new Locale("en", "US");

        // Set up Servlet API Objects
        this.scontext = Mockito.mock(ServletContext.class);

        this.session = Mockito.mock(HttpSession.class);
        Mockito.when(this.session.getServletContext()).thenReturn(this.scontext);

        this.request = Mockito.mock(HttpServletRequest.class);
        Mockito.when(this.request.getContextPath()).thenReturn("/context");
        Mockito.when(this.request.getServletPath()).thenReturn("/servlet");
        Mockito.when(this.request.getPathInfo()).thenReturn("/path/info");
        Mockito.when(this.request.getQueryString()).thenReturn("a=b&c=d");
        Mockito.when(this.request.getSession()).thenReturn(this.session);
        Mockito.when(this.request.getSession(true)).thenReturn(this.session);
        Mockito.when(this.request.getSession(false)).thenReturn(this.session);
        Mockito.when(this.request.getLocale()).thenReturn(this.locale);

        this.response = Mockito.mock(HttpServletResponse.class);

        // Set up Chain API Objects
        this.context = new ServletWebContextBase(this.scontext, this.request, this.response);
        this.command = new ServletGetLocaleCommand();

    }


    /**
     * Tear down instance variables required by this test case.
     */
    @After
    public void tearDown() {

        this.scontext = null;
        this.session = null;
        this.request = null;
        this.response = null;

        this.context = null;
        this.command = null;

    }


    // ------------------------------------------------- Individual Test Methods


    /**
     * Test configured behavior
     */
    @Test
    public void testConfigured() {

        this.command.setLocaleKey("special");
        assertEquals("special", this.command.getLocaleKey());
        check(this.context, this.command);

    }


    /**
     * Test default behavior
     */
    @Test
    public void testDefault() {

        assertEquals("locale", this.command.getLocaleKey());
        check(this.context, this.command);

    }


    // --------------------------------------------------------- Support Methods


    /**
     * @param ctx ServletWebContext
     * @param cmd ServletGetLocaleCommand
     */
    protected void check(final ServletWebContext<String, Object> ctx,
            final ServletGetLocaleCommand cmd) {

        String localeKey = cmd.getLocaleKey();
        assertNotNull(localeKey);
        Object value = ctx.get(localeKey);
        assertNull(value);
        Processing result = cmd.execute(ctx);
        assertEquals(Processing.CONTINUE, result);
        value = ctx.get(localeKey);
        assertNotNull(value);
        assertTrue(value instanceof Locale);
        assertEquals(this.locale, value);

    }


}
