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.TestUtil;
20  import com.atlassian.jira.rest.client.domain.Project;
21  import com.google.common.collect.Iterables;
22  import org.codehaus.jettison.json.JSONException;
23  import org.joda.time.DateMidnight;
24  import org.junit.Test;
25  
26  import static com.atlassian.jira.rest.client.IterableMatcher.hasOnlyElements;
27  import static org.junit.Assert.*;
28  
29  public class ProjectJsonParserTest {
30  	private final ProjectJsonParser parser = new ProjectJsonParser();
31  
32  	@Test
33  	public void testParse() throws Exception {
34  		final Project project = parser.parse(ResourceUtil.getJsonObjectFromResource("/json/project/valid.json"));
35  		assertEquals(TestUtil.toUri("http://localhost:8090/jira/rest/api/latest/project/TST"), project.getSelf());
36  		assertEquals("This is my description here.\r\nAnother line.", project.getDescription());
37  		assertEquals(TestConstants.USER_ADMIN, project.getLead());
38  		assertEquals("http://example.com", project.getUri().toString());
39  		assertEquals("TST", project.getKey());
40  		assertThat(project.getVersions(), hasOnlyElements(TestConstants.VERSION_1, TestConstants.VERSION_1_1));
41  		assertThat(project.getComponents(), hasOnlyElements(TestConstants.BCOMPONENT_A, TestConstants.BCOMPONENT_B));
42          assertNull(project.getName());
43  	}
44  
45  	@Test
46  	public void testParseProjectWithNoUrl() throws JSONException {
47  		final Project project = parser.parse(ResourceUtil.getJsonObjectFromResource("/json/project/project-no-url.json"));
48  		assertEquals("MYT", project.getKey());
49  		assertNull(project.getUri());
50  		assertNull(project.getDescription());
51  	}
52  
53  	@Test
54  	public void testParseProjectInJira4x4() throws JSONException {
55  		final Project project = parser.parse(ResourceUtil.getJsonObjectFromResource("/json/project/project-jira-4-4.json"));
56  		assertEquals("TST", project.getKey()); //2010-08-25
57  		assertEquals(new DateMidnight(2010, 8, 25).toInstant(), Iterables.getLast(project.getVersions()).getReleaseDate().toInstant());
58          assertEquals("Test Project", project.getName());
59  	}
60  }