View Javadoc
1   /**
2    * Copyright (c) 2013-2023, jcabi.com
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met: 1) Redistributions of source code must retain the above
8    * copyright notice, this list of conditions and the following
9    * disclaimer. 2) Redistributions in binary form must reproduce the above
10   * copyright notice, this list of conditions and the following
11   * disclaimer in the documentation and/or other materials provided
12   * with the distribution. 3) Neither the name of the jcabi.com nor
13   * the names of its contributors may be used to endorse or promote
14   * products derived from this software without specific prior written
15   * permission.
16   *
17   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
19   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21   * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28   * OF THE POSSIBILITY OF SUCH DAMAGE.
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   * Test case for {@link MkContent}.
47   * @author Andres Candal (andres.candal@rollasolution.com)
48   * @version $Id: 572a3f48db4521eaf15b3077c4f61b75fd618fdf $
49   * @since 0.8
50   */
51  public final class MkContentTest {
52  
53      /**
54       * MkContent should be able to fetch its own repo.
55       *
56       * @throws Exception if some problem inside
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       * MkContent should be able to fetch its own path.
73       *
74       * @throws Exception if some problem inside
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       * MkContent should be able to fetch its JSON representation.
91       *
92       * @throws Exception if some problem inside
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             // @checkstyle MultipleStringLiterals (1 line)
103             content.json().getString("name"),
104             Matchers.is(path)
105         );
106     }
107 
108     /**
109      * MkContent should be able to fetch its raw representation.
110      *
111      * @throws Exception if some problem inside
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      * Get a JSON object for content creation.
132      * @param path The path of the file
133      * @param message Commit message
134      * @param content File content
135      * @return JSON representation of content attributes
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 }