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.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 RtTrees}.
25   * @since 0.8
26   * @checkstyle MultipleStringLiteralsCheck (100 lines)
27   */
28  @ExtendWith(RandomPort.class)
29  final class RtTreesTest {
30  
31      /**
32       * The rule for skipping test if there's BindException.
33       * @checkstyle VisibilityModifierCheck (3 lines)
34       */
35      @Test
36      void createsTree() throws IOException {
37          final MkContainer container = new MkGrizzlyContainer().next(
38              new MkAnswer.Simple(
39                  HttpURLConnection.HTTP_CREATED,
40                  "{\"sha\":\"0abcd89jcabitest\", \"url\":\"http://localhost/1\"}"
41              )
42          ).start(RandomPort.port());
43          final Trees trees = new RtTrees(
44              new ApacheRequest(container.home()),
45              RtTreesTest.repo()
46          );
47          final JsonObject tree = Json.createObjectBuilder()
48              .add("path", "/path").add("mode", "100644 ")
49              .add("type", "blob").add("sha", "sha1")
50              .add("content", "content1").build();
51          final JsonObject input = Json.createObjectBuilder()
52              .add("tree", tree).add("base_tree", "SHA1")
53              .build();
54          try {
55              final Tree tri = trees.create(input);
56              MatcherAssert.assertThat(
57                  "Object is not of expected type",
58                  tri,
59                  Matchers.instanceOf(Tree.class)
60              );
61              MatcherAssert.assertThat(
62                  "Values are not equal",
63                  trees.get(tri.sha()),
64                  Matchers.equalTo(tri)
65              );
66              MatcherAssert.assertThat(
67                  "Values are not equal",
68                  container.take().method(),
69                  Matchers.equalTo(Request.POST)
70              );
71          } finally {
72              container.stop();
73          }
74      }
75  
76      @Test
77      void getTree() {
78          final String sha = "0abcd89jcabitest";
79          final Trees trees = new RtTrees(
80              new FakeRequest().withBody(
81                  Json.createObjectBuilder()
82                      .add("sha", sha)
83                      .build()
84                      .toString()
85              ),
86              RtTreesTest.repo()
87          );
88          MatcherAssert.assertThat(
89              "Values are not equal",
90              trees.get(sha).sha(), Matchers.equalTo(sha)
91          );
92      }
93  
94      @Test
95      void getTreeRec() {
96          final String sha = "0abcd89jcabitest";
97          final Trees trees = new RtTrees(
98              new FakeRequest().withBody(
99                  Json.createObjectBuilder()
100                     .add("sha", sha)
101                     .build()
102                     .toString()
103             ),
104             RtTreesTest.repo()
105         );
106         MatcherAssert.assertThat(
107             "Values are not equal",
108             trees.getRec(sha).sha(), Matchers.equalTo(sha)
109         );
110     }
111 
112     /**
113      * Create and return repo to test.
114      * @return Repo
115      */
116     private static Repo repo() {
117         final Repo repo = Mockito.mock(Repo.class);
118         Mockito.doReturn(new Coordinates.Simple("mark", "test"))
119             .when(repo).coordinates();
120         return repo;
121     }
122 
123 }