View Javadoc
1   /**
2    * Copyright (c) 2013-2022, jcabi.com
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met: 1) Redistributions of source code must retain the above
8    * copyright notice, this list of conditions and the following
9    * disclaimer. 2) Redistributions in binary form must reproduce the above
10   * copyright notice, this list of conditions and the following
11   * disclaimer in the documentation and/or other materials provided
12   * with the distribution. 3) Neither the name of the jcabi.com nor
13   * the names of its contributors may be used to endorse or promote
14   * products derived from this software without specific prior written
15   * permission.
16   *
17   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
19   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21   * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28   * OF THE POSSIBILITY OF SUCH DAMAGE.
29   */
30  package com.jcabi.github;
31  
32  import com.jcabi.github.mock.MkGithub;
33  import com.jcabi.http.Request;
34  import com.jcabi.http.mock.MkAnswer;
35  import com.jcabi.http.mock.MkContainer;
36  import com.jcabi.http.mock.MkGrizzlyContainer;
37  import com.jcabi.http.mock.MkQuery;
38  import com.jcabi.http.request.ApacheRequest;
39  import com.jcabi.http.request.FakeRequest;
40  import java.net.HttpURLConnection;
41  import javax.json.Json;
42  import org.hamcrest.MatcherAssert;
43  import org.hamcrest.Matchers;
44  import org.junit.Rule;
45  import org.junit.Test;
46  
47  /**
48   * Test case for {@link RtOrganization}.
49   *
50   * @author Carlos Miranda (miranda.cma@gmail.com)
51   * @version $Id: 4a229db3e58e144d4b311b0e6397e731a9b5d1f3 $
52   */
53  public final class RtOrganizationTest {
54      /**
55       * The rule for skipping test if there's BindException.
56       * @checkstyle VisibilityModifierCheck (3 lines)
57       */
58      @Rule
59      public final transient RandomPort resource = new RandomPort();
60  
61      /**
62       * RtOrganization should be able to describe itself in JSON format.
63       *
64       * @throws Exception if a problem occurs.
65       */
66      @Test
67      public void canFetchIssueAsJson() throws Exception {
68          final RtOrganization org = new RtOrganization(
69              new MkGithub(),
70              new FakeRequest().withBody("{\"organization\":\"json\"}"),
71              "testJson"
72          );
73          MatcherAssert.assertThat(
74              org.json().getString("organization"),
75              Matchers.equalTo("json")
76          );
77      }
78  
79      /**
80       * RtOrganization should be able to perform a patch request.
81       *
82       * @throws Exception if a problem occurs.
83       */
84      @Test
85      public void patchWithJson() throws Exception {
86          try (
87              final MkContainer container = new MkGrizzlyContainer().next(
88                  new MkAnswer.Simple(HttpURLConnection.HTTP_OK, "response")
89              ).start(this.resource.port())
90          ) {
91              final RtOrganization org = new RtOrganization(
92                  new MkGithub(),
93                  new ApacheRequest(container.home()),
94                  "testPatch"
95              );
96              org.patch(
97                  Json.createObjectBuilder().add("patch", "test").build()
98              );
99              final MkQuery query = container.take();
100             MatcherAssert.assertThat(
101                 query.method(),
102                 Matchers.equalTo(Request.PATCH)
103             );
104             MatcherAssert.assertThat(
105                 query.body(),
106                 Matchers.equalTo("{\"patch\":\"test\"}")
107             );
108             container.stop();
109         }
110     }
111 
112     /**
113      * RtOrganization should be able to compare instances of each other.
114      *
115      * @throws Exception if a problem occurs.
116      */
117     @Test
118     public void canCompareInstances() throws Exception {
119         final RtOrganization less = new RtOrganization(
120             new MkGithub(),
121             new FakeRequest(),
122             "abc"
123         );
124         final RtOrganization greater = new RtOrganization(
125             new MkGithub(),
126             new FakeRequest(),
127             "def"
128         );
129         MatcherAssert.assertThat(
130             less.compareTo(greater), Matchers.lessThan(0)
131         );
132         MatcherAssert.assertThat(
133             greater.compareTo(less), Matchers.greaterThan(0)
134         );
135         MatcherAssert.assertThat(
136             less.compareTo(less), Matchers.equalTo(0)
137         );
138     }
139 
140     /**
141      * RtOrganization can return a String representation correctly reflecting
142      * its URI.
143      *
144      * @throws Exception if something goes wrong.
145      */
146     @Test
147     public void canRepresentAsString() throws Exception {
148         try (
149             final MkContainer container = new MkGrizzlyContainer().next(
150                 new MkAnswer.Simple(HttpURLConnection.HTTP_OK, "blah")
151             ).start(this.resource.port())
152         ) {
153             final RtOrganization org = new RtOrganization(
154                 new MkGithub(),
155                 new ApacheRequest(container.home()),
156                 "testToString"
157             );
158             MatcherAssert.assertThat(
159                 org.toString(),
160                 Matchers.endsWith("/orgs/testToString")
161             );
162             container.stop();
163         }
164     }
165 
166 }