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