View Javadoc

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