/**
 * 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.activemq.jmx;

import org.apache.activemq.broker.BrokerService;
import org.apache.activemq.broker.jmx.BrokerViewMBean;
import org.apache.activemq.broker.jmx.NetworkConnectorViewMBean;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import javax.management.ObjectName;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static org.apache.activemq.util.TransportValidationUtils.DENIED_TRANSPORT_SCHEMES;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;

/**
 * This test shows that when we create a network connector via JMX,
 * the NC/bridge shows up in the MBean Server
 */
public class JmxCreateNCTest {

    private static final Logger LOG = LoggerFactory.getLogger(JmxCreateNCTest.class);

    private static final String BROKER_NAME = "jmx-broker";

    private BrokerService broker;
    private BrokerViewMBean proxy;

    @Before
    public void setUp() throws Exception {
        broker = new BrokerService();
        broker.setBrokerName(BROKER_NAME);
        broker.setUseJmx(true); // explicitly set this so no funny issues
        broker.start();
        broker.waitUntilStarted();

        ObjectName brokerObjectName = new ObjectName("org.apache.activemq:type=Broker,brokerName=" + BROKER_NAME);
        proxy = (BrokerViewMBean) broker.getManagementContext().newProxyInstance(brokerObjectName,
                BrokerViewMBean.class, true);
    }

    @After
    public void tearDown() throws Exception {
        broker.stop();
        broker.waitUntilStopped();
    }

    @Test
    public void testBridgeRegistration() throws Exception {
        assertNotNull("We could not retrieve the broker from JMX", proxy);

        // let's add the NC
        String connectorName = proxy.addNetworkConnector("static:(tcp://localhost:61617)");
        assertEquals("NC", connectorName);

        // Make sure we can retrieve the NC through JMX
        ObjectName networkConnectorObjectName = new ObjectName("org.apache.activemq:type=Broker,brokerName=" + BROKER_NAME +
                ",connector=networkConnectors,networkConnectorName=" + connectorName);
        NetworkConnectorViewMBean nc  = (NetworkConnectorViewMBean) broker.getManagementContext().newProxyInstance(networkConnectorObjectName,
                NetworkConnectorViewMBean.class, true);

        assertNotNull(nc);
        assertEquals("NC", nc.getName());
        assertEquals("static:(tcp://localhost:61617)", nc.getUri());
        assertNotNull(nc.getLocalUri());
    }

    @Test
    public void testTransportSchemeBridgeAllowed() throws Exception {
        // Test composite network connector uri
        String name = proxy.addNetworkConnector("static:(tcp://localhost,amqp://localhost)");
        proxy.removeNetworkConnector(name);

        // Test composite with missing parens
        name = proxy.addNetworkConnector("static:amqp://localhost,tcp://127.0.0.1:0");
        proxy.removeNetworkConnector(name);

        // verify direct connector as well
        name = proxy.addNetworkConnector("static:stomp://localhost");
        proxy.removeNetworkConnector(name);

        // verify nested composite URI
        name = proxy.addNetworkConnector(
                "static:(static:(static:(tcp+ssl://localhost:0,auto+nio+ssl://localhost)))");
        proxy.removeNetworkConnector(name);

        // verify nested composite URI is not blocked when not using parens
        name = proxy.addNetworkConnector(
                "static:static:static:123://localhost:0,auto+nio+ssl://localhost");
        proxy.removeNetworkConnector(name);
    }

    @Test
    public void testTransportSchemeBridgeBlocked() throws Exception {
        for (String deniedScheme : DENIED_TRANSPORT_SCHEMES) {
            LOG.info("verify testTransportSchemeBridgeBlocked scheme: {}", deniedScheme);
            testTransportSchemeBridgeBlocked(deniedScheme);
        }
    }

    protected void testTransportSchemeBridgeBlocked(String scheme) throws Exception {
        // Test composite network connector uri
        try {
            proxy.addNetworkConnector("static:(" + scheme + "://localhost)");
            fail("Should have failed trying to add connector bridge with scheme: " + scheme);
        } catch (IllegalArgumentException e) {
            assertEquals("Transport scheme '" + scheme + "' is not allowed", e.getMessage());
        }

        // Test composite with missing parens
        try {
            proxy.addNetworkConnector("static:" + scheme + "://localhost,tcp://127.0.0.1:0");
            fail("Should have failed trying to add connector bridge with scheme: " + scheme);
        } catch (IllegalArgumentException e) {
            assertEquals("Transport scheme '" + scheme + "' is not allowed", e.getMessage());
        }

        // verify direct connector as well
        try {
            proxy.addNetworkConnector(scheme + "://localhost");
            fail("Should have failed trying to add connector bridge with scheme: " + scheme);
        } catch (IllegalArgumentException e) {
            assertEquals("Transport scheme '" + scheme + "' is not allowed", e.getMessage());
        }

        try {
            // verify nested composite URI is blocked
            proxy.addNetworkConnector("static:(static:(static:(tcp://localhost:0," + scheme + "://localhost)))");
            fail("Should have failed trying to add connector bridge with scheme: " + scheme);
        } catch (IllegalArgumentException e) {
            assertEquals("Transport scheme '" + scheme + "' is not allowed", e.getMessage());
        }

        try {
            // verify nested composite URI is blocked when not using parens
            proxy.addNetworkConnector("static:static:static:tcp://localhost:0," + scheme + "://localhost");
            fail("Should have failed trying to add connector bridge with scheme: " + scheme);
        } catch (IllegalArgumentException e) {
            assertEquals("Transport scheme '" + scheme + "' is not allowed", e.getMessage());
        }
    }

    @Test
    public void testAddNetworkConnectorMaxComposite() throws Exception {
        // Should allow 5 nested (excludes first wrapper) so no exception thrown
        assertNotNull(proxy.addNetworkConnector(
                "static:(static:(static:(static:(static:(bad://localhost)))))"));

        try {
            // verify nested composite URI with more than 5 levels is blocked. This has 6 nested
            // (not including first wrapper url)
            proxy.addNetworkConnector(
                    "static:(static:(static:(static:(static:(static:(tcp://localhost:0))))))");
            fail("Should have failed trying to add more than 5 connector bridges");
        } catch (IllegalArgumentException e) {
            assertEquals("URI can't contain more than 5 nested composite URIs", e.getMessage());
        }

        try {
            // verify nested composite URI with more than 5 levels is blocked without parens
            proxy.addNetworkConnector(
                    "static:static:static:static:static:static:tcp://localhost:0");
            fail("Should have failed trying to add more than 5 connector bridges");
        } catch (IllegalArgumentException e) {
            assertEquals("URI can't contain more than 5 nested composite URIs", e.getMessage());
        }
    }
}
