package it.unisannio.ding.ids.wedroid.wrapper.api; import it.unisannio.ding.ids.wedroid.wrapper.entity.WList; 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 java.io.IOException; import java.net.HttpURLConnection; import java.util.List; import static org.junit.Assert.*; public class ListServiceTest { private MockWebServer mockWebServer = new MockWebServer(); private ListService service = null; @Before public void setUp() { try { mockWebServer.start(); service = new Retrofit.Builder() .baseUrl(mockWebServer.url("/")) .addConverterFactory(GsonConverterFactory.create()) .build() .create(ListService.class); } catch (IOException e) { e.printStackTrace(); } } @After public void teardown() { try { mockWebServer.shutdown(); } catch (IOException e) { e.printStackTrace(); } } @Test public void getAllList() { MockResponse response = new MockResponse() .setResponseCode(HttpURLConnection.HTTP_OK) .setBody( "[ " + WLIST_1 + ", " + WLIST_2 + " ]" ); mockWebServer.enqueue(response); try { List lists = service.getAllList("boardId") .execute().body(); assertNotNull(lists); assertEquals(2, lists.size()); } catch (IOException e) { e.printStackTrace(); } } @Test public void getList() { MockResponse response = new MockResponse() .setResponseCode(HttpURLConnection.HTTP_OK) .setBody(WLIST_1); mockWebServer.enqueue(response); try { WList list = service.getList("boardId", "listId") .execute().body(); assertNotNull(list); } catch (IOException e) { e.printStackTrace(); } } @Test public void newList() { MockResponse response = new MockResponse() .setResponseCode(HttpURLConnection.HTTP_OK) .setBody(WLIST_2); mockWebServer.enqueue(response); try { WList list = service.newList("boardId", "body") .execute().body(); assertNotNull(list); } catch (IOException e) { e.printStackTrace(); } } @Test public void deleteList() { MockResponse response = new MockResponse() .setResponseCode(HttpURLConnection.HTTP_OK) .setBody("{ \"_id\": \"list id\" }"); mockWebServer.enqueue(response); try { WList list = service.deleteList("boardId", "listId") .execute().body(); assertNotNull(list); } catch (IOException e) { e.printStackTrace(); } } private static final String WLIST_1 = "{\"_id\":\"j3gzdbCJQYGnzuLhu\"," + "\"title\":\"list 1\"," + "\"boardId\":\"9QttSbCKjJ8v7zB3g\"," + "\"sort\":1," + "\"type\":\"list\"," + "\"starred\":false," + "\"archived\":false," + "\"swimlaneId\":\"\"," + "\"createdAt\":\"2019-11-14T10:34:14.120Z\"," + "\"updatedAt\":\"2019-11-14T10:34:14.120Z\"," + "\"modifiedAt\":\"2019-11-14T10:34:14.666Z\"," + "\"wipLimit\":{\"value\":1,\"enabled\":false,\"soft\":false}" + "}"; private static final String WLIST_2 = "{\"_id\":\"j3gzdbCJQYGnzuLhu\"," + "\"title\":\"list 2\"," + "\"boardId\":\"9QttSbCKjJ8v7zB3d\"," + "\"sort\":1," + "\"type\":\"list\"," + "\"starred\":false," + "\"archived\":false," + "\"swimlaneId\":\"\"," + "\"createdAt\":\"2019-11-14T10:34:14.120Z\"," + "\"updatedAt\":\"2019-11-14T10:34:14.120Z\"," + "\"modifiedAt\":\"2019-11-14T10:34:14.666Z\"," + "\"wipLimit\":{\"value\":1,\"enabled\":false,\"soft\":false}" + "}"; }