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.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   * @since v1.0
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  		// test first project
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  		// check some issue types
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  		// get project with issue types expanded
97  		final CimProject project = EntityHelper.findEntityByName(
98  				createMetaProjects, "Anonymous Editable Project"
99  		);
100 		assertNotNull(project);
101 		assertEquals(5, Iterables.size(project.getIssueTypes()));
102 
103 		// get issue type and check if fields was parsed successfully
104 		final CimIssueType issueType = EntityHelper.findEntityByName(project.getIssueTypes(), "Bug");
105 		final Map<String,CimFieldInfo> issueTypeFields = issueType.getFields();
106 		assertEquals(19, issueTypeFields.size());
107 
108 		// test system field "components"
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 		// check custom field with allowed values
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 		// check allowed values types
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 }