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.Coordinates;
33  import com.jcabi.github.Repo;
34  import java.io.IOException;
35  import org.hamcrest.MatcherAssert;
36  import org.hamcrest.Matchers;
37  import org.junit.Test;
38  import org.xembly.Directives;
39  
40  /**
41   * Test case for {@link MkRepoCommit).
42   * @author Andrej Istomin (andrej.istomin.ikeen@gmail.com)
43   * @version $Id: 5fe4d97c88e59f63b1d905b149fff4d6b8d21f77 $
44   */
45  public final class MkRepoCommitTest {
46  
47      /**
48       * The fist test key.
49       */
50      private static final String SHA1 =
51          "6dcb09b5b57875f334f61aebed695e2e4193db5e";
52      /**
53       * The second test key.
54       */
55      private static final String SHA2 =
56          "51cabb8e759852a6a40a7a2a76ef0afd4beef96d";
57  
58      /**
59       * MkRepoCommit can return repository.
60       * @throws IOException If some problem inside
61       */
62      @Test
63      public void getRepo() throws IOException {
64          final MkStorage storage = new MkStorage.InFile();
65          final Repo repo = this.repo(storage);
66          MatcherAssert.assertThat(
67              new MkRepoCommit(
68                  storage, repo, SHA1
69              ).repo(), Matchers.equalTo(repo)
70          );
71      }
72  
73      /**
74       * MkRepoCommit can return sha.
75       * @throws IOException If some problem inside
76       */
77      @Test
78      public void getSha() throws IOException {
79          final MkStorage storage = new MkStorage.InFile();
80          MatcherAssert.assertThat(
81              new MkRepoCommit(storage, this.repo(storage), SHA2).sha(),
82              Matchers.equalTo(SHA2)
83          );
84      }
85  
86      /**
87       * MkRepoCommit should be able to compare different instances.
88       *
89       * @throws Exception when a problem occurs.
90       */
91      @Test
92      public void canCompareInstances() throws Exception {
93          final MkStorage storage = new MkStorage.InFile();
94          final Repo repoa = new MkRepo(
95              storage, "login1",
96              new Coordinates.Simple("test_login1", "test_repo1")
97          );
98          final Repo repob = new MkRepo(
99              storage, "login2",
100             new Coordinates.Simple("test_login2", "test_repo2")
101         );
102         final MkRepoCommit less =  new MkRepoCommit(
103             storage, repoa, SHA1
104         );
105         final MkRepoCommit greater =  new MkRepoCommit(
106             storage, repob, SHA2
107         );
108         MatcherAssert.assertThat(
109             less.compareTo(greater),
110             Matchers.lessThan(0)
111         );
112         MatcherAssert.assertThat(
113             greater.compareTo(less),
114             Matchers.greaterThan(0)
115         );
116     }
117     /**
118      * MkRepoCommit can get a JSON.
119      * @throws Exception if some problem inside
120      */
121     @Test
122     public void canGetJson() throws Exception {
123         final MkStorage storage = new MkStorage.InFile();
124         storage.apply(
125             new Directives().xpath("/github").add("repos")
126                 .add("repo").attr("coords", "test_login/test_repo")
127                 .add("commits").add("commit").add("sha").set(SHA1)
128         );
129         final MkRepoCommit repoCommit = new MkRepoCommit(
130             storage, this.repo(storage), SHA1
131         );
132         MatcherAssert.assertThat(
133             repoCommit.json(), Matchers.notNullValue()
134         );
135     }
136 
137     /**
138      * MkRepoCommit can compare equal commits.
139      * @throws Exception If some problem inside
140      */
141     @Test
142     public void compareEqual() throws Exception {
143         final String sha = "c2c53d66948214258a26ca9ca845d7ac0c17f8e7";
144         final MkStorage storage = new MkStorage.InFile();
145         final Repo repo = this.repo(storage);
146         final MkRepoCommit commit = new MkRepoCommit(storage, repo, sha);
147         final MkRepoCommit other = new MkRepoCommit(storage, repo, sha);
148         MatcherAssert.assertThat(
149             commit.compareTo(other), Matchers.equalTo(0)
150         );
151         MatcherAssert.assertThat(
152             other.compareTo(commit), Matchers.equalTo(0)
153         );
154     }
155 
156     /**
157      * MkRepoCommit can compare different commits.
158      * @throws Exception If some problem inside
159      */
160     @Test
161     public void compareDifferent() throws Exception {
162         final MkStorage storage = new MkStorage.InFile();
163         final Repo repo = this.repo(storage);
164         final MkRepoCommit commit = new MkRepoCommit(
165             storage, repo, "6dcd4ce23d88e2ee9568ba546c007c63d9131c1b"
166         );
167         final MkRepoCommit other = new MkRepoCommit(
168             storage, repo, "e9d71f5ee7c92d6dc9e92ffdad17b8bd49418f98"
169         );
170         MatcherAssert.assertThat(
171             commit.compareTo(other), Matchers.not(0)
172         );
173         MatcherAssert.assertThat(
174             other.compareTo(commit), Matchers.not(0)
175         );
176     }
177 
178     /**
179      * Create repository for test.
180      * @param storage The storage
181      * @return Repo
182      */
183     private Repo repo(final MkStorage storage) {
184         final String login = "test_login";
185         return new MkRepo(
186             storage,
187             login,
188             new Coordinates.Simple(login, "test_repo")
189         );
190     }
191 }