package com.github.phantomthief.jedis;

import static java.util.stream.Collectors.toSet;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.HashSet;
import java.util.Set;
import java.util.stream.Stream;

import org.junit.jupiter.api.Test;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.ScanParams;

/**
 * @author w.vela
 * Created on 2020-05-29.
 */
class JedisHelperScanTest extends BaseJedisTest{

    @Test
    void test() {
        try (JedisPool jedisPool = getPool()) {
            JedisHelper<Jedis> helper = JedisHelper.newBuilder(() -> jedisPool)
                    .build();
            for (int i = 0; i < 100; i++) {
                helper.get().set(i + "", i + "");
            }
            Stream<String> scan = helper.scan(new ScanParams());
            Set<Integer> set = new HashSet<>();
            assertEquals(100L,
                    scan.map(it -> { // force map, prevent peek optimize by jdk9+
                        set.add(Integer.parseInt(it));
                        assertEquals(helper.get().get(it + ""), it + "");
                        return it;
                    }).count());
            assertEquals(100, set.size());
            for (int i = 0; i < 100; i++) {
                assertTrue(set.contains(i));
            }
        }
    }

    @Test
    void testSscan() {
        try (JedisPool jedisPool = getPool()) {
            JedisHelper<Jedis> helper = JedisHelper.newBuilder(() -> jedisPool)
                    .build();
            String sscanKey = "sscan_key";
            for (int i = 0; i < 100; i++) {
                helper.get().sadd(sscanKey, i + "");
            }
            Stream<String> scan = helper.sscan(sscanKey, new ScanParams());
            Set<String> set = scan.collect(toSet());
            assertEquals(100, set.size());
            for (int i = 0; i < 100; i++) {
                assertTrue(set.contains(i + ""));
            }
        }
    }
}

@@ -220,23 +220,93 @@
 va.util.
-HashSet
+AbstractMap.SimpleEntry;%0Aimport java.util.HashSet;%0Aimport java.util.Map.Entry
 ;%0Aimport
@@ -500,16 +500,50 @@
 nParams;
+%0Aimport redis.clients.jedis.Tuple;
 %0A%0A/**%0A *
@@ -2071,32 +2071,32 @@
 i %3C 100; i++) %7B%0A
-
                 
@@ -2158,11 +2158,1384 @@
  %7D%0A    %7D
+%0A%0A    @Test%0A    void testHscan() %7B%0A        try (JedisPool jedisPool = getPool()) %7B%0A            JedisHelper%3CJedis%3E helper = JedisHelper.newBuilder(() -%3E jedisPool)%0A                    .build();%0A            String hscanKey = %22hscan_key%22;%0A            for (int i = 0; i %3C 100; i++) %7B%0A                helper.get().hset(hscanKey, i + %22%22, i + %22%22);%0A            %7D%0A            Stream%3CEntry%3CString, String%3E%3E scan = helper.hscan(hscanKey, new ScanParams());%0A            Set%3CEntry%3CString, String%3E%3E set = scan.collect(toSet());%0A            assertEquals(100, set.size());%0A            for (int i = 0; i %3C 100; i++) %7B%0A                assertTrue(set.contains(new SimpleEntry%3C%3E(i + %22%22, i + %22%22)));%0A            %7D%0A        %7D%0A    %7D%0A%0A    @Test%0A    void testZscan() %7B%0A        try (JedisPool jedisPool = getPool()) %7B%0A            JedisHelper%3CJedis%3E helper = JedisHelper.newBuilder(() -%3E jedisPool)%0A                    .build();%0A            String zscanKey = %22zscan_key%22;%0A            for (int i = 0; i %3C 100; i++) %7B%0A                helper.get().zadd(zscanKey, i, i + %22%22);%0A            %7D%0A            Stream%3CTuple%3E scan = helper.zscan(zscanKey, new ScanParams());%0A            Set%3CTuple%3E set = scan.collect(toSet());%0A            assertEquals(100, set.size());%0A            for (int i = 0; i %3C 100; i++) %7B%0A                assertTrue(set.contains(new Tuple(i + %22%22, (double) i)));%0A            %7D%0A        %7D%0A    %7D
 %0A%7D%0A

add xscan test cases.