View Javadoc
1   /**
2    * Copyright (c) 2013-2022, 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: c3d66a3728e4a7745f229d9bfa2aaeb64cb41835 $
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       * @throws Exception if some problem inside
98       */
99      @Test
100     public void getBlob() throws Exception {
101         final String sha = "6dcb09b5b57875f334f61aebed695e2e4193db52";
102         final Blobs blobs = new RtBlobs(
103             new FakeRequest().withBody(
104                 Json.createObjectBuilder()
105                     .add("sha", sha)
106                     .build()
107                     .toString()
108             ),
109             repo()
110         );
111         MatcherAssert.assertThat(blobs.get(sha).sha(), Matchers.equalTo(sha));
112     }
113 
114     /**
115      * Create and return repo to test.
116      * @return Repo
117      * @throws Exception If some problem inside
118      */
119     private static Repo repo() throws Exception {
120         final Repo repo = Mockito.mock(Repo.class);
121         Mockito.doReturn(new Coordinates.Simple("mark", "test"))
122             .when(repo).coordinates();
123         return repo;
124     }
125 
126     /**
127      * Create and return JsonObject to test.
128      * @return JsonObject
129      * @throws Exception If some problem inside
130      * @checkstyle MagicNumberCheck (10 lines)
131      */
132     private static JsonObject blob() throws Exception {
133         return Json.createObjectBuilder()
134             .add("url", "http://localhost/1")
135             .add("sha", RandomStringUtils.random(40, "0123456789abcdef"))
136             .build();
137     }
138 }