From 726a4b6a07d6abfa4fce4975ed881afb3d98134e Mon Sep 17 00:00:00 2001 From: norangebit Date: Tue, 12 Nov 2019 21:19:44 +0100 Subject: [PATCH 1/3] add getAllComments and postNewComment --- .../wekan/wrapper/api/CardCommentService.java | 41 +++++++ .../java/wekan/wrapper/entity/Comment.java | 31 +++++ .../wrapper/api/CardCommentServiceTest.java | 107 ++++++++++++++++++ 3 files changed, 179 insertions(+) create mode 100644 wrapper/src/main/java/wekan/wrapper/api/CardCommentService.java create mode 100644 wrapper/src/main/java/wekan/wrapper/entity/Comment.java create mode 100644 wrapper/src/test/java/wekan/wrapper/api/CardCommentServiceTest.java diff --git a/wrapper/src/main/java/wekan/wrapper/api/CardCommentService.java b/wrapper/src/main/java/wekan/wrapper/api/CardCommentService.java new file mode 100644 index 0000000..b3e5e55 --- /dev/null +++ b/wrapper/src/main/java/wekan/wrapper/api/CardCommentService.java @@ -0,0 +1,41 @@ +package wekan.wrapper.api; + +import retrofit2.Call; +import retrofit2.http.*; +import wekan.wrapper.entity.Comment; + +import java.util.List; + +public interface CardCommentService { + + /** + * Get all comments attached to a card. + * + * @param boardId The ID of the board + * @param cardId The ID of the card + * @return The list of the comments + */ + @GET("/api/boards/{boardId}/cards/{cardId}/comments") + Call> getAllComments( + @Path("boardId") String boardId, + @Path("cardId") String cardId + ); + + /** + * Post new comment attached on the give card. + * + * @param boardId The ID of the board + * @param cardId The ID of the card + * @param authorId The ID of the author + * @param comment The comment + * @return The id of the new comment + */ + @FormUrlEncoded + @POST("/api/boards/{boardId}/cards/{cardId}/comments") + Call postNewComment( + @Path("boardId") String boardId, + @Path("cardId") String cardId, + @Field("authorId") String authorId, + @Field("comment") String comment + ); +} diff --git a/wrapper/src/main/java/wekan/wrapper/entity/Comment.java b/wrapper/src/main/java/wekan/wrapper/entity/Comment.java new file mode 100644 index 0000000..2259dfa --- /dev/null +++ b/wrapper/src/main/java/wekan/wrapper/entity/Comment.java @@ -0,0 +1,31 @@ +package wekan.wrapper.entity; + +import com.google.gson.annotations.SerializedName; + +public class Comment { + @SerializedName("_id") + private String id; + private String comment; + private String authorId; + + public String getId() { + return id; + } + + public String getComment() { + return comment; + } + + public String getAuthorId() { + return authorId; + } + + @Override + public String toString() { + return "Comment{" + + "id='" + id + '\'' + + ", comment='" + comment + '\'' + + ", authorId='" + authorId + '\'' + + '}'; + } +} diff --git a/wrapper/src/test/java/wekan/wrapper/api/CardCommentServiceTest.java b/wrapper/src/test/java/wekan/wrapper/api/CardCommentServiceTest.java new file mode 100644 index 0000000..666b15b --- /dev/null +++ b/wrapper/src/test/java/wekan/wrapper/api/CardCommentServiceTest.java @@ -0,0 +1,107 @@ +package wekan.wrapper.api; + +import okhttp3.mockwebserver.MockResponse; +import okhttp3.mockwebserver.MockWebServer; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import retrofit2.Retrofit; +import retrofit2.converter.gson.GsonConverterFactory; +import wekan.wrapper.entity.Color; +import wekan.wrapper.entity.Comment; +import wekan.wrapper.entity.Swimlane; + +import java.io.IOException; +import java.net.HttpURLConnection; +import java.util.List; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +public class CardCommentServiceTest { + private MockWebServer mockWebServer = new MockWebServer(); + private CardCommentService service = null; + + @Before + public void setUp() { + try { + mockWebServer.start(); + service = new Retrofit.Builder() + .baseUrl(mockWebServer.url("/")) + .addConverterFactory(GsonConverterFactory.create()) + .build() + .create(CardCommentService.class); + } catch (IOException e) { + e.printStackTrace(); + } + } + + @After + public void teardown() { + try { + mockWebServer.shutdown(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + @Test + public void getAllComments() { + MockResponse response = new MockResponse() + .setResponseCode(HttpURLConnection.HTTP_OK) + .setBody( + "[ " + COMMENT_1 + ", " + COMMENT_2 + ", " + COMMENT_3 + " ]" + ); + + mockWebServer.enqueue(response); + + try { + List lists = service.getAllComments("board id", "card id") + .execute().body(); + + assertNotNull(lists); + assertEquals(3, lists.size()); + } catch (IOException e) { + e.printStackTrace(); + } + } + + @Test + public void postNewComment() { + MockResponse response = new MockResponse() + .setResponseCode(HttpURLConnection.HTTP_OK) + .setBody(COMMENT_1); + + mockWebServer.enqueue(response); + + try { + Comment comment = service.postNewComment( + "board id", + "card id", + "author id", + "comment" + ).execute().body(); + + assertNotNull(comment); + assertNotNull(comment.getId()); + } catch (IOException e) { + e.printStackTrace(); + } + } + + private final static String COMMENT_1 = "{" + + "\"_id\":\"zwzAgjHdW4fNJ9YLc\"," + + "\"comment\":\"comment 1\"," + + "\"authorId\":\"Si69gNgkJfQuk6uiJ\"" + + "}"; + private final static String COMMENT_2 = "{" + + "\"_id\":\"zwzAgjHdW4fNJ9YLt\"," + + "\"comment\":\"comment 2\"," + + "\"authorId\":\"Si69gNgkJfQuk6uiJ\"" + + "}"; + private final static String COMMENT_3 = "{" + + "\"_id\":\"zwzAgjHdW4fNJ9YLf\"," + + "\"comment\":\"comment 3\"," + + "\"authorId\":\"Si69gNgkJfQuk6uiJ\"" + + "}"; +} From 7429e2cbc744165b1b33124e3663e9bf184dad3f Mon Sep 17 00:00:00 2001 From: norangebit Date: Tue, 12 Nov 2019 21:39:38 +0100 Subject: [PATCH 2/3] add getComment and deleteComment --- .../wekan/wrapper/api/CardCommentService.java | 45 +++++++++++++++---- .../java/wekan/wrapper/entity/Comment.java | 10 +++++ .../wrapper/api/CardCommentServiceTest.java | 33 +++++++++++--- 3 files changed, 75 insertions(+), 13 deletions(-) diff --git a/wrapper/src/main/java/wekan/wrapper/api/CardCommentService.java b/wrapper/src/main/java/wekan/wrapper/api/CardCommentService.java index b3e5e55..e1c6dc5 100644 --- a/wrapper/src/main/java/wekan/wrapper/api/CardCommentService.java +++ b/wrapper/src/main/java/wekan/wrapper/api/CardCommentService.java @@ -11,9 +11,9 @@ public interface CardCommentService { /** * Get all comments attached to a card. * - * @param boardId The ID of the board - * @param cardId The ID of the card - * @return The list of the comments + * @param boardId The ID of the board + * @param cardId The ID of the card + * @return The list of the comments */ @GET("/api/boards/{boardId}/cards/{cardId}/comments") Call> getAllComments( @@ -24,11 +24,11 @@ public interface CardCommentService { /** * Post new comment attached on the give card. * - * @param boardId The ID of the board - * @param cardId The ID of the card - * @param authorId The ID of the author - * @param comment The comment - * @return The id of the new comment + * @param boardId The ID of the board + * @param cardId The ID of the card + * @param authorId The ID of the author + * @param comment The comment + * @return The id of the new comment */ @FormUrlEncoded @POST("/api/boards/{boardId}/cards/{cardId}/comments") @@ -38,4 +38,33 @@ public interface CardCommentService { @Field("authorId") String authorId, @Field("comment") String comment ); + + /** + * Get a comment attached to a card. + * + * @param boardId The ID of the board + * @param cardId The ID of the card + * @param commentId The ID of the comment + * @return The comment + */ + @GET("/api/boards/{boardId}/cards/{cardId}/comments/{commentId}") + Call getComment( + @Path("boardId") String boardId, + @Path("cardId") String cardId, + @Path("commentId") String commentId + ); + + /** + * Delete a comment attached to a card. + * + * @param boardId The ID of the board + * @param cardId The ID of the card + * @param commentId The ID of the comment + */ + @DELETE("/api/boards/{boardId}/cards/{cardId}/comments/{commentId}") + Call deleteComment( + @Path("boardId") String boardId, + @Path("cardId") String cardId, + @Path("commentId") String commentId + ); } diff --git a/wrapper/src/main/java/wekan/wrapper/entity/Comment.java b/wrapper/src/main/java/wekan/wrapper/entity/Comment.java index 2259dfa..3ae73fa 100644 --- a/wrapper/src/main/java/wekan/wrapper/entity/Comment.java +++ b/wrapper/src/main/java/wekan/wrapper/entity/Comment.java @@ -2,11 +2,17 @@ package wekan.wrapper.entity; import com.google.gson.annotations.SerializedName; +import java.util.Date; + public class Comment { @SerializedName("_id") private String id; private String comment; private String authorId; + private String boardId; + private String cardId; + private Date createdAt; + private Date modifiedAt; public String getId() { return id; @@ -26,6 +32,10 @@ public class Comment { "id='" + id + '\'' + ", comment='" + comment + '\'' + ", authorId='" + authorId + '\'' + + ", boardId='" + boardId + '\'' + + ", cardId='" + cardId + '\'' + + ", createdAt=" + createdAt + + ", modifiedAt=" + modifiedAt + '}'; } } diff --git a/wrapper/src/test/java/wekan/wrapper/api/CardCommentServiceTest.java b/wrapper/src/test/java/wekan/wrapper/api/CardCommentServiceTest.java index 666b15b..54fd35a 100644 --- a/wrapper/src/test/java/wekan/wrapper/api/CardCommentServiceTest.java +++ b/wrapper/src/test/java/wekan/wrapper/api/CardCommentServiceTest.java @@ -7,9 +7,7 @@ import org.junit.Before; import org.junit.Test; import retrofit2.Retrofit; import retrofit2.converter.gson.GsonConverterFactory; -import wekan.wrapper.entity.Color; import wekan.wrapper.entity.Comment; -import wekan.wrapper.entity.Swimlane; import java.io.IOException; import java.net.HttpURLConnection; @@ -89,6 +87,27 @@ public class CardCommentServiceTest { } } + @Test + public void getComment() { + MockResponse response = new MockResponse() + .setResponseCode(HttpURLConnection.HTTP_OK) + .setBody(COMMENT_3); + + mockWebServer.enqueue(response); + + try { + Comment comment = service.getComment( + "board id", + "card id", + "comment id" + ).execute().body(); + + assertNotNull(comment); + } catch (IOException e) { + e.printStackTrace(); + } + } + private final static String COMMENT_1 = "{" + "\"_id\":\"zwzAgjHdW4fNJ9YLc\"," + "\"comment\":\"comment 1\"," + @@ -100,8 +119,12 @@ public class CardCommentServiceTest { "\"authorId\":\"Si69gNgkJfQuk6uiJ\"" + "}"; private final static String COMMENT_3 = "{" + - "\"_id\":\"zwzAgjHdW4fNJ9YLf\"," + - "\"comment\":\"comment 3\"," + - "\"authorId\":\"Si69gNgkJfQuk6uiJ\"" + + "\"_id\":\"DofhD9v96DbX7Wirp\"," + + "\"userId\":\"Si69gNgkJfQuk6uiJ\"," + + "\"text\":\"commento api\"," + + "\"cardId\":\"XQtnuqWEJnEcm6iy2\"," + + "\"boardId\":\"TtjXrJyvPkG3xsbkw\"," + + "\"createdAt\":\"2019-11-12T20:12:55.749Z\"," + + "\"modifiedAt\":\"2019-11-12T20:12:55.749Z\"" + "}"; } From a79a765facba1e633c97eb5766c14691e5c1a4f5 Mon Sep 17 00:00:00 2001 From: norangebit Date: Wed, 13 Nov 2019 14:48:58 +0100 Subject: [PATCH 3/3] fix serialization of Comment fix #8 --- .../java/wekan/wrapper/entity/Comment.java | 18 ++++++++++++++++++ .../wrapper/api/CardCommentServiceTest.java | 2 ++ 2 files changed, 20 insertions(+) diff --git a/wrapper/src/main/java/wekan/wrapper/entity/Comment.java b/wrapper/src/main/java/wekan/wrapper/entity/Comment.java index 3ae73fa..cef3fd1 100644 --- a/wrapper/src/main/java/wekan/wrapper/entity/Comment.java +++ b/wrapper/src/main/java/wekan/wrapper/entity/Comment.java @@ -7,7 +7,9 @@ import java.util.Date; public class Comment { @SerializedName("_id") private String id; + @SerializedName(value = "comment", alternate = "text") private String comment; + @SerializedName(value = "authorId", alternate = "userId") private String authorId; private String boardId; private String cardId; @@ -26,6 +28,22 @@ public class Comment { return authorId; } + public String getBoardId() { + return boardId; + } + + public String getCardId() { + return cardId; + } + + public Date getCreatedAt() { + return createdAt; + } + + public Date getModifiedAt() { + return modifiedAt; + } + @Override public String toString() { return "Comment{" + diff --git a/wrapper/src/test/java/wekan/wrapper/api/CardCommentServiceTest.java b/wrapper/src/test/java/wekan/wrapper/api/CardCommentServiceTest.java index 54fd35a..31ab537 100644 --- a/wrapper/src/test/java/wekan/wrapper/api/CardCommentServiceTest.java +++ b/wrapper/src/test/java/wekan/wrapper/api/CardCommentServiceTest.java @@ -103,6 +103,8 @@ public class CardCommentServiceTest { ).execute().body(); assertNotNull(comment); + assertNotNull(comment.getAuthorId()); + assertNotNull(comment.getComment()); } catch (IOException e) { e.printStackTrace(); }