CommentJsonParser implements JsonParser<Comment> { @Override public Comment parse(JSONObject json) throws JSONException { final URI selfUri = JsonParseUtil.getSelfUri(json); final String body = json.getString("body"); final BasicUser author = JsonParseUtil.parseBasicUser(json.optJSONObject("author")); final BasicUser updateAuthor = JsonParseUtil.parseBasicUser(json.optJSONObject("updateAuthor")); final Visibility visibility = visibilityJsonParser.parseVisibility(json); return new Comment(selfUri, body, author, updateAuthor, JsonParseUtil.parseDateTime(json.getString("created")), JsonParseUtil.parseDateTime(json.getString("updated")), visibility); }  @Override Comment parse(JSONObject json); static final String VISIBILITY_KEY; }
@Test public void testParse() throws Exception { final JSONObject commentsJson = ResourceUtil.getJsonObjectFromResource("/json/comment/valid.json"); final CommentJsonParser parser = new CommentJsonParser(); final JSONObject comment1Json = commentsJson.getJSONArray("value").getJSONObject(0); final Comment comment1 = parser.parse(comment1Json); assertEquals("some comment", comment1.getBody()); assertEquals(TestConstants.USER_ADMIN, comment1.getAuthor()); assertEquals(TestConstants.USER_ADMIN, comment1.getUpdateAuthor()); assertEquals(TestUtil.toDateTime("2010-08-17T16:40:57.791+0200"), comment1.getCreationDate()); assertEquals(TestUtil.toDateTime("2010-08-17T16:40:57.791+0200"), comment1.getUpdateDate()); assertEquals(TestUtil.toUri("http: assertEquals(Visibility.role("Administrators"), comment1.getVisibility()); final JSONObject comment3Json = commentsJson.getJSONArray("value").getJSONObject(2); final Comment comment3 = parser.parse(comment3Json); assertEquals(Visibility.group("jira-users"), comment3.getVisibility()); final JSONObject comment2Json = commentsJson.getJSONArray("value").getJSONObject(1); final Comment comment2 = parser.parse(comment2Json); assertNull(comment2.getVisibility()); }
@Test public void testParseAnonymous() throws JSONException { final CommentJsonParser parser = new CommentJsonParser(); final JSONObject json = ResourceUtil.getJsonObjectFromResource("/json/comment/valid-anonymous.json"); final JSONObject commentJson = json.getJSONArray("value").getJSONObject(0); final Comment comment = parser.parse(commentJson); assertNull(comment.getAuthor()); assertNull(comment.getUpdateAuthor()); assertEquals("Comment from anonymous user", comment.getBody()); }