package redis.clients.jedis.tests;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.PipelineBlock;
import redis.clients.jedis.Response;
import redis.clients.jedis.exceptions.JedisDataException;
import redis.clients.jedis.tests.HostAndPortUtil.HostAndPort;

public class PipeliningTest extends Assert {
    private static HostAndPort hnp = HostAndPortUtil.getRedisServers().get(0);

    private Jedis jedis;

    @Before
    public void setUp() throws Exception {
        jedis = new Jedis(hnp.host, hnp.port, 500);
        jedis.connect();
        jedis.auth("foobared");
        jedis.flushAll();
    }

    @Test
    public void pipeline() throws UnsupportedEncodingException {
        List<Object> results = jedis.pipelined(new PipelineBlock() {
            public void execute() {
                set("foo", "bar");
                get("foo");
            }
        });

        assertEquals(2, results.size());
        assertEquals("OK", results.get(0));
        assertEquals("bar", results.get(1));

        Pipeline p = jedis.pipelined();
        p.set("foo", "bar");
        p.get("foo");
        results = p.sync();

        assertEquals(2, results.size());
        assertEquals("OK", results.get(0));
        assertEquals("bar", results.get(1));

    }

    @Test
    public void pipelineResponse() {
        jedis.set("string", "foo");
        jedis.lpush("list", "foo");
        jedis.hset("hash", "foo", "bar");
        jedis.zadd("zset", 1, "foo");
        jedis.sadd("set", "foo");

        Pipeline p = jedis.pipelined();
        Response<String> string = p.get("string");
        Response<String> list = p.lpop("list");
        Response<String> hash = p.hget("hash", "foo");
        Response<Set<String>> zset = p.zrange("zset", 0, -1);
        Response<String> set = p.spop("set");
        p.sync();

        assertEquals("foo", string.get());
        assertEquals("foo", list.get());
        assertEquals("bar", hash.get());
        assertEquals("foo", zset.get().iterator().next());
        assertEquals("foo", set.get());
    }

    @Test(expected = JedisDataException.class)
    public void pipelineResponseWithinPipeline() {
        jedis.set("string", "foo");

        Pipeline p = jedis.pipelined();
        Response<String> string = p.get("string");
        string.get();
        p.sync();
    }

    @Test
    public void lala() {

        Pipeline p = jedis.pipelined();

        HashMap<String, Integer> mydata = new HashMap<String, Integer>();

        mydata.put("test1", 1);
        mydata.put("test2", 1);
        mydata.put("test3", 1);
        mydata.put("test4", 1);
        mydata.put("test5", 1);

        for (String txtfield : mydata.keySet()) {
            p.zadd("somekey", mydata.get(txtfield), txtfield);
            p.zincrby("SUPERUNION", mydata.get(txtfield), txtfield);
        }
        p.sync();
    }
}

@@ -78,34 +78,8 @@
 on;%0A
-import java.util.HashMap;%0A
 impo
@@ -2533,537 +2533,7 @@
    %7D
-%0A%0A    @Test%0A    public void lala() %7B%0A%0A        Pipeline p = jedis.pipelined();%0A%0A        HashMap%3CString, Integer%3E mydata = new HashMap%3CString, Integer%3E();%0A%0A        mydata.put(%22test1%22, 1);%0A        mydata.put(%22test2%22, 1);%0A        mydata.put(%22test3%22, 1);%0A        mydata.put(%22test4%22, 1);%0A        mydata.put(%22test5%22, 1);%0A%0A        for (String txtfield : mydata.keySet()) %7B%0A            p.zadd(%22somekey%22, mydata.get(txtfield), txtfield);%0A            p.zincrby(%22SUPERUNION%22, mydata.get(txtfield), txtfield);%0A        %7D%0A        p.sync();%0A    %7D
 %0A%7D%0A

remove stupid test :(