// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See License.txt in the project root.

package com.microsoft.alm.plugin.idea.tfvc.core.tfs;

import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vcs.FilePath;
import com.intellij.openapi.vcs.LocalFilePath;
import com.microsoft.alm.plugin.external.models.Workspace;
import com.microsoft.alm.plugin.external.utils.CommandUtils;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.when;

@RunWith(PowerMockRunner.class)
@PrepareForTest({CommandUtils.class})
public class TFVCUtilTest {

    private final Workspace workspace = new Workspace(
            "server",
            "name",
            "computer",
            "owner",
            "comment",
            Collections.singletonList(new Workspace.Mapping("serverPath", "/tmp/localPath", false)));

    @Mock
    private Project mockProject;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);

        PowerMockito.mockStatic(CommandUtils.class);
        when(CommandUtils.getPartialWorkspace(mockProject)).thenReturn(workspace);
    }

    @Test
    public void isFileUnderTFVCMappingShouldTests() {
        Assert.assertTrue(TFVCUtil.isFileUnderTFVCMapping(mockProject, new LocalFilePath("/tmp/localPath/1.txt", false)));
        Assert.assertTrue(TFVCUtil.isFileUnderTFVCMapping(mockProject, new LocalFilePath("/tmp/localPath", true)));
        Assert.assertFalse(TFVCUtil.isFileUnderTFVCMapping(mockProject, new LocalFilePath("/tmp/localPath1", true)));
        Assert.assertFalse(TFVCUtil.isFileUnderTFVCMapping(mockProject, new LocalFilePath("/tmp/localPath1/1.txt", false)));
    }

    @Test
    public void filterValidTFVCPathsTest() {
        List<FilePath> localFiles = Arrays.asList(
                new LocalFilePath("/tmp/localPath", true),
                new LocalFilePath("/tmp/localPath/1.txt", false));
        List<FilePath> nonLocalFiles = Arrays.asList(
                new LocalFilePath("/tmp/localPath1", true),
                new LocalFilePath("/tmp/localPath1/1.txt", false));
        List<FilePath> allPaths = Lists.newArrayList(Iterables.concat(localFiles, nonLocalFiles));
        List<String> localFilePaths = localFiles.stream().map(FilePath::getPath).collect(Collectors.toList());
        assertThat(TFVCUtil.filterValidTFVCPaths(mockProject, allPaths), is(localFilePaths));
    }

    @Test
    public void filterDollarTFVCPathsTest() {
        List<FilePath> localFiles = Arrays.asList(
                new LocalFilePath("/tmp/localPath", true),
                new LocalFilePath("/tmp/localPath/1.txt", false));
        List<FilePath> dollarFiles = Arrays.asList(
                new LocalFilePath("/tmp/localPath/$1.txt", false),
                new LocalFilePath("/tmp/localPath/$1/1.txt", false),
                new LocalFilePath("/tmp/localPath/$1", true));
        List<FilePath> allPaths = Lists.newArrayList(Iterables.concat(localFiles, dollarFiles));
        List<String> localFilePaths = localFiles.stream().map(FilePath::getPath).collect(Collectors.toList());
        assertThat(TFVCUtil.filterValidTFVCPaths(mockProject, allPaths), is(localFilePaths));
    }
}

@@ -511,33 +511,8 @@
 ls;%0A
-import org.junit.Assert;%0A
 impo
@@ -1015,20 +1015,107 @@
 t.assert
-That
+False;%0Aimport static org.junit.Assert.assertThat;%0Aimport static org.junit.Assert.assertTrue
 ;%0Aimport
@@ -1850,39 +1850,32 @@
 sts() %7B%0A        
-Assert.
 assertTrue(TFVCU
@@ -1970,31 +1970,24 @@
 ));%0A        
-Assert.
 assertTrue(T
@@ -2075,39 +2075,32 @@
 rue)));%0A        
-Assert.
 assertFalse(TFVC
@@ -2194,23 +2194,16 @@
         
-Assert.
 assertFa
@@ -3827,11 +3827,529 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void isInServiceDirectoryTests() %7B%0A        assertFalse(TFVCUtil.isInServiceDirectory(new LocalFilePath(%22C:%5C%5CTemp%22, true)));%0A        assertFalse(TFVCUtil.isInServiceDirectory(new LocalFilePath(%22/tmp/_tf/xxx%22, true)));%0A        assertTrue(TFVCUtil.isInServiceDirectory(new LocalFilePath(%22/tmp/$tf/xxx%22, true)));%0A        assertTrue(TFVCUtil.isInServiceDirectory(new LocalFilePath(%22/tmp/.tf/xxx%22, true)));%0A        assertTrue(TFVCUtil.isInServiceDirectory(new LocalFilePath(%22/tmp/.tf%22, false)));%0A    %7D
 %0A%7D%0A

Add tests for isInServiceDirectory method (#325)