/*
 * 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 com.intellij.tasks.integration;

import com.intellij.tasks.Task;
import com.intellij.tasks.TaskManagerTestCase;
import com.intellij.tasks.TaskState;
import com.intellij.tasks.jira.JiraRepository;
import com.intellij.tasks.jira.JiraRepositoryType;

/**
 * @author Dmitry Avdeev
 *         Date: 1/15/13
 */
public class JiraIntegrationTest extends TaskManagerTestCase {

  private JiraRepository myRepository;

  public void testGerman() throws Exception {
    myRepository.setUsername("german");
    myRepository.setPassword("german");
    Task[] issues = myRepository.getIssues(null, 50, 0);
    assertEquals(3, issues.length);
    assertEquals(TaskState.OPEN, issues[0].getState());
    assertFalse(issues[0].isClosed());
  }

  public void testLogin() throws Exception {
    myRepository.setUsername("german");
    myRepository.setUsername("wrong password");
    Exception exception = myRepository.createCancellableConnection().call();
    assertNotNull(exception);
    assertEquals(JiraRepository.LOGIN_FAILED_CHECK_YOUR_PERMISSIONS, exception.getMessage());
  }

  /**
   * JIRA 5.0.6, REST API 2.0
   */
  public void testVersionDiscovery1() throws Exception {
    myRepository.setUrl("http://trackers-tests.labs.intellij.net:8015");
    myRepository.setUsername("deva");
    myRepository.setPassword("deva");
    assertEquals("2.0", myRepository.discoverRestApiVersion().getVersionName());
  }

  /**
   * JIRA 4.4.5, REST API 2.0.alpha1
   */
  public void testVersionDiscovery2() throws Exception {
    myRepository.setUrl("http://trackers-tests.labs.intellij.net:8014");
    myRepository.setUsername("deva");
    myRepository.setPassword("deva");
    assertEquals("2.0.alpha1", myRepository.discoverRestApiVersion().getVersionName());
  }

  public void testJqlQuery() throws Exception {
    myRepository.setUsername("deva");
    myRepository.setPassword("deva");
    myRepository.setSearchQuery("assignee = currentUser() AND summary ~ 'animal'");
    assertEquals(1, myRepository.getIssues("", 50, 0).length);
  }

  /**
   * Holds only for JIRA > 5.x.x
   */
  public void testExtractedErrorMessage() throws Exception {
    myRepository.setUsername("deva");
    myRepository.setPassword("deva");
    myRepository.setSearchQuery("foo < bar");
    try {
      myRepository.getIssues("", 50, 0);
      fail();
    }
    catch (Exception e) {
      assertEquals("Search failed. Reason: \"Field 'foo' does not exist or you do not have permission to view it.\"", e.getMessage());
    }
  }

  public void testEmptyQuerySelectsAllIssues() throws Exception {
    myRepository.setUsername("deva");
    myRepository.setPassword("deva");
    myRepository.setSearchQuery("");
    assertEquals(13, myRepository.getIssues("", 50, 0).length);
  }

  @Override
  public void setUp() throws Exception {
    super.setUp();
    myRepository = new JiraRepository(new JiraRepositoryType());
    myRepository.setUrl("http://trackers-tests.labs.intellij.net:8015");
  }
}

@@ -1864,36 +1864,41 @@
 ry.setUsername(%22
-deva
+buildtest
 %22);%0A    myReposi
@@ -1907,36 +1907,41 @@
 ry.setPassword(%22
-deva
+buildtest
 %22);%0A    assertEq
@@ -2215,36 +2215,41 @@
 ry.setUsername(%22
-deva
+buildtest
 %22);%0A    myReposi
@@ -2258,36 +2258,41 @@
 ry.setPassword(%22
-deva
+buildtest
 %22);%0A    assertEq
@@ -2442,36 +2442,41 @@
 ry.setUsername(%22
-deva
+buildtest
 %22);%0A    myReposi
@@ -2485,36 +2485,41 @@
 ry.setPassword(%22
-deva
+buildtest
 %22);%0A    myReposi
@@ -2568,16 +2568,17 @@
 r() AND 
+(
 summary 
@@ -2584,15 +2584,35 @@
  ~ '
-animal'
+foo' or resolution = Fixed)
 %22);%0A
@@ -2628,17 +2628,17 @@
 tEquals(
-1
+2
 , myRepo
@@ -2807,36 +2807,41 @@
 ry.setUsername(%22
-deva
+buildtest
 %22);%0A    myReposi
@@ -2850,36 +2850,41 @@
 ry.setPassword(%22
-deva
+buildtest
 %22);%0A    myReposi
@@ -3145,24 +3145,24 @@
 Message());%0A
+
     %7D%0A  %7D%0A%0A 
@@ -3164,256 +3164,8 @@
  %7D%0A%0A
-  public void testEmptyQuerySelectsAllIssues() throws Exception %7B%0A    myRepository.setUsername(%22deva%22);%0A    myRepository.setPassword(%22deva%22);%0A    myRepository.setSearchQuery(%22%22);%0A    assertEquals(13, myRepository.getIssues(%22%22, 50, 0).length);%0A  %7D%0A%0A
   @O

Update test and JQL query to use 'buildtest' as current user and not depend on issues not assigned to it