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.Comment;
21  import com.atlassian.jira.rest.client.domain.Visibility;
22  import org.codehaus.jettison.json.JSONException;
23  import org.codehaus.jettison.json.JSONObject;
24  import org.junit.Test;
25  
26  import static org.junit.Assert.assertEquals;
27  import static org.junit.Assert.assertNull;
28  
29  
30  public class CommentJsonParserTest {
31  	@Test
32  	public void testParse() throws Exception {
33  		final JSONObject commentsJson = ResourceUtil.getJsonObjectFromResource("/json/comment/valid.json");
34  		final CommentJsonParser parser = new CommentJsonParser();
35  
36  		final JSONObject comment1Json = commentsJson.getJSONArray("value").getJSONObject(0);
37  		final Comment comment1 = parser.parse(comment1Json);
38  		assertEquals("some comment", comment1.getBody());
39  		assertEquals(TestConstants.USER_ADMIN, comment1.getAuthor());
40  		assertEquals(TestConstants.USER_ADMIN, comment1.getUpdateAuthor());
41  		assertEquals(TestUtil.toDateTime("2010-08-17T16:40:57.791+0200"), comment1.getCreationDate());
42  		assertEquals(TestUtil.toDateTime("2010-08-17T16:40:57.791+0200"), comment1.getUpdateDate());
43  		assertEquals(TestUtil.toUri("http://localhost:8090/jira/rest/api/latest/comment/10020"), comment1.getSelf());
44  		assertEquals(Long.valueOf(10020), comment1.getId());
45  		assertEquals(Visibility.role("Administrators"), comment1.getVisibility());
46  
47  		final JSONObject comment3Json = commentsJson.getJSONArray("value").getJSONObject(2);
48  		final Comment comment3 = parser.parse(comment3Json);
49  		assertEquals(Long.valueOf(10022), comment3.getId());
50  		assertEquals(Visibility.group("jira-users"), comment3.getVisibility());
51  
52  		final JSONObject comment2Json = commentsJson.getJSONArray("value").getJSONObject(1);
53  		final Comment comment2 = parser.parse(comment2Json);
54  		assertEquals(Long.valueOf(10021), comment2.getId());
55  		assertNull(comment2.getVisibility());
56  	}
57  
58  	@Test
59  	public void testParseWithoutId() throws Exception {
60  		final JSONObject commentsJson = ResourceUtil.getJsonObjectFromResource("/json/comment/valid-without-id.json");
61  		final CommentJsonParser parser = new CommentJsonParser();
62  
63  		final JSONObject comment1Json = commentsJson.getJSONArray("value").getJSONObject(0);
64  		final Comment comment1 = parser.parse(comment1Json);
65  		assertEquals("some comment", comment1.getBody());
66  		assertEquals(TestConstants.USER_ADMIN, comment1.getAuthor());
67  		assertEquals(TestConstants.USER_ADMIN, comment1.getUpdateAuthor());
68  		assertEquals(TestUtil.toDateTime("2010-08-17T16:40:57.791+0200"), comment1.getCreationDate());
69  		assertEquals(TestUtil.toDateTime("2010-08-17T16:40:57.791+0200"), comment1.getUpdateDate());
70  		assertEquals(TestUtil.toUri("http://localhost:8090/jira/rest/api/latest/comment/10020"), comment1.getSelf());
71  		assertEquals(null, comment1.getId());
72  		assertEquals(Visibility.role("Administrators"), comment1.getVisibility());
73  
74  		final JSONObject comment3Json = commentsJson.getJSONArray("value").getJSONObject(2);
75  		final Comment comment3 = parser.parse(comment3Json);
76  		assertEquals(null, comment3.getId());
77  		assertEquals(Visibility.group("jira-users"), comment3.getVisibility());
78  
79  		final JSONObject comment2Json = commentsJson.getJSONArray("value").getJSONObject(1);
80  		final Comment comment2 = parser.parse(comment2Json);
81  		assertEquals(null, comment2.getId());
82  		assertNull(comment2.getVisibility());
83  
84  	}
85  
86  	@Test
87  	public void testParseAnonymous() throws JSONException {
88  		final CommentJsonParser parser = new CommentJsonParser();
89  		final JSONObject json = ResourceUtil.getJsonObjectFromResource("/json/comment/valid-anonymous.json");
90  		final JSONObject commentJson = json.getJSONArray("value").getJSONObject(0);
91  		final Comment comment = parser.parse(commentJson);
92  		assertNull(comment.getAuthor());
93  		assertNull(comment.getUpdateAuthor());
94  		assertEquals("Comment from anonymous user", comment.getBody());
95  
96  	}
97  }