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.gen;
18  
19  import com.atlassian.jira.rest.client.api.domain.input.ComplexIssueInputFieldValue;
20  import com.atlassian.jira.rest.client.api.domain.input.FieldInput;
21  import com.atlassian.jira.rest.client.api.domain.input.IssueInput;
22  import com.atlassian.jira.rest.client.api.domain.input.PropertyInput;
23  import com.atlassian.jira.rest.client.internal.json.ResourceUtil;
24  import com.atlassian.jira.rest.client.test.matchers.JSONObjectMatcher;
25  import com.google.common.collect.ImmutableMap;
26  import com.google.common.collect.Maps;
27  import org.codehaus.jettison.json.JSONObject;
28  import org.junit.Assert;
29  import org.junit.Test;
30  
31  import java.util.ArrayList;
32  
33  /**
34   * @since v1.0
35   */
36  public class IssueInputJsonGeneratorTest {
37  
38      @Test
39      public void testGenerate() throws Exception {
40          final IssueInputJsonGenerator generator = new IssueInputJsonGenerator();
41          final IssueInput issueInput = IssueInput.createWithFields(
42                  new FieldInput("string", "String value"),
43                  new FieldInput("integer", 1),
44                  new FieldInput("long", 1L),
45                  new FieldInput("complex", new ComplexIssueInputFieldValue(ImmutableMap.<String, Object>of(
46                          "string", "string",
47                          "integer", 1,
48                          "long", 1L,
49                          "complex", ComplexIssueInputFieldValue.with("test", "id")
50                  )))
51          );
52  
53          issueInput.getProperties().add(new PropertyInput("testKey", "{\"testValue\" : \"foo\"}"));
54  
55          final JSONObject expected = ResourceUtil.getJsonObjectFromResource("/json/issueInput/valid.json");
56          final JSONObject actual = generator.generate(issueInput);
57          Assert.assertThat(expected, JSONObjectMatcher.isEqual(actual));
58      }
59  
60      @Test
61      public void testGenerateWithEmptyInput() throws Exception {
62          final IssueInputJsonGenerator generator = new IssueInputJsonGenerator();
63          final IssueInput issueInput = new IssueInput(Maps.<String, FieldInput>newHashMap(), new ArrayList<PropertyInput>());
64  
65          final JSONObject expected = ResourceUtil.getJsonObjectFromResource("/json/issueInput/empty.json");
66          final JSONObject actual = generator.generate(issueInput);
67          Assert.assertThat(expected, JSONObjectMatcher.isEqual(actual));
68      }
69  
70      @Test
71      public void testGenerateWithNullInput() throws Exception {
72          final IssueInputJsonGenerator generator = new IssueInputJsonGenerator();
73          final IssueInput issueInput = null;
74  
75          final JSONObject expected = ResourceUtil.getJsonObjectFromResource("/json/issueInput/empty.json");
76          final JSONObject actual = generator.generate(issueInput);
77          Assert.assertThat(expected, JSONObjectMatcher.isEqual(actual));
78      }
79  }