1   /*
2    * Copyright (C) 2010 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.OptionalIterable;
20  import com.atlassian.jira.rest.client.TestUtil;
21  import com.atlassian.jira.rest.client.domain.BasicProjectRole;
22  import com.atlassian.jira.rest.client.domain.IssueType;
23  import com.atlassian.jira.rest.client.domain.Project;
24  import com.google.common.collect.Iterables;
25  import org.codehaus.jettison.json.JSONException;
26  import org.hamcrest.collection.IsEmptyIterable;
27  import org.joda.time.DateMidnight;
28  import org.junit.Test;
29  
30  import java.net.URISyntaxException;
31  
32  import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder;
33  import static org.junit.Assert.*;
34  
35  // Ignore "May produce NPE" warnings, as we know what we are doing in tests
36  @SuppressWarnings("ConstantConditions")
37  public class ProjectJsonParserTest {
38  	private final ProjectJsonParser parser = new ProjectJsonParser();
39  
40  	@Test
41  	public void testParse() throws Exception {
42  		final Project project = parser.parse(ResourceUtil.getJsonObjectFromResource("/json/project/valid.json"));
43  		assertEquals(TestUtil.toUri("http://localhost:8090/jira/rest/api/latest/project/TST"), project.getSelf());
44  		assertEquals("This is my description here.\r\nAnother line.", project.getDescription());
45  		assertEquals(TestConstants.USER_ADMIN, project.getLead());
46  		assertEquals("http://example.com", project.getUri().toString());
47  		assertEquals("TST", project.getKey());
48  		assertThat(project.getVersions(), containsInAnyOrder(TestConstants.VERSION_1, TestConstants.VERSION_1_1));
49  		assertThat(project.getComponents(), containsInAnyOrder(TestConstants.BCOMPONENT_A, TestConstants.BCOMPONENT_B));
50  		assertNull(project.getName());
51  		final OptionalIterable<IssueType> issueTypes = project.getIssueTypes();
52  		assertFalse(issueTypes.isSupported());
53  		assertThat(issueTypes, IsEmptyIterable.<IssueType>emptyIterable());
54  	}
55  
56  	@Test
57  	public void testParseProjectWithNoUrl() throws JSONException {
58  		final Project project = parser.parse(ResourceUtil.getJsonObjectFromResource("/json/project/project-no-url.json"));
59  		assertEquals("MYT", project.getKey());
60  		assertNull(project.getUri());
61  		assertNull(project.getDescription());
62  	}
63  
64  	@Test
65  	public void testParseProjectInJira4x4() throws JSONException, URISyntaxException {
66  		final Project project = parser.parse(ResourceUtil.getJsonObjectFromResource("/json/project/project-jira-4-4.json"));
67  		assertEquals("TST", project.getKey()); //2010-08-25
68  		assertEquals(new DateMidnight(2010, 8, 25).toInstant(), Iterables.getLast(project.getVersions()).getReleaseDate().toInstant());
69          assertEquals("Test Project", project.getName());
70  		final OptionalIterable<IssueType> issueTypes = project.getIssueTypes();
71  		assertTrue(issueTypes.isSupported());
72  		assertThat(issueTypes, containsInAnyOrder(
73  				new IssueType(TestUtil.toUri("http://localhost:2990/jira/rest/api/latest/issueType/1"), null, "Bug", false, null, null),
74  				new IssueType(TestUtil.toUri("http://localhost:2990/jira/rest/api/latest/issueType/2"), null, "New Feature", false, null, null),
75  				new IssueType(TestUtil.toUri("http://localhost:2990/jira/rest/api/latest/issueType/3"), null, "Task", false, null, null),
76  				new IssueType(TestUtil.toUri("http://localhost:2990/jira/rest/api/latest/issueType/4"), null, "Improvement", false, null, null),
77  				new IssueType(TestUtil.toUri("http://localhost:2990/jira/rest/api/latest/issueType/5"), null, "Sub-task", true, null, null)
78  		));
79  	}
80  
81  	@Test
82  	public void testParseProjectInJira5x0() throws JSONException, URISyntaxException {
83  		final Project project = parser.parse(ResourceUtil.getJsonObjectFromResource("/json/project/project-jira-5-0.json"));
84  		assertEquals("TST", project.getKey());
85  		assertEquals(new DateMidnight(2010, 8, 25).toInstant(), Iterables.getLast(project.getVersions()).getReleaseDate().toInstant());
86  		assertEquals("Test Project", project.getName());
87  		final OptionalIterable<IssueType> issueTypes = project.getIssueTypes();
88  		assertTrue(issueTypes.isSupported());
89  		assertThat(issueTypes, containsInAnyOrder(
90  				new IssueType(TestUtil.toUri("http://localhost:2990/jira/rest/api/latest/issuetype/1"), 1L, "Bug", false, "A problem which impairs or prevents the functions of the product.", TestUtil.toUri("http://localhost:2990/jira/images/icons/bug.gif")),
91  				new IssueType(TestUtil.toUri("http://localhost:2990/jira/rest/api/latest/issuetype/2"), 2L, "New Feature", false, "A new feature of the product, which has yet to be developed.", TestUtil.toUri("http://localhost:2990/jira/images/icons/newfeature.gif")),
92  				new IssueType(TestUtil.toUri("http://localhost:2990/jira/rest/api/latest/issuetype/3"), 3L, "Task", false, "A task that needs to be done.", TestUtil.toUri("http://localhost:2990/jira/images/icons/task.gif")),
93  				new IssueType(TestUtil.toUri("http://localhost:2990/jira/rest/api/latest/issuetype/4"), 4L, "Improvement", false, "An improvement or enhancement to an existing feature or task.", TestUtil.toUri("http://localhost:2990/jira/images/icons/improvement.gif")),
94  				new IssueType(TestUtil.toUri("http://localhost:2990/jira/rest/api/latest/issuetype/5"), 5L, "Sub-task", true, "The sub-task of the issue", TestUtil.toUri("http://localhost:2990/jira/images/icons/issue_subtask.gif"))
95  		));
96  	}
97  
98  	@Test
99  	public void testParseProjectWithBasicRoles() throws JSONException, URISyntaxException {
100 		final Project project = parser.parse(ResourceUtil.getJsonObjectFromResource("/json/project/project-jira-5-0.json"));
101 		final Iterable<BasicProjectRole> projectRoles = project.getProjectRoles();
102 		assertThat(projectRoles, containsInAnyOrder(
103 				new BasicProjectRole(TestUtil.toUri("http://localhost:2990/jira/rest/api/latest/project/TST/role/10000"), "Users"),
104 				new BasicProjectRole(TestUtil.toUri("http://localhost:2990/jira/rest/api/latest/project/TST/role/10001"), "Developers"),
105 				new BasicProjectRole(TestUtil.toUri("http://localhost:2990/jira/rest/api/latest/project/TST/role/10002"), "Administrators")
106 		));
107 	}
108 }