package pl.itcrowd.youtrack.api.rest;
import junit.framework.Assert;
import org.apache.commons.io.input.ReaderInputStream;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.auth.AuthenticationException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpUriRequest;
import org.junit.Test;
import pl.itcrowd.youtrack.api.Command;
import pl.itcrowd.youtrack.api.Filter;
import pl.itcrowd.youtrack.api.IssueWrapper;
import pl.itcrowd.youtrack.api.YoutrackAPI;
import pl.itcrowd.youtrack.api.defaults.Fields;
import pl.itcrowd.youtrack.api.defaults.StateValues;
import pl.itcrowd.youtrack.api.exceptions.NoResultFoundException;
import pl.itcrowd.youtrack.api.exceptions.YoutrackErrorException;
import javax.xml.bind.JAXBException;
import java.io.IOException;
import java.io.StringReader;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* This test requires Youtrack instance with "Test(TST)" project to be running and expects following JVM params:
* youtrackLocation, youtrackUsername and youtrackPassword.
* There should be no assigner "wacek" for project TST.
* There should be following assigners for TST project: bernard,root.
* Ticket TST-2 should be deleted.
*/
public class YoutrackAPIIT {
@Test
public void command() throws IOException, JAXBException
{
final YoutrackAPI api = new YoutrackAPI(getServiceLocation(), getUsername(), getPassword());
final String issueId = "TST-1";
IssueWrapper issue;
try {
api.command(issueId, Command.assigneeCommand("wacek").toString());
Assert.fail("Command should fail");
} catch (YoutrackErrorException e) {
// There is no such assignee
Assert.assertEquals(HttpStatus.SC_BAD_REQUEST, e.getStatusCode());
}
api.command(issueId, Command.assigneeCommand("bernard").assignee("root"));
issue = api.getIssue(issueId);
Assert.assertNotNull(issue);
Assert.assertEquals("root", issue.getFieldValue(Fields.assignee));
api.command(issueId, Command.assigneeCommand("bernard"));
issue = api.getIssue(issueId);
Assert.assertNotNull(issue);
Assert.assertEquals("bernard", issue.getFieldValue(Fields.assignee));
api.command(issueId, Command.stateCommand(StateValues.InProgress));
issue = api.getIssue(issueId);
Assert.assertNotNull(issue);
Assert.assertEquals("In Progress", issue.getFieldValue(Fields.state));
api.command(issueId, Command.stateCommand(StateValues.InProgress).assignee("root"));
issue = api.getIssue(issueId);
Assert.assertNotNull(issue);
Assert.assertEquals("In Progress", issue.getFieldValue(Fields.state));
Assert.assertEquals("root", issue.getFieldValue(Fields.assignee));
api.command(issueId, Command.assigneeCommand("bernard").state(StateValues.New));
issue = api.getIssue(issueId);
Assert.assertNotNull(issue);
Assert.assertEquals("New", issue.getFieldValue(Fields.state));
Assert.assertEquals("bernard", issue.getFieldValue(Fields.assignee));
}
@Test
public void commandAllStates() throws IOException, JAXBException
{
final YoutrackAPI api = new YoutrackAPI(getServiceLocation(), getUsername(), getPassword());
final String issueId = "TST-1";
IssueWrapper issue;
final StateValues[] stateValueses = StateValues.values();
for (StateValues state : stateValueses) {
if (state.getCommandValue() != null && !StateValues.Duplicate.equals(state)) {
api.command(issueId, Command.stateCommand(state));
issue = api.getIssue(issueId);
Assert.assertNotNull(issue);
Assert.assertEquals(state.getCommandValue(), issue.getFieldValue(Fields.state));
}
}
}
@Test
public void createIssue() throws IOException, AuthenticationException, JAXBException
{
YoutrackAPI api = new YoutrackAPI(getServiceLocation(), getUsername(), getPassword());
final String issueId = api.createIssue("TST", "Test summary", "Test description");
Assert.assertNotNull(issueId);
Assert.assertTrue(issueId.startsWith("TST"));
}
@Test
public void getAssignees() throws IOException, AuthenticationException, JAXBException
{
YoutrackAPI api = new YoutrackAPI(getServiceLocation(), getUsername(), getPassword());
final AssigneeList assigneeList = api.getAssignees("TST");
Assert.assertNotNull(assigneeList);
Assert.assertEquals(2, assigneeList.getAssignees().getAssignees().size());
}
@Test
public void getBundle() throws Exception
{
// Given
final HttpResponse httpResponse = mock(HttpResponse.class);
final HttpEntity httpEntity = mock(HttpEntity.class);
final String response = "Show-stopperCriticalMajorNormalMinor";
when(httpEntity.getContent()).thenReturn(new ReaderInputStream(new StringReader(response)));
when(httpEntity.getContentLength()).thenReturn((long) response.getBytes().length);
when(httpResponse.getEntity()).thenReturn(httpEntity);
final StatusLine statusLine = mock(StatusLine.class);
when(statusLine.getStatusCode()).thenReturn(200);
when(httpResponse.getStatusLine()).thenReturn(statusLine);
final HttpClient httpClient = mock(HttpClient.class);
when(httpClient.execute(any(HttpUriRequest.class))).thenReturn(httpResponse);
final YoutrackAPI youtrackAPI = new YoutrackAPI("zonk", httpClient);
// When
final Enumeration priorities = youtrackAPI.getBundle("Priorities");
// Then
assertNotNull(priorities);
assertEquals("Priorities", priorities.getName());
assertNotNull(priorities.getValues());
assertEquals(5, priorities.getValues().size());
assertEquals("Show-stopper", priorities.getValues().get(0).getValue());
assertEquals(new Long(20), priorities.getValues().get(0).getColorIndex());
assertEquals("Critical", priorities.getValues().get(1).getValue());
assertEquals(new Long(19), priorities.getValues().get(1).getColorIndex());
assertEquals("Major", priorities.getValues().get(2).getValue());
assertEquals(new Long(18), priorities.getValues().get(2).getColorIndex());
assertEquals("Normal", priorities.getValues().get(3).getValue());
assertEquals(new Long(17), priorities.getValues().get(3).getColorIndex());
assertEquals("Minor", priorities.getValues().get(4).getValue());
assertEquals(new Long(16), priorities.getValues().get(4).getColorIndex());
}
@Test
public void getIndividualAssignees() throws IOException, JAXBException
{
final YoutrackAPI api = new YoutrackAPI(getServiceLocation(), getUsername(), getPassword());
final List assignees = api.getIndividualAssignees("TST");
Assert.assertNotNull(assignees);
Assert.assertEquals(2, assignees.size());
for (User user : assignees) {
Assert.assertTrue("bernard".equals(user.getLogin()) || "root".equals(user.getLogin()));
}
}
@Test
public void getIssue() throws IOException, AuthenticationException, JAXBException
{
YoutrackAPI api = new YoutrackAPI(getServiceLocation(), getUsername(), getPassword());
final IssueWrapper issue = api.getIssue("TST-1");
Assert.assertNotNull(issue);
Assert.assertEquals("1", issue.getFieldValue(Fields.numberInProject));
try {
api.getIssue("TST-2");
Assert.fail("YoutrackErrorException expected");
} catch (NoResultFoundException e) {
Assert.assertEquals("Issue not found.", e.getMessage());
}
try {
api.getIssue("TSTX-1");
Assert.fail("YoutrackErrorException expected");
} catch (NoResultFoundException e) {
Assert.assertEquals("Issue not found.", e.getMessage());
}
}
@Test(expected = YoutrackErrorException.class)
public void loginFailure() throws IOException, JAXBException
{
final String username = "someFakeLogin";
final String password = "someFakePassword";
try {
new YoutrackAPI(getServiceLocation(), username, password);
} catch (YoutrackErrorException e) {
Assert.assertEquals("Incorrect login or password.", e.getMessage());
Assert.assertEquals(HttpStatus.SC_FORBIDDEN, e.getStatusCode());
throw e;
}
}
@Test
public void loginSuccess() throws IOException, AuthenticationException, JAXBException
{
final String username = getUsername();
final String password = getPassword();
new YoutrackAPI(getServiceLocation(), username, password);
YoutrackAPI api = new YoutrackAPI(getServiceLocation());
api.login(username, password);
}
@Test
public void searchIssuesByProject() throws IOException, AuthenticationException, JAXBException
{
YoutrackAPI api = new YoutrackAPI(getServiceLocation(), getUsername(), getPassword());
List issues = api.searchIssuesByProject("TST", null);
Assert.assertTrue(!issues.isEmpty());
for (IssueWrapper issue : issues) {
String summary = issue.getFieldValue(Fields.summary);
Assert.assertNotNull(summary);
Assert.assertTrue(!"".equals(summary.trim()));
}
api.createIssue("TST", "searchIssuesByProject " + System.currentTimeMillis(), "searchIssuesByProject");
issues = api.searchIssuesByProject("TST", Filter.stateFilter(StateValues.Unresolved));
Assert.assertTrue(!issues.isEmpty());
}
private String getPassword()
{
return System.getProperty("youtrackPassword");
}
private String getServiceLocation()
{
return System.getProperty("youtrackLocation");
}
private String getUsername()
{
return System.getProperty("youtrackUsername");
}
}