Skip to content

Commit

Permalink
Analysis: Add missing n-a to comment wp com rest response fields
Browse files Browse the repository at this point in the history
FYI: 'n-a' stands for 'nullability annotations'.

PS: This 'NotNullFieldNotInitialized' warning got suppressed because a
'response' class can never have its fields initialized via a constructor
initialization, or otherwise for that matter.
  • Loading branch information
ParaskP7 committed Oct 11, 2023
1 parent 4f4070a commit 645d3a5
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ class CommentsMapperTest {

@Test
fun `xmlrpc dto list is converted to entity list`() {
val commentList = getDefaultCommentList(false).map { it.copy(id = 0, authorProfileImageUrl = null) }
val commentList = getDefaultCommentList(false).map { it.copy(
id = 0,
authorProfileImageUrl = null
) }
val site = SiteModel().apply {
id = commentList.first().localSiteId
selfHostedSiteId = commentList.first().remoteSiteId
Expand Down Expand Up @@ -120,27 +123,27 @@ class CommentsMapperTest {
val entity = this
return CommentWPComRestResponse().apply {
ID = entity.remoteCommentId
URL = entity.url
URL = entity.url ?: ""
author = Author().apply {
ID = entity.authorId
URL = entity.authorUrl
avatar_URL = entity.authorProfileImageUrl
email = entity.authorEmail
name = entity.authorName
URL = entity.authorUrl ?: ""
avatar_URL = entity.authorProfileImageUrl ?: ""
email = entity.authorEmail ?: ""
name = entity.authorName ?: ""
}
content = entity.content
date = entity.datePublished
content = entity.content ?: ""
date = entity.datePublished ?: ""
i_like = entity.iLike
parent = CommentParent().apply {
ID = entity.parentId
}
post = Post().apply {
type = "post"
title = entity.postTitle
title = entity.postTitle ?: ""
link = "https://public-api.wordpress.com/rest/v1.1/sites/185464053/posts/85"
ID = entity.remotePostId
}
status = entity.status
status = entity.status ?: ""
}
}

Expand Down Expand Up @@ -188,22 +191,22 @@ class CommentsMapperTest {

private fun getDefaultComment(allowNulls: Boolean): CommentEntity {
return CommentEntity(
id = 0,
remoteCommentId = 10,
remotePostId = 100,
authorId = 44,
id = 0L,
remoteCommentId = 10L,
remotePostId = 100L,
authorId = 44L,
localSiteId = 10_000,
remoteSiteId = 100_000,
authorUrl = if (allowNulls) null else "https://test-debug-site.wordpress.com",
authorName = if (allowNulls) null else "authorname",
authorEmail = if (allowNulls) null else "[email protected]",
authorProfileImageUrl = if (allowNulls) null else "https://gravatar.com/avatar/111222333",
postTitle = if (allowNulls) null else "again",
remoteSiteId = 100_000L,
authorUrl = if (allowNulls) "" else "https://test-debug-site.wordpress.com",
authorName = if (allowNulls) "" else "authorname",
authorEmail = if (allowNulls) "" else "[email protected]",
authorProfileImageUrl = if (allowNulls) "" else "https://gravatar.com/avatar/111222333",
postTitle = if (allowNulls) "" else "again",
status = APPROVED.toString(),
datePublished = if (allowNulls) null else "2021-05-12T15:10:40+02:00",
datePublished = if (allowNulls) "" else "2021-05-12T15:10:40+02:00",
publishedTimestamp = 1_000_000,
content = if (allowNulls) null else "content example",
url = if (allowNulls) null else "https://test-debug-site.wordpress.com/2021/02/25/again/#comment-137",
content = if (allowNulls) "" else "content example",
url = if (allowNulls) "" else "https://test-debug-site.wordpress.com/2021/02/25/again/#comment-137",
hasParent = true,
parentId = 1_000L,
iLike = false
Expand All @@ -213,10 +216,10 @@ class CommentsMapperTest {
private fun getDefaultCommentList(allowNulls: Boolean): CommentEntityList {
val comment = getDefaultComment(allowNulls)
return listOf(
comment.copy(id = 1, remoteCommentId = 10, datePublished = "2021-07-24T00:51:43+02:00"),
comment.copy(id = 2, remoteCommentId = 20, datePublished = "2021-07-24T00:51:43+02:00"),
comment.copy(id = 3, remoteCommentId = 30, datePublished = "2021-07-24T00:51:43+02:00"),
comment.copy(id = 4, remoteCommentId = 40, datePublished = "2021-07-24T00:51:43+02:00")
comment.copy(id = 1L, remoteCommentId = 10L, datePublished = "2021-07-24T00:51:43+02:00"),
comment.copy(id = 2L, remoteCommentId = 20L, datePublished = "2021-07-24T00:51:43+02:00"),
comment.copy(id = 3L, remoteCommentId = 30L, datePublished = "2021-07-24T00:51:43+02:00"),
comment.copy(id = 4L, remoteCommentId = 40L, datePublished = "2021-07-24T00:51:43+02:00")
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class CommentsRestClientTest {
assertThat(payload.isError).isFalse

val comments = payload.response!!
assertThat(comments.size).isEqualTo(commentsResponse.comments.size)
assertThat(comments.size).isEqualTo(commentsResponse.comments?.size)
assertThat(urlCaptor.lastValue).isEqualTo(
"https://public-api.wordpress.com/rest/v1.1/sites/${site.siteId}/comments/"
)
Expand Down Expand Up @@ -600,10 +600,10 @@ class CommentsRestClientTest {

private fun getDefaultDto(): CommentWPComRestResponse {
return CommentWPComRestResponse().apply {
ID = 137
ID = 137L
URL = "https://test-site.wordpress.com/2021/02/25/again/#comment-137"
author = Author().apply {
ID = 0
ID = 0L
URL = "https://debugging-test.wordpress.com"
avatar_URL = "https://gravatar.com/avatar/avatarurl"
email = "[email protected]"
Expand All @@ -612,9 +612,9 @@ class CommentsRestClientTest {
content = "example content"
date = "2021-05-12T15:10:40+02:00"
i_like = true
parent = CommentParent().apply { ID = 41 }
parent = CommentParent().apply { ID = 41L }
post = Post().apply {
ID = 85
ID = 85L
link = "https://public-api.wordpress.com/rest/v1.1/sites/11111111/posts/85"
title = "again"
type = "post"
Expand All @@ -627,31 +627,31 @@ class CommentsRestClientTest {
return CommentLikeWPComRestResponse().apply {
success = true
i_like = iLike
like_count = 100
like_count = 100L
}
}

private fun CommentWPComRestResponse.toEntity(): CommentEntity {
val dto = this
return CommentEntity(
id = 0,
id = 0L,
remoteCommentId = dto.ID,
remotePostId = dto.post.ID,
authorId = dto.author.ID,
remotePostId = dto.post?.ID ?: 0L,
authorId = dto.author?.ID ?: 0L,
localSiteId = 10,
remoteSiteId = 200,
authorUrl = dto.author.URL,
authorName = dto.author.name,
authorEmail = dto.author.email,
authorProfileImageUrl = dto.author.avatar_URL,
postTitle = dto.post.title,
remoteSiteId = 200L,
authorUrl = dto.author?.URL,
authorName = dto.author?.name,
authorEmail = dto.author?.email,
authorProfileImageUrl = dto.author?.avatar_URL,
postTitle = dto.post?.title,
status = dto.status,
datePublished = dto.date,
publishedTimestamp = 132456,
publishedTimestamp = 132456L,
content = dto.content,
url = dto.URL,
hasParent = dto.parent != null,
parentId = dto.parent.ID,
parentId = dto.parent?.ID ?: 0L,
iLike = dto.i_like
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class CommentsMapper @Inject constructor(
}
},
authorProfileImageUrl = commentDto.author?.avatar_URL,
remotePostId = commentDto.post?.ID ?: 0,
remotePostId = commentDto.post?.ID ?: 0L,
postTitle = StringEscapeUtils.unescapeHtml4(commentDto.post?.title),
status = commentDto.status,
datePublished = commentDto.date,
Expand All @@ -46,7 +46,7 @@ class CommentsMapper @Inject constructor(
url = commentDto.URL,
authorId = commentDto.author?.ID ?: 0L,
hasParent = commentDto.parent != null,
parentId = commentDto.parent?.ID ?: 0,
parentId = commentDto.parent?.ID ?: 0L,
iLike = commentDto.i_like
)
}
Expand Down Expand Up @@ -139,7 +139,7 @@ class CommentsMapper @Inject constructor(
content = XMLRPCUtils.safeGetMapValue(commentMap, "content", ""),
url = XMLRPCUtils.safeGetMapValue(commentMap, "link", ""),
hasParent = remoteParentCommentId > 0,
parentId = if (remoteParentCommentId > 0) remoteParentCommentId else 0,
parentId = if (remoteParentCommentId > 0) remoteParentCommentId else 0L,
iLike = false
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ private CommentModel commentResponseToComment(
comment.setPublishedTimestamp(DateTimeUtils.timestampFromIso8601(response.date));

if (response.author != null) {
comment.setAuthorId(response.author.ID);
comment.setAuthorUrl(response.author.URL);
comment.setAuthorName(StringEscapeUtils.unescapeHtml4(response.author.name));
if ("false".equals(response.author.email)) {
Expand All @@ -311,10 +312,6 @@ private CommentModel commentResponseToComment(
comment.setPostTitle(StringEscapeUtils.unescapeHtml4(response.post.title));
}

if (response.author != null) {
comment.setAuthorId(response.author.ID);
}

if (response.parent != null) {
comment.setHasParent(true);
comment.setParentId(response.parent.ID);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,38 @@
package org.wordpress.android.fluxc.network.rest.wpcom.comment;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.List;

@SuppressWarnings("NotNullFieldNotInitialized")
public class CommentWPComRestResponse {
public static class CommentsWPComRestResponse {
public List<CommentWPComRestResponse> comments;
@Nullable public List<CommentWPComRestResponse> comments;
}

public static class Post {
public long ID;
public String title;
public String type;
public String link;
@NonNull public String title;
@NonNull public String type;
@NonNull public String link;
}

public static class Author {
public long ID;
public String email; // can be boolean "false" if not set
public String name;
public String URL;
public String avatar_URL;
@NonNull public String email; // can be boolean "false" if not set
@NonNull public String name;
@NonNull public String URL;
@NonNull public String avatar_URL;
}

public long ID;
public CommentParent parent;
public Post post;
public Author author;
public String date;
public String status;
public String content;
@Nullable public CommentParent parent;
@Nullable public Post post;
@Nullable public Author author;
@NonNull public String date;
@NonNull public String status;
@NonNull public String content;
public boolean i_like;
public String URL;
@NonNull public String URL;
}

0 comments on commit 645d3a5

Please sign in to comment.