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 jakarta.json.Json;
14  import java.io.IOException;
15  import java.net.HttpURLConnection;
16  import org.hamcrest.MatcherAssert;
17  import org.hamcrest.Matchers;
18  import org.junit.jupiter.api.Test;
19  import org.junit.jupiter.api.extension.ExtendWith;
20  
21  /**
22   * Test case for {@link RtReference}.
23   * @since 0.1
24   * @checkstyle MultipleStringLiterals (500 lines)
25   */
26  @ExtendWith(RandomPort.class)
27  final class RtReferenceTest {
28  
29      /**
30       * The rule for skipping test if there's BindException.
31       * @checkstyle VisibilityModifierCheck (3 lines)
32       */
33      @Test
34      void patchesContent() throws IOException {
35          try (
36              MkContainer container = new MkGrizzlyContainer().next(
37                  new MkAnswer.Simple(
38                      HttpURLConnection.HTTP_OK,
39                      "{\"ref\":\"refs/heads/featureA\"}"
40                  )
41              ).start(RandomPort.port())
42          ) {
43              final Reference reference = new RtReference(
44                  new ApacheRequest(container.home()),
45                  new MkGitHub().randomRepo(),
46                  "refs/heads/featureA"
47              );
48              reference.patch(
49                  Json.createObjectBuilder().add("sha", "abcdef12345")
50                  .add("force", "false").build()
51              );
52              MatcherAssert.assertThat(
53                  "Values are not equal",
54                  container.take().method(),
55                  Matchers.equalTo(Request.PATCH)
56              );
57              container.stop();
58          }
59      }
60  
61      @Test
62      void fetchesContent() throws IOException {
63          try (
64              MkContainer container = new MkGrizzlyContainer().next(
65                  new MkAnswer.Simple(
66                      HttpURLConnection.HTTP_OK,
67                      "{\"ref\":\"refs/heads/featureB\"}"
68                  )
69              ).start(RandomPort.port())
70          ) {
71              final Reference reference = new RtReference(
72                  new ApacheRequest(container.home()),
73                  new MkGitHub().randomRepo(),
74                  "refs/heads/featureB"
75              );
76              MatcherAssert.assertThat(
77                  "Values are not equal",
78                  reference.json().getString("ref"),
79                  Matchers.is("refs/heads/featureB")
80              );
81              container.stop();
82          }
83      }
84  
85      @Test
86      void returnsRef() throws IOException {
87          try (
88              MkContainer container = new MkGrizzlyContainer().next(
89                  new MkAnswer.Simple(
90                      HttpURLConnection.HTTP_OK,
91                      "{\"ref\":\"refs/heads/featureC\"}"
92                  )
93              ).start(RandomPort.port())
94          ) {
95              final Reference reference = new RtReference(
96                  new ApacheRequest(container.home()),
97                  new MkGitHub().randomRepo(),
98                  "refs/heads/featureC"
99              );
100             MatcherAssert.assertThat(
101                 "Values are not equal",
102                 reference.ref(),
103                 Matchers.is("refs/heads/featureC")
104             );
105             container.stop();
106         }
107     }
108 
109     @Test
110     void returnsOwner() throws IOException {
111         final Repo owner = new MkGitHub().randomRepo();
112         try (
113             MkContainer container = new MkGrizzlyContainer().next(
114                 new MkAnswer.Simple(
115                     HttpURLConnection.HTTP_OK,
116                     "{\"ref\":\"refs/heads/featureD\"}"
117                 )
118             ).start(RandomPort.port())
119         ) {
120             final Reference reference = new RtReference(
121                 new ApacheRequest(container.home()),
122                 owner,
123                 "refs/heads/featureD"
124             );
125             MatcherAssert.assertThat(
126                 "Values are not equal",
127                 reference.repo(),
128                 Matchers.is(owner)
129             );
130             container.stop();
131         }
132     }
133 }