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.hamcrest.MatcherAssert;
42  import org.hamcrest.Matchers;
43  import org.junit.Rule;
44  import org.junit.Test;
45  import org.mockito.Mockito;
46  
47  /**
48   * Test case for {@link RtTrees}.
49   * @author Alexander Lukashevich (sanai56967@gmail.com)
50   * @version $Id: 0c895b47f8f4fc618b8d1a842d9f5ae57b3ff6b8 $
51   * @checkstyle MultipleStringLiteralsCheck (100 lines)
52   */
53  public final class RtTreesTest {
54  
55      /**
56       * The rule for skipping test if there's BindException.
57       * @checkstyle VisibilityModifierCheck (3 lines)
58       */
59      @Rule
60      public final transient RandomPort resource = new RandomPort();
61  
62      /**
63       * RtTrees can create a tree.
64       * @throws Exception - If something goes wrong.
65       */
66      @Test
67      public void createsTree() throws Exception {
68          final MkContainer container = new MkGrizzlyContainer().next(
69              new MkAnswer.Simple(
70                  HttpURLConnection.HTTP_CREATED,
71                  "{\"sha\":\"0abcd89jcabitest\",\"url\":\"http://localhost/1\"}"
72              )
73          ).start(this.resource.port());
74          final Trees trees = new RtTrees(
75              new ApacheRequest(container.home()),
76              repo()
77          );
78          final JsonObject tree = Json.createObjectBuilder()
79              .add("path", "/path").add("mode", "100644 ")
80              .add("type", "blob").add("sha", "sha1")
81              .add("content", "content1").build();
82          final JsonObject input = Json.createObjectBuilder()
83              .add("tree", tree).add("base_tree", "SHA1")
84              .build();
85          try {
86              final Tree tri = trees.create(input);
87              MatcherAssert.assertThat(
88                  tri,
89                  Matchers.instanceOf(Tree.class)
90              );
91              MatcherAssert.assertThat(
92                  trees.get(tri.sha()),
93                  Matchers.equalTo(tri)
94              );
95              MatcherAssert.assertThat(
96                  container.take().method(),
97                  Matchers.equalTo(Request.POST)
98              );
99          } finally {
100             container.stop();
101         }
102     }
103 
104     /**
105      * RtTrees can get tree.
106      *
107      * @throws Exception if some problem inside
108      */
109     @Test
110     public void getTree() throws Exception {
111         final String sha = "0abcd89jcabitest";
112         final Trees trees = new RtTrees(
113             new FakeRequest().withBody(
114                 Json.createObjectBuilder()
115                     .add("sha", sha)
116                     .build()
117                     .toString()
118             ),
119             repo()
120         );
121         MatcherAssert.assertThat(
122             trees.get(sha).sha(), Matchers.equalTo(sha)
123         );
124     }
125 
126     /**
127      * RtTrees can get tree recursively.
128      *
129      * @throws Exception if some problem inside
130      */
131     @Test
132     public void getTreeRec() throws Exception {
133         final String sha = "0abcd89jcabitest";
134         final Trees trees = new RtTrees(
135             new FakeRequest().withBody(
136                 Json.createObjectBuilder()
137                     .add("sha", sha)
138                     .build()
139                     .toString()
140             ),
141             repo()
142         );
143         MatcherAssert.assertThat(
144             trees.getRec(sha).sha(), Matchers.equalTo(sha)
145         );
146     }
147 
148     /**
149      * Create and return repo to test.
150      * @return Repo
151      * @throws Exception If some problem inside
152      */
153     private static Repo repo() throws Exception {
154         final Repo repo = Mockito.mock(Repo.class);
155         Mockito.doReturn(new Coordinates.Simple("mark", "test"))
156             .when(repo).coordinates();
157         return repo;
158     }
159 
160 }