package acceptance;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import utils.CommandStatus;
import utils.TemporaryDigdagServer;

import java.nio.file.Path;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;
import static utils.TestUtils.copyResource;
import static utils.TestUtils.main;

public class LimitProjectArchiveFileSizeIT
{
    @Rule
    public TemporaryFolder folder = new TemporaryFolder();

    private Path projectDir;

    @Before
    public void setUp()
            throws Exception
    {
        String subDir = "project_resource_test";
        folder.newFolder(subDir);
        projectDir = folder.getRoot().toPath().resolve(subDir);
        copyResource("acceptance/basic.dig", projectDir.resolve("basic.dig"));
    }

    @Test
    public void uploadProject()
            throws Exception
    {
        TemporaryDigdagServer server = TemporaryDigdagServer.builder()
                .build();
        server.start();

        CommandStatus pushStatus = main("push", "--project", projectDir.toString(), "foobar", "-e", server.endpoint());

        assertThat(pushStatus.code(),is(0));
        server.close();
    }

    @Test
    public void uploadProjectLargerThanLimit()
            throws Exception
    {
        TemporaryDigdagServer server = TemporaryDigdagServer.builder()
                .configuration("api.max_archive_total_size_limit = 1")
                .build();
        server.start();

        String expectMessage = "Size of the uploaded archive file exceeds limit";

        CommandStatus pushStatus = main("push", "--project", projectDir.toString(), "foobar", "-e", server.endpoint());

        assertThat(pushStatus.errUtf8(), containsString(expectMessage));
        server.close();
    }
}
@@ -587,16 +587,41 @@
 jectDir;
+%0A    private Path config;
 %0A%0A    @B
@@ -689,91 +689,8 @@
   %7B%0A
-        String subDir = %22project_resource_test%22;%0A        folder.newFolder(subDir);%0A
     
@@ -744,14 +744,16 @@
 lve(
-subDir
+%22foobar%22
 );%0A 
@@ -765,74 +765,39 @@
   co
-pyResource(%22acceptance/basic.dig%22, projectDir.resolve(%22basic.dig%22)
+nfig = folder.newFile().toPath(
 );%0A 
@@ -879,32 +879,43 @@
 n%0A    %7B%0A        
+try (final 
 TemporaryDigdagS
@@ -989,128 +989,606 @@
 ld()
-;%0A        server.start();%0A%0A        CommandStatus pushStatus = main(%22push%22, %22--project%22, projectDir.toString(), %22foobar%22,
+) %7B%0A            server.start();%0A%0A            // Create new project%0A            CommandStatus initStatus = main(%22init%22,%0A                    %22-c%22, config.toString(),%0A                    projectDir.toString());%0A            assertThat(initStatus.code(), is(0));%0A%0A            copyResource(%22acceptance/basic.dig%22, projectDir.resolve(%22basic.dig%22));%0A%0A            // Push the project%0A            CommandStatus pushStatus = main(%0A                    %22push%22,%0A                    %22--project%22, projectDir.toString(),%0A                    %22foobar%22,%0A                    %22-c%22, config.toString(),%0A                   
  %22-e
@@ -1606,33 +1606,32 @@
 ndpoint());%0A
-%0A
         
 assertThat(p
@@ -1610,32 +1610,36 @@
 int());%0A        
+    
 assertThat(pushS
@@ -1651,16 +1651,17 @@
 .code(),
+ 
 is(0));%0A
@@ -1664,39 +1664,25 @@
 ));%0A        
-server.close();
+%7D
 %0A    %7D%0A%0A    
@@ -1777,16 +1777,27 @@
         
+try (final 
 Temporar
@@ -1946,26 +1946,31 @@
 .build()
-;%0A
+) %7B%0A   
         
 server.s
@@ -1953,32 +1953,33 @@
 )) %7B%0A           
+ 
 server.start();%0A
@@ -1991,300 +1991,732 @@
     
-String expectMessage = %22Size of the uploaded archive file exceeds limit%22;%0A%0A        CommandStatus pushStatus = main(%22push%22, %22--project%22, projectDir.toString(), %22foobar%22, %22-e%22, server.endpoint());%0A%0A        assertThat(pushStatus.errUtf8(), containsString(expectMessage));%0A        server.close();
+    // Create new project%0A            CommandStatus initStatus = main(%22init%22,%0A                    %22-c%22, config.toString(),%0A                    projectDir.toString());%0A            assertThat(initStatus.code(), is(0));%0A%0A            copyResource(%22acceptance/basic.dig%22, projectDir.resolve(%22basic.dig%22));%0A%0A            CommandStatus pushStatus = main(%0A                    %22push%22,%0A                    %22--project%22, projectDir.toString(),%0A                    %22foobar%22,%0A                    %22-c%22, config.toString(),%0A                    %22-e%22, server.endpoint());%0A            assertThat(pushStatus.code(), is(1));%0A            assertThat(pushStatus.errUtf8(), containsString(%22Size of the uploaded archive file exceeds limit%22));%0A        %7D
 %0A   

Refactor acceptance.LimitProjectArchiveFileSizeIT in digdag-tests