diff --git a/wrapper/src/main/java/wekan/wrapper/api/ChecklistService.java b/wrapper/src/main/java/wekan/wrapper/api/ChecklistService.java new file mode 100644 index 0000000..ca0047d --- /dev/null +++ b/wrapper/src/main/java/wekan/wrapper/api/ChecklistService.java @@ -0,0 +1,162 @@ +package wekan.wrapper.api; + +import retrofit2.Call; +import retrofit2.http.*; +import wekan.wrapper.entity.Checklist; +import wekan.wrapper.entity.ChecklistItem; +import wekan.wrapper.entity.ChecklistPrototype; +import wekan.wrapper.entity.ChecklistItemStatus; + +import java.util.List; + +public interface ChecklistService { + + /** + * Get all checklists attached on a card. + * + * @param boardId The ID of the board + * @param cardId The ID of the card + * @return The list with the checklists + */ + @GET("/api/boards/{boardId}/cards/{cardId}/checklists") + Call> getAllChecklists( + @Path("boardId") String boardId, + @Path("cardId") String cardId + ); + + /** + * Attach new empty checklist to a card. + * + * @param boardId The ID of the board + * @param cardId The ID of the card + * @param title The title of the new checklist + * @return The new checklist + */ + @FormUrlEncoded + @POST("/api/boards/{boardId}/cards/{cardId}/checklists") + Call postChecklist( + @Path("boardId") String boardId, + @Path("cardId") String cardId, + @Field("title") String title + ); + + /** + * Attach new checklist to a card. + * + * @param boardId The ID of the board + * @param cardId The ID of the card + * @param checklistPrototype The prototype of the checklist + * @return The new checklist + * @see ChecklistPrototype + */ + @POST("/api/boards/{boardId}/cards/{cardId}/checklists") + Call postChecklist( + @Path("boardId") String boardId, + @Path("cardId") String cardId, + @Body ChecklistPrototype checklistPrototype + ); + + /** + * Get the checklist attached on a card. + * + * @param boardId The ID of the board + * @param cardId The ID of the card + * @param checklistId The ID of the checklist + * @return The checklist + */ + @GET("/api/boards/{boardId}/cards/{cardId}/checklists/{checklistId}") + Call getChecklist( + @Path("boardId") String boardId, + @Path("cardId") String cardId, + @Path("checklistId") String checklistId + ); + + /** + * Delete the checklist attached on a card. + * + * @param boardId The ID of the board + * @param cardId The ID of the card + * @param checklistId The ID of the checklist + * @return The id of the checklist + */ + @DELETE("/api/boards/{boardId}/cards/{cardId}/checklists/{checklistId}") + Call deleteChecklist( + @Path("boardId") String boardId, + @Path("cardId") String cardId, + @Path("checklistId") String checklistId + ); + + /** + * Get the checklist item attached on a checklist. + * + * @param boardId The ID of the board + * @param cardId The ID of the card + * @param checklistId The ID of the checklist + * @param itemId The ID of the item + * @return The checklist item + */ + @GET("/api/boards/{boardId}/cards/{cardId}/checklists/{checklistId}/items/{itemId}") + Call getChecklistItem( + @Path("boardId") String boardId, + @Path("cardId") String cardId, + @Path("checklistId") String checklistId, + @Path("itemId") String itemId + ); + + /** + * Change the title of the checklist item. + * + * @param boardId The ID of the board + * @param cardId The ID of the card + * @param checklistId The ID of the checklist + * @param itemId The ID of the item + * @param title The new title + * @return The ID of the checklist item + */ + @FormUrlEncoded + @PUT("/api/boards/{boardId}/cards/{cardId}/checklists/{checklistId}/items/{itemId}") + Call editChecklistItem( + @Path("boardId") String boardId, + @Path("cardId") String cardId, + @Path("checklistId") String checklistId, + @Path("itemId") String itemId, + @Field("title") String title + ); + + /** + * Change the state of the checklist item. + * + * @param boardId The ID of the board + * @param cardId The ID of the card + * @param checklistId The ID of the checklist + * @param itemId The ID of the item + * @param itemStatus The status of the item + * @return The ID of the checklist item + * @see ChecklistItemStatus + */ + @PUT("/api/boards/{boardId}/cards/{cardId}/checklists/{checklistId}/items/{itemId}") + Call editChecklistItem( + @Path("boardId") String boardId, + @Path("cardId") String cardId, + @Path("checklistId") String checklistId, + @Path("itemId") String itemId, + @Body ChecklistItemStatus itemStatus + ); + + /** + * Delete a checklist item from the checklist. + * + * @param boardId The ID of the board + * @param cardId The ID of the card + * @param checklistId The ID of the checklist + * @param itemId The ID of the item + * @return The ID of the checklist item + */ + @DELETE("/api/boards/{boardId}/cards/{cardId}/checklists/{checklistId}/items/{itemId}") + Call deleteChecklistItem( + @Path("boardId") String boardId, + @Path("cardId") String cardId, + @Path("checklistId") String checklistId, + @Path("itemId") String itemId + ); +} diff --git a/wrapper/src/main/java/wekan/wrapper/entity/Checklist.java b/wrapper/src/main/java/wekan/wrapper/entity/Checklist.java new file mode 100644 index 0000000..dbd335d --- /dev/null +++ b/wrapper/src/main/java/wekan/wrapper/entity/Checklist.java @@ -0,0 +1,64 @@ +package wekan.wrapper.entity; + +import com.google.gson.annotations.SerializedName; + +import java.util.Date; +import java.util.List; + +public class Checklist { + @SerializedName("_id") + private String id; + private String cardId; + private String title; + private List items; + private Date finishedAt; + private Date createdAt; + private Date modifiedAt; + private int sort; + + public String getId() { + return id; + } + + public String getCardId() { + return cardId; + } + + public String getTitle() { + return title; + } + + public List getItems() { + return items; + } + + public Date getFinishedAt() { + return finishedAt; + } + + public Date getCreatedAt() { + return createdAt; + } + + public Date getModifiedAt() { + return modifiedAt; + } + + public int getSort() { + return sort; + } + + @Override + public String toString() { + return "Checklist{" + + "id='" + id + '\'' + + ", cardId='" + cardId + '\'' + + ", title='" + title + '\'' + + ", items=" + items + + ", finishedAt=" + finishedAt + + ", createdAt=" + createdAt + + ", modifiedAt=" + modifiedAt + + ", sort=" + sort + + '}'; + } +} diff --git a/wrapper/src/main/java/wekan/wrapper/entity/ChecklistItem.java b/wrapper/src/main/java/wekan/wrapper/entity/ChecklistItem.java new file mode 100644 index 0000000..bc43118 --- /dev/null +++ b/wrapper/src/main/java/wekan/wrapper/entity/ChecklistItem.java @@ -0,0 +1,70 @@ +package wekan.wrapper.entity; + +import com.google.gson.annotations.SerializedName; + +import java.util.Date; + +public class ChecklistItem { + @SerializedName("_id") + private String id; + private String title; + private String checklistId; + private String cardId; + private String userId; + private int sort; + private Date createdAt; + private Date modifiedAt; + private boolean isFinished; + + public String getId() { + return id; + } + + public String getTitle() { + return title; + } + + public String getChecklistId() { + return checklistId; + } + + public String getCardId() { + return cardId; + } + + public String getUserId() { + return userId; + } + + public int getSort() { + return sort; + } + + public Date getCreatedAt() { + return createdAt; + } + + public Date getModifiedAt() { + return modifiedAt; + } + + public boolean isFinished() { + return isFinished; + } + + @Override + public String toString() { + return "ChecklistItem{" + + "id='" + id + '\'' + + ", title='" + title + '\'' + + ", checklistId='" + checklistId + '\'' + + ", cardId='" + cardId + '\'' + + ", userId='" + userId + '\'' + + ", sort=" + sort + + ", createdAt=" + createdAt + + ", modifiedAt=" + modifiedAt + + ", isFinished=" + isFinished + + '}'; + } +} + diff --git a/wrapper/src/main/java/wekan/wrapper/entity/ChecklistItemStatus.java b/wrapper/src/main/java/wekan/wrapper/entity/ChecklistItemStatus.java new file mode 100644 index 0000000..f7f9cdb --- /dev/null +++ b/wrapper/src/main/java/wekan/wrapper/entity/ChecklistItemStatus.java @@ -0,0 +1,9 @@ +package wekan.wrapper.entity; + +public class ChecklistItemStatus { + private boolean isFinished; + + public ChecklistItemStatus(boolean isFinished) { + this.isFinished = isFinished; + } +} diff --git a/wrapper/src/main/java/wekan/wrapper/entity/ChecklistPrototype.java b/wrapper/src/main/java/wekan/wrapper/entity/ChecklistPrototype.java new file mode 100644 index 0000000..1f36dce --- /dev/null +++ b/wrapper/src/main/java/wekan/wrapper/entity/ChecklistPrototype.java @@ -0,0 +1,57 @@ +package wekan.wrapper.entity; + +import java.util.ArrayList; +import java.util.List; + +public class ChecklistPrototype { + private String title; + private List items; + + private ChecklistPrototype(String title, List items) { + this.title = title; + this.items = items; + } + + public static class Builder { + private String title; + private List items; + + public Builder() { + items = new ArrayList<>(); + } + + /** + * Create a new ChecklistPrototype. + * + * @return The ChecklistPrototype + */ + public ChecklistPrototype build() { + return new ChecklistPrototype( + title, + items + ); + } + + /** + * Set the title of the new checklist + * + * @param title The title of the new board + * @return The builder + */ + public Builder setTitle(String title) { + this.title = title; + return this; + } + + /** + * Add a new checklist item to the checklist + * + * @param item The item to add to the checklist + * @return The builder + */ + public Builder addItems(String item) { + items.add(item); + return this; + } + } +} diff --git a/wrapper/src/test/java/wekan/wrapper/api/ChecklistServiceTest.java b/wrapper/src/test/java/wekan/wrapper/api/ChecklistServiceTest.java new file mode 100644 index 0000000..99d9784 --- /dev/null +++ b/wrapper/src/test/java/wekan/wrapper/api/ChecklistServiceTest.java @@ -0,0 +1,294 @@ +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.*; + +import java.io.IOException; +import java.net.HttpURLConnection; +import java.util.List; + +import static org.junit.Assert.*; + +public class ChecklistServiceTest { + private MockWebServer mockWebServer = new MockWebServer(); + private ChecklistService service = null; + + @Before + public void setUp() { + try { + mockWebServer.start(); + service = new Retrofit.Builder() + .baseUrl(mockWebServer.url("/")) + .addConverterFactory(GsonConverterFactory.create()) + .build() + .create(ChecklistService.class); + } catch (IOException e) { + e.printStackTrace(); + } + + } + + @After + public void teardown() { + try { + mockWebServer.shutdown(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + @Test + public void getAllChecklist() { + MockResponse response = new MockResponse() + .setResponseCode(HttpURLConnection.HTTP_OK) + .setBody( + "[ " + CHECKLIST_1 + ", " + CHECKLIST_2 + " ]" + ); + + mockWebServer.enqueue(response); + + try { + List checklists = service.getAllChecklists( + "board id", "card id" + ). + execute().body(); + + assertNotNull(checklists); + assertEquals(2, checklists.size()); + + } catch (IOException e) { + e.printStackTrace(); + } + } + + @Test + public void postChecklist() { + MockResponse response = new MockResponse() + .setResponseCode(HttpURLConnection.HTTP_OK) + .setBody( + CHECKLIST_2 + ); + + mockWebServer.enqueue(response); + + try { + Checklist checklist = service.postChecklist( + "board id", + "card id", + "title" + ) + .execute().body(); + + assertNotNull(checklist); + + } catch (IOException e) { + e.printStackTrace(); + } + } + + @Test + public void postChecklistChecklistPrototype() { + MockResponse response = new MockResponse() + .setResponseCode(HttpURLConnection.HTTP_OK) + .setBody( + CHECKLIST_2 + ); + + mockWebServer.enqueue(response); + + try { + Checklist checklist = service.postChecklist( + "board id", + "card id", + new ChecklistPrototype.Builder() + .setTitle("title") + .addItems("item 1") + .addItems("item 2") + .build() + ) + .execute().body(); + + assertNotNull(checklist); + } catch (IOException e) { + e.printStackTrace(); + } + } + + @Test + public void getChecklist() { + MockResponse response = new MockResponse() + .setResponseCode(HttpURLConnection.HTTP_OK) + .setBody( + CHECKLIST_1 + ); + + mockWebServer.enqueue(response); + + try { + Checklist checklist = service.getChecklist( + "board id", + "card id", + "checklist id" + ) + .execute().body(); + + assertNotNull(checklist); + } catch (IOException e) { + e.printStackTrace(); + } + } + + @Test + public void deleteChecklist() { + MockResponse response = new MockResponse() + .setResponseCode(HttpURLConnection.HTTP_OK) + .setBody( + "{ \"_id\": \"jdisfoSIkf0\" }" + ); + + mockWebServer.enqueue(response); + + try { + Checklist checklist = service.deleteChecklist( + "board id", + "card id", + "checklist id" + ) + .execute().body(); + + assertNotNull(checklist); + } catch (IOException e) { + e.printStackTrace(); + } + } + + @Test + public void getChecklistItem() { + MockResponse response = new MockResponse() + .setResponseCode(HttpURLConnection.HTTP_OK) + .setBody(ITEM); + + mockWebServer.enqueue(response); + + try { + ChecklistItem item = service.getChecklistItem( + "board id", + "card id", + "checklist id", + "item id" + ) + .execute().body(); + + assertNotNull(item); + } catch (IOException e) { + e.printStackTrace(); + } + } + + @Test + public void editChecklistItemTitle() { + MockResponse response = new MockResponse() + .setResponseCode(HttpURLConnection.HTTP_OK) + .setBody("{ \"_id\": \"id\" }"); + + mockWebServer.enqueue(response); + + try { + ChecklistItem item = service.editChecklistItem( + "board id", + "card id", + "checklist id", + "item id", + "new title" + ) + .execute().body(); + + assertNotNull(item); + } catch (IOException e) { + e.printStackTrace(); + } + } + + @Test + public void editChecklistItemState() { + MockResponse response = new MockResponse() + .setResponseCode(HttpURLConnection.HTTP_OK) + .setBody("{ \"_id\": \"id\" }"); + + mockWebServer.enqueue(response); + + try { + ChecklistItem item = service.editChecklistItem( + "board id", + "card id", + "checklist id", + "item id", + new ChecklistItemStatus(true) + ) + .execute().body(); + + assertNotNull(item); + } catch (IOException e) { + e.printStackTrace(); + } + } + + @Test + public void deleteChecklistItem() { + MockResponse response = new MockResponse() + .setResponseCode(HttpURLConnection.HTTP_OK) + .setBody("{ \"_id\": \"id\" }"); + + mockWebServer.enqueue(response); + + try { + ChecklistItem item = service.deleteChecklistItem( + "board id", + "card id", + "checklist id", + "item id" + ) + .execute().body(); + + assertNotNull(item); + } catch (IOException e) { + e.printStackTrace(); + } + } + + private static final String CHECKLIST_1 = "{" + + "\"_id\":\"Tv6FuHLWaDR4Z3ig2\"," + + "\"title\":\"clist 1\"," + + "\"cardId\":\"bndtZCrJ9kshcFuw8\"," + + "\"sort\":0," + + "\"createdAt\":\"2019-11-13T18:22:59.987Z\"," + + "\"modifiedAt\":\"2019-11-13T19:34:48.650Z\"," + + "\"items\":[" + + "{\"_id\":\"9J8n6d6u3TkCAGvhw\",\"title\":\"item 1\",\"isFinished\":false}," + + "{\"_id\":\"iehKbi7qFruCyMRNQ\",\"title\":\"item 2\",\"isFinished\":false}" + + "]" + + "}"; + + private static final String CHECKLIST_2 = "{" + + "\"_id\":\"Tv6FuHLWaDR4Z3ig2\"," + + "\"title\":\"clist 1\"" + + "}"; + + private static final String ITEM = "{" + + "\"_id\":\"3HhzyoYBYGSr4mLdY\"," + + "\"title\":\"cosa da fare 1\"," + + "\"checklistId\":\"LyE7odMhtqZrgrXL9\"," + + "\"cardId\":\"bndtZCrJ9kshcFuw8\"," + + "\"sort\":0," + + "\"isFinished\":true," + + "\"createdAt\":\"2019-11-14T16:12:46.672Z\"," + + "\"modifiedAt\":\"2019-11-14T16:17:16.159Z\"," + + "\"userId\":\"Si69gNgkJfQuk6uiJ\"" + + "}"; +}