1
2
3
4
5 package com.jcabi.github;
6
7 import com.jcabi.github.mock.MkGitHub;
8 import com.jcabi.http.mock.MkAnswer;
9 import com.jcabi.http.mock.MkContainer;
10 import com.jcabi.http.mock.MkGrizzlyContainer;
11 import com.jcabi.http.request.ApacheRequest;
12 import jakarta.json.Json;
13 import jakarta.json.JsonObject;
14 import java.io.IOException;
15 import java.net.HttpURLConnection;
16 import org.hamcrest.MatcherAssert;
17 import org.hamcrest.Matchers;
18 import org.junit.jupiter.api.Test;
19 import org.junit.jupiter.api.extension.ExtendWith;
20
21
22
23
24
25 @ExtendWith(RandomPort.class)
26 final class RtOrganizationsTest {
27
28
29
30
31
32 @Test
33 void fetchesSingleOrganization() throws IOException {
34 try (
35 MkContainer container = new MkGrizzlyContainer().next(
36 new MkAnswer.Simple(HttpURLConnection.HTTP_OK, "")
37 ).start(RandomPort.port())
38 ) {
39 final Organizations orgs = new RtOrganizations(
40 new MkGitHub(),
41 new ApacheRequest(container.home())
42 );
43 MatcherAssert.assertThat(
44 "Value is null",
45 orgs.get("org"),
46 Matchers.notNullValue()
47 );
48 container.stop();
49 }
50 }
51
52
53
54
55
56
57 @Test
58 void retrievesOrganizations() throws IOException {
59 final GitHub github = new MkGitHub();
60 try (
61 MkContainer container = new MkGrizzlyContainer().next(
62 new MkAnswer.Simple(
63 HttpURLConnection.HTTP_OK,
64 Json.createArrayBuilder()
65 .add(RtOrganizationsTest.org(1, "org1"))
66 .add(RtOrganizationsTest.org(2, "org2"))
67 .add(RtOrganizationsTest.org(3, "org3"))
68 .build().toString()
69 )
70 ).start(RandomPort.port())
71 ) {
72 final Organizations orgs = new RtOrganizations(
73 github,
74 new ApacheRequest(container.home())
75 );
76 MatcherAssert.assertThat(
77 "Collection size is incorrect",
78 orgs.iterate(),
79 Matchers.iterableWithSize(3)
80 );
81 MatcherAssert.assertThat(
82 "String does not end with expected value",
83 container.take().uri().toString(),
84 Matchers.endsWith("/user/orgs")
85 );
86 container.stop();
87 }
88 }
89
90
91
92
93
94
95
96 private static JsonObject org(final int number, final String login) {
97 return Json.createObjectBuilder()
98 .add("id", number)
99 .add("login", login)
100 .build();
101 }
102 }