package it.unisannio.ding.ids.wedroid.wrapper.api; import it.unisannio.ding.ids.wedroid.wrapper.entity.Comment; 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.assertEquals; import static org.junit.Assert.assertNotNull; public class CardCommentServiceTest { private MockWebServer mockWebServer = new MockWebServer(); private CardCommentService service = null; @Before public void setUp() { try { mockWebServer.start(); service = new Retrofit.Builder() .baseUrl(mockWebServer.url("/")) .addConverterFactory(GsonConverterFactory.create()) .build() .create(CardCommentService.class); } catch (IOException e) { e.printStackTrace(); } } @After public void teardown() { try { mockWebServer.shutdown(); } catch (IOException e) { e.printStackTrace(); } } @Test public void getAllComments() { MockResponse response = new MockResponse() .setResponseCode(HttpURLConnection.HTTP_OK) .setBody( "[ " + COMMENT_1 + ", " + COMMENT_2 + ", " + COMMENT_3 + " ]" ); mockWebServer.enqueue(response); try { List lists = service.getAllComments("board id", "card id") .execute().body(); assertNotNull(lists); assertEquals(3, lists.size()); } catch (IOException e) { e.printStackTrace(); } } @Test public void postNewComment() { MockResponse response = new MockResponse() .setResponseCode(HttpURLConnection.HTTP_OK) .setBody(COMMENT_1); mockWebServer.enqueue(response); try { Comment comment = service.postNewComment( "board id", "card id", "author id", "comment" ).execute().body(); assertNotNull(comment); assertNotNull(comment.getId()); } catch (IOException e) { e.printStackTrace(); } } @Test public void getComment() { MockResponse response = new MockResponse() .setResponseCode(HttpURLConnection.HTTP_OK) .setBody(COMMENT_3); mockWebServer.enqueue(response); try { Comment comment = service.getComment( "board id", "card id", "comment id" ).execute().body(); assertNotNull(comment); assertNotNull(comment.getAuthorId()); assertNotNull(comment.getComment()); } catch (IOException e) { e.printStackTrace(); } } private final static String COMMENT_1 = "{" + "\"_id\":\"zwzAgjHdW4fNJ9YLc\"," + "\"comment\":\"comment 1\"," + "\"authorId\":\"Si69gNgkJfQuk6uiJ\"" + "}"; private final static String COMMENT_2 = "{" + "\"_id\":\"zwzAgjHdW4fNJ9YLt\"," + "\"comment\":\"comment 2\"," + "\"authorId\":\"Si69gNgkJfQuk6uiJ\"" + "}"; private final static String COMMENT_3 = "{" + "\"_id\":\"DofhD9v96DbX7Wirp\"," + "\"userId\":\"Si69gNgkJfQuk6uiJ\"," + "\"text\":\"commento api\"," + "\"cardId\":\"XQtnuqWEJnEcm6iy2\"," + "\"boardId\":\"TtjXrJyvPkG3xsbkw\"," + "\"createdAt\":\"2019-11-12T20:12:55.749Z\"," + "\"modifiedAt\":\"2019-11-12T20:12:55.749Z\"" + "}"; }