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.Branch;
33 import com.jcabi.github.Coordinates;
34 import com.jcabi.github.Repo;
35 import java.io.IOException;
36 import java.util.Iterator;
37 import org.hamcrest.MatcherAssert;
38 import org.hamcrest.Matchers;
39 import org.junit.Test;
40
41 /**
42 * Test case for {@link MkBranches}.
43 *
44 * @author Chris Rebert (github@rebertia.com)
45 * @version $Id: a57ef57b9b598be43d908d34086d6395a6ef25de $
46 */
47 public final class MkBranchesTest {
48 /**
49 * MkBranches can create a new branch.
50 * @throws IOException if there is any I/O problem
51 */
52 @Test
53 public void createsBranch() throws IOException {
54 final String name = "my-new-feature";
55 final String sha = "590e188e3d52a8da38cf51d3f9bf598bb46911af";
56 final Repo repo = new MkGithub().randomRepo();
57 final Branch branch = ((MkBranches) repo.branches())
58 .create(name, sha);
59 MatcherAssert.assertThat(branch.name(), Matchers.equalTo(name));
60 MatcherAssert.assertThat(
61 branch.commit().sha(),
62 Matchers.equalTo(sha)
63 );
64 final Coordinates coords = branch.commit().repo().coordinates();
65 MatcherAssert.assertThat(
66 coords.user(),
67 Matchers.equalTo(repo.coordinates().user())
68 );
69 MatcherAssert.assertThat(
70 coords.repo(),
71 Matchers.equalTo(repo.coordinates().repo())
72 );
73 }
74
75 /**
76 * MkBranches can iterate over the repo's branches.
77 * @throws IOException if there is any I/O problem
78 */
79 @Test
80 public void iteratesOverBranches() throws IOException {
81 final MkBranches branches = (MkBranches) new MkGithub().randomRepo()
82 .branches();
83 final String onename = "narf";
84 final String onesha = "a86da33b875e8ecbaf75cefcf6d8957cbecb654e";
85 branches.create(onename, onesha);
86 final String twoname = "zort";
87 final String twosha = "ba00fa4fe331c59736b87f52f760e1ccfb293b5f";
88 branches.create(twoname, twosha);
89 MatcherAssert.assertThat(
90 branches.iterate(),
91 Matchers.<Branch>iterableWithSize(2)
92 );
93 final Iterator<Branch> iter = branches.iterate().iterator();
94 final Branch one = iter.next();
95 MatcherAssert.assertThat(one.name(), Matchers.equalTo(onename));
96 MatcherAssert.assertThat(
97 one.commit().sha(),
98 Matchers.equalTo(onesha)
99 );
100 final Branch two = iter.next();
101 MatcherAssert.assertThat(two.name(), Matchers.equalTo(twoname));
102 MatcherAssert.assertThat(
103 two.commit().sha(),
104 Matchers.equalTo(twosha)
105 );
106 }
107 }