package it.unisannio.ding.ids.wedroid.wrapper.api; import it.unisannio.ding.ids.wedroid.wrapper.entity.Checklist; import it.unisannio.ding.ids.wedroid.wrapper.entity.ChecklistItem; import it.unisannio.ding.ids.wedroid.wrapper.entity.ChecklistItemStatus; import it.unisannio.ding.ids.wedroid.wrapper.entity.ChecklistPrototype; 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 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\"" + "}"; }