package acceptance;

import org.junit.Test;
import utils.CommandStatus;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

import static java.util.Arrays.asList;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
import static utils.TestUtils.addWorkflow;
import static utils.TestUtils.assertCommandStatus;
import static utils.TestUtils.main;

public class ParamSetRedisIT
        extends BaseParamRedisIT
{
    @Test
    public void setValueToRedis()
            throws IOException
    {
        Path projectDir = folder.newFolder().toPath();
        addWorkflow(projectDir, "acceptance/params/set.dig");
        Path config = projectDir.resolve("config");
        Files.write(config, asList(
                "param_server.type=redis",
                "param_server.host=" + DIGDAG_TEST_REDIS
        ));

        CommandStatus status = main("run",
                "-o", folder.newFolder().getAbsolutePath(),
                "--config", config.toString(),
                "--project", projectDir.toString(),
                projectDir.resolve("set.dig").toString()
        );
        assertCommandStatus(status);
        assertThat(redisClient.get("0:key1"), is("{\"value_type\":0,\"value\":{\"value\":\"value1\"}}"));
        assertThat(redisClient.get("0:key2"), is("{\"value_type\":0,\"value\":{\"value\":\"value2\"}}"));
    }

    @Test
    public void parallelSetValueToRedis()
            throws IOException
    {
        Path projectDir = folder.newFolder().toPath();
        addWorkflow(projectDir, "acceptance/params/parallel_set.dig");
        Path config = projectDir.resolve("config");
        Files.write(config, asList(
                "param_server.type=redis",
                "param_server.host=" + DIGDAG_TEST_REDIS
        ));

        CommandStatus status = main("run",
                "-o", folder.newFolder().getAbsolutePath(),
                "--config", config.toString(),
                "--project", projectDir.toString(),
                projectDir.resolve("parallel_set.dig").toString()
        );
        assertCommandStatus(status);
        assertThat(redisClient.get("0:key1"), is("{\"value_type\":0,\"value\":{\"value\":\"value1\"}}"));
        assertThat(redisClient.get("0:key2"), is("{\"value_type\":0,\"value\":{\"value\":\"value2\"}}"));
    }
}

@@ -185,24 +185,75 @@
 ays.asList;%0A
+import static junit.framework.TestCase.assertTrue;%0A
 import stati
@@ -279,16 +279,16 @@
 .Is.is;%0A
-
 import s
@@ -517,16 +517,172 @@
 disIT%0A%7B%0A
+    private static long TTL_90_DAYS_PLUS_1SEC = 60 * 60 * 24 * 90 + 1; // 90days + 1sec%0A    private static long TTL_89_DAYS = 60 * 60 * 24 * 89; // 89days%0A%0A
     @Tes
@@ -1371,32 +1371,297 @@
 Status(status);%0A
+%0A        long key1_ttl = redisClient.ttl(%220:key1%22);%0A        long key2_ttl = redisClient.ttl(%220:key2%22);%0A%0A        assertTrue(key1_ttl %3C TTL_90_DAYS_PLUS_1SEC && key1_ttl %3E TTL_89_DAYS);%0A        assertTrue(key2_ttl %3C TTL_90_DAYS_PLUS_1SEC && key2_ttl %3E TTL_89_DAYS);%0A%0A
         assertTh
@@ -2550,32 +2550,32 @@
 ng()%0A        );%0A
-
         assertCo
@@ -2595,16 +2595,281 @@
 tatus);%0A
+%0A        long key1_ttl = redisClient.ttl(%220:key1%22);%0A        long key2_ttl = redisClient.ttl(%220:key2%22);%0A%0A        assertTrue(key1_ttl %3C TTL_90_DAYS_PLUS_1SEC && key1_ttl %3E TTL_89_DAYS);%0A        assertTrue(key2_ttl %3C TTL_90_DAYS_PLUS_1SEC && key2_ttl %3E TTL_89_DAYS);%0A%0A
         

Add Redis test