View Javadoc
1   /**
2    * Copyright (c) 2013-2023, 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.io.IOException;
39  import java.net.HttpURLConnection;
40  import javax.json.Json;
41  import javax.json.JsonObject;
42  import org.hamcrest.MatcherAssert;
43  import org.hamcrest.Matchers;
44  import org.junit.Rule;
45  import org.junit.Test;
46  import org.mockito.Mockito;
47  
48  /**
49   * Test case for {@link RtPublicKeys}.
50   *
51   * @author Carlos Miranda (miranda.cma@gmail.com)
52   * @version $Id: 44cfaaf2e4c1a345f4758abca3a93cafb7ee6102 $
53   */
54  public final class RtPublicKeysTest {
55      /**
56       * The rule for skipping test if there's BindException.
57       * @checkstyle VisibilityModifierCheck (3 lines)
58       */
59      @Rule
60      public final transient RandomPort resource = new RandomPort();
61  
62      /**
63       * RtPublicKeys should be able to iterate its keys.
64       *
65       * @throws Exception if a problem occurs.
66       */
67      @Test
68      public void retrievesKeys() throws Exception {
69          try (
70              final MkContainer container = new MkGrizzlyContainer().next(
71                  new MkAnswer.Simple(
72                      HttpURLConnection.HTTP_OK,
73                      Json.createArrayBuilder()
74                          .add(key(1))
75                          .add(key(2))
76                          .build().toString()
77                  )
78              ).start(this.resource.port())
79          ) {
80              final RtPublicKeys keys = new RtPublicKeys(
81                  new ApacheRequest(container.home()),
82                  Mockito.mock(User.class)
83              );
84              MatcherAssert.assertThat(
85                  keys.iterate(),
86                  Matchers.<PublicKey>iterableWithSize(2)
87              );
88              container.stop();
89          }
90      }
91  
92      /**
93       * RtPublicKeys should be able to obtain a single key.
94       *
95       * @throws Exception if a problem occurs.
96       */
97      @Test
98      public void canFetchSingleKey() throws Exception {
99          try (
100             final MkContainer container = new MkGrizzlyContainer().next(
101                 new MkAnswer.Simple(
102                     HttpURLConnection.HTTP_OK,
103                     ""
104                 )
105             ).start(this.resource.port())
106         ) {
107             final RtPublicKeys keys = new RtPublicKeys(
108                 new ApacheRequest(container.home()),
109                 Mockito.mock(User.class)
110             );
111             MatcherAssert.assertThat(
112                 keys.get(1),
113                 Matchers.notNullValue()
114             );
115             container.stop();
116         }
117     }
118 
119     /**
120      * RtPublicKeys should be able to remove a key.
121      *
122      * @throws Exception if a problem occurs.
123      */
124     @Test
125     public void canRemoveKey() throws Exception {
126         try (
127             final MkContainer container = new MkGrizzlyContainer().next(
128                 new MkAnswer.Simple(
129                     HttpURLConnection.HTTP_NO_CONTENT,
130                     ""
131                 )
132             ).start(this.resource.port())
133         ) {
134             final RtPublicKeys keys = new RtPublicKeys(
135                 new ApacheRequest(container.home()),
136                 Mockito.mock(User.class)
137             );
138             keys.remove(1);
139             final MkQuery query = container.take();
140             MatcherAssert.assertThat(
141                 query.uri().toString(),
142                 Matchers.endsWith("/user/keys/1")
143             );
144             MatcherAssert.assertThat(
145                 query.method(),
146                 Matchers.equalTo(Request.DELETE)
147             );
148             container.stop();
149         }
150     }
151 
152     /**
153      * RtPublicKeys can create a key.
154      * @throws IOException If some problem inside.
155      */
156     @Test
157     public void canCreatePublicKey() throws IOException {
158         try (
159             final MkContainer container = new MkGrizzlyContainer().next(
160                 new MkAnswer.Simple(
161                     HttpURLConnection.HTTP_CREATED, key(1).toString()
162                 )
163             ).start(this.resource.port())
164         ) {
165             final RtPublicKeys keys = new RtPublicKeys(
166                 new ApacheRequest(container.home()),
167                 Mockito.mock(User.class)
168             );
169             MatcherAssert.assertThat(
170                 keys.create("theTitle", "theKey").number(),
171                 Matchers.is(1)
172             );
173             final MkQuery query = container.take();
174             MatcherAssert.assertThat(
175                 query.uri().toString(),
176                 Matchers.endsWith("/user/keys")
177             );
178             MatcherAssert.assertThat(
179                 query.body(),
180                 Matchers.equalTo(
181                     "{\"title\":\"theTitle\",\"key\":\"theKey\"}"
182                 )
183             );
184             container.stop();
185         }
186     }
187 
188     /**
189      * Create and return key to test.
190      * @param number Public Key Id
191      * @return JsonObject
192      */
193     private static JsonObject key(final int number) {
194         return Json.createObjectBuilder()
195             .add("id", number)
196             .add("key", "ssh-rsa AAA")
197             .build();
198     }
199 }