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