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.DateTimeMatcher;
20  import com.atlassian.jira.rest.client.domain.Version;
21  import org.codehaus.jettison.json.JSONException;
22  import org.joda.time.DateTime;
23  import org.joda.time.DateTimeZone;
24  import org.junit.Assert;
25  import org.junit.Test;
26  
27  import java.net.URI;
28  import java.net.URISyntaxException;
29  
30  import static org.junit.Assert.assertEquals;
31  
32  public class VersionJsonParserTest {
33  
34  	@Test
35  	public void testParse() throws JSONException, URISyntaxException {
36  		VersionJsonParser parser = new VersionJsonParser();
37  		final Version version = parser.parse(ResourceUtil.getJsonObjectFromResource("/json/version/valid.json"));
38  		
39  		assertEquals(new URI("http://localhost:8090/jira/rest/api/latest/version/10000"), version.getSelf());
40  		assertEquals("1.1", version.getName());
41  		assertEquals("Some version", version.getDescription());
42  		Assert.assertFalse(version.isReleased());
43  		Assert.assertTrue(version.isArchived());
44  		Assert.assertThat(version.getReleaseDate(), DateTimeMatcher.isEqual(
45  				new DateTime(2010, 8, 25, 0, 0, 0, 0, DateTimeZone.forOffsetHours(2))));
46  	}
47  
48  	@Test
49  	public void testParse2() throws JSONException, URISyntaxException {
50  		VersionJsonParser parser = new VersionJsonParser();
51  		final Version version = parser.parse(ResourceUtil.getJsonObjectFromResource("/json/version/valid2-no-releaseDate.json"));
52  
53  		assertEquals(new URI("http://localhost:8090/jira/rest/api/latest/version/10000"), version.getSelf());
54  		assertEquals("1.1abc", version.getName());
55  		Assert.assertNull(version.getDescription());
56  		Assert.assertTrue(version.isReleased());
57  		Assert.assertFalse(version.isArchived());
58  		Assert.assertNull(version.getReleaseDate());
59  	}
60  
61  	@Test
62  	public void testParseNoDescription() throws JSONException, URISyntaxException {
63  		VersionJsonParser parser = new VersionJsonParser();
64  		final Version version = parser.parse(ResourceUtil.getJsonObjectFromResource("/json/version/valid-no-description.json"));
65  
66  		assertEquals(new URI("http://localhost:8090/jira/rest/api/latest/version/10000"), version.getSelf());
67  		assertEquals("1.1", version.getName());
68  		Assert.assertNull(version.getDescription());
69  		Assert.assertFalse(version.isReleased());
70  		Assert.assertTrue(version.isArchived());
71  		Assert.assertThat(version.getReleaseDate(), DateTimeMatcher.isEqual(
72  				new DateTime(2010, 8, 25, 0, 0, 0, 0, DateTimeZone.forOffsetHours(2))));
73  	}
74  
75  }