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.Branch;
8   import com.jcabi.github.Coordinates;
9   import com.jcabi.github.Repo;
10  import java.io.IOException;
11  import java.util.Iterator;
12  import org.hamcrest.MatcherAssert;
13  import org.hamcrest.Matchers;
14  import org.junit.jupiter.api.Test;
15  
16  /**
17   * Test case for {@link MkBranches}.
18   * @since 0.8
19   */
20  @SuppressWarnings("PMD.AvoidDuplicateLiterals")
21  final class MkBranchesTest {
22      /**
23       * MkBranches can create a new branch.
24       * @throws IOException if there is any I/O problem
25       */
26      @Test
27      void createsBranch() throws IOException {
28          final String name = "my-new-feature";
29          final String sha = "590e188e3d52a8da38cf51d3f9bf598bb46911af";
30          final Repo repo = new MkGitHub().randomRepo();
31          final Branch branch = ((MkBranches) repo.branches())
32              .create(name, sha);
33          MatcherAssert.assertThat(
34              "Values are not equal", branch.name(), Matchers.equalTo(name)
35          );
36          MatcherAssert.assertThat(
37              "Values are not equal",
38              branch.commit().sha(),
39              Matchers.equalTo(sha)
40          );
41          final Coordinates coords = branch.commit().repo().coordinates();
42          MatcherAssert.assertThat(
43              "Values are not equal",
44              coords.user(),
45              Matchers.equalTo(repo.coordinates().user())
46          );
47          MatcherAssert.assertThat(
48              "Values are not equal",
49              coords.repo(),
50              Matchers.equalTo(repo.coordinates().repo())
51          );
52      }
53  
54      /**
55       * MkBranches can iterate over the repo's branches.
56       * @throws IOException if there is any I/O problem
57       */
58      @Test
59      void iteratesOverBranches() throws IOException {
60          final MkBranches branches = (MkBranches) new MkGitHub().randomRepo()
61              .branches();
62          final String onename = "narf";
63          final String onesha = "a86da33b875e8ecbaf75cefcf6d8957cbecb654e";
64          branches.create(onename, onesha);
65          final String twoname = "zort";
66          final String twosha = "ba00fa4fe331c59736b87f52f760e1ccfb293b5f";
67          branches.create(twoname, twosha);
68          MatcherAssert.assertThat(
69              "Collection size is incorrect",
70              branches.iterate(),
71              Matchers.iterableWithSize(2)
72          );
73          final Iterator<Branch> iter = branches.iterate().iterator();
74          final Branch one = iter.next();
75          MatcherAssert.assertThat(
76              "Values are not equal", one.name(), Matchers.equalTo(onename)
77          );
78          MatcherAssert.assertThat(
79              "Values are not equal",
80              one.commit().sha(),
81              Matchers.equalTo(onesha)
82          );
83          final Branch two = iter.next();
84          MatcherAssert.assertThat(
85              "Values are not equal", two.name(), Matchers.equalTo(twoname)
86          );
87          MatcherAssert.assertThat(
88              "Values are not equal",
89              two.commit().sha(),
90              Matchers.equalTo(twosha)
91          );
92      }
93  }