/*
 * 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.felix.http.jetty.internal;

import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Dictionary;
import java.util.EnumSet;
import java.util.Hashtable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import javax.servlet.DispatcherType;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import junit.framework.TestCase;

import org.apache.felix.http.base.internal.DispatcherServlet;
import org.apache.felix.http.base.internal.EventDispatcher;
import org.apache.felix.http.base.internal.HttpServiceController;
import org.apache.felix.http.jetty.internal.JettyService.Deployment;
import org.eclipse.jetty.servlet.FilterHolder;
import org.eclipse.jetty.servlet.ServletHolder;
import org.mockito.Matchers;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceFactory;
import org.osgi.framework.ServiceRegistration;
import org.osgi.framework.Version;

public class JettyServiceTest extends TestCase
{

    private static final String OSGI_BUNDLECONTEXT = "osgi-bundlecontext";

    private JettyService jettyService;

    private BundleContext mockBundleContext;

    private DispatcherServlet dispatcherServlet;

    private EventDispatcher mockEventDispatcher;

    private HttpServiceController httpServiceController;

    private Bundle mockBundle;

    @Override
    public void setUp() throws Exception
    {
        //Setup Mocks
        mockBundleContext = mock(BundleContext.class);
        mockEventDispatcher = mock(EventDispatcher.class);
        mockBundle = mock(Bundle.class);

        //Setup Behaviors
        when(mockBundleContext.getBundle()).thenReturn(mockBundle);
        final org.osgi.framework.Filter f = mock(org.osgi.framework.Filter.class);
        when(f.toString()).thenReturn("(prop=*)");
        when(mockBundleContext.createFilter(anyString())).thenReturn(f);
        when(mockBundle.getSymbolicName()).thenReturn("main");
        when(mockBundle.getVersion()).thenReturn(new Version("1.0.0"));
        when(mockBundle.getHeaders()).thenReturn(new Hashtable<String, String>());
        final ServiceRegistration reg = mock(ServiceRegistration.class);
        when(mockBundleContext.registerService((Class)Matchers.isNotNull(),
                Matchers.any(ServiceFactory.class),
                Matchers.any(Dictionary.class))).thenReturn(reg);

        httpServiceController = new HttpServiceController(mockBundleContext);
        dispatcherServlet = new DispatcherServlet(httpServiceController);
        jettyService = new JettyService(mockBundleContext, dispatcherServlet, mockEventDispatcher, httpServiceController);

        jettyService.start();
    }

    @Override
    protected void tearDown() throws Exception {
        jettyService.stop();
    }

    /**
     *
     * Tests to ensure the osgi-bundlecontext is available for init methods.
     *
     * @throws MalformedURLException
     * @throws InterruptedException
     */
    public void testInitBundleContextDeployIT() throws Exception
    {
        //Setup mocks
        Deployment mockDeployment = mock(Deployment.class);
        Bundle mockBundle = mock(Bundle.class);
        BundleContext mockBundleContext = mock(BundleContext.class);

        //Setup behaviors
        when(mockDeployment.getBundle()).thenReturn(mockBundle);
        final org.osgi.framework.Filter f = mock(org.osgi.framework.Filter.class);
        when(f.toString()).thenReturn("(prop=*)");
        when(mockBundleContext.createFilter(anyString())).thenReturn(f);
        when(mockBundle.getBundleContext()).thenReturn(mockBundleContext);
        when(mockBundle.getSymbolicName()).thenReturn("test");
        when(mockBundle.getVersion()).thenReturn(new Version("0.0.1"));

        Dictionary<String, String> headerProperties = new Hashtable<String, String>();
        headerProperties.put("Web-ContextPath", "test");
        when(mockBundle.getHeaders()).thenReturn(headerProperties);
        when(mockDeployment.getContextPath()).thenReturn("test");
        when(mockBundle.getEntry("/")).thenReturn(new URL("http://www.apache.com"));
        when(mockBundle.getState()).thenReturn(Bundle.ACTIVE);

        EnumSet<DispatcherType> dispatcherSet = EnumSet.allOf(DispatcherType.class);
        dispatcherSet.add(DispatcherType.REQUEST);

        WebAppBundleContext webAppBundleContext = new WebAppBundleContext("/", mockBundle, this.getClass().getClassLoader());

        final CountDownLatch testLatch = new CountDownLatch(2);

        //Add a Filter to test whether the osgi-bundlecontext is available at init
        webAppBundleContext.addServlet(new ServletHolder(new Servlet()
        {
            @Override
            public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException
            {
                // Do Nothing
            }

            @Override
            public void init(ServletConfig config) throws ServletException
            {
                ServletContext context = config.getServletContext();

                assertNotNull(context.getAttribute(OSGI_BUNDLECONTEXT));

                testLatch.countDown();
            }

            @Override
            public String getServletInfo()
            {
                return null;
            }

            @Override
            public ServletConfig getServletConfig()
            {
                return null;
            }

            @Override
            public void destroy()
            {
                // Do Nothing
            }
        }), "/test1");

        webAppBundleContext.addFilter(new FilterHolder(new Filter()
        {
            @Override
            public void init(FilterConfig filterConfig) throws ServletException
            {
                ServletContext context = filterConfig.getServletContext();

                assertNotNull(context.getAttribute(OSGI_BUNDLECONTEXT));

                testLatch.countDown();
            }

            @Override
            public void doFilter(ServletRequest arg0, ServletResponse response, FilterChain chain) throws IOException, ServletException
            {
                // Do Nothing
            }

            @Override
            public void destroy()
            {
                // Do Nothing

            }
        }), "/test2", dispatcherSet);

        jettyService.deploy(mockDeployment, webAppBundleContext);

        //Pause since service is multi-threaded.
        //Fail if takes too long.
        if (!testLatch.await(6, TimeUnit.SECONDS))
        {
            fail("Test Was not asserted");
        }
    }
}
@@ -2190,16 +2190,75 @@
 Version;
+%0Aimport org.osgi.service.http.context.ServletContextHelper;
 %0A%0Apublic
@@ -3537,16 +3537,38 @@
 e((Class
+%3CServletContextHelper%3E
 )Matcher
@@ -3590,32 +3590,70 @@
                 
+(ServiceFactory%3CServletContextHelper%3E)
 Matchers.any(Ser

Fix signature for mock invocation