wedroid/wrapper/src/test/java/it/unisannio/ding/ids/wedroid/wrapper/api/BoardServiceTest.java

220 lines
9.1 KiB
Java

package it.unisannio.ding.ids.wedroid.wrapper.api;
import static junit.framework.TestCase.assertNotNull;
import static junit.framework.TestCase.assertTrue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import it.unisannio.ding.ids.wedroid.wrapper.entity.Board;
import it.unisannio.ding.ids.wedroid.wrapper.entity.BoardBackgroundColor;
import it.unisannio.ding.ids.wedroid.wrapper.entity.BoardPermission;
import it.unisannio.ding.ids.wedroid.wrapper.entity.BoardPrototype;
import it.unisannio.ding.ids.wedroid.wrapper.entity.PresentParentTask;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.List;
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;
public class BoardServiceTest {
private MockWebServer mockWebServer = new MockWebServer();
private BoardService service = null;
@Before
public void setUp() {
try {
mockWebServer.start();
service = new Retrofit.Builder()
.baseUrl(mockWebServer.url("/"))
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(BoardService.class);
} catch (IOException e) {
e.printStackTrace();
}
}
@After
public void teardown() {
try {
mockWebServer.shutdown();
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void getPublicBoard() {
MockResponse response = new MockResponse()
.setResponseCode(HttpURLConnection.HTTP_OK)
.setBody(
"[{\"_id\":\"e7pJ3T7WciDz5P2v0\",\"title\":\"board 0\"}," +
"{\"_id\":\"e7pJ3T7WciDz5P2v1\",\"title\":\"board 1\"}]"
);
mockWebServer.enqueue(response);
try {
List<Board> boards = service.getPublicBoards()
.execute().body();
assertNotNull(boards);
assertEquals(2, boards.size());
for (int i = 0; i < boards.size(); i++) {
assertEquals("e7pJ3T7WciDz5P2v" + i, boards.get(i).getId());
assertEquals("board " + i, boards.get(i).getTitle());
}
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void newBoardTest() {
MockResponse response = new MockResponse()
.setResponseCode(HttpURLConnection.HTTP_OK)
.setBody(
"{\"_id\":\"cYGkSRfKfEEFDQjv2\",\"defaultSwimlaneId\":\"TffifkXqDvwz9LJNs\"}"
);
mockWebServer.enqueue(response);
try {
Board board = service.newBoard(
new BoardPrototype.Builder()
.setTitle("title")
.setOwner("owner")
.build()
)
.execute().body();
assertNotNull(board);
assertEquals("cYGkSRfKfEEFDQjv2", board.getId());
assertEquals("TffifkXqDvwz9LJNs", board.getDefaultSwimlaneId());
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void getBoardTest() {
MockResponse response = new MockResponse()
.setResponseCode(HttpURLConnection.HTTP_OK)
.setBody(board1);
mockWebServer.enqueue(response);
try {
Board board = service.getBoard("id")
.execute().body();
assertNotNull(board);
assertEquals("id1", board.getId());
assertEquals("my title1", board.getTitle());
assertEquals("my-title-1", board.getSlug());
assertFalse(board.isArchived());
assertEquals(0, board.getStarts());
assertEquals(BoardPermission.PRIVATE, board.getPermission());
assertEquals(BoardBackgroundColor.CORTEZA, board.getBackgroundColor());
assertTrue(board.isAllowsSubtasks());
assertEquals(PresentParentTask.NO_PARENT, board.getPresentParentTask());
assertEquals(0, board.getSpentTime());
assertFalse(board.isOvertime());
assertEquals("board", board.getType());
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void getBoardsFromUser() {
MockResponse response = new MockResponse()
.setResponseCode(HttpURLConnection.HTTP_OK)
.setBody("[ " + board1 + ", " + board2 + ", " + board3 + " ]");
mockWebServer.enqueue(response);
try {
List<Board> boards = service.getBoardsFromUser("user-id")
.execute().body();
assertNotNull(boards);
assertEquals(3, boards.size());
} catch (IOException e) {
e.printStackTrace();
}
}
private static final String board1 =
"{" +
"\"_id\":\"id1\"," +
"\"title\":\"my title1\"," +
"\"members\":[{\"userId\":\"Si69gNgkJfQuk6uiJ\",\"isAdmin\":true,\"isActive\":true,\"isNoComments\":false,\"isCommentOnly\":false}]," +
"\"permission\":\"private\"," +
"\"color\":\"corteza\"," +
"\"slug\":\"my-title-1\"," +
"\"archived\":false," +
"\"createdAt\":\"2019-11-09T10:01:21.280Z\"," +
"\"modifiedAt\":\"2019-11-09T10:01:21.280Z\"," +
"\"stars\":0," +
"\"labels\":[{\"color\":\"green\",\"_id\":\"3NvZnG\",\"name\":\"\"},{\"color\":\"yellow\",\"_id\":\"AcBqR9\",\"name\":\"\"},{\"color\":\"orange\",\"_id\":\"JxEw9Z\",\"name\":\"\"},{\"color\":\"red\",\"_id\":\"grdRCS\",\"name\":\"\"},{\"color\":\"purple\",\"_id\":\"buMfKA\",\"name\":\"\"},{\"color\":\"blue\",\"_id\":\"sbi9FZ\",\"name\":\"\"}]," +
"\"subtasksDefaultBoardId\":null," +
"\"subtasksDefaultListId\":null," +
"\"allowsSubtasks\":true," +
"\"presentParentTask\":\"no-parent\"," +
"\"isOvertime\":false," +
"\"type\":\"board\"" +
"}";
private static final String board2 =
"{" +
"\"_id\":\"id2\"," +
"\"title\":\"my title2\"," +
"\"members\":[{\"userId\":\"Si69gNgkJfQuk6uiJ\",\"isAdmin\":true,\"isActive\":true,\"isNoComments\":false,\"isCommentOnly\":false}]," +
"\"permission\":\"private\"," +
"\"color\":\"corteza\"," +
"\"slug\":\"my-title-2\"," +
"\"archived\":false," +
"\"createdAt\":\"2019-11-09T10:01:21.280Z\"," +
"\"modifiedAt\":\"2019-11-09T10:01:21.280Z\"," +
"\"stars\":0," +
"\"labels\":[{\"color\":\"green\",\"_id\":\"3NvZnG\",\"name\":\"\"},{\"color\":\"yellow\",\"_id\":\"AcBqR9\",\"name\":\"\"},{\"color\":\"orange\",\"_id\":\"JxEw9Z\",\"name\":\"\"},{\"color\":\"red\",\"_id\":\"grdRCS\",\"name\":\"\"},{\"color\":\"purple\",\"_id\":\"buMfKA\",\"name\":\"\"},{\"color\":\"blue\",\"_id\":\"sbi9FZ\",\"name\":\"\"}]," +
"\"subtasksDefaultBoardId\":null," +
"\"subtasksDefaultListId\":null," +
"\"allowsSubtasks\":true," +
"\"presentParentTask\":\"no-parent\"," +
"\"isOvertime\":false," +
"\"type\":\"board\"" +
"}";
private static final String board3 =
"{" +
"\"_id\":\"id3\"," +
"\"title\":\"my title3\"," +
"\"members\":[{\"userId\":\"Si69gNgkJfQuk6uiJ\",\"isAdmin\":true,\"isActive\":true,\"isNoComments\":false,\"isCommentOnly\":false}]," +
"\"permission\":\"private\"," +
"\"color\":\"corteza\"," +
"\"slug\":\"my-title-3\"," +
"\"archived\":false," +
"\"createdAt\":\"2019-11-09T10:01:21.280Z\"," +
"\"modifiedAt\":\"2019-11-09T10:01:21.280Z\"," +
"\"stars\":0," +
"\"labels\":[{\"color\":\"green\",\"_id\":\"3NvZnG\",\"name\":\"\"},{\"color\":\"yellow\",\"_id\":\"AcBqR9\",\"name\":\"\"},{\"color\":\"orange\",\"_id\":\"JxEw9Z\",\"name\":\"\"},{\"color\":\"red\",\"_id\":\"grdRCS\",\"name\":\"\"},{\"color\":\"purple\",\"_id\":\"buMfKA\",\"name\":\"\"},{\"color\":\"blue\",\"_id\":\"sbi9FZ\",\"name\":\"\"}]," +
"\"subtasksDefaultBoardId\":null," +
"\"subtasksDefaultListId\":null," +
"\"allowsSubtasks\":true," +
"\"presentParentTask\":\"no-parent\"," +
"\"isOvertime\":false," +
"\"type\":\"board\"" +
"}";
}