YoutrackAPITest.java
10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
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 YoutrackAPITest {
// -------------------------- OTHER METHODS --------------------------
@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 = "<?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
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
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<IssueWrapper> 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");
}
}