package io.digdag.core.workflow;

import com.google.common.base.Throwables;
import com.google.common.io.Resources;
import io.digdag.client.config.Config;
import io.digdag.client.config.ConfigException;
import io.digdag.client.config.ConfigFactory;
import io.digdag.core.DigdagEmbed;
import io.digdag.core.config.YamlConfigLoader;
import org.junit.Before;
import org.junit.After;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import java.io.IOException;

import static io.digdag.core.workflow.WorkflowTestingUtils.setupEmbed;
import static java.nio.charset.StandardCharsets.UTF_8;

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

    private DigdagEmbed embed;

    private WorkflowCompiler compiler;

    @Before
    public void setUp()
            throws Exception
    {
        embed = setupEmbed();
        compiler = new WorkflowCompiler();
    }

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

    private Config loadYamlResource(String name)
    {
        try {
            String content = Resources.toString(getClass().getResource(name), UTF_8);
            return embed.getInjector().getInstance(YamlConfigLoader.class)
                    .loadString(content)
                    .toConfig(embed.getInjector().getInstance(ConfigFactory.class));
        }
        catch (IOException ex) {
            throw Throwables.propagate(ex);
        }
    }

    @Test
    public void verifySingleOperatorPasses()
    {
        Config config = loadYamlResource("/io/digdag/core/workflow/single_operator.dig");
        compiler.compile("single_operator", config);
    }

    @Test
    public void verifyMultipleOperatorsFail()
    {
        Config config = loadYamlResource("/io/digdag/core/workflow/multiple_operators.dig");
        exception.expect(ConfigException.class);
        compiler.compile("multiple_operators", config);
    }

    @Test
    public void verifyUnusedKeysInGroupingTask()
    {
        Config config = loadYamlResource("/io/digdag/core/workflow/unused_keys_in_group.dig");
        exception.expect(ConfigException.class);
        compiler.compile("unused_keys_in_group", config);
    }

    @Test
    public void verifyErrorTaskIsValidated()
    {
        Config config = loadYamlResource("/io/digdag/core/workflow/invalid_error_task.dig");
        exception.expect(ConfigException.class);
        compiler.compile("invalid_error_task", config);
    }
}

@@ -319,24 +319,53 @@
 nfigLoader;%0A
+import org.junit.AfterClass;%0A
 import org.j
@@ -397,24 +397,54 @@
 unit.After;%0A
+import org.junit.BeforeClass;%0A
 import org.j
@@ -553,79 +553,8 @@
 n;%0A%0A
-import static io.digdag.core.workflow.WorkflowTestingUtils.setupEmbed;%0A
 impo
@@ -641,16 +641,55 @@
 rTest%0A%7B%0A
+    private static DigdagEmbed embed;%0A%0A
     @Rul
@@ -758,40 +758,8 @@
 );%0A%0A
-    private DigdagEmbed embed;%0A%0A
     
@@ -805,16 +805,21 @@
  @Before
+Class
 %0A    pub
@@ -826,18 +826,37 @@
 lic 
-void setUp
+static void createDigdagEmbed
 ()%0A 
@@ -905,16 +905,37 @@
 embed = 
+WorkflowTestingUtils.
 setupEmb
@@ -948,43 +948,125 @@
     
+%7D%0A%0A
     
-compiler = new WorkflowCompiler
+@AfterClass%0A    public static void destroyDigdagEmbed()%0A            throws Exception%0A    %7B%0A        embed.close
 ();%0A
@@ -1077,21 +1077,22 @@
 %7D%0A%0A    @
-After
+Before
 %0A    pub
@@ -1100,23 +1100,21 @@
 ic void 
-destroy
+setUp
 ()%0A     
@@ -1108,32 +1108,36 @@
 setUp()%0A        
+ 
+   
 throws Exception
@@ -1147,35 +1147,55 @@
   %7B%0A        
-embed.close
+compiler = new WorkflowCompiler
 ();%0A    %7D%0A%0A 

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