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.http.Request;
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.mock.MkQuery;
12  import com.jcabi.http.request.ApacheRequest;
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 RtPublicKeys}.
25   * @since 0.8
26   */
27  @ExtendWith(RandomPort.class)
28  final class RtPublicKeysTest {
29      /**
30       * The rule for skipping test if there's BindException.
31       * @checkstyle VisibilityModifierCheck (3 lines)
32       */
33      @Test
34      void retrievesKeys() throws IOException {
35          try (
36              MkContainer container = new MkGrizzlyContainer().next(
37                  new MkAnswer.Simple(
38                      HttpURLConnection.HTTP_OK,
39                      Json.createArrayBuilder()
40                          .add(RtPublicKeysTest.key(1))
41                          .add(RtPublicKeysTest.key(2))
42                          .build().toString()
43                  )
44              ).start(RandomPort.port())
45          ) {
46              final RtPublicKeys keys = new RtPublicKeys(
47                  new ApacheRequest(container.home()),
48                  Mockito.mock(User.class)
49              );
50              MatcherAssert.assertThat(
51                  "Collection size is incorrect",
52                  keys.iterate(),
53                  Matchers.iterableWithSize(2)
54              );
55              container.stop();
56          }
57      }
58  
59      @Test
60      void canFetchSingleKey() throws IOException {
61          try (
62              MkContainer container = new MkGrizzlyContainer().next(
63                  new MkAnswer.Simple(
64                      HttpURLConnection.HTTP_OK,
65                      ""
66                  )
67              ).start(RandomPort.port())
68          ) {
69              final RtPublicKeys keys = new RtPublicKeys(
70                  new ApacheRequest(container.home()),
71                  Mockito.mock(User.class)
72              );
73              MatcherAssert.assertThat(
74                  "Value is null",
75                  keys.get(1),
76                  Matchers.notNullValue()
77              );
78              container.stop();
79          }
80      }
81  
82      @Test
83      void canRemoveKey() throws IOException {
84          try (
85              MkContainer container = new MkGrizzlyContainer().next(
86                  new MkAnswer.Simple(
87                      HttpURLConnection.HTTP_NO_CONTENT,
88                      ""
89                  )
90              ).start(RandomPort.port())
91          ) {
92              final RtPublicKeys keys = new RtPublicKeys(
93                  new ApacheRequest(container.home()),
94                  Mockito.mock(User.class)
95              );
96              keys.remove(1);
97              final MkQuery query = container.take();
98              MatcherAssert.assertThat(
99                  "String does not end with expected value",
100                 query.uri().toString(),
101                 Matchers.endsWith("/user/keys/1")
102             );
103             MatcherAssert.assertThat(
104                 "Values are not equal",
105                 query.method(),
106                 Matchers.equalTo(Request.DELETE)
107             );
108             container.stop();
109         }
110     }
111 
112     /**
113      * RtPublicKeys can create a key.
114      * @throws IOException If some problem inside.
115      */
116     @Test
117     void canCreatePublicKey() throws IOException {
118         try (
119             MkContainer container = new MkGrizzlyContainer().next(
120                 new MkAnswer.Simple(
121                     HttpURLConnection.HTTP_CREATED, RtPublicKeysTest.key(1).toString()
122                 )
123             ).start(RandomPort.port())
124         ) {
125             final RtPublicKeys keys = new RtPublicKeys(
126                 new ApacheRequest(container.home()),
127                 Mockito.mock(User.class)
128             );
129             MatcherAssert.assertThat(
130                 "Values are not equal",
131                 keys.create("theTitle", "theKey").number(),
132                 Matchers.is(1)
133             );
134             final MkQuery query = container.take();
135             MatcherAssert.assertThat(
136                 "String does not end with expected value",
137                 query.uri().toString(),
138                 Matchers.endsWith("/user/keys")
139             );
140             MatcherAssert.assertThat(
141                 "Values are not equal",
142                 query.body(),
143                 Matchers.equalTo(
144                     "{\"title\":\"theTitle\",\"key\":\"theKey\"}"
145                 )
146             );
147             container.stop();
148         }
149     }
150 
151     /**
152      * Create and return key to test.
153      * @param number Public Key Id
154      * @return JsonObject
155      */
156     private static JsonObject key(final int number) {
157         return Json.createObjectBuilder()
158             .add("id", number)
159             .add("key", "ssh-rsa AAA")
160             .build();
161     }
162 }