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 @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
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
148
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 }