1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
49
50
51
52
53 public final class RtTreesTest {
54
55
56
57
58
59 @Rule
60 public final transient RandomPort resource = new RandomPort();
61
62
63
64
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
106
107
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
128
129
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
150
151
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 }