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.aspects.Immutable;
8   import com.jcabi.http.mock.MkAnswer;
9   import com.jcabi.http.mock.MkContainer;
10  import com.jcabi.http.mock.MkGrizzlyContainer;
11  import com.jcabi.http.request.ApacheRequest;
12  import com.jcabi.http.request.FakeRequest;
13  import jakarta.json.Json;
14  import jakarta.json.JsonObject;
15  import java.io.IOException;
16  import java.net.HttpURLConnection;
17  import org.hamcrest.MatcherAssert;
18  import org.hamcrest.Matchers;
19  import org.junit.jupiter.api.Test;
20  import org.junit.jupiter.api.extension.ExtendWith;
21  import org.mockito.Mockito;
22  
23  /**
24   * Test case for {@link RtDeployKeys}.
25   * @since 0.8
26   */
27  @Immutable
28  @ExtendWith(RandomPort.class)
29  final class RtDeployKeysTest {
30  
31      /**
32       * The rule for skipping test if there's BindException.
33       * @checkstyle VisibilityModifierCheck (3 lines)
34       */
35      @Test
36      void canFetchEmptyListOfDeployKeys() {
37          final DeployKeys keys = new RtDeployKeys(
38              new FakeRequest().withBody("[]"),
39              RtDeployKeysTest.repo()
40          );
41          MatcherAssert.assertThat(
42              "Collection is not empty",
43              keys.iterate(),
44              Matchers.emptyIterable()
45          );
46      }
47  
48      /**
49       * RtDeployKeys can fetch non empty list of deploy keys.
50       *
51       * @throws IOException If some problem inside.
52       */
53      @Test
54      void canFetchNonEmptyListOfDeployKeys() throws IOException {
55          try (MkContainer container = new MkGrizzlyContainer().next(
56              new MkAnswer.Simple(
57                  HttpURLConnection.HTTP_OK,
58                  Json.createArrayBuilder()
59                      .add(RtDeployKeysTest.key(1))
60                      .add(RtDeployKeysTest.key(2))
61                      .build().toString()
62              )
63          )) {
64              container.start(RandomPort.port());
65              MatcherAssert.assertThat(
66                  "Collection size is incorrect",
67                  new RtDeployKeys(
68                      new ApacheRequest(container.home()),
69                      RtDeployKeysTest.repo()
70                  ).iterate(),
71                  Matchers.iterableWithSize(2)
72              );
73          }
74      }
75  
76      /**
77       * RtDeployKeys can fetch single deploy key.
78       * @throws IOException If some problem inside
79       */
80      @Test
81      void canFetchSingleDeployKey() throws IOException {
82          final int number = 1;
83          final DeployKeys keys = new RtDeployKeys(
84              // @checkstyle MultipleStringLiterals (1 line)
85              new FakeRequest().withBody(String.format("{\"id\":%d}", number)),
86              RtDeployKeysTest.repo()
87          );
88          MatcherAssert.assertThat(
89              "Values are not equal",
90              // @checkstyle MultipleStringLiterals (1 line)
91              keys.get(number).json().getInt("id"),
92              Matchers.equalTo(number)
93          );
94      }
95  
96      /**
97       * RtDeployKeys can create a key.
98       * @throws IOException If some problem inside.
99       */
100     @Test
101     void canCreateDeployKey() throws IOException {
102         final int number = 2;
103         try (MkContainer container = new MkGrizzlyContainer().next(
104             new MkAnswer.Simple(
105                 HttpURLConnection.HTTP_CREATED,
106                 String.format("{\"id\":%d}", number)
107             )
108         )) {
109             container.start(RandomPort.port());
110             final DeployKeys keys = new RtDeployKeys(
111                 new ApacheRequest(container.home()), RtDeployKeysTest.repo()
112             );
113             MatcherAssert.assertThat(
114                 "Values are not equal",
115                 keys.create("Title", "Key").number(),
116                 Matchers.equalTo(number)
117             );
118         }
119     }
120 
121     /**
122      * Create and return repo for testing.
123      * @return Repo
124      */
125     private static Repo repo() {
126         final Repo repo = Mockito.mock(Repo.class);
127         Mockito.doReturn(new Coordinates.Simple("test", "keys"))
128             .when(repo).coordinates();
129         return repo;
130     }
131 
132     /**
133      * Create and return key to test.
134      * @param number Deploy Key Id
135      * @return JsonObject
136      */
137     private static JsonObject key(final int number) {
138         return Json.createObjectBuilder()
139             .add("id", number)
140             .add("key", "ssh-rsa AAA")
141             .build();
142     }
143 }