package org.zenbeni.jedis.job;

import java.util.Arrays;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.zenbeni.jedis.lua.LuaScript;

public class JedisJobTest {

	private static final Logger LOGGER = LoggerFactory.getLogger(JedisJobTest.class);

	private JedisJobConfiguration jobConfiguration;

	@BeforeClass
	public void before() {
		jobConfiguration = JedisJobConfigurationBuilder.create().withHost("localhost").withDatabase(15).build();
	}

	@Test
	public void basicTest() {
		final String key = "my.key.test";
		final String value = "value.of.test";
		JedisExecutor.submitJedisJob(new JedisSimpleJob(jobConfiguration) {
			@Override
			public void runSimpleJob() {
				jedis.set(key, value);
			}
		});
		final String result = JedisExecutor.submitJedisJob(new JedisJob<String>(jobConfiguration) {
			@Override
			public String runJedisJob() {
				return jedis.get(key);
			}
		});
		Assert.assertEquals(result, value);
	}

	@Test
	public void luaTest() {
		final LuaScript<String> script = LuaScript.buildFromPath("/lua/helloworld.lua");
		final String key = "storm.is.cool";
		final String arg = "redis.is.cool";
		final String result = JedisExecutor.submitJedisJob(new JedisLuaJob<String>(jobConfiguration, script) {
			@Override
			protected void initKeysAndArgv() {
				keys.add(key);
				argv.add(arg);
			}
		});
		LOGGER.info("Result:{}", result);
		Assert.assertEquals(result, "hello world:" + key + " " + arg);

		final String resultOther = JedisExecutor.submitLuaJob(jobConfiguration, script, Arrays.asList(key), Arrays.asList(arg));
		LOGGER.info("Result:{}", resultOther);
		Assert.assertEquals(resultOther, "hello world:" + key + " " + arg);
	}

}

@@ -134,16 +134,58 @@
 Assert;%0A
+import org.testng.annotations.AfterClass;%0A
 import o
@@ -215,24 +215,24 @@
 eforeClass;%0A
-
 import org.t
@@ -604,24 +604,211 @@
 uild();%0A%09%7D%0A%0A
+%09@AfterClass%0A%09public void after() %7B%0A%09%09JedisExecutor.submitJedisJob(new JedisSimpleJob(jobConfiguration) %7B%0A%09%09%09@Override%0A%09%09%09public void runSimpleJob() %7B%0A%09%09%09%09jedis.flushDB();%0A%09%09%09%7D%0A%09%09%7D);%0A%09%7D%0A%0A
 %09@Test%0A%09publ
@@ -1940,16 +1940,16 @@
 Other);%0A
-
 %09%09Assert
@@ -2014,10 +2014,626 @@
 g);%0A%09%7D%0A%0A
+%09@Test%0A%09public void testByteArray() %7B%0A%09%09final String id = %22xyzd12%22;%0A%09%09JedisExecutor.submitJedisJob(new JedisSimpleJob(jobConfiguration) %7B%0A%09%09%09@Override%0A%09%09%09public void runSimpleJob() %7B%0A%09%09%09%09final byte%5B%5D data = %7B12, 23, 45, 67, 127%7D;%0A%09%09%09%09final byte%5B%5D data2 = %7B13, 25, 46, 68, 121, 44%7D;%0A%09%09%09%09jedis.lpush(id.getBytes(), data);%0A%09%09%09%09jedis.rpush(id.getBytes(), data2);%0A%09%09%09%7D%0A%09%09%7D);%0A%09%09final long result = JedisExecutor.submitJedisJob(new JedisJob%3CLong%3E(jobConfiguration) %7B%0A%09%09%09@Override%0A%09%09%09public Long runJedisJob() %7B%0A%09%09%09%09return jedis.llen(id);%0A%09%09%09%7D%0A%09%09%7D);%0A%09%09LOGGER.info(%22Result:%7B%7D%22, result);%0A%09%09Assert.assertEquals(result, 2);%0A%09%7D%0A%0A
 %7D%0A

Add new test.