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.Repo;
33  import com.jcabi.github.Repos;
34  import org.hamcrest.MatcherAssert;
35  import org.hamcrest.Matchers;
36  import org.junit.Rule;
37  import org.junit.Test;
38  import org.junit.rules.ExpectedException;
39  
40  /**
41   * Test case for {@link MkRepos}.
42   * @author Yegor Bugayenko (yegor256@gmail.com)
43   * @version $Id: e13d35d26d72707f750294d6749d57509a50571e $
44   * @checkstyle MultipleStringLiterals (500 lines)
45   */
46  public final class MkReposTest {
47  
48      /**
49       * Rule for checking thrown exception.
50       * @checkstyle VisibilityModifier (3 lines)
51       */
52      @Rule
53      @SuppressWarnings("deprecation")
54      public transient ExpectedException thrown = ExpectedException.none();
55  
56      /**
57       * MkRepos can create a repo.
58       * @throws Exception If some problem inside
59       */
60      @Test
61      public void createsRepository() throws Exception {
62          final Repos repos = new MkRepos(new MkStorage.InFile(), "jeff");
63          final Repo repo = MkReposTest.repo(repos, "test", "test repo");
64          MatcherAssert.assertThat(
65              repo.coordinates(),
66              Matchers.hasToString("jeff/test")
67          );
68      }
69  
70      /**
71       * MkRepos can create a repo with organization.
72       * @throws Exception If some problem inside
73       */
74      @Test
75      public void createsRepositoryWithOrganization() throws Exception {
76          final Repos repos = new MkRepos(new MkStorage.InFile(), "john");
77          final Repo repo = MkReposTest.repoWithOrg(repos, "test", "myorg");
78          MatcherAssert.assertThat(
79              repo.coordinates(),
80              Matchers.hasToString("/orgs/myorg/repos/test")
81          );
82      }
83  
84      /**
85       * MkRepos can create a repo with details.
86       * @throws Exception If some problem inside
87       */
88      @Test
89      public void createsRepositoryWithDetails() throws Exception {
90          final Repos repos = new MkRepos(new MkStorage.InFile(), "jeff");
91          final Repo repo = MkReposTest.repo(repos, "hello", "my test repo");
92          MatcherAssert.assertThat(
93              new Repo.Smart(repo).description(),
94              Matchers.startsWith("my test")
95          );
96      }
97  
98      /**
99       * MkRepos can remove an existing repo.
100      * @throws Exception If some problem inside
101      */
102     @Test
103     public void removesRepo() throws Exception {
104         final Repos repos = new MkRepos(new MkStorage.InFile(), "jeff");
105         final Repo repo = MkReposTest.repo(repos, "remove-me", "remove repo");
106         MatcherAssert.assertThat(
107             repos.get(repo.coordinates()),
108             Matchers.notNullValue()
109         );
110     }
111 
112     /**
113      * MkRepos can iterate repos.
114      * @throws Exception if there is any error
115      */
116     @Test
117     public void iterateRepos() throws Exception {
118         final String since = "1";
119         final Repos repos = new MkRepos(new MkStorage.InFile(), "tom");
120         MkReposTest.repo(repos, since, "repo 1");
121         MkReposTest.repo(repos, "2", "repo 2");
122         MatcherAssert.assertThat(
123             repos.iterate(since),
124             Matchers.<Repo>iterableWithSize(2)
125         );
126     }
127 
128     /**
129      * MkRepos can create a private repo.
130      * @throws Exception If there is any error
131      */
132     @Test
133     public void createsPrivateRepo() throws Exception {
134         final boolean priv = true;
135         MatcherAssert.assertThat(
136             new Repo.Smart(
137                 new MkGithub().repos().create(
138                     new Repos.RepoCreate("test", priv)
139                 )
140             ).isPrivate(),
141             Matchers.is(priv)
142         );
143     }
144 
145     /**
146      * MkRepos can check for existing repos.
147      * @throws Exception If some problem inside
148      */
149     @Test
150     public void existsRepo() throws Exception {
151         final Repos repos = new MkRepos(new MkStorage.InFile(), "john");
152         final Repo repo = MkReposTest.repo(repos, "exist", "existing repo");
153         MatcherAssert.assertThat(
154             repos.exists(repo.coordinates()),
155             Matchers.is(true)
156         );
157     }
158 
159     /**
160      * Create and return Repo to test.
161      * @param repos Repos
162      * @param name Repo name
163      * @param desc Repo description
164      * @return Repo
165      * @throws Exception if there is any error
166      */
167     private static Repo repo(final Repos repos, final String name,
168         final String desc) throws Exception {
169         return repos.create(
170             new Repos.RepoCreate(name, false).withDescription(desc)
171         );
172     }
173 
174     /**
175      * Create and return Repo to test.
176      * @param repos Repos
177      * @param name Repo name
178      * @param org Repo organization
179      * @return Repo
180      * @throws Exception if there is any error
181      */
182     private static Repo repoWithOrg(final Repos repos, final String name,
183         final String org) throws Exception {
184         return repos.create(
185             new Repos.RepoCreate(name, false).withOrganization(org)
186         );
187     }
188 }