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;
31  
32  import java.io.IOException;
33  import java.net.URL;
34  import javax.json.Json;
35  import org.hamcrest.MatcherAssert;
36  import org.hamcrest.Matchers;
37  import org.junit.Test;
38  import org.mockito.Mockito;
39  
40  /**
41   * Test case for {@link RepoCommit}.
42   * @author Paul Polischuk (ppol@ua.fm)
43   * @version $Id: eccd96026036b9657217937811fa87f0d01e843f $
44   * @checkstyle MultipleStringLiterals (500 lines)
45   */
46  public class RepoCommitTest {
47  
48      /**
49       * RepoCommit.Smart can fetch url property from RepoCommit.
50       * @throws Exception If some problem inside
51       */
52      @Test
53      public final void fetchesUrl() throws Exception {
54          final RepoCommit commit = Mockito.mock(RepoCommit.class);
55          // @checkstyle LineLength (1 line)
56          final String prop = "https://api.github.com/repos/pengwynn/octokit/contents/README.md";
57          Mockito.doReturn(
58              Json.createObjectBuilder()
59                  .add("url", prop)
60                  .build()
61          ).when(commit).json();
62          MatcherAssert.assertThat(
63              new RepoCommit.Smart(commit).url(),
64              Matchers.is(new URL(prop))
65          );
66      }
67  
68      /**
69       * RepoCommit.Smart can fetch message property from RepoCommit.
70       * @throws Exception If some problem inside
71       */
72      @Test
73      public final void fetchesMessage() throws Exception {
74          final RepoCommit commit = Mockito.mock(RepoCommit.class);
75          Mockito.doReturn(
76              Json.createObjectBuilder().add(
77                  "commit",
78                  Json.createObjectBuilder().add("message", "hello, world!")
79              ).build()
80          ).when(commit).json();
81          MatcherAssert.assertThat(
82              new RepoCommit.Smart(commit).message(),
83              Matchers.startsWith("hello, ")
84          );
85      }
86  
87      /**
88       * RtRepoCommit can verify status.
89       * @throws IOException If fails
90       */
91      @Test
92      public final void verifiesStatus() throws IOException {
93          final RepoCommit commit = Mockito.mock(RepoCommit.class);
94          Mockito.doReturn(
95              Json.createObjectBuilder().add(
96                  "commit",
97                  Json.createObjectBuilder().add(
98                      "verification",
99                      Json.createObjectBuilder().add("verified", true)
100                 ).build()
101             ).build()
102         ).when(commit).json();
103         MatcherAssert.assertThat(
104             new RepoCommit.Smart(commit).isVerified(),
105             Matchers.is(true)
106         );
107     }
108 
109     /**
110      * RtRepoCommit can read author's login.
111      * @throws IOException If fails
112      */
113     @Test
114     public final void readsAuthorLogin() throws IOException {
115         final RepoCommit commit = Mockito.mock(RepoCommit.class);
116         final String login = "jeff";
117         Mockito.doReturn(
118             Json.createObjectBuilder().add(
119                 "commit",
120                 Json.createObjectBuilder().add(
121                     "author",
122                     Json.createObjectBuilder().add("name", login)
123                 ).build()
124             ).build()
125         ).when(commit).json();
126         MatcherAssert.assertThat(
127             new RepoCommit.Smart(commit).author(),
128             Matchers.equalTo(login)
129         );
130     }
131 }