add getAllComments and postNewComment
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Raffaele Mignone 2019-11-12 21:19:44 +01:00
parent 3b0f522f65
commit 726a4b6a07
Signed by: norangebit
GPG Key ID: F5255658CB220573
3 changed files with 179 additions and 0 deletions

View File

@ -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<List<Comment>> 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<Comment> postNewComment(
@Path("boardId") String boardId,
@Path("cardId") String cardId,
@Field("authorId") String authorId,
@Field("comment") String comment
);
}

View File

@ -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 + '\'' +
'}';
}
}

View File

@ -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<Comment> 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\"" +
"}";
}