package omq.ztest.calculator;

import static org.junit.Assert.assertEquals;

import java.util.Properties;

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

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

public class CalculatorTest {
	private static Calculator remoteCalc;
	private static Calculator remoteCalc2;

	@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.SERIALIZERNAME,
		// "omq.common.util.Serializers.KryoImp");
		env.setProperty(ParameterQueue.SERIALIZERNAME, "omq.common.util.Serializers.GsonImp");
		env.setProperty(ParameterQueue.ENABLECOMPRESSION, "false");

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

		// 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");

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

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

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

		assertEquals(sum, sync);
	}

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

		int sync = remoteCalc2.add(x, y);
		int sum = x + y;

		assertEquals(sum, sync);
	}

	@Test
	public void mult() throws Exception {
		int x = 5;
		int y = 15;

		remoteCalc.mult(x, y);
		Thread.sleep(200);
	}

	@Test
	public void notifyEvent() throws Exception {
		ZeroListener zL = new ZeroListener();

		remoteCalc.addListener(zL);

		remoteCalc.divideByZero();

		Thread.sleep(200);
	}

	@Test
	public void sendMessage() throws Exception {
		Message m = new Message(2334, "Hello objectmq");
		remoteCalc.sendMessage(m);
	}
}
