GithubTicketSystemTest.java 6.99 KB
Newer Older
1
2
package de.hftstuttgart.unifiedticketing.systems.github;

3
4
5
6
7
8
9
10
11
12
13
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import de.hftstuttgart.unifiedticketing.core.Logging;
import de.hftstuttgart.unifiedticketing.core.TicketSystem;
import de.hftstuttgart.unifiedticketing.exceptions.AssertionException;
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;
14
import org.junit.jupiter.api.Test;
15
16
17
18
19
20
import org.mockito.ArgumentCaptor;

import java.io.IOException;
import java.util.*;
import java.util.logging.Level;
import java.util.stream.Collectors;
21
22

import static org.junit.jupiter.api.Assertions.*;
23
import static org.mockito.Mockito.*;
24
25
26

public class GithubTicketSystemTest
{
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
    public GithubTicketSystem instance;
    public Call call;
    public ArgumentCaptor<Request> requestCaptor;
    public Response.Builder responseBuilder;

    @BeforeAll
    public static void initBeforeAll()
    {
        Logging.setLevel(Level.ALL);
    }

    @BeforeEach
    public void initBeforeEach()
    {
        instance = spy(new GithubTicketSystem("someHeader", "https://api.github.com"));

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

55
    @Test
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
    public void testFromUri()
    {
        String base = "api.github.com";
        String owner = "myOwner";
        String repo = "myRepo";
        String username = "someUser";
        String apikey = "afdaf3aqraf3afafmyxcbvmyxvbas3wrawra";
        GithubTicketSystem actual = (GithubTicketSystem) TicketSystem.fromUri(buildTestUri(true, base, owner, repo, username, apikey));

        assertEquals(buildFinalBaseurl(true, base, owner, repo), actual.baseUrl);
        assertEquals(username, actual.username);
        assertEquals(apikey, actual.apiKey);
        assertEquals(GithubTicketSystem.class, actual.getClass());

        base = "api.github.somedomain.tld:8080";
        owner = "otherOwner";
        repo = "otherRepo";
        username = "me";
        apikey = "a34adfae3ra3rasd";
        actual = (GithubTicketSystem) TicketSystem.fromUri(buildTestUri(false, base, owner, repo, username, apikey));

        assertEquals(buildFinalBaseurl(false, base, owner, repo), actual.baseUrl);
        assertEquals(username, actual.username);
        assertEquals(apikey, actual.apiKey);
        assertEquals(GithubTicketSystem.class, actual.getClass());

        actual = (GithubTicketSystem) TicketSystem.fromUri(buildTestUri(false, base, owner, repo, null, null));
        assertEquals(buildFinalBaseurl(false, base, owner, repo), actual.baseUrl);
        assertNull(actual.username);
        assertNull(actual.apiKey);
        assertEquals(GithubTicketSystem.class, actual.getClass());

        assertThrows(AssertionException.class, () -> TicketSystem.fromUri("unifiedticketing:github:blablabal"));
    }

    @Test
    public void testCreateTicket()
    {
        assertEquals(GithubTicketBuilder.class, instance.createTicket().getClass());
        assertSame(instance, instance.createTicket().parent);
    }

    @Test
    public void testFind()
    {
        assertEquals(GithubFilter.class, instance.find().getClass());
        assertSame(instance, instance.find().parent);
    }

    @Test
    public void testGetServerError() throws IOException
    {
        doReturn(responseBuilder.code(500).build()).when(call).execute();
        assertThrows(HttpResponseException.class, () -> instance.getTicketById("bla"));
    }

    @Test
    public void testGetClientError() throws IOException
    {
        doReturn(responseBuilder.code(400).build()).when(call).execute();
        assertThrows(HttpResponseException.class, () -> instance.getTicketById("bla"));
    }

    @Test
    public void testGetNullBody() throws IOException
    {
        doReturn(responseBuilder.code(200).build()).when(call).execute();
        assertThrows(HttpResponseException.class, () -> instance.getTicketById("bla"));
    }

    @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.getTicketById("bla"));
    }

    @Test
    public void testGetTicketById() 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();

        doReturn(
            responseBuilder
                .code(200)
                .body(ResponseBody.create(mapper.writeValueAsString(res), MediaType.get("application/json")))
                .build())
            .when(call).execute();

        GithubTicket expected = GithubTicket.fromTicketResponse(instance, res);
        GithubTicket actual = instance.getTicketById(res.number);

        assertEquals(expected, actual);
        assertTrue(expected.deepEquals(actual));
    }

    @Test
    public void testSupport()
    {
        assertAll(
            () -> assertTrue(instance.hasAssigneeSupport()),
            () -> assertTrue(instance.hasDefaultPagination()),
            () -> assertTrue(instance.hasLabelSupport()),
            () -> assertTrue(instance.hasPaginationSupport()),
            () -> assertTrue(instance.hasReturnNullOnErrorSupport())
        );
    }

    private static String buildTestUri(boolean https, String base, String owner, String repo, String username, String apikey)
    {
        return String.format(
            "unifiedticketing:github:%s://%s::%s:%s%s",
            (https) ? "https": "http",
            base,
            owner,
            repo,
            (username == null || apikey == null) ? "" : ":" + username + ":" + apikey);
    }

    private static String buildFinalBaseurl(boolean https, String base, String owner, String repo)
195
    {
196
        return String.format("%s://%s/repos/%s/%s/issues", (https) ? "https" : "http", base, owner, repo);
197
198
    }
}