1
2
3
4
5 package com.jcabi.github.mock;
6
7 import com.jcabi.github.Content;
8 import com.jcabi.github.Contents;
9 import com.jcabi.github.Repo;
10 import jakarta.json.Json;
11 import jakarta.json.JsonObject;
12 import java.io.IOException;
13 import java.io.InputStream;
14 import java.nio.charset.StandardCharsets;
15 import javax.xml.bind.DatatypeConverter;
16 import org.apache.commons.io.IOUtils;
17 import org.hamcrest.MatcherAssert;
18 import org.hamcrest.Matchers;
19 import org.junit.jupiter.api.Test;
20
21
22
23
24
25 final class MkContentTest {
26
27 @Test
28 void canGetOwnRepo() throws IOException {
29 final Repo repo = new MkGitHub().randomRepo();
30 final Contents contents = repo.contents();
31 final Content content = contents.create(
32 MkContentTest.jsonContent("repo.txt", "for repo", "json repo")
33 );
34 MatcherAssert.assertThat(
35 "Values are not equal",
36 content.repo(),
37 Matchers.is(repo)
38 );
39 }
40
41 @Test
42 void canGetOwnPath() throws IOException {
43 final Contents contents = new MkGitHub().randomRepo().contents();
44 final String path = "dummy.txt";
45 final Content content = contents.create(
46 MkContentTest.jsonContent(path, "for path", "path test")
47 );
48 MatcherAssert.assertThat(
49 "Values are not equal",
50 content.path(),
51 Matchers.is(path)
52 );
53 }
54
55 @Test
56 void fetchesJsonRepresentation() throws IOException {
57 final Contents contents = new MkGitHub().randomRepo().contents();
58 final String path = "fake.txt";
59 final Content content = contents.create(
60 MkContentTest.jsonContent(path, "for json", "json test")
61 );
62 MatcherAssert.assertThat(
63 "Values are not equal",
64
65 content.json().getString("name"),
66 Matchers.is(path)
67 );
68 }
69
70 @Test
71 void fetchesRawRepresentation() throws IOException {
72 final Contents contents = new MkGitHub().randomRepo().contents();
73 final String raw = "raw test \u20ac\u0000";
74 try (
75 InputStream stream = contents.create(
76 MkContentTest.jsonContent("raw.txt", "for raw", raw)
77 ).raw()
78 ) {
79 MatcherAssert.assertThat(
80 "Values are not equal",
81 IOUtils.toString(stream, StandardCharsets.UTF_8),
82 Matchers.is(raw)
83 );
84 }
85 }
86
87
88
89
90
91
92
93
94 private static JsonObject jsonContent(
95 final String path,
96 final String message,
97 final String content
98 ) {
99 return Json.createObjectBuilder()
100 .add("path", path)
101 .add("message", message)
102 .add(
103 "content",
104 DatatypeConverter.printBase64Binary(
105 content.getBytes(StandardCharsets.UTF_8)
106 )
107 ).build();
108 }
109 }