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

package com.microsoft.alm.L2.tfvc;

import com.intellij.openapi.vcs.VcsException;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import com.microsoft.alm.plugin.context.ServerContext;
import com.microsoft.alm.plugin.context.ServerContextManager;
import com.microsoft.alm.plugin.external.utils.CommandUtils;
import com.microsoft.alm.plugin.external.utils.TfvcCheckoutResultUtils;
import com.microsoft.alm.plugin.idea.common.ui.checkout.VsoCheckoutPageModel;
import com.microsoft.tfs.model.connector.TfvcCheckoutResult;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.Objects;

public class TfvcServerWorkspaceTests extends TfvcCheckoutTestBase {

    @Override
    protected void setUpCheckoutModel(VsoCheckoutPageModel model, String path) {
        super.setUpCheckoutModel(model, path);
        model.setTfvcServerCheckout(true);
    }

    private void checkoutFile(File file) {
        ServerContext serverContext = ServerContextManager.getInstance().get(getServerUrl());
        TfvcCheckoutResult checkoutResult = CommandUtils.checkoutFilesForEdit(
                serverContext,
                Collections.singletonList(file.toPath()),
                false);
        try {
            TfvcCheckoutResultUtils.verify(checkoutResult);
        } catch (VcsException e) {
            throw new RuntimeException(e);
        }
    }

    @Test(timeout = 60000)
    public void testCheckout_VSO() throws InterruptedException, IOException {
        checkoutTestRepository(workspace -> {
            File readmeIoFile = workspace.resolve(README_FILE).toFile();
            assertTrue(readmeIoFile.exists());
            VirtualFile readme = Objects.requireNonNull(LocalFileSystem.getInstance().findFileByIoFile(readmeIoFile));
            assertFalse(readme.isWritable());
            checkoutFile(readmeIoFile);
            readme.refresh(false, false);
            assertTrue(readme.isWritable());
        });
    }
}

@@ -287,24 +287,91 @@
 irtualFile;%0A
+import com.microsoft.alm.plugin.authentication.AuthenticationInfo;%0A
 import com.m
@@ -475,16 +475,75 @@
 anager;%0A
+import com.microsoft.alm.plugin.external.models.Workspace;%0A
 import c
@@ -806,16 +806,58 @@
 Result;%0A
+import org.jetbrains.annotations.NotNull;%0A
 import o
@@ -917,24 +917,51 @@
 OException;%0A
+import java.nio.file.Path;%0A
 import java.
@@ -1275,54 +1275,28 @@
     
-private void checkoutFile(File file) %7B%0A       
+@NotNull%0A    private
  Ser
@@ -1306,17 +1306,20 @@
 Context 
-s
+getS
 erverCon
@@ -1322,18 +1322,35 @@
 rContext
- =
+() %7B%0A        return
  ServerC
@@ -1394,24 +1394,74 @@
 rverUrl());%0A
+    %7D%0A%0A    private void checkoutFile(File file) %7B%0A
         Tfvc
@@ -1543,17 +1543,20 @@
         
-s
+getS
 erverCon
@@ -1559,16 +1559,18 @@
 rContext
+()
 ,%0A      
@@ -1822,159 +1822,591 @@
     
-@Test(timeout = 60000)%0A    public void testCheckout_VSO() throws InterruptedException, IOException %7B
+private void assertIsServerWorkspace(Path workspace) %7B%0A        Workspace partialWorkspace = CommandUtils.getPartialWorkspace(workspace, false);%0A        AuthenticationInfo authenticationInfo = getServerContext().getAuthenticationInfo();%0A        Workspace workspaceInfo = CommandUtils.getDetailedWorkspace(%0A                partialWorkspace.getServerDisplayName(),%0A                partialWorkspace.getName(),%0A                authenticationInfo);%0A        assertEquals(Workspace.Location.SERVER, workspaceInfo.getLocation());
 %0A    
+%7D%0A%0A
     
-checkoutTestRepository(workspace -%3E %7B%0A    
+private void testCheckoutCommand(Path workspace) %7B%0A
     
@@ -2470,36 +2470,32 @@
 File();%0A        
-    
 assertTrue(readm
@@ -2521,20 +2521,16 @@
         
-   
- 
 VirtualF
@@ -2632,28 +2632,24 @@
 ));%0A        
-    
 assertFalse(
@@ -2678,20 +2678,16 @@
         
-    
 checkout
@@ -2702,28 +2702,24 @@
 dmeIoFile);%0A
-    
         read
@@ -2748,28 +2748,24 @@
 e);%0A        
-    
 assertTrue(r
@@ -2777,32 +2777,284 @@
 .isWritable());%0A
+    %7D%0A%0A    @Test(timeout = 60000)%0A    public void testServerCheckout() throws InterruptedException, IOException %7B%0A        checkoutTestRepository(workspace -%3E %7B%0A            assertIsServerWorkspace(workspace);%0A            testCheckoutCommand(workspace);%0A
         %7D);%0A    

Add a workspace kind check (#325)