CamelTestSupport read placeholders from yml file

1.2k views Asked by At

I am trying to test my Camel Routes using CamelTestSupport. I have my routes defined in a class like this

public class ActiveMqConfig{

@Bean
public RoutesBuilder route() {
    return new SpringRouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("activemq:{{push.queue.name}}").to("bean:PushEventHandler?method=handlePushEvent");
        }
    };
}

}

And my test class look like this

@RunWith(SpringRunner.class)
public class AmqTest extends CamelTestSupport {

@Override
protected RoutesBuilder createRouteBuilder() throws Exception {
    return new ActiveMqConfig().route();
}

@Override
protected Properties useOverridePropertiesWithPropertiesComponent() {
    Properties properties = new Properties();
    properties.put("pim2.push.queue.name", "pushevent");
    return properties;
}

protected Boolean ignoreMissingLocationWithPropertiesComponent() {
    return true;
}

@Mock
private PushEventHandler pushEventHandler;

@BeforeClass
public static void setUpClass() throws Exception {
    BrokerService brokerSvc = new BrokerService();
    brokerSvc.setBrokerName("TestBroker");
    brokerSvc.addConnector("tcp://localhost:61616");
    brokerSvc.setPersistent(false);
    brokerSvc.setUseJmx(false);
    brokerSvc.start();
}

@Override
protected JndiRegistry createRegistry() throws Exception {
    JndiRegistry jndi = super.createRegistry();
    MockitoAnnotations.initMocks(this);
    jndi.bind("pushEventHandler", pushEventHandler);

    return jndi;
}

@Test
public void testConfigure() throws Exception {
    template.sendBody("activemq:pushevent", "HelloWorld!");
    Thread.sleep(2000);
    verify(pushEventHandler, times(1)).handlePushEvent(any());
}}

This is working perfectly fine. But I have to set the placeholder {{push.queue.name}} using useOverridePropertiesWithPropertiesComponent function. But I want it to be read from my .yml file.
I am not able to do it. Can someone suggest.

Thanks

2

There are 2 answers

0
pvpkiran On BEST ANSWER

Thank Claus.
I got it working by doing this

@Override
    protected Properties useOverridePropertiesWithPropertiesComponent() {
        YamlPropertySourceLoader loader = new YamlPropertySourceLoader();
        try {
            PropertySource<?> applicationYamlPropertySource = loader.load(
                "properties", new ClassPathResource("application.yml"),null);
            Map source = ((MapPropertySource) applicationYamlPropertySource).getSource();
            Properties properties = new Properties();
            properties.putAll(source);
            return properties;
        } catch (IOException e) {
            LOG.error("Config file cannot be found.");
        }

        return null;
    }
0
Claus Ibsen On

Properties are typically read from .properties files. But you can write some code that read the yaml file in the useOverridePropertiesWithPropertiesComponent method and put them into the Properties instance which is returned.