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.aspects.Immutable;
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.request.ApacheRequest;
37  import com.jcabi.http.request.FakeRequest;
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 RtDeployKeys}.
50   * @author Andres Candal (andres.candal@rollasolution.com)
51   * @version $Id: c0ac278c673ef7f735728f9bc8e8fea6a6507ec0 $
52   * @since 0.8
53   */
54  @Immutable
55  public final class RtDeployKeysTest {
56  
57      /**
58       * The rule for skipping test if there's BindException.
59       * @checkstyle VisibilityModifierCheck (3 lines)
60       */
61      @Rule
62      public final transient RandomPort resource = new RandomPort();
63  
64      /**
65       * RtDeployKeys can fetch empty list of deploy keys.
66       */
67      @Test
68      public void canFetchEmptyListOfDeployKeys() {
69          final DeployKeys deployKeys = new RtDeployKeys(
70              new FakeRequest().withBody("[]"),
71              RtDeployKeysTest.repo()
72          );
73          MatcherAssert.assertThat(
74              deployKeys.iterate(),
75              Matchers.emptyIterable()
76          );
77      }
78  
79      /**
80       * RtDeployKeys can fetch non empty list of deploy keys.
81       *
82       * @throws IOException If some problem inside.
83       */
84      @Test
85      public void canFetchNonEmptyListOfDeployKeys() throws IOException {
86          try (final MkContainer container = new MkGrizzlyContainer().next(
87              new MkAnswer.Simple(
88                  HttpURLConnection.HTTP_OK,
89                  Json.createArrayBuilder()
90                      .add(key(1))
91                      .add(key(2))
92                      .build().toString()
93              )
94          )) {
95              container.start(this.resource.port());
96              MatcherAssert.assertThat(
97                  new RtDeployKeys(
98                      new ApacheRequest(container.home()),
99                      RtDeployKeysTest.repo()
100                 ).iterate(),
101                 Matchers.<DeployKey>iterableWithSize(2)
102             );
103         }
104     }
105 
106     /**
107      * RtDeployKeys can fetch single deploy key.
108      * @throws IOException If some problem inside
109      */
110     @Test
111     public void canFetchSingleDeployKey() throws IOException {
112         final int number = 1;
113         final DeployKeys keys = new RtDeployKeys(
114             // @checkstyle MultipleStringLiterals (1 line)
115             new FakeRequest().withBody(String.format("{\"id\":%d}", number)),
116             RtDeployKeysTest.repo()
117         );
118         MatcherAssert.assertThat(
119             // @checkstyle MultipleStringLiterals (1 line)
120             keys.get(number).json().getInt("id"),
121             Matchers.equalTo(number)
122         );
123     }
124 
125     /**
126      * RtDeployKeys can create a key.
127      * @throws IOException If some problem inside.
128      */
129     @Test
130     public void canCreateDeployKey() throws IOException {
131         final int number = 2;
132         try (final MkContainer container = new MkGrizzlyContainer().next(
133             new MkAnswer.Simple(
134                 HttpURLConnection.HTTP_CREATED,
135                 String.format("{\"id\":%d}", number)
136             )
137         )) {
138             container.start(this.resource.port());
139             final DeployKeys keys = new RtDeployKeys(
140                 new ApacheRequest(container.home()), RtDeployKeysTest.repo()
141             );
142             MatcherAssert.assertThat(
143                 keys.create("Title", "Key").number(),
144                 Matchers.equalTo(number)
145             );
146         }
147     }
148 
149     /**
150      * Create and return repo for testing.
151      * @return Repo
152      */
153     private static Repo repo() {
154         final Repo repo = Mockito.mock(Repo.class);
155         Mockito.doReturn(new Coordinates.Simple("test", "keys"))
156             .when(repo).coordinates();
157         return repo;
158     }
159 
160     /**
161      * Create and return key to test.
162      * @param number Deploy Key Id
163      * @return JsonObject
164      */
165     private static JsonObject key(final int number) {
166         return Json.createObjectBuilder()
167             .add("id", number)
168             .add("key", "ssh-rsa AAA")
169             .build();
170     }
171 }