View Javadoc

1   /*
2    * Copyright (C) 2012 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 com.atlassian.jira.rest.client.internal.json;
18  
19  import com.atlassian.jira.rest.client.api.domain.BasicPriority;
20  import com.atlassian.jira.rest.client.api.domain.BasicProject;
21  import com.atlassian.jira.rest.client.api.domain.CimFieldInfo;
22  import com.atlassian.jira.rest.client.api.domain.CimIssueType;
23  import com.atlassian.jira.rest.client.api.domain.CimProject;
24  import com.atlassian.jira.rest.client.api.domain.CustomFieldOption;
25  import com.atlassian.jira.rest.client.api.domain.EntityHelper;
26  import com.atlassian.jira.rest.client.api.domain.FieldSchema;
27  import com.atlassian.jira.rest.client.api.domain.IssueType;
28  import com.atlassian.jira.rest.client.api.domain.StandardOperation;
29  import com.google.common.collect.ImmutableMap;
30  import com.google.common.collect.Iterables;
31  import com.google.common.collect.Sets;
32  import org.codehaus.jettison.json.JSONException;
33  import org.hamcrest.Matchers;
34  import org.hamcrest.collection.IsIterableContainingInAnyOrder;
35  import org.junit.Assert;
36  import org.junit.Test;
37  import org.junit.matchers.JUnitMatchers;
38  
39  import java.util.Collections;
40  import java.util.Map;
41  
42  import static com.atlassian.jira.rest.client.TestUtil.toUri;
43  import static org.junit.Assert.assertEquals;
44  
45  /**
46   * @since v1.0
47   */
48  public class CreateIssueMetadataJsonParserTest {
49  
50      @Test
51      public void testParse() throws JSONException {
52          final CreateIssueMetadataJsonParser parser = new CreateIssueMetadataJsonParser();
53          final Iterable<CimProject> createMetaProjects = parser.parse(
54                  ResourceUtil.getJsonObjectFromResource("/json/createmeta/valid.json")
55          );
56  
57          Assert.assertEquals(4, Iterables.size(createMetaProjects));
58  
59          // test first project
60          final CimProject project = createMetaProjects.iterator().next();
61          assertEquals("http://localhost:2990/jira/rest/api/2/project/ANONEDIT", project.getSelf().toString());
62          assertEquals("ANONEDIT", project.getKey());
63          assertEquals("Anonymous Editable Project", project.getName());
64          Assert.assertEquals(ImmutableMap.of(
65                  "16x16", toUri("http://localhost:2990/jira/secure/projectavatar?size=small&pid=10030&avatarId=10011"),
66                  "48x48", toUri("http://localhost:2990/jira/secure/projectavatar?pid=10030&avatarId=10011")
67          ), project.getAvatarUris());
68  
69  
70          // check some issue types
71          Assert.assertThat(project.getIssueTypes(), IsIterableContainingInAnyOrder.containsInAnyOrder(
72                  new CimIssueType(toUri("http://localhost:2990/jira/rest/api/latest/issuetype/1"), 1L, "Bug", false,
73                          "A problem which impairs or prevents the functions of the product.", toUri("http://localhost:2990/jira/images/icons/bug.gif"),
74                          Collections.<String, CimFieldInfo>emptyMap()),
75                  new CimIssueType(toUri("http://localhost:2990/jira/rest/api/latest/issuetype/2"), 2L, "New Feature", false,
76                          "A new feature of the product, which has yet to be developed.", toUri("http://localhost:2990/jira/images/icons/newfeature.gif"),
77                          Collections.<String, CimFieldInfo>emptyMap()),
78                  new CimIssueType(toUri("http://localhost:2990/jira/rest/api/latest/issuetype/3"), 3L, "Task", false,
79                          "A task that needs to be done.", toUri("http://localhost:2990/jira/images/icons/task.gif"),
80                          Collections.<String, CimFieldInfo>emptyMap()),
81                  new CimIssueType(toUri("http://localhost:2990/jira/rest/api/latest/issuetype/4"), 4L, "Improvement", false,
82                          "An improvement or enhancement to an existing feature or task.", toUri("http://localhost:2990/jira/images/icons/improvement.gif"),
83                          Collections.<String, CimFieldInfo>emptyMap()),
84                  new CimIssueType(toUri("http://localhost:2990/jira/rest/api/latest/issuetype/5"), 5L, "Sub-task", true,
85                          "The sub-task of the issue", toUri("http://localhost:2990/jira/images/icons/issue_subtask.gif"),
86                          Collections.<String, CimFieldInfo>emptyMap())
87          ));
88      }
89  
90      @Test
91      public void testParseWithFieldsExpanded() throws JSONException {
92          final CreateIssueMetadataJsonParser parser = new CreateIssueMetadataJsonParser();
93          final Iterable<CimProject> createMetaProjects = parser.parse(
94                  ResourceUtil.getJsonObjectFromResource("/json/createmeta/valid-with-fields-expanded.json")
95          );
96  
97          Assert.assertEquals(4, Iterables.size(createMetaProjects));
98  
99          // get project with issue types expanded
100         final CimProject project = EntityHelper.findEntityByName(
101                 createMetaProjects, "Anonymous Editable Project"
102         );
103         Assert.assertNotNull(project);
104         Assert.assertEquals(5, Iterables.size(project.getIssueTypes()));
105 
106         // get issue type and check if fields was parsed successfully
107         final CimIssueType issueType = EntityHelper.findEntityByName(project.getIssueTypes(), "Bug");
108         final Map<String, CimFieldInfo> issueTypeFields = issueType.getFields();
109         Assert.assertEquals(19, issueTypeFields.size());
110 
111         // test system field "components"
112         final CimFieldInfo componentsFieldInfo = issueTypeFields.get("components");
113         final CimFieldInfo expectedComponentsFieldInfo = new CimFieldInfo(
114                 "components", false, "Component/s", new FieldSchema("array", "component", "components", null, null),
115                 Sets.newHashSet(StandardOperation.ADD, StandardOperation.REMOVE, StandardOperation.SET),
116                 Collections.emptyList(), null
117         );
118         Assert.assertEquals(expectedComponentsFieldInfo, componentsFieldInfo);
119 
120         // check custom field with allowed values
121         final CimFieldInfo cf1001 = issueTypeFields.get("customfield_10001");
122         assertEquals(new FieldSchema("string", null, null, "com.atlassian.jira.plugin.system.customfieldtypes:radiobuttons", 10001L), cf1001
123                 .getSchema());
124         Assert.assertEquals(3, Iterables.size(cf1001.getAllowedValues()));
125         Assert.assertThat(cf1001.getOperations(), IsIterableContainingInAnyOrder.containsInAnyOrder(StandardOperation.SET));
126 
127         // check allowed values types
128         assertAllowedValuesOfType(issueTypeFields.get("issuetype").getAllowedValues(), IssueType.class);
129         assertAllowedValuesOfType(issueTypeFields.get("priority").getAllowedValues(), BasicPriority.class);
130         assertAllowedValuesOfType(issueTypeFields.get("customfield_10001").getAllowedValues(), CustomFieldOption.class);
131         assertAllowedValuesOfType(issueTypeFields.get("project").getAllowedValues(), BasicProject.class);
132         assertAllowedValuesOfType(issueTypeFields.get("customfield_10010").getAllowedValues(), BasicProject.class);
133     }
134 
135     private void assertAllowedValuesOfType(final Iterable<Object> allowedValues, Class type) {
136         Assert.assertThat(allowedValues, JUnitMatchers.everyItem(Matchers.instanceOf(type)));
137     }
138 }