package io.digdag.core.workflow;

import io.digdag.client.config.Config;
import io.digdag.core.DigdagEmbed;
import io.digdag.core.LocalSite;
import io.digdag.core.database.TransactionManager;
import io.digdag.core.session.ArchivedTask;
import io.digdag.core.session.StoredSessionAttemptWithSession;
import java.nio.file.Path;
import java.util.List;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import static io.digdag.client.config.ConfigUtils.newConfig;
import static io.digdag.core.workflow.WorkflowTestingUtils.loadYamlResource;
import static io.digdag.core.workflow.WorkflowTestingUtils.runWorkflow;
import static io.digdag.core.workflow.WorkflowTestingUtils.setupEmbed;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;

public class StoreParameterTest
{
    @Rule
    public ExpectedException exception = ExpectedException.none();

    @Rule
    public TemporaryFolder folder = new TemporaryFolder();

    private DigdagEmbed embed;
    private LocalSite localSite;
    private Path projectPath;
    private TransactionManager tm;

    @Before
    public void setUp()
        throws Exception
    {
        this.embed = setupEmbed();
        this.localSite = embed.getLocalSite();
        this.tm = embed.getTransactionManager();
        this.projectPath = folder.newFolder().toPath();
    }

    @After
    public void destroy()
        throws Exception
    {
        embed.close();
    }

    @Test
    public void run()
        throws Exception
    {
        StoredSessionAttemptWithSession attempt =
                runWorkflow(embed, projectPath, "store", loadYamlResource("/io/digdag/core/workflow/store.dig"));

        tm.begin(() -> {
            assertThat(attempt.getStateFlags().isSuccess(), is(true));

            List<ArchivedTask> tasks = localSite.getSessionStore().getTasksOfAttempt(attempt.getId());

            assertStoreParams(tasks, "+verify1", newConfig()
                    .set("verify1_text", "old")
            );
            assertStoreParams(tasks, "+verify2", newConfig()
                    .set("verify2_override", "new")
                    .set("verify2_merge", "merged")
                    .set("verify2_merge_nested", "nested")
                    .set("verify2_reset", "true")
            );
            assertStoreParams(tasks, "+verify3", newConfig()
                    .set("verify3_reset", "")
                    .set("verify3_reset_set", "set")
                    .set("verify3_keep_nested", "kept")
                    .set("verify3_reset_nested", "")
                    .set("verify3_merge_nested", "new")
            );
            return null;
        });
    }

    private void assertStoreParams(List<ArchivedTask> tasks, String taskName, Config expected)
    {
        ArchivedTask task = tasks.stream()
            .filter(t -> t.getFullName().endsWith(taskName))
            .findFirst()
            .orElseThrow(() -> new NullPointerException(taskName));

        assertThat(task.getStoreParams(), is(expected));
    }
}

@@ -383,22 +383,81 @@
 g.junit.
-Before
+AfterClass;%0Aimport org.junit.Before;%0Aimport org.junit.BeforeClass
 ;%0Aimport
@@ -792,79 +792,8 @@
 ow;%0A
-import static io.digdag.core.workflow.WorkflowTestingUtils.setupEmbed;%0A
 impo
@@ -906,16 +906,333 @@
 rTest%0A%7B%0A
+    private static DigdagEmbed embed;%0A%0A    @BeforeClass%0A    public static void createDigdagEmbed()%0A            throws Exception%0A    %7B%0A        embed = WorkflowTestingUtils.setupEmbed();%0A    %7D%0A%0A    @AfterClass%0A    public static void destroyDigdagEmbed()%0A            throws Exception%0A    %7B%0A        embed.close();%0A    %7D%0A%0A
     @Rul
@@ -1375,39 +1375,8 @@
 );%0A%0A
-    private DigdagEmbed embed;%0A
     
@@ -1541,43 +1541,8 @@
   %7B%0A
-        this.embed = setupEmbed();%0A
     
@@ -1629,24 +1629,24 @@
 nManager();%0A
+
         this
@@ -1700,106 +1700,8 @@
  %7D%0A%0A
-    @After%0A    public void destroy()%0A        throws Exception%0A    %7B%0A        embed.close();%0A    %7D%0A%0A
     

Fix StoreParameterTest not to recreate DigdagEmbed instance for each test method to make tests faster