package clientToleranceTest;

import static org.junit.Assert.assertEquals;

import java.util.Properties;

import omq.common.broker.Broker;
import omq.common.util.ParameterQueue;
import omq.common.util.Serializer;

import org.junit.BeforeClass;
import org.junit.Test;

import calculatorTest.Calculator;

public class ClientTest {
	private static Calculator remoteCalc;

	@BeforeClass
	public static void startClient() throws Exception {
		Properties env = new Properties();
		env.setProperty(ParameterQueue.USER_NAME, "guest");
		env.setProperty(ParameterQueue.USER_PASS, "guest");

		// Set host info of rabbimq (where it is)
		env.setProperty(ParameterQueue.SERVER_HOST, "127.0.0.1");
		env.setProperty(ParameterQueue.SERVER_PORT, "5672");
		env.setProperty(ParameterQueue.DURABLE_QUEUES, "false");
		env.setProperty(ParameterQueue.SERIALIZER_NAME, Serializer.java);
		env.setProperty(ParameterQueue.ENABLECOMPRESSION, "false");

		// Set info about where the message will be sent
		env.setProperty(ParameterQueue.RPC_EXCHANGE, "rpc_exchange");
		// env.setProperty(ParameterQueue.DEBUGFILE, "c:\\middlewareDebug");

		// Set info about the queue & the exchange where the ResponseListener
		// will listen to.
		env.setProperty(ParameterQueue.RPC_REPLY_QUEUE, "reply_queue");
		env.setProperty(ParameterQueue.EVENT_REPLY_QUEUE, "event_queue");
		env.setProperty(ParameterQueue.RETRY_TIME_CONNECTION, "5000");

		Broker.initBroker(env);
		remoteCalc = (Calculator) Broker.lookup("calculator1", Calculator.class);
	}

	@Test
	public void toleranceTest() throws Exception {
		int x = 10;
		int y = 20;
		int sum = 10 + 20;

		int sync = remoteCalc.add(x, y);

		String password = "unpc";
		String[] command = { "/bin/bash", "-c", "echo " + password + " | sudo -S service rabbitmq-server restart" };

		Runtime runtime = Runtime.getRuntime();
		runtime.exec(command);

		Thread.sleep(15000);
		int resp = remoteCalc.add(x, y);

		assertEquals(sum, sync);
		assertEquals(sum, resp);
	}

}
