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(Visibility.role("Administrators"), comment1.getVisibility());
45  
46  		final JSONObject comment3Json = commentsJson.getJSONArray("value").getJSONObject(2);
47  		final Comment comment3 = parser.parse(comment3Json);
48  		assertEquals(Visibility.group("jira-users"), comment3.getVisibility());
49  
50  		final JSONObject comment2Json = commentsJson.getJSONArray("value").getJSONObject(1);
51  		final Comment comment2 = parser.parse(comment2Json);
52  		assertNull(comment2.getVisibility());
53  	}
54  
55  	@Test
56  	public void testParseAnonymous() throws JSONException {
57  		final CommentJsonParser parser = new CommentJsonParser();
58  		final JSONObject json = ResourceUtil.getJsonObjectFromResource("/json/comment/valid-anonymous.json");
59  		final JSONObject commentJson = json.getJSONArray("value").getJSONObject(0);
60  		final Comment comment = parser.parse(commentJson);
61  		assertNull(comment.getAuthor());
62  		assertNull(comment.getUpdateAuthor());
63  		assertEquals("Comment from anonymous user", comment.getBody());
64  
65  	}
66  }