/*
 *  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 groovy.jmx.builder

import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Assumptions
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test

import javax.management.remote.rmi.RMIConnector
import javax.management.remote.rmi.RMIConnectorServer

import static groovy.test.GroovyAssert.shouldFail

final class JmxClientConnectorFactoryTest {

    private JmxBuilder builder
    private Map rmi

    @BeforeEach
    void setUp() {
        System.setProperty('java.rmi.server.hostname', hostAddress)

        final int defaultPort = 10990
        rmi = JmxConnectorHelper.createRmiRegistry(defaultPort)

        builder = new JmxBuilder()
        try {
            builder.getMBeanServer()
        } catch (e) {
            Assumptions.abort(e.getMessage())
        }
    }

    @AfterEach
    void tearDown() {
        System.clearProperty('java.rmi.server.hostname')
        JmxConnectorHelper.destroyRmiRegistry(rmi.registry)
    }

    @Test
    void testJmxClientConnectorNode() {
        RMIConnectorServer server = builder.serverConnector(port: rmi.port)
        server.start()

        RMIConnector client = builder.clientConnector(port: rmi.port)
        client.connect()

        server.stop()
    }

    @Test
    void testJmxClientConnectorUrl() {
        RMIConnectorServer server = builder.serverConnector(port: rmi.port)
        server.start()

        RMIConnector client = builder.clientConnector(url: "service:jmx:rmi:///jndi/rmi://localhost:${rmi.port}/jmxrmi")
        client.connect()
        client.close()
        server.stop()
    }

    @Test
    void testJmxClientConnectorFailure() {
        shouldFail {
            RMIConnector client = builder.clientConnector(port: 1099)
            client.connect()
        }
    }

    //--------------------------------------------------------------------------

    private static String getHostAddress() {
        try {
            Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces()
            while (allNetInterfaces.hasMoreElements()) {
                NetworkInterface netInterface = allNetInterfaces.nextElement()
                Enumeration<InetAddress> addresses = netInterface.getInetAddresses()
                while (addresses.hasMoreElements()) {
                    InetAddress ip = (InetAddress) addresses.nextElement()
                    if (ip != null
                            && ip instanceof Inet4Address
                            && !ip.isLoopbackAddress()
                            && ip.getHostAddress().indexOf(':') == -1) {
                        return ip.getHostAddress()
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace()
        }
    }
}
