package io.digdag.core.workflow;

import java.nio.file.Files;

import io.digdag.core.database.TransactionManager;
import io.digdag.core.LocalSite;
import io.digdag.client.config.Config;
import io.digdag.client.config.ConfigException;
import io.digdag.core.DigdagEmbed;
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 java.nio.charset.StandardCharsets.UTF_8;
import static io.digdag.client.config.ConfigUtils.newConfig;
import static io.digdag.core.workflow.WorkflowTestingUtils.setupEmbed;
import static io.digdag.core.workflow.WorkflowTestingUtils.loadYamlResource;
import static org.junit.Assert.*;
import static org.hamcrest.Matchers.is;

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

    @Rule
    public TemporaryFolder folder = new TemporaryFolder();

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

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

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

    @Test
    public void run()
        throws Exception
    {
        runWorkflow("basic", loadYamlResource("/io/digdag/core/workflow/basic.dig"));
    }

    @Test
    public void validateUnknownTopLevelKeys()
        throws Exception
    {
        exception.expect(ConfigException.class);
        runWorkflow("unknown_schedule", newConfig()
                .set("invalid_schedule", newConfig().set("daily>", "00:00:00"))
                .set("+step1", newConfig().set("sh>", "echo ok"))
                );
    }

    @Test
    public void retryOnGroupingTask()
        throws Exception
    {
        runWorkflow("retry_on_group", loadYamlResource("/io/digdag/core/workflow/retry_on_group.dig"));
        assertThat(new String(Files.readAllBytes(folder.getRoot().toPath().resolve("out")), UTF_8), is("try1try2try1try2try1try2try1try2"));
    }

    private void runWorkflow(String workflowName, Config config)
            throws Exception
    {
        WorkflowTestingUtils.runWorkflow(embed, folder.getRoot().toPath(), workflowName, config);
    }
}

@@ -303,22 +303,81 @@
 g.junit.
-Before
+AfterClass;%0Aimport org.junit.Before;%0Aimport org.junit.BeforeClass
 ;%0Aimport
@@ -873,16 +873,304 @@
 rTest%0A%7B%0A
+    private static DigdagEmbed embed;%0A%0A    @BeforeClass%0A    public static void createDigdagEmbed()%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
@@ -1313,39 +1313,8 @@
 );%0A%0A
-    private DigdagEmbed embed;%0A
     
@@ -1449,43 +1449,8 @@
   %7B%0A
-        this.embed = setupEmbed();%0A
     
@@ -1488,24 +1488,24 @@
 ocalSite();%0A
+
         this
@@ -1552,106 +1552,8 @@
  %7D%0A%0A
-    @After%0A    public void destroy()%0A        throws Exception%0A    %7B%0A        embed.close();%0A    %7D%0A%0A
     

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