package com.boundary.sdk.event.service;

import static org.junit.Assert.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.camel.Exchange;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import com.boundary.camel.component.common.ServiceStatus;
import com.boundary.camel.component.ping.PingConfiguration;
import com.boundary.camel.component.ping.PingInfo;
import com.boundary.camel.component.port.PortConfiguration;
import com.boundary.camel.component.port.PortInfo;
import com.boundary.camel.component.port.PortStatus;

public class ExtractServiceTestConfigurationTest extends CamelTestSupport {
	
	private final static String HOST = "google.com";
	private final static int PORT = 80;

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
	}

	@Before
	public void setUp() throws Exception {
		super.setUp();
	}

	@After
	public void tearDown() throws Exception {
		super.tearDown();
	}

	@Test
	public void testGetPortConfiguration() throws InterruptedException {

		MockEndpoint endPoint = getMockEndpoint("mock:port-service-test-out");
		endPoint.expectedMessageCount(1);

		ServiceCheckRequest request = new ServiceCheckRequest();

		PortConfiguration configuration = new PortConfiguration();
		configuration.setHost(HOST);
		configuration.setPort(PORT);
		ServiceTest<PortConfiguration> serviceTest = new ServiceTest<PortConfiguration>(
				"port", request.getRequestId(), configuration);

		Map<String, Object> properties = new HashMap<String, Object>();
		properties.put(ServiceCheckPropertyNames.SERVICE_REQUEST_ID,request.getRequestId());
		properties.put(ServiceCheckPropertyNames.SERVICE_REQUEST_INSTANCE,request);
		properties.put(ServiceCheckPropertyNames.SERVICE_TEST_INSTANCE,serviceTest);
		properties.put(ServiceCheckPropertyNames.SERVICE_TEST_NAME,"port");

		template.sendBodyAndHeaders("direct:port-service-test-in",serviceTest, properties);
		
		endPoint.assertIsSatisfied();
		
		List<Exchange> receivedExchanges = endPoint.getReceivedExchanges();
		for (Exchange e : receivedExchanges) {
			PortConfiguration config = e.getIn().getBody(PortConfiguration.class);

			assertEquals("check host",HOST, config.getHost());
			assertEquals("check port",PORT,config.getPort());
		}
	}
	
	@Test
	public void testGetPingConfiguration() throws InterruptedException {

		MockEndpoint endPoint = getMockEndpoint("mock:ping-service-test-out");
		endPoint.expectedMessageCount(1);

		ServiceCheckRequest request = new ServiceCheckRequest();

		PingConfiguration configuration = new PingConfiguration();
		configuration.setHost(HOST);
		ServiceTest<PingConfiguration> serviceTest = new ServiceTest<PingConfiguration>(
				"ping", request.getRequestId(), configuration);

		Map<String, Object> properties = new HashMap<String, Object>();
		properties.put(ServiceCheckPropertyNames.SERVICE_REQUEST_ID,request.getRequestId());
		properties.put(ServiceCheckPropertyNames.SERVICE_REQUEST_INSTANCE,request);
		properties.put(ServiceCheckPropertyNames.SERVICE_TEST_INSTANCE,serviceTest);
		properties.put(ServiceCheckPropertyNames.SERVICE_TEST_NAME,"ping");

		template.sendBodyAndHeaders("direct:ping-service-test-in",serviceTest, properties);
		
		endPoint.assertIsSatisfied();
		
		List<Exchange> receivedExchanges = endPoint.getReceivedExchanges();
		for (Exchange e : receivedExchanges) {
			PingConfiguration config = e.getIn().getBody(PingConfiguration.class);

			assertEquals("check host",HOST, config.getHost());
		}
	}


	@Override
	protected RouteBuilder createRouteBuilder() throws Exception {
		return new RouteBuilder() {
			@Override
			public void configure() throws Exception {
				from("direct:ping-service-test-in")
				.log("ServiceTest: ${body}")
				.bean(new ExtractServiceTestConfiguration(), "extractPingConfiguration")
				.to("mock:ping-service-test-out");
				
				from("direct:port-service-test-in")
				.log("ServiceTest: ${body}")
				.bean(new ExtractServiceTestConfiguration(), "extractPortConfiguration")
				.to("mock:port-service-test-out");

			}
		};
	}

}

@@ -175,16 +175,49 @@
 change;%0A
+import org.apache.camel.Message;%0A
 import o
@@ -2421,182 +2421,682 @@
 %0A%09%09%09
-PortConfiguration config = e.getIn().getBody(PortConfiguration.class);%0A%0A%09%09%09assertEquals(%22check host%22,HOST, config.getHost());%0A%09%09%09assertEquals(%22check port%22,PORT,config.getPort
+Message message = e.getIn();%0A%09%09%09PortConfiguration config = message.getBody(PortConfiguration.class);%0A%09%09%09ServiceCheckRequest req = message.getHeader(%0A%09%09%09%09%09ServiceCheckPropertyNames.SERVICE_REQUEST_INSTANCE,ServiceCheckRequest.class);%0A%09%09%09ServiceTest%3CPortConfiguration%3E test = message.getHeader(%0A%09%09%09%09%09ServiceCheckPropertyNames.SERVICE_TEST_INSTANCE,ServiceTest.class);%0A%0A%09%09%09assertEquals(%22check host%22,HOST, config.getHost());%0A%09%09%09assertEquals(%22check port%22,PORT,config.getPort());%0A%09%09%09%0A%09%09%09assertEquals(%22check service request id%22,request.getRequestId(),req.getRequestId());%0A%09%09%09assertEquals(%22check service request id on service test%22,%0A%09%09%09%09%09serviceTest.getRequestId(),test.getRequestId
 ());

Add additional tests on ServiceTest stored in the header