/**
 * 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.pulsar.websocket.proxy;

import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import static org.testng.Assert.assertEquals;

import java.util.Optional;

import org.apache.pulsar.client.api.ProducerConsumerBase;
import org.apache.pulsar.client.impl.PulsarClientImpl;
import org.apache.pulsar.websocket.WebSocketService;
import org.apache.pulsar.websocket.service.WebSocketProxyConfiguration;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class ProxyConfigurationTest extends ProducerConsumerBase {
    private WebSocketProxyConfiguration config;

    @BeforeMethod
    public void setup() throws Exception {
        super.internalSetup();
        super.producerBaseSetup();

        config = new WebSocketProxyConfiguration();
        config.setWebServicePort(Optional.of(0));
        config.setClusterName("test");
        config.setConfigurationStoreServers("dummy-zk-servers");
    }

    @AfterMethod
    protected void cleanup() throws Exception {
        super.internalCleanup();
    }

    @DataProvider(name = "setProxyConfig")
    public Object[][] setProxyConfig() {
        return new Object[][] { {2, 1}, {4, 2} };
    }

    @Test(dataProvider = "setProxyConfig", timeOut = 10000)
    public void configTest(int numIoThreads, int connectionsPerBroker) throws Exception {
        config.setWebSocketNumIoThreads(numIoThreads);
        config.setWebSocketConnectionsPerBroker(connectionsPerBroker);
        WebSocketService service = spy(new WebSocketService(config));
        doReturn(mockZooKeeperClientFactory).when(service).getZooKeeperClientFactory();
        service.start();

        PulsarClientImpl client = (PulsarClientImpl) service.getPulsarClient();
        assertEquals(client.getConfiguration().getNumIoThreads(), numIoThreads);
        assertEquals(client.getConfiguration().getConnectionsPerBroker(), connectionsPerBroker);

        service.close();
    }
}
