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.mock;
31
32 import com.jcabi.github.Content;
33 import com.jcabi.github.Contents;
34 import com.jcabi.github.Repo;
35 import java.io.InputStream;
36 import java.nio.charset.StandardCharsets;
37 import javax.json.Json;
38 import javax.json.JsonObject;
39 import javax.xml.bind.DatatypeConverter;
40 import org.apache.commons.io.IOUtils;
41 import org.hamcrest.MatcherAssert;
42 import org.hamcrest.Matchers;
43 import org.junit.Test;
44
45
46
47
48
49
50
51 public final class MkContentTest {
52
53
54
55
56
57
58 @Test
59 public void canGetOwnRepo() throws Exception {
60 final Repo repo = new MkGithub().randomRepo();
61 final Contents contents = repo.contents();
62 final Content content = contents.create(
63 jsonContent("repo.txt", "for repo", "json repo")
64 );
65 MatcherAssert.assertThat(
66 content.repo(),
67 Matchers.is(repo)
68 );
69 }
70
71
72
73
74
75
76 @Test
77 public void canGetOwnPath() throws Exception {
78 final Contents contents = new MkGithub().randomRepo().contents();
79 final String path = "dummy.txt";
80 final Content content = contents.create(
81 jsonContent(path, "for path", "path test")
82 );
83 MatcherAssert.assertThat(
84 content.path(),
85 Matchers.is(path)
86 );
87 }
88
89
90
91
92
93
94 @Test
95 public void fetchesJsonRepresentation() throws Exception {
96 final Contents contents = new MkGithub().randomRepo().contents();
97 final String path = "fake.txt";
98 final Content content = contents.create(
99 jsonContent(path, "for json", "json test")
100 );
101 MatcherAssert.assertThat(
102
103 content.json().getString("name"),
104 Matchers.is(path)
105 );
106 }
107
108
109
110
111
112
113 @Test
114 public void fetchesRawRepresentation() throws Exception {
115 final Contents contents = new MkGithub().randomRepo().contents();
116 final String raw = "raw test \u20ac\u0000";
117 final InputStream stream = contents.create(
118 jsonContent("raw.txt", "for raw", raw)
119 ).raw();
120 try {
121 MatcherAssert.assertThat(
122 IOUtils.toString(stream, StandardCharsets.UTF_8),
123 Matchers.is(raw)
124 );
125 } finally {
126 stream.close();
127 }
128 }
129
130
131
132
133
134
135
136
137 private static JsonObject jsonContent(
138 final String path,
139 final String message,
140 final String content
141 ) {
142 return Json.createObjectBuilder()
143 .add("path", path)
144 .add("message", message)
145 .add(
146 "content",
147 DatatypeConverter.printBase64Binary(
148 content.getBytes(StandardCharsets.UTF_8)
149 )
150 ).build();
151 }
152 }