View Javadoc
1   /**
2    * Copyright (c) 2013-2023, jcabi.com
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met: 1) Redistributions of source code must retain the above
8    * copyright notice, this list of conditions and the following
9    * disclaimer. 2) Redistributions in binary form must reproduce the above
10   * copyright notice, this list of conditions and the following
11   * disclaimer in the documentation and/or other materials provided
12   * with the distribution. 3) Neither the name of the jcabi.com nor
13   * the names of its contributors may be used to endorse or promote
14   * products derived from this software without specific prior written
15   * permission.
16   *
17   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
19   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21   * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28   * OF THE POSSIBILITY OF SUCH DAMAGE.
29   */
30  package com.jcabi.github;
31  
32  import com.jcabi.http.Request;
33  import com.jcabi.http.mock.MkAnswer;
34  import com.jcabi.http.mock.MkContainer;
35  import com.jcabi.http.mock.MkGrizzlyContainer;
36  import com.jcabi.http.request.ApacheRequest;
37  import com.jcabi.http.request.FakeRequest;
38  import java.net.HttpURLConnection;
39  import javax.json.Json;
40  import javax.json.JsonObject;
41  import org.apache.commons.lang3.RandomStringUtils;
42  import org.hamcrest.MatcherAssert;
43  import org.hamcrest.Matchers;
44  import org.junit.Rule;
45  import org.junit.Test;
46  import org.mockito.Mockito;
47  
48  /**
49   * Test case for {@link RtBlobs}.
50   *
51   * @author Alexander Lukashevich (sanai56967@gmail.com)
52   * @version $Id: ca90433163778c42aaa26c75d9d804b7448596b5 $
53   * @checkstyle MultipleStringLiteralsCheck (100 lines)
54   * @checkstyle ClassDataAbstractionCouplingCheck (200 lines)
55   */
56  public final class RtBlobsTest {
57      /**
58       * The rule for skipping test if there's BindException.
59       * @checkstyle VisibilityModifierCheck (3 lines)
60       */
61      @Rule
62      public final transient RandomPort resource = new RandomPort();
63  
64      /**
65       * RtBlobs can create a blob.
66       *
67       * @throws Exception if some problem inside
68       */
69      @Test
70      public void canCreateBlob() throws Exception {
71          final String content = "Content of the blob";
72          final String body = blob().toString();
73          try (
74              final MkContainer container = new MkGrizzlyContainer().next(
75                  new MkAnswer.Simple(HttpURLConnection.HTTP_CREATED, body)
76              ).next(new MkAnswer.Simple(HttpURLConnection.HTTP_OK, body))
77              .start(this.resource.port())) {
78              final RtBlobs blobs = new RtBlobs(
79                  new ApacheRequest(container.home()),
80                  repo()
81              );
82              final Blob blob = blobs.create(content, "utf-8");
83              MatcherAssert.assertThat(
84                  container.take().method(),
85                  Matchers.equalTo(Request.POST)
86              );
87              MatcherAssert.assertThat(
88                  new Blob.Smart(blob).url(),
89                  Matchers.equalTo("http://localhost/1")
90              );
91          }
92      }
93  
94      /**
95       * RtBlobs can get blob.
96       *
97       */
98      @Test
99      public void getBlob() {
100         final String sha = "6dcb09b5b57875f334f61aebed695e2e4193db52";
101         final Blobs blobs = new RtBlobs(
102             new FakeRequest().withBody(
103                 Json.createObjectBuilder()
104                     .add("sha", sha)
105                     .build()
106                     .toString()
107             ),
108             repo()
109         );
110         MatcherAssert.assertThat(blobs.get(sha).sha(), Matchers.equalTo(sha));
111     }
112 
113     /**
114      * Create and return repo to test.
115      * @return Repo
116      */
117     private static Repo repo() {
118         final Repo repo = Mockito.mock(Repo.class);
119         Mockito.doReturn(new Coordinates.Simple("mark", "test"))
120             .when(repo).coordinates();
121         return repo;
122     }
123 
124     /**
125      * Create and return JsonObject to test.
126      * @return JsonObject
127      * @checkstyle MagicNumberCheck (10 lines)
128      */
129     private static JsonObject blob() {
130         return Json.createObjectBuilder()
131             .add("url", "http://localhost/1")
132             .add("sha", RandomStringUtils.random(40, "0123456789abcdef"))
133             .build();
134     }
135 }