View Javadoc
1   /**
2    * Copyright (c) 2013-2022, 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.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   * Test case for {@link MkContent}.
48   * @author Andres Candal (andres.candal@rollasolution.com)
49   * @version $Id: 48d9f114d6cfc0e9f1646e9aadb2f0e03a2dcab3 $
50   * @since 0.8
51   */
52  public final class MkContentTest {
53  
54      /**
55       * MkContent should be able to fetch its own repo.
56       *
57       * @throws Exception if some problem inside
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       * MkContent should be able to fetch its own path.
74       *
75       * @throws Exception if some problem inside
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       * MkContent should be able to fetch its JSON representation.
92       *
93       * @throws Exception if some problem inside
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             // @checkstyle MultipleStringLiterals (1 line)
104             content.json().getString("name"),
105             Matchers.is(path)
106         );
107     }
108 
109     /**
110      * MkContent should be able to fetch its raw representation.
111      *
112      * @throws Exception if some problem inside
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      * Get a JSON object for content creation.
133      * @param path The path of the file
134      * @param message Commit message
135      * @param content File content
136      * @throws java.io.UnsupportedEncodingException
137      *  if UTF-8 encoding is not supported.
138      * @return JSON representation of content attributes
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 }