org.eclipse.orion.server.tests.servlets.git.GitResetTest.java Source code

Java tutorial

Introduction

Here is the source code for org.eclipse.orion.server.tests.servlets.git.GitResetTest.java

Source

/*******************************************************************************
 * Copyright (c) 2011 IBM Corporation and others
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 * IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.orion.server.tests.servlets.git;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URI;
import java.util.List;

import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.ResetCommand.ResetType;
import org.eclipse.jgit.lib.ConfigConstants;
import org.eclipse.jgit.lib.StoredConfig;
import org.eclipse.orion.internal.server.servlets.ProtocolConstants;
import org.eclipse.orion.server.git.GitConstants;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Ignore;
import org.junit.Test;

import com.meterware.httpunit.PostMethodWebRequest;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;

public class GitResetTest extends GitTest {

    // modified + add = changed, changed + reset = modified
    @Test
    public void testResetChanged() throws Exception {
        URI workspaceLocation = createWorkspace(getMethodName());

        String projectName = getMethodName();
        JSONObject project = createProjectOrLink(workspaceLocation, projectName, gitDir.toString());
        String projectId = project.getString(ProtocolConstants.KEY_ID);

        WebRequest request = getPutFileRequest(projectId + "/test.txt", "hello");
        WebResponse response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

        // TODO: don't create URIs out of thin air
        request = getPutFileRequest(projectId + "/folder/folder.txt", "hello");
        response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

        JSONObject gitSection = project.getJSONObject(GitConstants.KEY_GIT);
        String gitIndexUri = gitSection.getString(GitConstants.KEY_INDEX);
        String gitStatusUri = gitSection.getString(GitConstants.KEY_STATUS);

        // TODO: don't create URIs out of thin air
        request = GitAddTest.getPutGitIndexRequest(gitIndexUri + "test.txt");
        response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

        // TODO: don't create URIs out of thin air
        request = GitAddTest.getPutGitIndexRequest(gitIndexUri + "folder/folder.txt");
        response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

        request = GitStatusTest.getGetGitStatusRequest(gitStatusUri);
        response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
        JSONObject statusResponse = new JSONObject(response.getText());
        JSONArray statusArray = statusResponse.getJSONArray(GitConstants.KEY_STATUS_ADDED);
        assertEquals(0, statusArray.length());
        statusArray = statusResponse.getJSONArray(GitConstants.KEY_STATUS_CHANGED);
        assertEquals(2, statusArray.length());
        statusArray = statusResponse.getJSONArray(GitConstants.KEY_STATUS_MISSING);
        assertEquals(0, statusArray.length());
        statusArray = statusResponse.getJSONArray(GitConstants.KEY_STATUS_MODIFIED);
        assertEquals(0, statusArray.length());
        statusArray = statusResponse.getJSONArray(GitConstants.KEY_STATUS_REMOVED);
        assertEquals(0, statusArray.length());
        statusArray = statusResponse.getJSONArray(GitConstants.KEY_STATUS_UNTRACKED);
        assertEquals(0, statusArray.length());

        // TODO: don't create URIs out of thin air
        request = getPostGitIndexRequest(gitIndexUri + "test.txt", null, null, (String) null);
        response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

        request = GitStatusTest.getGetGitStatusRequest(gitStatusUri);
        response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
        statusResponse = new JSONObject(response.getText());
        statusArray = statusResponse.getJSONArray(GitConstants.KEY_STATUS_ADDED);
        assertEquals(0, statusArray.length());
        statusArray = statusResponse.getJSONArray(GitConstants.KEY_STATUS_CHANGED);
        assertEquals(1, statusArray.length());
        statusArray = statusResponse.getJSONArray(GitConstants.KEY_STATUS_MISSING);
        assertEquals(0, statusArray.length());
        statusArray = statusResponse.getJSONArray(GitConstants.KEY_STATUS_MODIFIED);
        assertEquals(1, statusArray.length());
        statusArray = statusResponse.getJSONArray(GitConstants.KEY_STATUS_REMOVED);
        assertEquals(0, statusArray.length());
        statusArray = statusResponse.getJSONArray(GitConstants.KEY_STATUS_UNTRACKED);
        assertEquals(0, statusArray.length());
    }

    @Test
    public void testResetChangedWithPath() throws Exception {
        URI workspaceLocation = createWorkspace(getMethodName());

        String projectName = getMethodName();
        JSONObject project = createProjectOrLink(workspaceLocation, projectName, gitDir.toString());
        String projectId = project.getString(ProtocolConstants.KEY_ID);

        WebRequest request = getPutFileRequest(projectId + "/test.txt", "hello");
        WebResponse response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

        // TODO: don't create URIs out of thin air
        request = getPutFileRequest(projectId + "/folder/folder.txt", "hello");
        response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

        JSONObject gitSection = project.getJSONObject(GitConstants.KEY_GIT);
        String gitIndexUri = gitSection.getString(GitConstants.KEY_INDEX);
        String gitStatusUri = gitSection.getString(GitConstants.KEY_STATUS);

        // TODO: don't create URIs out of thin air
        request = GitAddTest.getPutGitIndexRequest(gitIndexUri + "test.txt");
        response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

        // TODO: don't create URIs out of thin air
        request = GitAddTest.getPutGitIndexRequest(gitIndexUri + "folder/folder.txt");
        response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

        request = GitStatusTest.getGetGitStatusRequest(gitStatusUri);
        response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
        JSONObject statusResponse = new JSONObject(response.getText());
        JSONArray statusArray = statusResponse.getJSONArray(GitConstants.KEY_STATUS_ADDED);
        assertEquals(0, statusArray.length());
        statusArray = statusResponse.getJSONArray(GitConstants.KEY_STATUS_CHANGED);
        assertEquals(2, statusArray.length());
        statusArray = statusResponse.getJSONArray(GitConstants.KEY_STATUS_MISSING);
        assertEquals(0, statusArray.length());
        statusArray = statusResponse.getJSONArray(GitConstants.KEY_STATUS_MODIFIED);
        assertEquals(0, statusArray.length());
        statusArray = statusResponse.getJSONArray(GitConstants.KEY_STATUS_REMOVED);
        assertEquals(0, statusArray.length());
        statusArray = statusResponse.getJSONArray(GitConstants.KEY_STATUS_UNTRACKED);
        assertEquals(0, statusArray.length());

        // TODO: don't create URIs out of thin air
        request = getPostGitIndexRequest(gitIndexUri, new String[] { "test.txt" }, null, (String) null);
        response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

        request = GitStatusTest.getGetGitStatusRequest(gitStatusUri);
        response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
        statusResponse = new JSONObject(response.getText());
        statusArray = statusResponse.getJSONArray(GitConstants.KEY_STATUS_ADDED);
        assertEquals(0, statusArray.length());
        statusArray = statusResponse.getJSONArray(GitConstants.KEY_STATUS_CHANGED);
        assertEquals(1, statusArray.length());
        statusArray = statusResponse.getJSONArray(GitConstants.KEY_STATUS_MISSING);
        assertEquals(0, statusArray.length());
        statusArray = statusResponse.getJSONArray(GitConstants.KEY_STATUS_MODIFIED);
        assertEquals(1, statusArray.length());
        statusArray = statusResponse.getJSONArray(GitConstants.KEY_STATUS_REMOVED);
        assertEquals(0, statusArray.length());
        statusArray = statusResponse.getJSONArray(GitConstants.KEY_STATUS_UNTRACKED);
        assertEquals(0, statusArray.length());
    }

    @Test
    public void testResetNull() throws Exception {
        URI workspaceLocation = createWorkspace(getMethodName());

        String projectName = getMethodName();
        JSONObject project = createProjectOrLink(workspaceLocation, projectName, gitDir.toString());
        String projectId = project.getString(ProtocolConstants.KEY_ID);

        WebRequest request = getPutFileRequest(projectId + "/test.txt", "hello");
        WebResponse response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

        JSONObject gitSection = project.getJSONObject(GitConstants.KEY_GIT);
        String gitIndexUri = gitSection.getString(GitConstants.KEY_INDEX);

        request = getPostGitIndexRequest(gitIndexUri, null, null, (String) null);
        response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_BAD_REQUEST, response.getResponseCode());
    }

    @Test
    public void testResetNotImplemented() throws Exception {
        URI workspaceLocation = createWorkspace(getMethodName());

        String projectName = getMethodName();
        JSONObject project = createProjectOrLink(workspaceLocation, projectName, gitDir.toString());
        String projectId = project.getString(ProtocolConstants.KEY_ID);

        WebRequest request = getPutFileRequest(projectId + "/test.txt", "hello");
        WebResponse response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

        JSONObject gitSection = project.optJSONObject(GitConstants.KEY_GIT);
        assertNotNull(gitSection);

        String gitIndexUri = gitSection.getString(GitConstants.KEY_INDEX);

        request = getPostGitIndexRequest(gitIndexUri, ResetType.KEEP);
        response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_NOT_IMPLEMENTED, response.getResponseCode());

        request = getPostGitIndexRequest(gitIndexUri, ResetType.MERGE);
        response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_NOT_IMPLEMENTED, response.getResponseCode());

        request = getPostGitIndexRequest(gitIndexUri, ResetType.SOFT);
        response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_NOT_IMPLEMENTED, response.getResponseCode());
    }

    @Test
    public void testResetBadType() throws Exception {
        URI workspaceLocation = createWorkspace(getMethodName());

        String projectName = getMethodName();
        JSONObject project = createProjectOrLink(workspaceLocation, projectName, gitDir.toString());
        String projectId = project.getString(ProtocolConstants.KEY_ID);

        WebRequest request = getPutFileRequest(projectId + "/test.txt", "hello");
        WebResponse response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

        JSONObject gitSection = project.optJSONObject(GitConstants.KEY_GIT);
        assertNotNull(gitSection);

        String gitIndexUri = gitSection.getString(GitConstants.KEY_INDEX);

        request = getPostGitIndexRequest(gitIndexUri, null, null, "BAD");
        response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_BAD_REQUEST, response.getResponseCode());
    }

    @Test
    public void testResetMixedAll() throws Exception {
        URI workspaceLocation = createWorkspace(getMethodName());

        String projectName = getMethodName();
        JSONObject project = createProjectOrLink(workspaceLocation, projectName, gitDir.toString());
        String projectId = project.getString(ProtocolConstants.KEY_ID);

        WebRequest request = getPutFileRequest(projectId + "/test.txt", "hello");
        WebResponse response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

        String fileName = "new.txt";
        request = getPostFilesRequest(projectId + "/", getNewFileJSON(fileName).toString(), fileName);
        response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_CREATED, response.getResponseCode());

        // TODO: don't create URIs out of thin air
        request = getDeleteFilesRequest(projectId + "/folder/folder.txt");
        response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

        JSONObject gitSection = project.getJSONObject(GitConstants.KEY_GIT);
        String gitIndexUri = gitSection.getString(GitConstants.KEY_INDEX);
        String gitStatusUri = gitSection.getString(GitConstants.KEY_STATUS);

        // "git status"
        request = GitStatusTest.getGetGitStatusRequest(gitStatusUri);
        response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
        JSONObject statusResponse = new JSONObject(response.getText());
        JSONArray statusArray = statusResponse.getJSONArray(GitConstants.KEY_STATUS_ADDED);
        assertEquals(0, statusArray.length());
        statusArray = statusResponse.getJSONArray(GitConstants.KEY_STATUS_CHANGED);
        assertEquals(0, statusArray.length());
        statusArray = statusResponse.getJSONArray(GitConstants.KEY_STATUS_MISSING);
        assertEquals(1, statusArray.length());
        statusArray = statusResponse.getJSONArray(GitConstants.KEY_STATUS_MODIFIED);
        assertEquals(1, statusArray.length());
        statusArray = statusResponse.getJSONArray(GitConstants.KEY_STATUS_REMOVED);
        assertEquals(0, statusArray.length());
        statusArray = statusResponse.getJSONArray(GitConstants.KEY_STATUS_UNTRACKED);
        assertEquals(1, statusArray.length());

        // "git add ."
        request = GitAddTest.getPutGitIndexRequest(gitIndexUri /* add all */);
        response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

        // "git status"
        request = GitStatusTest.getGetGitStatusRequest(gitStatusUri);
        response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
        statusResponse = new JSONObject(response.getText());
        statusArray = statusResponse.getJSONArray(GitConstants.KEY_STATUS_ADDED);
        assertEquals(1, statusArray.length());
        statusArray = statusResponse.getJSONArray(GitConstants.KEY_STATUS_CHANGED);
        assertEquals(1, statusArray.length());
        statusArray = statusResponse.getJSONArray(GitConstants.KEY_STATUS_MISSING);
        assertEquals(0, statusArray.length());
        statusArray = statusResponse.getJSONArray(GitConstants.KEY_STATUS_MODIFIED);
        assertEquals(0, statusArray.length());
        statusArray = statusResponse.getJSONArray(GitConstants.KEY_STATUS_REMOVED);
        assertEquals(1, statusArray.length());
        statusArray = statusResponse.getJSONArray(GitConstants.KEY_STATUS_UNTRACKED);
        assertEquals(0, statusArray.length());

        // "git reset --mixed HEAD"
        request = getPostGitIndexRequest(gitIndexUri /* reset all */, ResetType.MIXED);
        response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

        // "git status", should be the same result as called for the first time
        request = GitStatusTest.getGetGitStatusRequest(gitStatusUri);
        response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
        statusResponse = new JSONObject(response.getText());
        statusArray = statusResponse.getJSONArray(GitConstants.KEY_STATUS_ADDED);
        assertEquals(0, statusArray.length());
        statusArray = statusResponse.getJSONArray(GitConstants.KEY_STATUS_CHANGED);
        assertEquals(0, statusArray.length());
        statusArray = statusResponse.getJSONArray(GitConstants.KEY_STATUS_MISSING);
        assertEquals(1, statusArray.length());
        statusArray = statusResponse.getJSONArray(GitConstants.KEY_STATUS_MODIFIED);
        assertEquals(1, statusArray.length());
        statusArray = statusResponse.getJSONArray(GitConstants.KEY_STATUS_REMOVED);
        assertEquals(0, statusArray.length());
        statusArray = statusResponse.getJSONArray(GitConstants.KEY_STATUS_UNTRACKED);
        assertEquals(1, statusArray.length());
    }

    @Test
    public void testResetHardAll() throws Exception {
        URI workspaceLocation = createWorkspace(getMethodName());

        String projectName = getMethodName();
        JSONObject project = createProjectOrLink(workspaceLocation, projectName, gitDir.toString());
        String projectId = project.getString(ProtocolConstants.KEY_ID);

        WebRequest request = getPutFileRequest(projectId + "/test.txt", "hello");
        WebResponse response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

        String fileName = "new.txt";
        request = getPostFilesRequest(projectId + "/", getNewFileJSON(fileName).toString(), fileName);
        response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_CREATED, response.getResponseCode());

        // TODO: don't create URIs out of thin air
        request = getDeleteFilesRequest(projectId + "/folder/folder.txt");
        response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

        JSONObject gitSection = project.getJSONObject(GitConstants.KEY_GIT);
        String gitIndexUri = gitSection.getString(GitConstants.KEY_INDEX);
        String gitStatusUri = gitSection.getString(GitConstants.KEY_STATUS);

        // "git status"
        request = GitStatusTest.getGetGitStatusRequest(gitStatusUri);
        response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
        JSONObject statusResponse = new JSONObject(response.getText());
        JSONArray statusArray = statusResponse.getJSONArray(GitConstants.KEY_STATUS_ADDED);
        assertEquals(0, statusArray.length());
        statusArray = statusResponse.getJSONArray(GitConstants.KEY_STATUS_CHANGED);
        assertEquals(0, statusArray.length());
        statusArray = statusResponse.getJSONArray(GitConstants.KEY_STATUS_MISSING);
        assertEquals(1, statusArray.length());
        statusArray = statusResponse.getJSONArray(GitConstants.KEY_STATUS_MODIFIED);
        assertEquals(1, statusArray.length());
        statusArray = statusResponse.getJSONArray(GitConstants.KEY_STATUS_REMOVED);
        assertEquals(0, statusArray.length());
        statusArray = statusResponse.getJSONArray(GitConstants.KEY_STATUS_UNTRACKED);
        assertEquals(1, statusArray.length());

        // "git add ."
        request = GitAddTest.getPutGitIndexRequest(gitIndexUri /* add all */);
        response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

        // "git status"
        request = GitStatusTest.getGetGitStatusRequest(gitStatusUri);
        response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
        statusResponse = new JSONObject(response.getText());
        statusArray = statusResponse.getJSONArray(GitConstants.KEY_STATUS_ADDED);
        assertEquals(1, statusArray.length());
        statusArray = statusResponse.getJSONArray(GitConstants.KEY_STATUS_CHANGED);
        assertEquals(1, statusArray.length());
        statusArray = statusResponse.getJSONArray(GitConstants.KEY_STATUS_MISSING);
        assertEquals(0, statusArray.length());
        statusArray = statusResponse.getJSONArray(GitConstants.KEY_STATUS_MODIFIED);
        assertEquals(0, statusArray.length());
        statusArray = statusResponse.getJSONArray(GitConstants.KEY_STATUS_REMOVED);
        assertEquals(1, statusArray.length());
        statusArray = statusResponse.getJSONArray(GitConstants.KEY_STATUS_UNTRACKED);
        assertEquals(0, statusArray.length());

        // "git reset --hard HEAD"
        request = getPostGitIndexRequest(gitIndexUri /* reset all */, ResetType.HARD);
        response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

        // "git status", should be clean, nothing to commit
        request = GitStatusTest.getGetGitStatusRequest(gitStatusUri);
        response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
        statusResponse = new JSONObject(response.getText());
        GitStatusTest.assertStatusClean(statusResponse);
    }

    @Test
    @Ignore("see bug 339397")
    public void testResetAutocrlfTrue() throws Exception {

        // "git config core.autocrlf true"
        Git git = new Git(db);
        StoredConfig config = git.getRepository().getConfig();
        config.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null, ConfigConstants.CONFIG_KEY_AUTOCRLF,
                Boolean.TRUE);
        config.save();

        URI workspaceLocation = createWorkspace(getMethodName());

        String projectName = getMethodName();
        JSONObject project = createProjectOrLink(workspaceLocation, projectName, gitDir.toString());
        String projectId = project.getString(ProtocolConstants.KEY_ID);

        JSONObject gitSection = project.getJSONObject(GitConstants.KEY_GIT);
        String gitIndexUri = gitSection.getString(GitConstants.KEY_INDEX);
        String gitStatusUri = gitSection.getString(GitConstants.KEY_STATUS);
        String gitCommitUri = gitSection.getString(GitConstants.KEY_COMMIT);

        // CRLF
        // TODO: don't create URIs out of thin air
        WebRequest request = getPutFileRequest(projectId + "/test.txt", "f" + "\r\n" + "older");
        WebResponse response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

        // "git add {path}"
        // TODO: don't create URIs out of thin air
        request = GitAddTest.getPutGitIndexRequest(gitIndexUri + "test.txt");
        response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

        // commit
        request = GitCommitTest.getPostGitCommitRequest(gitCommitUri, "added new line - crlf", false);
        response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

        // assert there is nothing to commit
        request = GitStatusTest.getGetGitStatusRequest(gitStatusUri);
        response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
        JSONObject statusResponse = new JSONObject(response.getText());
        GitStatusTest.assertStatusClean(statusResponse);

        // create new file
        String fileName = "new.txt";
        request = getPostFilesRequest(projectId + "/", getNewFileJSON(fileName).toString(), fileName);
        response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_CREATED, response.getResponseCode());
        JSONObject file = new JSONObject(response.getText());
        String location = file.optString(ProtocolConstants.KEY_LOCATION, null);
        assertNotNull(location);

        // LF
        request = getPutFileRequest(location, "i'm" + "\n" + "new");
        response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

        // "git add ."
        request = GitAddTest.getPutGitIndexRequest(gitIndexUri /* stage all */);
        response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

        // reset
        request = getPostGitIndexRequest(gitIndexUri /* reset all */, ResetType.MIXED);
        response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

        request = GitStatusTest.getGetGitStatusRequest(gitStatusUri);
        response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
        statusResponse = new JSONObject(response.getText());
        JSONArray statusArray = statusResponse.getJSONArray(GitConstants.KEY_STATUS_ADDED);
        assertEquals(0, statusArray.length());
        statusArray = statusResponse.getJSONArray(GitConstants.KEY_STATUS_CHANGED);
        assertEquals(0, statusArray.length());
        statusArray = statusResponse.getJSONArray(GitConstants.KEY_STATUS_MISSING);
        assertEquals(0, statusArray.length());
        statusArray = statusResponse.getJSONArray(GitConstants.KEY_STATUS_MODIFIED);
        assertEquals(0, statusArray.length());
        statusArray = statusResponse.getJSONArray(GitConstants.KEY_STATUS_REMOVED);
        assertEquals(0, statusArray.length());
        statusArray = statusResponse.getJSONArray(GitConstants.KEY_STATUS_UNTRACKED);
        assertEquals(1, statusArray.length());
    }

    @Test
    public void testResetToRemoteBranch() throws Exception {
        URI workspaceLocation = createWorkspace(getMethodName());
        JSONObject projectTop = createProjectOrLink(workspaceLocation, getMethodName() + "-top", null);
        IPath clonePathTop = new Path("file").append(projectTop.getString(ProtocolConstants.KEY_ID)).makeAbsolute();

        JSONObject projectFolder = createProjectOrLink(workspaceLocation, getMethodName() + "-folder", null);
        IPath clonePathFolder = new Path("file").append(projectFolder.getString(ProtocolConstants.KEY_ID))
                .append("folder").makeAbsolute();

        IPath[] clonePaths = new IPath[] { clonePathTop, clonePathFolder };

        for (IPath clonePath : clonePaths) {
            // clone a  repo
            JSONObject clone = clone(clonePath);
            String cloneContentLocation = clone.getString(ProtocolConstants.KEY_CONTENT_LOCATION);

            // get project/folder metadata
            WebRequest request = getGetFilesRequest(cloneContentLocation);
            WebResponse response = webConversation.getResponse(request);
            assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
            JSONObject folder = new JSONObject(response.getText());
            String folderChildrenLocation = folder.getString(ProtocolConstants.KEY_CHILDREN_LOCATION);

            request = getGetFilesRequest(folderChildrenLocation);
            response = webConversation.getResponse(request);
            assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
            List<JSONObject> children = getDirectoryChildren(new JSONObject(response.getText()));
            JSONObject testTxt = getChildByName(children, "test.txt");
            String testTxtLocation = testTxt.getString(ProtocolConstants.KEY_LOCATION);
            JSONObject testTxtGitSection = testTxt.getJSONObject(GitConstants.KEY_GIT);
            String testTxtGitIndexUri = testTxtGitSection.getString(GitConstants.KEY_INDEX);
            String testTxtGitHeadUri = testTxtGitSection.getString(GitConstants.KEY_HEAD);

            request = getPutFileRequest(testTxtLocation, "file change");
            response = webConversation.getResponse(request);
            assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

            request = GitAddTest.getPutGitIndexRequest(testTxtGitIndexUri);
            response = webConversation.getResponse(request);
            assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

            request = GitCommitTest.getPostGitCommitRequest(testTxtGitHeadUri, "message", false);
            response = webConversation.getResponse(request);
            assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

            // git section for the folder
            JSONObject folderGitSection = folder.getJSONObject(GitConstants.KEY_GIT);
            String folderGitIndexUri = folderGitSection.getString(GitConstants.KEY_INDEX);
            String folderGitStatusUri = folderGitSection.getString(GitConstants.KEY_STATUS);

            request = GitStatusTest.getGetGitStatusRequest(folderGitStatusUri);
            response = webConversation.getResponse(request);
            assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
            JSONObject statusResponse = new JSONObject(response.getText());
            GitStatusTest.assertStatusClean(statusResponse);

            JSONArray commitsArray = log(testTxtGitHeadUri, false);
            assertEquals(2, commitsArray.length());

            // TODO: get "origin/master" from the remote branch 
            request = getPostGitIndexRequest(folderGitIndexUri, "origin/master", ResetType.HARD);
            response = webConversation.getResponse(request);
            assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

            request = GitStatusTest.getGetGitStatusRequest(folderGitStatusUri);
            response = webConversation.getResponse(request);
            assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
            statusResponse = new JSONObject(response.getText());
            GitStatusTest.assertStatusClean(statusResponse);

            commitsArray = log(testTxtGitHeadUri, false);
            assertEquals(1, commitsArray.length());
        }
    }

    @Test
    public void testResetPathsAndRef() throws Exception {
        URI workspaceLocation = createWorkspace(getMethodName());

        String projectName = getMethodName();
        JSONObject project = createProjectOrLink(workspaceLocation, projectName, gitDir.toString());
        String projectId = project.getString(ProtocolConstants.KEY_ID);

        WebRequest request = getPutFileRequest(projectId + "/test.txt", "hello");
        WebResponse response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

        JSONObject gitSection = project.optJSONObject(GitConstants.KEY_GIT);
        assertNotNull(gitSection);

        String gitIndexUri = gitSection.getString(GitConstants.KEY_INDEX);

        request = getPostGitIndexRequest(gitIndexUri, new String[] { "test.txt" }, "origin/master", null);
        response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_BAD_REQUEST, response.getResponseCode());
    }

    /**
     * Creates a request to reset HEAD to the given commit.
     * @param location
     * @param resetType 
     * @throws JSONException 
     * @throws UnsupportedEncodingException 
     */
    private WebRequest getPostGitIndexRequest(String location, String commit, ResetType resetType)
            throws JSONException, UnsupportedEncodingException {
        return getPostGitIndexRequest(location, null, commit, resetType.toString());
    }

    /**
     * Creates a request to reset index.
     * @param location
     * @param resetType 
     * @throws JSONException 
     * @throws UnsupportedEncodingException 
     */
    static WebRequest getPostGitIndexRequest(String location, ResetType resetType)
            throws JSONException, UnsupportedEncodingException {
        return getPostGitIndexRequest(location, null, null, resetType.toString());
    }

    private static WebRequest getPostGitIndexRequest(String location, String[] paths, String commit,
            String resetType) throws JSONException, UnsupportedEncodingException {
        String requestURI;
        if (location.startsWith("http://"))
            requestURI = location;
        else if (location.startsWith("/"))
            requestURI = SERVER_LOCATION + location;
        else
            requestURI = SERVER_LOCATION + GIT_SERVLET_LOCATION + GitConstants.INDEX_RESOURCE + location;

        JSONObject body = new JSONObject();
        if (resetType != null)
            body.put(GitConstants.KEY_RESET_TYPE, resetType);
        if (paths != null) {
            //         assertNull("Cannot mix paths and commit", commit);
            JSONArray jsonPaths = new JSONArray();
            for (String path : paths)
                jsonPaths.put(path);
            body.put(ProtocolConstants.KEY_PATH, jsonPaths);
        }
        if (commit != null)
            body.put(GitConstants.KEY_TAG_COMMIT, commit);
        WebRequest request = new PostMethodWebRequest(requestURI, getJsonAsStream(body.toString()), "UTF-8");
        request.setHeaderField(ProtocolConstants.HEADER_ORION_VERSION, "1");
        setAuthentication(request);
        return request;
    }
}