package com.github.dockerjava.core.command;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.isEmptyOrNullString;
import static org.hamcrest.Matchers.not;

import java.io.InputStream;
import java.lang.reflect.Method;

import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import com.github.dockerjava.api.exception.NotFoundException;
import com.github.dockerjava.api.command.CreateContainerResponse;
import com.github.dockerjava.client.AbstractDockerClientTest;

@Test(groups = "integration")
public class CopyArchiveFromContainerCmdImplTest extends AbstractDockerClientTest {

    @BeforeTest
    public void beforeTest() throws Exception {
        super.beforeTest();
    }

    @AfterTest
    public void afterTest() {
        super.afterTest();
    }

    @BeforeMethod
    public void beforeMethod(Method method) {
        super.beforeMethod(method);
    }

    @AfterMethod
    public void afterMethod(ITestResult result) {
        super.afterMethod(result);
    }

    @Test
    public void copyFromContainer() throws Exception {
        // TODO extract this into a shared method
        CreateContainerResponse container = dockerClient.createContainerCmd("busybox")
                .withName("docker-java-itest-copyFromContainer").withCmd("touch", "/copyFromContainer").exec();

        LOG.info("Created container: {}", container);
        assertThat(container.getId(), not(isEmptyOrNullString()));

        dockerClient.startContainerCmd(container.getId()).exec();

        InputStream response = dockerClient.copyArchiveFromContainerCmd(container.getId(), "/copyFromContainer").exec();
        Boolean bytesAvailable = response.available() > 0;
        assertTrue(bytesAvailable, "The file was not copied from the container.");

        // read the stream fully. Otherwise, the underlying stream will not be closed.
        String responseAsString = asString(response);
        assertNotNull(responseAsString);
        assertTrue(responseAsString.length() > 0);
    }

    @Test
    public void copyFromNonExistingContainer() throws Exception {
        try {
            dockerClient.copyArchiveFromContainerCmd("non-existing", "/test").exec();
            fail("expected NotFoundException");
        } catch (NotFoundException ignored) {
        }
    }
}

@@ -251,16 +251,316 @@
 Method;%0A
+import java.nio.file.Files;%0Aimport java.nio.file.Path;%0Aimport java.nio.file.Paths;%0Aimport java.nio.file.StandardOpenOption;%0A%0Aimport org.apache.commons.compress.archivers.tar.TarArchiveEntry;%0Aimport org.apache.commons.compress.archivers.tar.TarArchiveInputStream;%0Aimport org.apache.commons.io.IOUtils;
 %0Aimport 
@@ -979,16 +979,76 @@
 entTest;
+%0Aimport com.github.dockerjava.core.util.CompressArchiveUtil;
 %0A%0A@Test(
@@ -2843,11 +2843,1622 @@
  %7D%0A    %7D
+%0A%0A    @Test%0A    public void copyFromContainerBinaryFile() throws Exception %7B%0A        CreateContainerResponse container = dockerClient.createContainerCmd(%22busybox%22)%0A                .withName(%22docker-java-itest-copyFromContainerBinaryFile%22).exec();%0A%0A        LOG.info(%22Created container: %7B%7D%22, container);%0A        assertThat(container.getId(), not(isEmptyOrNullString()));%0A%0A        Path temp = Files.createTempFile(%22%22, %22.tar.gz%22);%0A        Path binaryFile = Paths.get(%22src/test/resources/testCopyFromArchive/binary.dat%22);%0A        CompressArchiveUtil.tar(binaryFile, temp, true, false);%0A%0A        try (InputStream uploadStream = Files.newInputStream(temp)) %7B%0A            dockerClient.copyArchiveToContainerCmd(container.getId()).withTarInputStream(uploadStream).exec();%0A        %7D%0A%0A        InputStream response = dockerClient.copyArchiveFromContainerCmd(container.getId(), %22/binary.dat%22).exec();%0A        Boolean bytesAvailable = response.available() %3E 0;%0A        assertTrue(bytesAvailable, %22The file was not copied from the container.%22);%0A%0A        try (TarArchiveInputStream tarInputStream = new TarArchiveInputStream(response)) %7B%0A            TarArchiveEntry nextTarEntry = tarInputStream.getNextTarEntry();%0A%0A            assertEquals(nextTarEntry.getName(), %22binary.dat%22);%0A            try (InputStream binaryFileInputStream = Files.newInputStream(binaryFile, StandardOpenOption.READ)) %7B%0A                assertTrue(IOUtils.contentEquals(binaryFileInputStream, tarInputStream));%0A            %7D%0A%0A            assertNull(tarInputStream.getNextTarEntry(), %22Nothing except binary.dat is expected to be copied.%22);%0A        %7D%0A    %7D
 %0A%7D%0A

Add test originated from netty impl