GithubTicketBuilderTest.java 5.62 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
55
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
package de.hftstuttgart.unifiedticketing.systems.github;

import com.fasterxml.jackson.databind.ObjectMapper;
import de.hftstuttgart.unifiedticketing.core.Logging;
import de.hftstuttgart.unifiedticketing.exceptions.AssertionException;
import de.hftstuttgart.unifiedticketing.exceptions.DeserializationException;
import de.hftstuttgart.unifiedticketing.exceptions.HttpResponseException;
import okhttp3.*;
import okio.Buffer;
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.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.stream.Collectors;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

public class GithubTicketBuilderTest
{
    public GithubTicketBuilder 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()
    {
        GithubTicketSystem parent =
            spy(new GithubTicketSystem("something", "https://example.org"));
        instance = spy(new GithubTicketBuilder(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 testSaveNoCreationWithoutMinimumRequirements()
    {
        assertThrows(AssertionException.class, () -> instance.create());
        verify(instance, never()).getHttpClient();
    }

    @Test
    public void testSaveServerError() throws IOException
    {
        doReturn(responseBuilder.code(500).build()).when(call).execute();
        instance.title("we have a new title");
        assertThrows(HttpResponseException.class, () -> instance.create());
    }

    @Test
    public void testSaveClientError() throws IOException
    {
        doReturn(responseBuilder.code(400).build()).when(call).execute();
        instance.title("we have another new title");
        assertThrows(HttpResponseException.class, () -> instance.create());
    }

    @Test
    public void testSaveNullBody() throws IOException
    {
        doReturn(responseBuilder.code(200).build()).when(call).execute();
        instance.title("we have no body this time");
        assertThrows(HttpResponseException.class, () -> instance.create());
    }

    @Test
    public void testSaveNoJsonBody() throws IOException
    {
        doReturn(
            responseBuilder
                .code(200)
                .body(ResponseBody.create("somestrangething", MediaType.get("application/json")))
                .build())
            .when(call).execute();
        instance.title("this body is no json");
        assertThrows(DeserializationException.class, () -> instance.create());
    }

    @Test
    public void testSaveSuccessfulUpdate() throws IOException
    {
        GithubTicketResponse ticketResponse = new GithubTicketResponse();
        ticketResponse.title = "title of ticket";
        ticketResponse.body = "description";
        ticketResponse.number = 5;
        ticketResponse.labels = Arrays.stream(new String[]{"unifiedticketing", "feature-request"})
            .map(l -> {
                GithubTicketResponse.Label label = new GithubTicketResponse.Label();
                label.name = l;
                return label;
            })
            .collect(Collectors.toSet());
        GithubTicketResponse.Assignee assignee = new GithubTicketResponse.Assignee();
        assignee.id = 234;
        assignee.login = "username";
        ticketResponse.assignees = new HashSet<>(Collections.singleton(assignee));

        GithubTicket expected = GithubTicket.fromTicketResponse(instance.parent, ticketResponse);
        String responseJson = new ObjectMapper().writeValueAsString(ticketResponse);

        doReturn(
            responseBuilder
                .code(200)
                .body(ResponseBody.create(responseJson.getBytes(), MediaType.get("application/json")))
                .build())
            .when(call).execute();
        GithubTicket actual = instance.title("some title").create();
        assertEquals(expected, actual);
        assertTrue(expected.deepEquals(actual));
    }

    @Test
    public void testSaveRequestJson() throws IOException
    {
        doReturn(
            responseBuilder
                .code(200)
                .body(ResponseBody.create("{}".getBytes(), MediaType.get("application/json")))
                .build())
            .when(call).execute();

        instance.title("title of ticket")
            .description("description")
            .labels(new HashSet<>(Arrays.asList("bug", "unifiedticketing")))
            .assignees("somebody")
            .create();

        Buffer buffer = new Buffer();
        ObjectMapper mapper = new ObjectMapper();
        requestCaptor.getValue().body().writeTo(buffer);
        String expectedJson = "{\"assignees\":[\"somebody\"],\"body\":\"description\"," +
            "\"title\":\"title of ticket\",\"labels\":[\"bug\",\"unifiedticketing\"]}";
        assertEquals(mapper.readTree(expectedJson), mapper.readTree(buffer.readUtf8()));
    }
}