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