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.github.mock.MkGitHub;
8   import com.jcabi.http.Request;
9   import com.jcabi.http.mock.MkAnswer;
10  import com.jcabi.http.mock.MkContainer;
11  import com.jcabi.http.mock.MkGrizzlyContainer;
12  import com.jcabi.http.request.ApacheRequest;
13  import java.io.IOException;
14  import java.net.HttpURLConnection;
15  import org.hamcrest.MatcherAssert;
16  import org.hamcrest.Matchers;
17  import org.junit.jupiter.api.Test;
18  import org.junit.jupiter.api.extension.ExtendWith;
19  
20  /**
21   * Test case for {@link RtReferences}.
22   * @since 0.1
23   * @checkstyle MultipleStringLiterals (500 lines)
24   */
25  @ExtendWith(RandomPort.class)
26  final class RtReferencesTest {
27  
28      /**
29       * The rule for skipping test if there's BindException.
30       * @checkstyle VisibilityModifierCheck (3 lines)
31       */
32      @Test
33      void createsReference() throws IOException {
34          try (
35              MkContainer container = new MkGrizzlyContainer().next(
36                  new MkAnswer.Simple(
37                      HttpURLConnection.HTTP_CREATED,
38                      "{\"ref\":\"refs/heads/feature-a\"}"
39                  )
40              ).start(RandomPort.port())
41          ) {
42              final References refs = new RtReferences(
43                  new ApacheRequest(container.home()),
44                  new MkGitHub().randomRepo()
45              );
46              MatcherAssert.assertThat(
47                  "Object is not of expected type",
48                  refs.create("abceefgh3456", "refs/heads/feature-a"),
49                  Matchers.instanceOf(Reference.class)
50              );
51              MatcherAssert.assertThat(
52                  "Values are not equal",
53                  container.take().method(),
54                  Matchers.equalTo(Request.POST)
55              );
56              container.stop();
57          }
58      }
59  
60      @Test
61      void iteratesReferences() throws IOException {
62          try (
63              MkContainer container = new MkGrizzlyContainer().next(
64                  new MkAnswer.Simple(
65                      HttpURLConnection.HTTP_OK,
66                      "{\"ref\":\"refs/heads/feature-a\"}"
67                  )
68              ).start(RandomPort.port())
69          ) {
70              final References refs = new RtReferences(
71                  new ApacheRequest(container.home()),
72                  new MkGitHub().randomRepo()
73              );
74              MatcherAssert.assertThat(
75                  "Value is null",
76                  refs.iterate(),
77                  Matchers.notNullValue()
78              );
79              container.stop();
80          }
81      }
82  
83      @Test
84      void removesReference() throws IOException {
85          try (
86              MkContainer container = new MkGrizzlyContainer().next(
87                  new MkAnswer.Simple(HttpURLConnection.HTTP_NO_CONTENT, "")
88              ).start(RandomPort.port())
89          ) {
90              final References refs = new RtReferences(
91                  new ApacheRequest(container.home()),
92                  new MkGitHub().randomRepo()
93              );
94              refs.remove("heads/feature-a");
95              MatcherAssert.assertThat(
96                  "Values are not equal",
97                  container.take().method(),
98                  Matchers.equalTo(Request.DELETE)
99              );
100             container.stop();
101         }
102     }
103 
104     @Test
105     void iteratesTags() throws IOException {
106         try (
107             MkContainer container = new MkGrizzlyContainer().next(
108                 new MkAnswer.Simple(
109                     HttpURLConnection.HTTP_OK,
110                     "[{\"ref\":\"refs/tags/feature-b\"}]"
111                 )
112             ).start(RandomPort.port())
113         ) {
114             final References refs = new RtReferences(
115                 new ApacheRequest(container.home()),
116                 new MkGitHub().randomRepo()
117             );
118             MatcherAssert.assertThat(
119                 "Collection size is incorrect",
120                 refs.tags(),
121                 Matchers.iterableWithSize(1)
122             );
123             MatcherAssert.assertThat(
124                 "String does not end with expected value",
125                 container.take().uri().toString(),
126                 Matchers.endsWith("/git/refs/tags")
127             );
128             container.stop();
129         }
130     }
131 
132     @Test
133     void iteratesHeads() throws IOException {
134         try (
135             MkContainer container = new MkGrizzlyContainer().next(
136                 new MkAnswer.Simple(
137                     HttpURLConnection.HTTP_OK,
138                     "[{\"ref\":\"refs/heads/feature-c\"}]"
139                 )
140             ).start(RandomPort.port())
141         ) {
142             final References refs = new RtReferences(
143                 new ApacheRequest(container.home()),
144                 new MkGitHub().randomRepo()
145             );
146             MatcherAssert.assertThat(
147                 "Collection size is incorrect",
148                 refs.heads(),
149                 Matchers.iterableWithSize(1)
150             );
151             MatcherAssert.assertThat(
152                 "String does not end with expected value",
153                 container.take().uri().toString(),
154                 Matchers.endsWith("/git/refs/heads")
155             );
156             container.stop();
157         }
158     }
159 }