package com.jollydays.camel;

import org.apache.camel.CamelContext;
import org.apache.camel.EndpointInject;
import org.apache.camel.Produce;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Test;

public class HystrixProducerTest extends CamelTestSupport {

	@Produce(uri = "direct:start")
	protected ProducerTemplate template;

	@EndpointInject(uri = "mock:result")
	protected MockEndpoint resultEndpoint;

	private class TestRoute extends RouteBuilder {

		@Override
		public void configure() throws Exception {
			from("direct:start").to("hystrix:direct:mitm?groupId=test");
			from("direct:mitm").to("mock:result");
		}
	}

	@Override
	protected CamelContext createCamelContext() throws Exception {
		CamelContext context = super.createCamelContext();
		return context;
	}

	@Override
	protected RouteBuilder createRouteBuilder() throws Exception {
		return new TestRoute();
	}

	@Test
	public void shouldUseHystrixProducer() throws InterruptedException {
		resultEndpoint.expectedBodiesReceived("test");
		template.sendBody("test");
		assertMockEndpointsSatisfied();
	}
}

@@ -23,16 +23,126 @@
 camel;%0A%0A
+import com.netflix.config.ConfigurationManager;%0Aimport com.netflix.hystrix.exception.HystrixRuntimeException;%0A
 import o
@@ -171,16 +171,16 @@
 ontext;%0A
-
 import o
@@ -439,16 +439,41 @@
 upport;%0A
+import org.junit.Before;%0A
 import o
@@ -549,16 +549,126 @@
 port %7B%0A%0A
+%09public static final String CIRCUIT_BREAKER_FORCE_OPEN = %22hystrix.command.default.circuitBreaker.forceOpen%22;%0A%0A
 %09@Produc
@@ -915,16 +915,105 @@
 ption %7B%0A
+%09%09%09onException(HystrixRuntimeException.class).handled(true).setBody().constant(%22error%22);%0A
 %09%09%09from(
@@ -1114,24 +1114,187 @@
 %22);%0A%09%09%7D%0A%09%7D%0A%0A
+%09@Before%0A%09public void setUp() throws Exception %7B%0A%09%09super.setUp();%0A%09%09ConfigurationManager.getConfigInstance().setProperty(CIRCUIT_BREAKER_FORCE_OPEN, %22false%22);%0A%09%7D%0A%0A
 %09@Override%0A%09
@@ -1691,16 +1691,16 @@
 test%22);%0A
-
 %09%09assert
@@ -1727,11 +1727,275 @@
 ed();%0A%09%7D
+%0A%0A%09@Test%0A%09public void shouldReactOnOpenCircuit() throws InterruptedException %7B%0A%09%09ConfigurationManager.getConfigInstance().setProperty(CIRCUIT_BREAKER_FORCE_OPEN, %22true%22);%0A%09%09final Object response = template.requestBody(%22test%22);%0A%09%09assertEquals(%22error%22, response);%0A%09%7D
 %0A%7D%0A

Test if open circuit is handled