View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2013-2025 Yegor Bugayenko
3    * SPDX-License-Identifier: MIT
4    */
5   package com.jcabi.github;
6   
7   import com.jcabi.github.mock.MkGitHub;
8   import com.jcabi.http.Request;
9   import com.jcabi.http.mock.MkAnswer;
10  import com.jcabi.http.mock.MkContainer;
11  import com.jcabi.http.mock.MkGrizzlyContainer;
12  import com.jcabi.http.mock.MkQuery;
13  import com.jcabi.http.request.ApacheRequest;
14  import com.jcabi.http.request.FakeRequest;
15  import jakarta.json.Json;
16  import java.io.IOException;
17  import java.net.HttpURLConnection;
18  import org.hamcrest.MatcherAssert;
19  import org.hamcrest.Matchers;
20  import org.junit.jupiter.api.Test;
21  import org.junit.jupiter.api.extension.ExtendWith;
22  
23  /**
24   * Test case for {@link RtOrganization}.
25   * @since 0.24
26   */
27  @ExtendWith(RandomPort.class)
28  final class RtOrganizationTest {
29      /**
30       * The rule for skipping test if there's BindException.
31       * @checkstyle VisibilityModifierCheck (3 lines)
32       */
33      @Test
34      void canFetchIssueAsJson() throws IOException {
35          final RtOrganization org = new RtOrganization(
36              new MkGitHub(),
37              new FakeRequest().withBody("{\"organization\":\"json\"}"),
38              "testJson"
39          );
40          MatcherAssert.assertThat(
41              "Values are not equal",
42              org.json().getString("organization"),
43              Matchers.equalTo("json")
44          );
45      }
46  
47      @Test
48      void patchWithJson() throws IOException {
49          try (
50              MkContainer container = new MkGrizzlyContainer().next(
51                  new MkAnswer.Simple(HttpURLConnection.HTTP_OK, "response")
52              ).start(RandomPort.port())
53          ) {
54              final RtOrganization org = new RtOrganization(
55                  new MkGitHub(),
56                  new ApacheRequest(container.home()),
57                  "testPatch"
58              );
59              org.patch(
60                  Json.createObjectBuilder().add("patch", "test").build()
61              );
62              final MkQuery query = container.take();
63              MatcherAssert.assertThat(
64                  "Values are not equal",
65                  query.method(),
66                  Matchers.equalTo(Request.PATCH)
67              );
68              MatcherAssert.assertThat(
69                  "Values are not equal",
70                  query.body(),
71                  Matchers.equalTo("{\"patch\":\"test\"}")
72              );
73              container.stop();
74          }
75      }
76  
77      @Test
78      void canCompareInstances() throws IOException {
79          final RtOrganization less = new RtOrganization(
80              new MkGitHub(),
81              new FakeRequest(),
82              "abc"
83          );
84          final RtOrganization greater = new RtOrganization(
85              new MkGitHub(),
86              new FakeRequest(),
87              "def"
88          );
89          MatcherAssert.assertThat(
90              "Value is not less than expected",
91              less.compareTo(greater), Matchers.lessThan(0)
92          );
93          MatcherAssert.assertThat(
94              "Value is not greater than expected",
95              greater.compareTo(less), Matchers.greaterThan(0)
96          );
97          MatcherAssert.assertThat(
98              "Values are not equal",
99              less.compareTo(less), Matchers.equalTo(0)
100         );
101     }
102 
103     @Test
104     void canRepresentAsString() throws IOException {
105         try (
106             MkContainer container = new MkGrizzlyContainer().next(
107                 new MkAnswer.Simple(HttpURLConnection.HTTP_OK, "blah")
108             ).start(RandomPort.port())
109         ) {
110             final RtOrganization org = new RtOrganization(
111                 new MkGitHub(),
112                 new ApacheRequest(container.home()),
113                 "testToString"
114             );
115             MatcherAssert.assertThat(
116                 "String does not end with expected value",
117                 org.toString(),
118                 Matchers.endsWith("/orgs/testToString")
119             );
120             container.stop();
121         }
122     }
123 
124 }