GithubTicketSystemBuilderTest.java 3.38 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
package de.hftstuttgart.unifiedticketing.systems.github;

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

import de.hftstuttgart.unifiedticketing.core.Logging;
import de.hftstuttgart.unifiedticketing.exceptions.AssertionException;
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.Mockito;

import java.util.logging.Level;
import java.util.regex.Pattern;

public class GithubTicketSystemBuilderTest
{
    public GithubTicketSystemBuilder instance;

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

    @BeforeEach
    public void initBeforeEach()
    {
        instance = Mockito.spy(new GithubTicketSystemBuilder());
    }

    @Test
    public void testInit()
    {
        assertAll(
            () -> assertEquals("application/vnd.github.v3+json", instance.acceptHeader),
            () -> assertNull(instance.apiKey),
            () -> assertNull(instance.owner),
            () -> assertNull(instance.repo),
            () -> assertTrue(instance.https),
            () -> assertNull(instance.username)
        );
    }

    @Test
    public void testWithApiKey()
    {
        String username = "someUser";
        String apiKey = "asoashdboahasjhdfbeuazhvfef4q34v3h4v435v2";
        instance.withAuthentication(username, apiKey);
        assertAll(
            () -> assertEquals(username, instance.username),
            () -> assertEquals(apiKey, instance.apiKey)
        );
    }

    @Test
    public void testWithHttp()
    {
        instance.https = true;
        instance.withHttp();

        assertFalse(instance.https);
    }

    @Test
    public void testWithHttps()
    {
        instance.https = false;
        instance.withHttps();
        assertTrue(instance.https);
    }

    @Test
    public void testWithProjectIdByInt()
    {
        String expected = "255254123";
        instance.withRepo(expected);

        assertEquals(expected, instance.repo);
    }

    @Test
    public void testBuild()
    {
        instance.withBaseUrl("blabla.tld");
        assertThrows(AssertionException.class, () -> instance.build());

        String apiKey = "aaesfsef32qqfq3f";
        String baseUrl = "api.github.com";
        String owner = "someOwner";
        String repo = "42";
        String username = "someUsername";
        instance = new GithubTicketSystemBuilder();
        instance.withOwner(owner);
        instance.withRepo(repo);
        assertThrows(AssertionException.class, () -> instance.build());

        instance = new GithubTicketSystemBuilder();
        String regex = "^%s://%s/repos/%s/%s/issues$";
        Pattern pattern = Pattern.compile(
            String.format(regex, "https", baseUrl, owner, repo));

        instance.withBaseUrl(baseUrl)
            .withOwner(owner)
            .withRepo(repo);
        GithubTicketSystem system = instance.build();
        assertTrue(pattern.matcher(system.baseUrl).matches());

        pattern = Pattern.compile(String.format(regex, "http", baseUrl, owner, repo));
        instance
            .withHttp()
            .withAuthentication(username, apiKey);
        system = instance.build();
        assertTrue(pattern.matcher(system.baseUrl).matches());
        assertEquals(username, system.username);
        assertEquals(apiKey, system.apiKey);
    }
}