package de.hftstuttgart.unifiedticketing.systems.github; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ArrayNode; import de.hftstuttgart.unifiedticketing.core.Logging; import de.hftstuttgart.unifiedticketing.exceptions.DeserializationException; import de.hftstuttgart.unifiedticketing.exceptions.HttpResponseException; import okhttp3.*; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.ArgumentCaptor; import java.io.IOException; import java.util.*; import java.util.logging.Level; import java.util.stream.Collectors; import static org.junit.jupiter.api.Assertions.*; import static org.mockito.Mockito.*; public class GithubFilterTest { public GithubFilter instance; public Call call; public ArgumentCaptor requestCaptor; public Response.Builder responseBuilder; @BeforeAll public static void initBeforeAll() { Logging.setLevel(Level.ALL); } @BeforeEach public void initBeforeEach() { GithubTicketSystem parent = spy(new GithubTicketSystem("someHeader", "https://api.github.com")); instance = spy(new GithubFilter(parent)); OkHttpClient client = mock(OkHttpClient.class); call = mock(Call.class); requestCaptor = ArgumentCaptor.forClass(Request.class); responseBuilder = new Response.Builder() .request(new Request.Builder().url("http://test.some.tld/").build()) .protocol(Protocol.HTTP_1_1) .message("some message"); doReturn(client).when(instance).getHttpClient(); doReturn(call).when(client).newCall(requestCaptor.capture()); } @Test public void testGetServerError() throws IOException { doReturn(responseBuilder.code(500).build()).when(call).execute(); assertThrows(HttpResponseException.class, () -> instance.get()); } @Test public void testGetClientError() throws IOException { doReturn(responseBuilder.code(400).build()).when(call).execute(); assertThrows(HttpResponseException.class, () -> instance.get()); } @Test public void testGetNullBody() throws IOException { doReturn(responseBuilder.code(200).build()).when(call).execute(); assertThrows(HttpResponseException.class, () -> instance.get()); } @Test public void testGetNoJsonBody() throws IOException { doReturn( responseBuilder .code(200) .body(ResponseBody.create("somestrangething", MediaType.get("application/json"))) .build()) .when(call).execute(); assertThrows(DeserializationException.class, () -> instance.get()); } @Test public void testGetRequestParamsPart1() throws IOException { doReturn( responseBuilder .code(200) .body(ResponseBody.create("[]".getBytes(), MediaType.get("application/json"))) .build()) .when(call).execute(); String[] values = new String[] { "bug", "label1", "2", "50", }; instance .withLabel(values[0]) .withLabel(values[1]) .setPage(Integer.parseInt(values[2])) .setPageSize(Integer.parseInt(values[3])) .isOpen() .get(); HttpUrl url = requestCaptor.getValue().url(); assertAll( () -> assertEquals(String.format("%s,%s", values[0], values[1]), url.queryParameter("labels")), () -> assertEquals(values[2], url.queryParameter("page")), () -> assertEquals(values[3], url.queryParameter("per_page")), () -> assertEquals("open", url.queryParameter("state")) ); } @Test public void testGetRequestParamsPart2() throws IOException { doReturn( responseBuilder .code(200) .body(ResponseBody.create("[]".getBytes(), MediaType.get("application/json"))) .build()) .when(call).execute(); instance .isClosed() .get(); HttpUrl url = requestCaptor.getValue().url(); assertAll( () -> assertEquals("closed", url.queryParameter("state")) ); } @Test public void testGetLocalFilter() throws IOException { GithubTicketResponse res = new GithubTicketResponse(); res.number = 5; res.title = "some title"; res.body = "descriptive text"; res.assignees = new HashSet<>(); res.state = "open"; res.labels = Arrays.stream(new String[]{"unifiedticketing", "bug"}) .map(l -> { GithubTicketResponse.Label label = new GithubTicketResponse.Label(); label.name = l; return label; }) .collect(Collectors.toSet()); ObjectMapper mapper = new ObjectMapper(); ArrayNode arrayNode = mapper.createArrayNode(); arrayNode.add(mapper.valueToTree(res)); res.number = 8; res.title = "some special title"; arrayNode.add(mapper.valueToTree(res)); res.number = 94; res.body = "description with @username marked"; arrayNode.add(mapper.valueToTree(res)); doReturn( responseBuilder .code(200) .body(ResponseBody.create(arrayNode.toString().getBytes(), MediaType.get("application/json"))) .build()) .when(call).execute(); List result = instance .withTitleMatch("^.*special.*$") .withDescriptionMatch("^.*@username.*$") .get(); assertAll( () -> assertEquals(1, result.size()), () -> Assertions.assertEquals("94", result.get(0).getId()) ); } @Test public void testGetDeserialization() throws IOException { GithubTicketResponse res = new GithubTicketResponse(); res.number = 99; res.title = "some title"; res.body = "descriptive text"; res.assignees = new HashSet<>(); res.state = "open"; res.labels = Arrays.stream(new String[]{"unifiedticketing", "feature-request"}) .map(l -> { GithubTicketResponse.Label label = new GithubTicketResponse.Label(); label.name = l; return label; }) .collect(Collectors.toSet()); ObjectMapper mapper = new ObjectMapper(); ArrayNode arrayNode = mapper.createArrayNode(); arrayNode.add(mapper.valueToTree(res)); doReturn( responseBuilder .code(200) .body(ResponseBody.create(arrayNode.toString().getBytes(), MediaType.get("application/json"))) .build()) .when(call).execute(); List expected = new LinkedList<>( Collections.singleton(GithubTicket.fromTicketResponse(instance.parent, res))); List actual = instance.get(); assertEquals(expected, actual); assertTrue(expected.get(0).deepEquals(actual.get(0))); } }