Blame view

src/test/java/pl/itcrowd/youtrack/api/rest/YoutrackAPITest.java 10.5 KB
bernard authored
1
package pl.itcrowd.youtrack.api.rest;
bernard authored
2 3

import junit.framework.Assert;
bernard authored
4 5 6
import org.apache.commons.io.input.ReaderInputStream;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
bernard authored
7
import org.apache.http.HttpStatus;
bernard authored
8
import org.apache.http.StatusLine;
bernard authored
9
import org.apache.http.auth.AuthenticationException;
bernard authored
10 11
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpUriRequest;
bernard authored
12
import org.junit.Test;
bernard authored
13 14 15 16 17 18 19 20
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;
bernard authored
21 22 23

import javax.xml.bind.JAXBException;
import java.io.IOException;
bernard authored
24
import java.io.StringReader;
bernard authored
25 26
import java.util.List;
bernard authored
27 28 29 30 31 32
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;
bernard authored
33
/**
bernard authored
34 35
 * This test requires Youtrack instance with "Test(TST)" project to be running and expects following JVM params:
 * youtrackLocation, youtrackUsername and youtrackPassword.
bernard authored
36 37 38
 * 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.
bernard authored
39 40 41 42
 */
public class YoutrackAPITest {
// -------------------------- OTHER METHODS --------------------------
bernard authored
43
    @Test
bernard authored
44
    public void command() throws IOException, JAXBException
bernard authored
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
    {
        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
bernard authored
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
    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
bernard authored
102
    public void createIssue() throws IOException, AuthenticationException, JAXBException
bernard authored
103 104 105 106 107 108 109 110
    {
        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
bernard authored
111 112 113 114 115 116 117 118 119
    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
bernard authored
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
    public void getBundle() throws Exception
    {
//        Given
        final HttpResponse httpResponse = mock(HttpResponse.class);
        final HttpEntity httpEntity = mock(HttpEntity.class);
        final String response = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><enumeration name=\"Priorities\"><value colorIndex=\"20\">Show-stopper</value><value colorIndex=\"19\">Critical</value><value colorIndex=\"18\">Major</value><value colorIndex=\"17\">Normal</value><value colorIndex=\"16\">Minor</value></enumeration>";
        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
bernard authored
157 158 159 160 161 162 163 164 165 166 167 168
    public void getIndividualAssignees() throws IOException, JAXBException
    {
        final YoutrackAPI api = new YoutrackAPI(getServiceLocation(), getUsername(), getPassword());
        final List<User> 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
bernard authored
169
    public void getIssue() throws IOException, AuthenticationException, JAXBException
bernard authored
170 171 172 173
    {
        YoutrackAPI api = new YoutrackAPI(getServiceLocation(), getUsername(), getPassword());
        final IssueWrapper issue = api.getIssue("TST-1");
        Assert.assertNotNull(issue);
bernard authored
174
        Assert.assertEquals("1", issue.getFieldValue(Fields.numberInProject));
bernard authored
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190

        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)
bernard authored
191
    public void loginFailure() throws IOException, JAXBException
bernard authored
192
    {
bernard authored
193 194
        final String username = "someFakeLogin";
        final String password = "someFakePassword";
bernard authored
195 196 197 198 199 200 201
        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;
        }
bernard authored
202 203 204
    }

    @Test
bernard authored
205
    public void loginSuccess() throws IOException, AuthenticationException, JAXBException
bernard authored
206
    {
bernard authored
207 208
        final String username = getUsername();
        final String password = getPassword();
bernard authored
209 210
        new YoutrackAPI(getServiceLocation(), username, password);
        YoutrackAPI api = new YoutrackAPI(getServiceLocation());
bernard authored
211 212 213 214
        api.login(username, password);
    }

    @Test
bernard authored
215
    public void searchIssuesByProject() throws IOException, AuthenticationException, JAXBException
bernard authored
216
    {
bernard authored
217 218
        YoutrackAPI api = new YoutrackAPI(getServiceLocation(), getUsername(), getPassword());
        List<IssueWrapper> issues = api.searchIssuesByProject("TST", null);
bernard authored
219
        Assert.assertTrue(!issues.isEmpty());
bernard authored
220
        for (IssueWrapper issue : issues) {
bernard authored
221
            String summary = issue.getFieldValue(Fields.summary);
bernard authored
222 223 224
            Assert.assertNotNull(summary);
            Assert.assertTrue(!"".equals(summary.trim()));
        }
bernard authored
225 226 227
        api.createIssue("TST", "searchIssuesByProject " + System.currentTimeMillis(), "searchIssuesByProject");
        issues = api.searchIssuesByProject("TST", Filter.stateFilter(StateValues.Unresolved));
        Assert.assertTrue(!issues.isEmpty());
bernard authored
228 229
    }
bernard authored
230 231
    private String getPassword()
    {
bernard authored
232 233 234
        return System.getProperty("youtrackPassword");
    }
bernard authored
235 236 237 238 239
    private String getServiceLocation()
    {
        return System.getProperty("youtrackLocation");
    }
bernard authored
240 241
    private String getUsername()
    {
bernard authored
242 243 244
        return System.getProperty("youtrackUsername");
    }
}