
package org.apache.camel.itest.osgi.blueprint;

import javax.management.MBeanServer;
import javax.management.ObjectName;

import org.apache.camel.CamelContext;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.component.mock.MockEndpoint;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.ops4j.pax.exam.Option;
import org.ops4j.pax.exam.junit.Configuration;
import org.ops4j.pax.exam.junit.JUnit4TestRunner;
import org.osgi.framework.Constants;

import static org.ops4j.pax.exam.OptionUtils.combine;
import static org.ops4j.pax.swissbox.tinybundles.core.TinyBundles.newBundle;

/**
 * @version 
 */
@RunWith(JUnit4TestRunner.class)
public class CamelBlueprintManagedNamePatternFixedTest extends OSGiBlueprintTestSupport {

    @Test
    public void testManagedNamePatternFixed() throws Exception {
        getInstalledBundle("CamelBlueprintTestBundleFixed").start();
        CamelContext ctx = getOsgiService(CamelContext.class, "(camel.context.symbolicname=CamelBlueprintTestBundleFixed)", 10000);

        ProducerTemplate template = ctx.createProducerTemplate();

        MockEndpoint mock = ctx.getEndpoint("mock:result", MockEndpoint.class);
        template.sendBody("direct:start", "World");
        mock.assertIsSatisfied();

        MBeanServer mbeanServer = ctx.getManagementStrategy().getManagementAgent().getMBeanServer();

        assertEquals("cool", ctx.getManagementName());

        ObjectName on = ObjectName.getInstance("org.apache.camel:context=" + ctx.getManagementName()
                + ",type=context,name=\"" + ctx.getName() + "\"");
        assertTrue("Should be registered", mbeanServer.isRegistered(on));
    }

    @Configuration
    public static Option[] configure() throws Exception {

        Option[] options = combine(
                getDefaultCamelKarafOptions(),

                bundle(newBundle()
                        .add("OSGI-INF/blueprint/test.xml", OSGiBlueprintTestSupport.class.getResource("blueprint-fixed.xml"))
                        .set(Constants.BUNDLE_SYMBOLICNAME, "CamelBlueprintTestBundleFixed")
                        .set(Constants.DYNAMICIMPORT_PACKAGE, "*")
                        .build()).noStart(),

                // using the features to install the camel components
                loadCamelFeatures("camel-blueprint"));

        return options;
    }

}
