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