/*
 * Copyright 2000-2013 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jetbrains.plugins.github;

import com.intellij.mock.MockProgressIndicator;
import com.intellij.notification.NotificationType;
import com.intellij.openapi.progress.ProgressIndicator;
import org.jetbrains.plugins.github.util.GithubAuthData;
import org.jetbrains.plugins.github.util.GithubAuthDataHolder;

import java.util.Collections;
import java.util.List;

import static org.jetbrains.plugins.github.api.GithubGist.FileContent;

/**
 * @author Aleksey Pivovarov
 */
public class GithubCreateGistTest extends GithubCreateGistTestBase {
  private final ProgressIndicator myIndicator = new MockProgressIndicator();

  public void testSimple() throws Throwable {
    List<FileContent> expected = createContent();

    String url = GithubCreateGistAction.createGist(myProject, getAuthDataHolder(), myIndicator, expected, true, GIST_DESCRIPTION, null);
    assertNotNull(url);
    GIST_ID = url.substring(url.lastIndexOf('/') + 1);

    checkGistExists();
    checkGistNotAnonymous();
    checkGistPrivate();
    checkGistDescription(GIST_DESCRIPTION);
    checkGistContent(expected);
  }

  public void testAnonymous() throws Throwable {
    List<FileContent> expected = createContent();

    String url = GithubCreateGistAction
      .createGist(myProject, new GithubAuthDataHolder(GithubAuthData.createAnonymous(myHost)), myIndicator, expected, true,
                  GIST_DESCRIPTION, null);
    assertNotNull(url);
    GIST_ID = url.substring(url.lastIndexOf('/') + 1);

    checkGistExists();
    checkGistAnonymous();
    checkGistPrivate();
    checkGistDescription(GIST_DESCRIPTION);
    checkGistContent(expected);

    // anonymous gists - undeletable
    GIST_ID = null;
    GIST = null;
  }

  public void testUnusedFilenameField() throws Throwable {
    List<FileContent> expected = createContent();

    String url =
      GithubCreateGistAction.createGist(myProject, getAuthDataHolder(), myIndicator, expected, true, GIST_DESCRIPTION, "filename");
    assertNotNull(url);
    GIST_ID = url.substring(url.lastIndexOf('/') + 1);

    checkGistExists();
    checkGistNotAnonymous();
    checkGistPrivate();
    checkGistDescription(GIST_DESCRIPTION);
    checkGistContent(expected);
  }

  public void testUsedFilenameField() throws Throwable {
    List<FileContent> content = Collections.singletonList(new FileContent("file.txt", "file.txt content"));
    List<FileContent> expected = Collections.singletonList(new FileContent("filename", "file.txt content"));

    String url =
      GithubCreateGistAction.createGist(myProject, getAuthDataHolder(), myIndicator, content, true, GIST_DESCRIPTION, "filename");
    assertNotNull(url);
    GIST_ID = url.substring(url.lastIndexOf('/') + 1);

    checkGistExists();
    checkGistNotAnonymous();
    checkGistPrivate();
    checkGistDescription(GIST_DESCRIPTION);
    checkGistContent(expected);
  }

  public void testPublic() throws Throwable {
    List<FileContent> expected = createContent();

    String url = GithubCreateGistAction.createGist(myProject, getAuthDataHolder(), myIndicator, expected, false, GIST_DESCRIPTION, null);
    assertNotNull(url);
    GIST_ID = url.substring(url.lastIndexOf('/') + 1);

    checkGistExists();
    checkGistNotAnonymous();
    checkGistPublic();
    checkGistDescription(GIST_DESCRIPTION);
    checkGistContent(expected);
  }

  public void testEmpty() throws Throwable {
    List<FileContent> expected = Collections.emptyList();

    String url = GithubCreateGistAction.createGist(myProject, getAuthDataHolder(), myIndicator, expected, true, GIST_DESCRIPTION, null);
    assertNull("Gist was created", url);

    checkNotification(NotificationType.WARNING, "Can't create Gist", "Can't create empty gist");
  }

  public void testWrongLogin() throws Throwable {
    registerCancelingLoginDialogHandler();

    List<FileContent> expected = createContent();

    GithubAuthData auth = myGitHubSettings.getAuthData();
    GithubAuthData myAuth = GithubAuthData.createBasicAuth(auth.getHost(), myLogin1 + "some_suffix", myPassword);
    String url =
      GithubCreateGistAction.createGist(myProject, new GithubAuthDataHolder(myAuth), myIndicator, expected, true, GIST_DESCRIPTION, null);
    assertNull("Gist was created", url);

    checkNotification(NotificationType.ERROR, "Can't create Gist", null);
  }

  public void testWrongPassword() throws Throwable {
    registerCancelingLoginDialogHandler();

    List<FileContent> expected = createContent();

    GithubAuthData auth = myGitHubSettings.getAuthData();
    GithubAuthData myAuth = GithubAuthData.createBasicAuth(auth.getHost(), myLogin1, myPassword + "some_suffix");
    String url =
      GithubCreateGistAction.createGist(myProject, new GithubAuthDataHolder(myAuth), myIndicator, expected, true, GIST_DESCRIPTION, null);
    assertNull("Gist was created", url);

    checkNotification(NotificationType.ERROR, "Can't create Gist", null);
  }


}

@@ -4320,1200 +4320,7 @@
 %0A  %7D
-%0A%0A  public void testWrongLogin() throws Throwable %7B%0A    registerCancelingLoginDialogHandler();%0A%0A    List%3CFileContent%3E expected = createContent();%0A%0A    GithubAuthData auth = myGitHubSettings.getAuthData();%0A    GithubAuthData myAuth = GithubAuthData.createBasicAuth(auth.getHost(), myLogin1 + %22some_suffix%22, myPassword);%0A    String url =%0A      GithubCreateGistAction.createGist(myProject, new GithubAuthDataHolder(myAuth), myIndicator, expected, true, GIST_DESCRIPTION, null);%0A    assertNull(%22Gist was created%22, url);%0A%0A    checkNotification(NotificationType.ERROR, %22Can't create Gist%22, null);%0A  %7D%0A%0A  public void testWrongPassword() throws Throwable %7B%0A    registerCancelingLoginDialogHandler();%0A%0A    List%3CFileContent%3E expected = createContent();%0A%0A    GithubAuthData auth = myGitHubSettings.getAuthData();%0A    GithubAuthData myAuth = GithubAuthData.createBasicAuth(auth.getHost(), myLogin1, myPassword + %22some_suffix%22);%0A    String url =%0A      GithubCreateGistAction.createGist(myProject, new GithubAuthDataHolder(myAuth), myIndicator, expected, true, GIST_DESCRIPTION, null);%0A    assertNull(%22Gist was created%22, url);%0A%0A    checkNotification(NotificationType.ERROR, %22Can't create Gist%22, null);%0A  %7D%0A%0A
 %0A%7D%0A

remove test