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.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: 58f421df36eef948c9477843048f4f0e19ba4570 $
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 */
108 @Test
109 public void getTree() {
110 final String sha = "0abcd89jcabitest";
111 final Trees trees = new RtTrees(
112 new FakeRequest().withBody(
113 Json.createObjectBuilder()
114 .add("sha", sha)
115 .build()
116 .toString()
117 ),
118 repo()
119 );
120 MatcherAssert.assertThat(
121 trees.get(sha).sha(), Matchers.equalTo(sha)
122 );
123 }
124
125 /**
126 * RtTrees can get tree recursively.
127 *
128 */
129 @Test
130 public void getTreeRec() {
131 final String sha = "0abcd89jcabitest";
132 final Trees trees = new RtTrees(
133 new FakeRequest().withBody(
134 Json.createObjectBuilder()
135 .add("sha", sha)
136 .build()
137 .toString()
138 ),
139 repo()
140 );
141 MatcherAssert.assertThat(
142 trees.getRec(sha).sha(), Matchers.equalTo(sha)
143 );
144 }
145
146 /**
147 * Create and return repo to test.
148 * @return Repo
149 */
150 private static Repo repo() {
151 final Repo repo = Mockito.mock(Repo.class);
152 Mockito.doReturn(new Coordinates.Simple("mark", "test"))
153 .when(repo).coordinates();
154 return repo;
155 }
156
157 }