View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2013-2025 Yegor Bugayenko
3    * SPDX-License-Identifier: MIT
4    */
5   package com.jcabi.github.mock;
6   
7   import com.jcabi.github.Repo;
8   import com.jcabi.github.Repos;
9   import java.io.IOException;
10  import org.hamcrest.MatcherAssert;
11  import org.hamcrest.Matchers;
12  import org.junit.jupiter.api.Test;
13  
14  /**
15   * Test case for {@link MkRepos}.
16   * @since 0.5
17   * @checkstyle MultipleStringLiterals (500 lines)
18   */
19  final class MkReposTest {
20  
21      /**
22       * MkRepos can create a repo.
23       * @throws Exception If some problem inside
24       */
25      @Test
26      void createsRepository() throws Exception {
27          final Repos repos = new MkRepos(new MkStorage.InFile(), "jeff");
28          final Repo repo = MkReposTest.repo(repos, "test", "test repo");
29          MatcherAssert.assertThat(
30              "Assertion failed",
31              repo.coordinates(),
32              Matchers.hasToString("jeff/test")
33          );
34      }
35  
36      /**
37       * MkRepos can create a repo with organization.
38       * @throws Exception If some problem inside
39       */
40      @Test
41      void createsRepositoryWithOrganization() throws Exception {
42          final Repos repos = new MkRepos(new MkStorage.InFile(), "john");
43          final Repo repo = MkReposTest.repoWithOrg(repos, "test", "myorg");
44          MatcherAssert.assertThat(
45              "Assertion failed",
46              repo.coordinates(),
47              Matchers.hasToString("/orgs/myorg/repos/test")
48          );
49      }
50  
51      /**
52       * MkRepos can create a repo with details.
53       * @throws Exception If some problem inside
54       */
55      @Test
56      void createsRepositoryWithDetails() throws Exception {
57          final Repos repos = new MkRepos(new MkStorage.InFile(), "jeff");
58          final Repo repo = MkReposTest.repo(repos, "hello", "my test repo");
59          MatcherAssert.assertThat(
60              "String does not start with expected value",
61              new Repo.Smart(repo).description(),
62              Matchers.startsWith("my test")
63          );
64      }
65  
66      /**
67       * MkRepos can remove an existing repo.
68       * @throws Exception If some problem inside
69       */
70      @Test
71      void removesRepo() throws Exception {
72          final Repos repos = new MkRepos(new MkStorage.InFile(), "jeff");
73          final Repo repo = MkReposTest.repo(repos, "remove-me", "remove repo");
74          MatcherAssert.assertThat(
75              "Value is null",
76              repos.get(repo.coordinates()),
77              Matchers.notNullValue()
78          );
79      }
80  
81      /**
82       * MkRepos can iterate repos.
83       * @throws Exception if there is any error
84       */
85      @Test
86      void iterateRepos() throws Exception {
87          final String since = "1";
88          final Repos repos = new MkRepos(new MkStorage.InFile(), "tom");
89          MkReposTest.repo(repos, since, "repo 1");
90          MkReposTest.repo(repos, "2", "repo 2");
91          MatcherAssert.assertThat(
92              "Collection size is incorrect",
93              repos.iterate(since),
94              Matchers.iterableWithSize(2)
95          );
96      }
97  
98      @Test
99      void createsPrivateRepo() throws IOException {
100         final boolean priv = true;
101         MatcherAssert.assertThat(
102             "Values are not equal",
103             new Repo.Smart(
104                 new MkGitHub().repos().create(
105                     new Repos.RepoCreate("test", priv)
106                 )
107             ).isPrivate(),
108             Matchers.is(priv)
109         );
110     }
111 
112     /**
113      * MkRepos can check for existing repos.
114      * @throws Exception If some problem inside
115      */
116     @Test
117     void existsRepo() throws Exception {
118         final Repos repos = new MkRepos(new MkStorage.InFile(), "john");
119         final Repo repo = MkReposTest.repo(repos, "exist", "existing repo");
120         MatcherAssert.assertThat(
121             "Values are not equal",
122             repos.exists(repo.coordinates()),
123             Matchers.is(true)
124         );
125     }
126 
127     /**
128      * Create and return Repo to test.
129      * @param repos Repos
130      * @param name Repo name
131      * @param desc Repo description
132      * @return Repo
133      */
134     private static Repo repo(final Repos repos, final String name,
135         final String desc) throws IOException {
136         return repos.create(
137             new Repos.RepoCreate(name, false).withDescription(desc)
138         );
139     }
140 
141     /**
142      * Create and return Repo to test.
143      * @param repos Repos
144      * @param name Repo name
145      * @param org Repo organization
146      * @return Repo
147      */
148     private static Repo repoWithOrg(final Repos repos, final String name,
149         final String org) throws IOException {
150         return repos.create(
151             new Repos.RepoCreate(name, false).withOrganization(org)
152         );
153     }
154 }