package org.broad.igv.gs.dm;

import org.broad.igv.Globals;
import org.broad.igv.PreferenceManager;
import org.broad.igv.gs.GSUtils;
import org.broad.igv.util.HttpUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.net.URL;
import java.util.List;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.fail;

/**
 * @author Jim Robinson
 * @date 1/16/12
 */
public class DMUtilsTest {


    @Before
    public void setup() {
        Globals.setTesting(true);
        GSUtils.clearGSToken();
        HttpUtils.getInstance().setAuthenticator(new GSTestAuthenticator());
    }

    @After
    public void teardown() {
        //       HttpUtils.getInstance().resetAuthenticator();
    }

    @Test
    public void testListPeronsalDirectory() throws Exception {

        URL defaultURL = new URL(PreferenceManager.getInstance().get(PreferenceManager.GENOME_SPACE_DM_SERVER) +
                DMUtils.PERSONAL_DIRECTORY);
        GSDirectoryListing dirListing = DMUtils.getDirectoryListing(defaultURL);
        assertNotNull("Directory listing", dirListing);

        List<GSFileMetadata> gsFiles = dirListing.getContents();

        //Search for known test file
        boolean found = false;
        final String testFileName = "Broad.080528.subtypes.seg.gz";
        for (GSFileMetadata fileMetadata : gsFiles) {
            if (fileMetadata.getName().equals(testFileName)) {
                found = true;
                String path = "/Home/igvtest/" + testFileName;
                assertEquals("Test file path", path, fileMetadata.getPath());
            }
        }
        if (!found) {
            fail("Test file not found: " + testFileName);
        }
    }

    @Test
    public void testGetDirectoryListing() throws Exception {
        // TODO -- test for arbitrary directory
    }

    @Test
    public void testUploadFile() throws Exception {
        // TODO -- implementation
    }

    @Test
    public void testCreateDirectory() throws Exception {
        // TODO -- implementation
    }


    static class GSTestAuthenticator extends Authenticator {

        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication("igvtest", "igvtest".toCharArray());
        }
    }

}

@@ -1,8 +1,557 @@
+/*%0A * Copyright (c) 2007-2012 The Broad Institute, Inc.%0A * SOFTWARE COPYRIGHT NOTICE%0A * This software and its documentation are the copyright of the Broad Institute, Inc. All rights are reserved.%0A *%0A * This software is supplied without any warranty or guaranteed support whatsoever. The Broad Institute is not responsible for its use, misuse, or functionality.%0A *%0A * This software is licensed under the terms of the GNU Lesser General Public License (LGPL),%0A * Version 2.1 which is available at http://www.opensource.org/licenses/lgpl-2.1.php.%0A */%0A%0A
 package 
@@ -942,115 +942,9 @@
 ert.
-assertEquals;%0Aimport static junit.framework.Assert.assertNotNull;%0Aimport static junit.framework.Assert.fail
+*
 ;%0A%0A/
@@ -1355,19 +1355,19 @@
 tListPer
+s
 on
-s
 alDirect

Fix typo in method name