// Path: sonar-plugin-server/src/test/java/jetbrains/buildserver/sonarplugin/DataProviders.java
// public class DataProviders {
//     @DataProvider
//     private static Object[][] getFileSystemAndRoot() {
//         return new Object[][]{
//                 {Jimfs.newFileSystem(Configuration.windows()), "C:\\"},
//                 {Jimfs.newFileSystem(Configuration.unix()), "/"}
// //                , {Jimfs.newFileSystem(Configuration.osX()), "/"}
//         };
//     }
// }
// 
// Path: sonar-plugin-agent/src/test/java/jetbrains/buildserver/sonarplugin/TestTools.java
// public final class TestTools {
//     public static Path createTempDirectory() throws IOException {
//         return Files.createTempDirectory("keystoreTemp");
//     }
// 
//     public static File writeTo(String prefix, String content) {
//         return writeTo(prefix, content, ".tmp", null);
//     }
// 
//     public static File writeTo(String prefix, String content, String suffix) {
//         return writeTo(prefix, content, suffix, null);
//     }
// 
//     public static File writeTo(String prefix, String content, String suffix, File baseFolder) {
//         File tempFile = null;
//         try {
//             tempFile = File.createTempFile(prefix, suffix, baseFolder);
//             FileWriter fw = new FileWriter(tempFile);
//             fw.write(content);
//             fw.flush();
//         } catch (IOException e) {
//             e.printStackTrace();
//         }
//         return tempFile;
//     }
// 
// }


