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.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
50
51
52
53
54
55
56 public final class RtBlobsTest {
57
58
59
60
61 @Rule
62 public final transient RandomPort resource = new RandomPort();
63
64
65
66
67
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
96
97
98 @Test
99 public void getBlob() {
100 final String sha = "6dcb09b5b57875f334f61aebed695e2e4193db52";
101 final Blobs blobs = new RtBlobs(
102 new FakeRequest().withBody(
103 Json.createObjectBuilder()
104 .add("sha", sha)
105 .build()
106 .toString()
107 ),
108 repo()
109 );
110 MatcherAssert.assertThat(blobs.get(sha).sha(), Matchers.equalTo(sha));
111 }
112
113
114
115
116
117 private static Repo repo() {
118 final Repo repo = Mockito.mock(Repo.class);
119 Mockito.doReturn(new Coordinates.Simple("mark", "test"))
120 .when(repo).coordinates();
121 return repo;
122 }
123
124
125
126
127
128
129 private static JsonObject blob() {
130 return Json.createObjectBuilder()
131 .add("url", "http://localhost/1")
132 .add("sha", RandomStringUtils.random(40, "0123456789abcdef"))
133 .build();
134 }
135 }