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.http.Request;
33  import com.jcabi.http.mock.MkAnswer;
34  import com.jcabi.http.mock.MkContainer;
35  import com.jcabi.http.mock.MkGrizzlyContainer;
36  import com.jcabi.http.mock.MkQuery;
37  import com.jcabi.http.request.ApacheRequest;
38  import java.net.HttpURLConnection;
39  import javax.json.Json;
40  import javax.json.JsonObject;
41  import org.hamcrest.MatcherAssert;
42  import org.hamcrest.Matchers;
43  import org.hamcrest.core.IsEqual;
44  import org.junit.Rule;
45  import org.junit.Test;
46  import org.mockito.Mockito;
47  
48  /**
49   * Test case for {@link com.jcabi.github.Repos}.
50   * @author Gena Svarovski (g.svarovski@gmail.com)
51   * @author Paulo Lobo (pauloeduardolobo@gmail.com)
52   * @version $Id: 7850f2579d608f1a9a565f15fcfd15b074b8a646 $
53   * @since 0.8
54   * @checkstyle ClassDataAbstractionCoupling (500 lines)
55   */
56  public final class RtReposTest {
57  
58      /**
59       * The rule for skipping test if there's BindException.
60       * @checkstyle VisibilityModifierCheck (3 lines)
61       */
62      @Rule
63      public final transient RandomPort resource = new RandomPort();
64      /**
65       * RepoRule.
66       * @checkstyle VisibilityModifierCheck (3 lines)
67       */
68      @Rule
69      public final transient RepoRule rule = new RepoRule();
70  
71      /**
72       * RtRepos can create a repo.
73       * @throws Exception if some problem inside
74       */
75      @Test
76      public void createRepo() throws Exception {
77          final String owner = "test-owner";
78          final String name = "test-repo";
79          final String response = response(owner, name).toString();
80          try (
81              final MkContainer container = new MkGrizzlyContainer().next(
82                  new MkAnswer.Simple(HttpURLConnection.HTTP_CREATED, response)
83              ).next(new MkAnswer.Simple(HttpURLConnection.HTTP_OK, response))
84                  .start(this.resource.port())
85          ) {
86              final RtRepos repos = new RtRepos(
87                  Mockito.mock(Github.class),
88                  new ApacheRequest(container.home())
89              );
90              final Repo repo = this.rule.repo(repos);
91              MatcherAssert.assertThat(
92                  container.take().method(),
93                  Matchers.equalTo(Request.POST)
94              );
95              MatcherAssert.assertThat(
96                  repo.coordinates(),
97                  new IsEqual<>(new Coordinates.Simple(owner, name))
98              );
99              container.stop();
100         }
101     }
102 
103     /**
104      * RtUsers can iterate users.
105      * @throws Exception if there is any Error
106      */
107     @Test
108     public void iterateRepos() throws Exception {
109         final String identifier = "1";
110         try (
111             final MkContainer container = new MkGrizzlyContainer().next(
112                 new MkAnswer.Simple(
113                     HttpURLConnection.HTTP_OK,
114                     Json.createArrayBuilder()
115                         .add(response("octocat", identifier))
116                         .add(response("dummy", "2"))
117                         .build().toString()
118                 )
119             ).start(this.resource.port())
120         ) {
121             final RtRepos repos = new RtRepos(
122                 Mockito.mock(Github.class),
123                 new ApacheRequest(container.home())
124             );
125             MatcherAssert.assertThat(
126                 repos.iterate(identifier),
127                 Matchers.<Repo>iterableWithSize(2)
128             );
129             container.stop();
130         }
131     }
132 
133     /**
134      * RtRepos can remove a repo.
135      * @throws Exception if some problem inside
136      */
137     @Test
138     public void removeRepo() throws Exception {
139         try (
140             final MkContainer container = new MkGrizzlyContainer().next(
141                 new MkAnswer.Simple(HttpURLConnection.HTTP_NO_CONTENT, "")
142             ).start(this.resource.port())
143         ) {
144             final Repos repos = new RtRepos(
145                 Mockito.mock(Github.class),
146                 new ApacheRequest(container.home())
147             );
148             repos.remove(new Coordinates.Simple("", ""));
149             final MkQuery query = container.take();
150             MatcherAssert.assertThat(
151                 query.method(),
152                 Matchers.equalTo(Request.DELETE)
153             );
154             MatcherAssert.assertThat(
155                 query.body(),
156                 Matchers.isEmptyString()
157             );
158             container.stop();
159         }
160     }
161 
162     /**
163      * Create and return JsonObject to test response.
164      *
165      * @param owner Owner name
166      * @param name Repo name
167      * @return JsonObject
168      * @throws Exception If some problem inside
169      */
170     private static JsonObject response(
171         final String owner, final String name)
172         throws Exception {
173         return Json.createObjectBuilder()
174             .add("name", name)
175             .add("full_name", String.format("%s/%s", owner, name))
176             .add(
177                 "owner",
178                 Json.createObjectBuilder().add("login", owner).build()
179             )
180             .build();
181     }
182 
183 }