package io.dockstore.client.cli;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import io.dockstore.webservice.DockstoreWebserviceApplication;
import io.dockstore.webservice.DockstoreWebserviceConfiguration;
import io.dropwizard.testing.ResourceHelpers;
import io.dropwizard.testing.junit.DropwizardAppRule;
import io.swagger.client.ApiException;
import io.swagger.client.api.UsersApi;
import io.swagger.client.model.SourceFile;
import io.swagger.client.model.User;
import io.swagger.quay.client.api.UserApi;

import static io.dockstore.common.CommonTestUtilities.clearState;
import static org.mockito.Mockito.mock;
import static org.powermock.api.easymock.PowerMock.replayAll;
import static org.powermock.api.easymock.PowerMock.verifyAll;
import static org.powermock.api.mockito.PowerMockito.doReturn;
import static org.powermock.api.mockito.PowerMockito.spy;
import static org.powermock.api.mockito.PowerMockito.when;
import static org.powermock.api.mockito.PowerMockito.whenNew;

/**
 * These tests use mocking to simulate responses from GitHub, BitBucket, and Quay.io
 * 
 * @author dyuen
 */
@PowerMockIgnore("javax.management.*")
@RunWith(PowerMockRunner.class)
@PrepareForTest({Client.class, UserApi.class})
public class MockedIT {

    @ClassRule
    public static final DropwizardAppRule<DockstoreWebserviceConfiguration> RULE = new DropwizardAppRule<>(
            DockstoreWebserviceApplication.class, ResourceHelpers.resourceFilePath("dockstore.yml"));

    @Before
    public void clearDB() throws Exception {
        clearState();
        spy(Client.class);

        final UsersApi userApiMock = mock(UsersApi.class);
        when(userApiMock.getUser()).thenReturn(new User());
        whenNew(UsersApi.class).withAnyArguments().thenReturn(userApiMock);

        File sourceFile = new File(ResourceHelpers.resourceFilePath("dockstore-tool-linux-sort.cwl"));
        final String sourceFileContents = FileUtils.readFileToString(sourceFile);
        SourceFile file = mock(SourceFile.class);
        when(file.getContent()).thenReturn(sourceFileContents);
        doReturn(file).when(Client.class, "getCWLFromServer", "quay.io/collaboratory/dockstore-tool-linux-sort");
    }

    @Test
    public void runLaunchOneJson() throws IOException, ApiException {
        replayAll();
        Client.main(new String[] { "--config", ClientIT.getConfigFileLocation(true), "dev", "launch", "--entry",
            "quay.io/collaboratory/dockstore-tool-linux-sort", "--json", ResourceHelpers.resourceFilePath("testOneRun.json") });
        verifyAll();
    }

    @Test
    public void runLaunchNJson() throws IOException {
        replayAll();
        Client.main(new String[] { "--config", ClientIT.getConfigFileLocation(true), "dev", "launch", "--entry",
                "quay.io/collaboratory/dockstore-tool-linux-sort", "--json", ResourceHelpers.resourceFilePath("testMultipleRun.json") });
        verifyAll();
    }

    @Test
    public void runLaunchTSV() throws IOException {
        replayAll();
        Client.main(new String[] { "--config", ClientIT.getConfigFileLocation(true), "dev", "launch", "--entry",
                "quay.io/collaboratory/dockstore-tool-linux-sort", "--tsv", ResourceHelpers.resourceFilePath("testMultipleRun.tsv") });
        verifyAll();
    }
}

@@ -2699,39 +2699,32 @@
 eLocation(true),
- %22dev%22,
  %22launch%22, %22--en
@@ -2721,32 +2721,32 @@
 ch%22, %22--entry%22,%0A
+
             %22qua
@@ -3047,39 +3047,32 @@
 eLocation(true),
- %22dev%22,
  %22launch%22, %22--en
@@ -3318,32 +3318,32 @@
    replayAll();%0A
+
         Client.m
@@ -3414,15 +3414,8 @@
 ue),
- %22dev%22,
  %22la

Fix test