View Javadoc

1   /*
2    * Copyright (C) 2010 Atlassian
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package it;
18  
19  import com.atlassian.jira.nimblefunctests.annotation.JiraBuildNumberDependent;
20  import com.atlassian.jira.rest.client.IntegrationTestUtil;
21  import com.atlassian.jira.rest.client.TestUtil;
22  import com.atlassian.jira.rest.client.api.domain.BasicPriority;
23  import com.atlassian.jira.rest.client.api.domain.EntityHelper;
24  import com.atlassian.jira.rest.client.api.domain.Field;
25  import com.atlassian.jira.rest.client.api.domain.FieldSchema;
26  import com.atlassian.jira.rest.client.api.domain.FieldType;
27  import com.atlassian.jira.rest.client.api.domain.Issue;
28  import com.atlassian.jira.rest.client.api.domain.IssueType;
29  import com.atlassian.jira.rest.client.api.domain.IssuelinksType;
30  import com.atlassian.jira.rest.client.api.domain.Priority;
31  import com.atlassian.jira.rest.client.api.domain.Resolution;
32  import com.atlassian.jira.rest.client.api.domain.ServerInfo;
33  import com.atlassian.jira.rest.client.api.domain.Status;
34  import com.atlassian.jira.rest.client.api.domain.Transition;
35  import com.atlassian.jira.rest.client.api.domain.input.TransitionInput;
36  import com.atlassian.jira.rest.client.internal.json.TestConstants;
37  import com.atlassian.jira.rest.client.test.matchers.RegularExpressionMatcher;
38  import com.google.common.collect.Iterables;
39  import com.google.common.collect.Maps;
40  import org.hamcrest.Matchers;
41  import org.joda.time.DateTime;
42  import org.junit.Before;
43  import org.junit.Test;
44  
45  import javax.ws.rs.core.Response;
46  import java.util.Map;
47  
48  import static com.atlassian.jira.rest.client.internal.ServerVersionConstants.BN_JIRA_4_3;
49  import static org.hamcrest.Matchers.containsInAnyOrder;
50  import static org.hamcrest.Matchers.hasItems;
51  import static org.hamcrest.Matchers.is;
52  import static org.hamcrest.core.StringEndsWith.endsWith;
53  import static org.hamcrest.core.StringStartsWith.startsWith;
54  import static org.junit.Assert.assertEquals;
55  import static org.junit.Assert.assertNotNull;
56  import static org.junit.Assert.assertNull;
57  import static org.junit.Assert.assertThat;
58  import static org.junit.Assert.assertTrue;
59  
60  /**
61   * Those tests mustn't change anything on server side, as jira is restored only once
62   */
63  // Ignore "May produce NPE" warnings, as we know what we are doing in tests
64  @SuppressWarnings("ConstantConditions")
65  public class AsynchronousMetadataRestClientReadOnlyTest extends AbstractAsynchronousRestClientTest {
66  
67      private static boolean alreadyRestored;
68  
69      @Before
70      public void setup() {
71          if (!alreadyRestored) {
72              IntegrationTestUtil.restoreAppropriateJiraData(TestConstants.DEFAULT_JIRA_DUMP_FILE, administration);
73              alreadyRestored = true;
74          }
75      }
76  
77      @Test
78      public void testGetServerInfo() throws Exception {
79          final ServerInfo serverInfo = client.getMetadataClient().getServerInfo().claim();
80          assertEquals("Your Company JIRA", serverInfo.getServerTitle());
81          assertTrue(serverInfo.getBuildDate().isBeforeNow());
82          assertTrue(serverInfo.getServerTime().isAfter(new DateTime().minusMinutes(5)));
83          assertTrue(serverInfo.getServerTime().isBefore(new DateTime().plusMinutes(5)));
84      }
85  
86      @Test
87      public void testGetIssueTypeNonExisting() throws Exception {
88          final IssueType issueType = client.getIssueClient().getIssue("TST-1").claim().getIssueType();
89          TestUtil.assertErrorCode(Response.Status.NOT_FOUND, new Runnable() {
90              @Override
91              public void run() {
92                  client.getMetadataClient().getIssueType(TestUtil.toUri(issueType.getSelf() + "fake")).claim();
93              }
94          });
95      }
96  
97      @Test
98      public void testGetIssueType() {
99          final IssueType getIssueIssueType = client.getIssueClient().getIssue("TST-1").claim().getIssueType();
100         final IssueType issueType = client.getMetadataClient().getIssueType(getIssueIssueType.getSelf()).claim();
101         assertEquals("Bug", issueType.getName());
102         assertEquals("A problem which impairs or prevents the functions of the product.", issueType.getDescription());
103         Long expectedId = isJira5xOrNewer() ? 1L : null;
104         assertEquals(expectedId, issueType.getId());
105         assertThat(issueType.getIconUri().toString(), Matchers.anyOf(
106                 endsWith("bug.png"),
107                 endsWith("bug.gif"),
108                 endsWith("viewavatar?size=xsmall&avatarId=10163&avatarType=issuetype")));
109     }
110 
111     @JiraBuildNumberDependent(BN_JIRA_4_3)
112     @Test
113     public void testGetIssueTypes() {
114         final Iterable<IssuelinksType> issueTypes = client.getMetadataClient().getIssueLinkTypes().claim();
115         assertEquals(1, Iterables.size(issueTypes));
116         final IssuelinksType issueType = Iterables.getOnlyElement(issueTypes);
117         assertEquals("Duplicate", issueType.getName());
118         assertEquals("is duplicated by", issueType.getInward());
119         assertEquals("duplicates", issueType.getOutward());
120     }
121 
122     @Test
123     public void testGetStatuses() {
124         final Iterable<Status> statuses = client.getMetadataClient().getStatuses().claim();
125         final Map<String, Status> statusMap = Maps.uniqueIndex(statuses, EntityHelper.GET_ENTITY_NAME_FUNCTION);
126         assertThat(statusMap.keySet(), containsInAnyOrder("Open", "In Progress", "Reopened", "Resolved", "Closed"));
127 
128         final Status status = statusMap.get("Open");
129         assertThat(status.getSelf().toString(), Matchers.endsWith("status/1"));
130         assertThat(status.getId(), is(1L));
131         assertThat(status.getName(), is("Open"));
132         assertThat(status.getDescription(), is("The issue is open and ready for the assignee to start work on it."));
133         assertThat(status.getIconUrl().toString(), RegularExpressionMatcher.matchesRegexp(".*open\\.(png|gif)$"));
134     }
135 
136     @Test
137     public void testGetStatus() {
138         final Status basicStatus = client.getIssueClient().getIssue("TST-1").claim().getStatus();
139         final Status status = client.getMetadataClient().getStatus(basicStatus.getSelf()).claim();
140         assertEquals("The issue is open and ready for the assignee to start work on it.", status.getDescription());
141         assertThat(status.getIconUrl().toString(), Matchers.anyOf(endsWith("status_open.gif"), endsWith("open.png")));
142         assertEquals("Open", status.getName());
143     }
144 
145     @Test
146     public void testGetStatusNonExisting() throws Exception {
147         final Status status = client.getIssueClient().getIssue("TST-1").claim().getStatus();
148         TestUtil.assertErrorCode(Response.Status.NOT_FOUND, "The status with id '" +
149                 TestUtil.getLastPathSegment(status.getSelf()) + "fake" +
150                 "' does not exist", new Runnable() {
151             @Override
152             public void run() {
153                 client.getMetadataClient().getStatus(TestUtil.toUri(status.getSelf() + "fake")).claim();
154             }
155         });
156     }
157 
158     @Test
159     public void testGetPriority() {
160         final BasicPriority basicPriority = client.getIssueClient().getIssue("TST-2").claim().getPriority();
161         final Priority priority = client.getMetadataClient().getPriority(basicPriority.getSelf()).claim();
162         assertEquals(basicPriority.getSelf(), priority.getSelf());
163         assertEquals("Major", priority.getName());
164         assertEquals("#009900", priority.getStatusColor());
165         assertEquals("Major loss of function.", priority.getDescription());
166         final Long expectedId = isJira5xOrNewer() ? 3L : null;
167         assertEquals(expectedId, priority.getId());
168         assertThat(priority.getIconUri().toString(), startsWith(jiraUri.toString()));
169         assertThat(priority.getIconUri().toString(),
170                 Matchers.anyOf(
171                         endsWith("images/icons/priority_major.gif"),
172                         endsWith("images/icons/priorities/major.png"),
173                         endsWith("images/icons/priorities/major.svg"))
174         );
175     }
176 
177     @Test
178     public void testGetResolution() {
179         final Issue issue = client.getIssueClient().getIssue("TST-2").claim();
180         assertNull(issue.getResolution());
181         final Iterable<Transition> transitions = client.getIssueClient().getTransitions(issue).claim();
182         final Transition resolveTransition = TestUtil.getTransitionByName(transitions, "Resolve Issue");
183 
184         client.getIssueClient().transition(issue, new TransitionInput(resolveTransition.getId())).claim();
185 
186         final Issue resolvedIssue = client.getIssueClient().getIssue("TST-2").claim();
187         final Resolution basicResolution = resolvedIssue.getResolution();
188         assertNotNull(basicResolution);
189 
190         final Resolution resolution = client.getMetadataClient().getResolution(basicResolution.getSelf()).claim();
191         assertEquals(basicResolution.getName(), resolution.getName());
192         assertEquals(basicResolution.getSelf(), resolution.getSelf());
193         assertEquals("A fix for this issue is checked into the tree and tested.", resolution.getDescription());
194     }
195 
196     @Test
197     public void testGetAllFieldsAtOnce() {
198 
199         // the declared schema of "votes" field has been corrected in JIRA 7.1
200         Field votesField = isJira7_1_OrNewer() ?
201                 new Field("votes", "Votes", FieldType.JIRA, false, true, false, new FieldSchema("votes", null, "votes", null, null)) :
202                 new Field("votes", "Votes", FieldType.JIRA, false, true, false, new FieldSchema("array", "votes", "votes", null, null));
203 
204         final Iterable<Field> fields = client.getMetadataClient().getFields().claim();
205         assertThat(fields, hasItems(
206                 new Field("progress", "Progress", FieldType.JIRA, false, true, false,
207                         new FieldSchema("progress", null, "progress", null, null)),
208                 new Field("summary", "Summary", FieldType.JIRA, true, true, true,
209                         new FieldSchema("string", null, "summary", null, null)),
210                 new Field("timetracking", "Time Tracking", FieldType.JIRA, true, false, true,
211                         new FieldSchema("timetracking", null, "timetracking", null, null)),
212                 new Field("issuekey", "Key", FieldType.JIRA, false, true, false, null),
213                 new Field("issuetype", "Issue Type", FieldType.JIRA, true, true, true,
214                         new FieldSchema("issuetype", null, "issuetype", null, null)),
215                 votesField,
216                 new Field("components", "Component/s", FieldType.JIRA, true, true, true,
217                         new FieldSchema("array", "component", "components", null, null)),
218                 new Field("aggregatetimespent", "Σ Time Spent", FieldType.JIRA, false, true, false,
219                         new FieldSchema("number", null, "aggregatetimespent", null, null)),
220                 new Field("thumbnail", "Images", FieldType.JIRA, false, true, false, null),
221                 new Field("customfield_10000", "My Number Field New", FieldType.CUSTOM, true, true, true,
222                         new FieldSchema("number", null, null, "com.atlassian.jira.plugin.system.customfieldtypes:float", 10000l)),
223                 new Field("customfield_10011", "project2", FieldType.CUSTOM, true, true, true,
224                         new FieldSchema("string", null, null, "com.atlassian.jira.plugin.system.customfieldtypes:textarea", 10011l)),
225                 new Field("workratio", "Work Ratio", FieldType.JIRA, false, true, true,
226                         new FieldSchema("number", null, "workratio", null, null))
227         ));
228     }
229 }