/*
 * 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.dubbo.rpc.protocol.webservice;

import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.URLBuilder;
import org.apache.dubbo.common.extension.ExtensionLoader;
import org.apache.dubbo.common.utils.NetUtils;
import org.apache.dubbo.remoting.http.servlet.DispatcherServlet;
import org.apache.dubbo.remoting.http.servlet.ServletManager;
import org.apache.dubbo.rpc.Exporter;
import org.apache.dubbo.rpc.Protocol;
import org.apache.dubbo.rpc.ProxyFactory;

import org.apache.catalina.Context;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.connector.Connector;
import org.apache.catalina.startup.Tomcat;
import org.junit.jupiter.api.Test;

import java.io.File;

import static org.apache.dubbo.common.constants.CommonConstants.SERVICE_PATH_PREFIX;
import static org.junit.jupiter.api.Assertions.assertEquals;


/**
 *
 */

public class WebserviceProtocolTest {
    private Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension();
    private ProxyFactory proxy = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension();

    @Test
    public void testDemoProtocol() throws Exception {
        DemoService service = new DemoServiceImpl();
        int port = NetUtils.getAvailablePort();
        Exporter<DemoService> exporter = protocol.export(proxy.getInvoker(service, DemoService.class, URL.valueOf("webservice://127.0.0.1:" + port + "/" + DemoService.class.getName() + "?codec=exchange")));
        service = proxy.getProxy(protocol.refer(DemoService.class, URL.valueOf("webservice://127.0.0.1:" + port + "/" + DemoService.class.getName() + "?codec=exchange&timeout=3000")));
        assertEquals(service.getSize(new String[]{"", "", ""}), 3);
        exporter.unexport();
    }

    @Test
    public void testWebserviceProtocol() throws Exception {
        DemoService service = new DemoServiceImpl();
        int port = NetUtils.getAvailablePort();
        protocol.export(proxy.getInvoker(service, DemoService.class, URL.valueOf("webservice://127.0.0.1:" + port + "/" + DemoService.class.getName())));
        service = proxy.getProxy(protocol.refer(DemoService.class, URL.valueOf("webservice://127.0.0.1:" + port + "/" + DemoService.class.getName() + "?timeout=3000")));
        assertEquals(service.create(1, "kk").getName(), "kk");
        assertEquals(service.getSize(null), -1);
        assertEquals(service.getSize(new String[]{"", "", ""}), 3);
        Object object = service.invoke("webservice://127.0.0.1:" + port + "/" + DemoService.class.getName() + "", "invoke");
        System.out.println(object);
        assertEquals("webservice://127.0.0.1:" + port + "/org.apache.dubbo.rpc.protocol.webservice.DemoService:invoke", object);

        StringBuilder buf = new StringBuilder();
        for (int i = 0; i < 1024 * 32 + 32; i++)
            buf.append('A');
        assertEquals(32800, service.stringLength(buf.toString()));

//  a method start with $ is illegal in soap
//        // cast to EchoService
//        EchoService echo = proxy.getProxy(protocol.refer(EchoService.class, URL.valueOf("webservice://127.0.0.1:9010/" + DemoService.class.getName() + "?client=netty")));
//        assertEquals(echo.echo(buf.toString()), buf.toString());
//        assertEquals(echo.$echo("test"), "test");
//        assertEquals(echo.$echo("abcdefg"), "abcdefg");
//        assertEquals(echo.$echo(1234), 1234);
    }

    @Test
    public void testWebserviceServlet() throws LifecycleException {
        int port = NetUtils.getAvailablePort();
        Tomcat tomcat = buildTomcat("/dubbo-webservice", "/services/*", port);
        DemoService service = new DemoServiceImpl();


        URLBuilder builder = new URLBuilder()
            .setProtocol("webservice")
            .setHost("127.0.0.1")
            .setPort(port)
            .setPath("dubbo-webservice2/" + DemoService.class.getName())
            .addParameter("server", "servlet")
            .addParameter("bind.port", port)
            .addParameter("contextpath", "dubbo-webservice2")
            .addParameter(SERVICE_PATH_PREFIX, "dubbo-webservice/services")
            .addParameter("codec", "exchange")
            .addParameter("timeout", 600000);
        URL url = builder.build();

        tomcat.start();
        Exporter<DemoService> exporter = protocol.export(proxy.getInvoker(service, DemoService.class, url));
        service = proxy.getProxy(protocol.refer(DemoService.class, url));
        assertEquals(service.getSize(new String[]{"", "", ""}), 3);
        exporter.unexport();
        tomcat.stop();
        tomcat.destroy();
    }

    @Test
    public void testWebserviceJetty() throws LifecycleException {
        Tomcat tomcat = buildTomcat("/dubbo-webservice", "/services/*", 55065);
        DemoService service = new DemoServiceImpl();
        int port = NetUtils.getAvailablePort();

        URLBuilder builder = new URLBuilder()
            .setProtocol("webservice")
            .setHost("127.0.0.1")
            .setPort(port)
            .setPath("dubbo-webservice3/" + DemoService.class.getName())
            .addParameter("server", "jetty")
            .addParameter("bind.port", port)
            .addParameter("contextpath", "dubbo-webservice2")
            .addParameter("codec", "exchange")
            .addParameter("timeout", 3000);
        URL url = builder.build();

        tomcat.start();
        Exporter<DemoService> exporter = protocol.export(proxy.getInvoker(service, DemoService.class, url));
        service = proxy.getProxy(protocol.refer(DemoService.class, url));
        assertEquals(service.getSize(new String[]{"", "", ""}), 3);
        exporter.unexport();
        tomcat.stop();
        tomcat.destroy();
    }

    private Tomcat buildTomcat(String servicePathPrefix, String servletPattern, int port) {
        String baseDir = new File(System.getProperty("java.io.tmpdir")).getAbsolutePath();

        Tomcat tomcat = new Tomcat();
        Connector connector = tomcat.getConnector();
        connector.setPort(port);
        connector.setProperty("maxThreads", "5");
        connector.setProperty("maxConnections", "-1");
        connector.setProperty("URIEncoding", "UTF-8");
        connector.setProperty("connectionTimeout", "60000");
        connector.setProperty("maxKeepAliveRequests", "-1");

        tomcat.setBaseDir(baseDir);
        tomcat.setPort(port);

        Context context = tomcat.addContext(servicePathPrefix, baseDir);
        Tomcat.addServlet(context, "dispatcher", new DispatcherServlet());

        context.addServletMappingDecoded(servletPattern, "dispatcher");
        ServletManager.getInstance().addServletContext(port, context.getServletContext());

        // tell tomcat to fail on startup failures.
        System.setProperty("org.apache.catalina.startup.EXIT_ON_INIT_FAILURE", "true");
        return tomcat;
    }
}