import jetbrains.buildServer.tools.GetPackageVersionResult;
import jetbrains.buildServer.tools.ToolException;
import jetbrains.buildServer.web.openapi.PluginDescriptor;
import jetbrains.buildserver.sonarplugin.DataProviders;
import jetbrains.buildserver.sonarplugin.TestTools;
import org.jetbrains.annotations.NotNull;
import org.jmock.Mockery;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.io.IOException;
import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.fail;
import static org.assertj.core.api.BDDAssertions.then;
    public void testValidatePackedTool(@NotNull final FileSystem jimfs, @NotNull final String root) throws IOException, ToolException {
        myToolProvider.validatePackedTool(prepareCorrectPluginWithName(jimfs, "sonar-scanner-msbuild.1.2.3.zip", root, "some", "path"));
        myToolProvider.validatePackedTool(prepareCorrectPluginWithName(jimfs, "sonar-scanner-msbuild-1.2.3.zip", root, "some", "path"));
        myToolProvider.validatePackedTool(prepareCorrectPluginWithName(jimfs, "sonar-scanner-msbuild-1.zip", root, "some", "path"));
        myToolProvider.validatePackedTool(prepareCorrectPluginWithName(jimfs, "sonar-scanner-msbuild-1.2.3.4.zip", root, "some", "path"));
        myToolProvider.validatePackedTool(prepareCorrectPluginWithName(jimfs, "sonar-scanner-msbuild-5.1.0.28487-net5.0.zip", root, "some", "path"));
        myToolProvider.validatePackedTool(prepareCorrectPluginWithName(jimfs, "sonar-scanner-msbuild-5.1.0.28487-netcoreapp2.0.zip", root, "some", "path"));
        myToolProvider.validatePackedTool(prepareCorrectPluginWithName(jimfs, "sonar-scanner-msbuild-5.1.0.28487-net46.zip", root, "some", "path"));

        assertThatThrownBy(() -> myToolProvider.validatePackedTool(prepareCorrectPluginWithName(jimfs, "sonar-scanner-msbuild.zip", root, "some", "path"))).isInstanceOf(ToolException.class);
        assertThatThrownBy(() -> myToolProvider.validatePackedTool(prepareCorrectPluginWithName(jimfs, "sonar-scanner-msbuil-1.2.3.zip", root, "some", "path"))).isInstanceOf(ToolException.class);
        assertThatThrownBy(() -> myToolProvider.validatePackedTool(prepareCorrectPluginWithName(jimfs, "sonar-scanner-msbuild-1.2.3a.zip", root, "some", "path"))).isInstanceOf(ToolException.class);
        assertThatThrownBy(() -> myToolProvider.validatePackedTool(prepareCorrectPluginWithName(jimfs, "sonar-scanner-msbuild-1.2.3.ip", root, "some", "path"))).isInstanceOf(ToolException.class);
    }

    public void testParseVersion(@NotNull final FileSystem jimfs, @NotNull final String root) throws Exception {
        Path zip = prepareCorrectPlugin(jimfs, root, "some", "path");

        GetPackageVersionResult res = myToolProvider.parseVersion(zip, "1.2.3");
        then(res.getToolVersion()).describedAs("Should be correct plugin but was '" + res.getDetails() + "'").isNotNull();
        then(res.getToolVersion().getVersion()).isEqualTo("1.2.3");
    }

    public void testParseVersionIncorrect(@NotNull final FileSystem jimfs, @NotNull final String root) throws Exception {
        Path zip = prepareIncorrectPlugin(jimfs, root, "some", "path");

        GetPackageVersionResult res = myToolProvider.parseVersion(zip, "1.2.3");
        then(res.getToolVersion()).isNull();
        then(res.getDetails()).doesNotContain(SimpleZipToolProviderSQMSBuild.BIN).contains(SimpleZipToolProviderSQMSBuild.SONAR_QUBE_SCANNER_MSBUILD_EXE);

// Path: sonar-plugin-server/src/test/java/jetbrains/buildserver/sonarplugin/DataProviders.java
// public class DataProviders {
//     @DataProvider
//     private static Object[][] getFileSystemAndRoot() {
//         return new Object[][]{
//                 {Jimfs.newFileSystem(Configuration.windows()), "C:\\"},
//                 {Jimfs.newFileSystem(Configuration.unix()), "/"}
// //                , {Jimfs.newFileSystem(Configuration.osX()), "/"}
//         };
//     }
// }
// 
// Path: sonar-plugin-agent/src/test/java/jetbrains/buildserver/sonarplugin/TestTools.java
// public final class TestTools {
//     public static Path createTempDirectory() throws IOException {
//         return Files.createTempDirectory("keystoreTemp");
//     }
// 
//     public static File writeTo(String prefix, String content) {
//         return writeTo(prefix, content, ".tmp", null);
//     }
// 
//     public static File writeTo(String prefix, String content, String suffix) {
//         return writeTo(prefix, content, suffix, null);
//     }
// 
//     public static File writeTo(String prefix, String content, String suffix, File baseFolder) {
//         File tempFile = null;
//         try {
//             tempFile = File.createTempFile(prefix, suffix, baseFolder);
//             FileWriter fw = new FileWriter(tempFile);
//             fw.write(content);
//             fw.flush();
//         } catch (IOException e) {
//             e.printStackTrace();
//         }
//         return tempFile;
//     }
// 
// }


// Path: sonar-plugin-server/src/test/java/jetbrains/buildserver/sonarplugin/msbuild/tool/SimpleZipToolProviderSQMSBuildTest.java
import jetbrains.buildServer.tools.GetPackageVersionResult;
import jetbrains.buildServer.tools.ToolException;
import jetbrains.buildServer.web.openapi.PluginDescriptor;
import jetbrains.buildserver.sonarplugin.DataProviders;
import jetbrains.buildserver.sonarplugin.TestTools;
import org.jetbrains.annotations.NotNull;
import org.jmock.Mockery;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.io.IOException;
import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.fail;
import static org.assertj.core.api.BDDAssertions.then;

    public void testValidatePackedTool(@NotNull final FileSystem jimfs, @NotNull final String root) throws IOException, ToolException {
        myToolProvider.validatePackedTool(prepareCorrectPluginWithName(jimfs, "sonar-scanner-msbuild.1.2.3.zip", root, "some", "path"));
        myToolProvider.validatePackedTool(prepareCorrectPluginWithName(jimfs, "sonar-scanner-msbuild-1.2.3.zip", root, "some", "path"));
        myToolProvider.validatePackedTool(prepareCorrectPluginWithName(jimfs, "sonar-scanner-msbuild-1.zip", root, "some", "path"));
        myToolProvider.validatePackedTool(prepareCorrectPluginWithName(jimfs, "sonar-scanner-msbuild-1.2.3.4.zip", root, "some", "path"));
        myToolProvider.validatePackedTool(prepareCorrectPluginWithName(jimfs, "sonar-scanner-msbuild-5.1.0.28487-net5.0.zip", root, "some", "path"));
        myToolProvider.validatePackedTool(prepareCorrectPluginWithName(jimfs, "sonar-scanner-msbuild-5.1.0.28487-netcoreapp2.0.zip", root, "some", "path"));
        myToolProvider.validatePackedTool(prepareCorrectPluginWithName(jimfs, "sonar-scanner-msbuild-5.1.0.28487-net46.zip", root, "some", "path"));

        assertThatThrownBy(() -> myToolProvider.validatePackedTool(prepareCorrectPluginWithName(jimfs, "sonar-scanner-msbuild.zip", root, "some", "path"))).isInstanceOf(ToolException.class);
        assertThatThrownBy(() -> myToolProvider.validatePackedTool(prepareCorrectPluginWithName(jimfs, "sonar-scanner-msbuil-1.2.3.zip", root, "some", "path"))).isInstanceOf(ToolException.class);
        assertThatThrownBy(() -> myToolProvider.validatePackedTool(prepareCorrectPluginWithName(jimfs, "sonar-scanner-msbuild-1.2.3a.zip", root, "some", "path"))).isInstanceOf(ToolException.class);
        assertThatThrownBy(() -> myToolProvider.validatePackedTool(prepareCorrectPluginWithName(jimfs, "sonar-scanner-msbuild-1.2.3.ip", root, "some", "path"))).isInstanceOf(ToolException.class);
    }

    public void testParseVersion(@NotNull final FileSystem jimfs, @NotNull final String root) throws Exception {
        Path zip = prepareCorrectPlugin(jimfs, root, "some", "path");

        GetPackageVersionResult res = myToolProvider.parseVersion(zip, "1.2.3");
        then(res.getToolVersion()).describedAs("Should be correct plugin but was '" + res.getDetails() + "'").isNotNull();
        then(res.getToolVersion().getVersion()).isEqualTo("1.2.3");
    }

    public void testParseVersionIncorrect(@NotNull final FileSystem jimfs, @NotNull final String root) throws Exception {
        Path zip = prepareIncorrectPlugin(jimfs, root, "some", "path");

        GetPackageVersionResult res = myToolProvider.parseVersion(zip, "1.2.3");
        then(res.getToolVersion()).isNull();
        then(res.getDetails()).doesNotContain(SimpleZipToolProviderSQMSBuild.BIN).contains(SimpleZipToolProviderSQMSBuild.SONAR_QUBE_SCANNER_MSBUILD_EXE);

        zip = TestTools.prepareZip(getPluginPath(jimfs, root, "some", "path"), CORRECT_PLUGIN_NAME, zfs -> {