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(); } } 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\"" + "}"; }