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.apache.commons.lang3.RandomStringUtils;
18  import org.hamcrest.MatcherAssert;
19  import org.hamcrest.Matchers;
20  import org.junit.jupiter.api.Test;
21  import org.junit.jupiter.api.extension.ExtendWith;
22  import org.mockito.Mockito;
23  
24  /**
25   * Test case for {@link RtBlobs}.
26   * @since 0.8
27   * @checkstyle MultipleStringLiteralsCheck (100 lines)
28   * @checkstyle ClassDataAbstractionCouplingCheck (200 lines)
29   */
30  @ExtendWith(RandomPort.class)
31  final class RtBlobsTest {
32      /**
33       * The rule for skipping test if there's BindException.
34       * @checkstyle VisibilityModifierCheck (3 lines)
35       */
36      @Test
37      void canCreateBlob() throws IOException {
38          final String body = RtBlobsTest.blob().toString();
39          try (
40              MkContainer container = new MkGrizzlyContainer().next(
41                  new MkAnswer.Simple(HttpURLConnection.HTTP_CREATED, body)
42              ).next(new MkAnswer.Simple(HttpURLConnection.HTTP_OK, body))
43                  .start(RandomPort.port())
44          ) {
45              final RtBlobs blobs = new RtBlobs(
46                  new ApacheRequest(container.home()),
47                  RtBlobsTest.repo()
48              );
49              final String content = "Content of the blob";
50              final Blob blob = blobs.create(content, "utf-8");
51              MatcherAssert.assertThat(
52                  "Values are not equal",
53                  container.take().method(),
54                  Matchers.equalTo(Request.POST)
55              );
56              MatcherAssert.assertThat(
57                  "Values are not equal",
58                  new Blob.Smart(blob).url(),
59                  Matchers.equalTo("http://localhost/1")
60              );
61          }
62      }
63  
64      @Test
65      void getBlob() {
66          final String sha = "6dcb09b5b57875f334f61aebed695e2e4193db52";
67          final Blobs blobs = new RtBlobs(
68              new FakeRequest().withBody(
69                  Json.createObjectBuilder()
70                      .add("sha", sha)
71                      .build()
72                      .toString()
73              ),
74              RtBlobsTest.repo()
75          );
76          MatcherAssert.assertThat(
77              "Values are not equal", blobs.get(sha).sha(), Matchers.equalTo(sha)
78          );
79      }
80  
81      /**
82       * Create and return repo to test.
83       * @return Repo
84       */
85      private static Repo repo() {
86          final Repo repo = Mockito.mock(Repo.class);
87          Mockito.doReturn(new Coordinates.Simple("mark", "test"))
88              .when(repo).coordinates();
89          return repo;
90      }
91  
92      /**
93       * Create and return JsonObject to test.
94       * @return JsonObject
95       * @checkstyle MagicNumberCheck (10 lines)
96       */
97      private static JsonObject blob() {
98          return Json.createObjectBuilder()
99              .add("url", "http://localhost/1")
100             .add("sha", RandomStringUtils.secure().next(40, "0123456789abcdef"))
101             .build();
102     }
103 }