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  package com.atlassian.jira.rest.client.internal.json;
17  
18  import com.atlassian.jira.rest.client.api.domain.Field;
19  import com.atlassian.jira.rest.client.api.domain.FieldSchema;
20  import com.atlassian.jira.rest.client.api.domain.FieldType;
21  import org.codehaus.jettison.json.JSONException;
22  import org.hamcrest.Matchers;
23  import org.junit.Rule;
24  import org.junit.Test;
25  import org.junit.rules.ExpectedException;
26  
27  import static junit.framework.Assert.assertEquals;
28  import static org.junit.Assert.assertThat;
29  
30  public class FieldJsonParserTest {
31  
32      private static final FieldJsonParser fieldJsonParser = new FieldJsonParser();
33  
34      @Rule
35      public final ExpectedException expectedException = ExpectedException.none();
36  
37      @Test
38      public void testParseValidSingleField() throws JSONException {
39          final Field field = fieldJsonParser.parse(ResourceUtil.getJsonObjectFromResource("/json/field/valid-system-field.json"));
40          final FieldSchema schema = new FieldSchema("status", null, "status", null, null);
41          final Field expectedField = new Field("status", "Status", FieldType.JIRA, false, true, true, schema);
42          assertEquals(expectedField, field);
43      }
44  
45      @Test
46      public void testParseValidCustomField() throws JSONException {
47          final Field field = fieldJsonParser.parse(ResourceUtil.getJsonObjectFromResource("/json/field/valid-custom-field.json"));
48          final FieldSchema schema = new FieldSchema("array", "string", null, "com.atlassian.jira.plugin.system.customfieldtypes:multiselect", 10000l);
49          final Field expectedField = new Field("customfield_10000", "MultiSelect Custom IssueField", FieldType.CUSTOM, true, true, true, schema);
50          assertEquals(expectedField, field);
51      }
52  
53      @Test
54      public void testParseMultipleCustomFields() throws JSONException {
55          JsonArrayParser<Iterable<Field>> fieldsParser = FieldJsonParser.createFieldsArrayParser();
56          final Iterable<Field> fields = fieldsParser.parse(ResourceUtil.getJsonArrayFromResource("/json/field/valid-multiple-fields.json"));
57  
58          assertThat(fields, Matchers.hasItems(
59                  new Field("progress", "Progress", FieldType.JIRA, false, true, false,
60                          new FieldSchema("progress", null, "progress", null, null)),
61                  new Field("customfield_10000", "MultiSelect Custom IssueField", FieldType.CUSTOM, true, true, true,
62                          new FieldSchema("array", "string", null, "com.atlassian.jira.plugin.system.customfieldtypes:multiselect", 10000l)),
63                  new Field("thumbnail", "Images", FieldType.JIRA, false, true, false, null),
64                  new Field("issuekey", "Key", FieldType.JIRA, false, true, false, null),
65                  new Field("timetracking", "Time Tracking", FieldType.JIRA, true, false, true,
66                          new FieldSchema("timetracking", null, "timetracking", null, null)),
67                  new Field("components", "Component/s", FieldType.JIRA, true, true, true,
68                          new FieldSchema("array", "component", "components", null, null)),
69                  new Field("aggregatetimespent", "Σ Time Spent", FieldType.JIRA, false, true, false,
70                          new FieldSchema("number", null, "aggregatetimespent", null, null))
71          ));
72      }
73  
74      @Test
75      public void testParseFieldWithoutSomeFields() throws JSONException {
76          expectedException.expect(JSONException.class);
77          expectedException.expectMessage("JSONObject[\"orderable\"] not found.");
78          fieldJsonParser.parse(ResourceUtil.getJsonObjectFromResource("/json/field/invalid-field.json"));
79      }
80  
81  }