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.Reference;
8   import jakarta.json.Json;
9   import jakarta.json.JsonObject;
10  import java.io.IOException;
11  import org.hamcrest.MatcherAssert;
12  import org.hamcrest.Matchers;
13  import org.junit.jupiter.api.Test;
14  
15  /**
16   * Testcase for {@link MkReference}.
17   * @since 0.1
18   * @checkstyle MultipleStringLiterals (500 lines)
19   */
20  final class MkReferenceTest {
21  
22      /**
23       * MkReference can return its name.
24       * @throws Exception - If something goes wrong.
25       */
26      @Test
27      void returnsName() throws Exception {
28          MatcherAssert.assertThat(
29              "Values are not equal",
30              MkReferenceTest.reference().ref(),
31              Matchers.is("refs/tags/hello")
32          );
33      }
34  
35      /**
36       * MkReference can return its owner.
37       * @throws Exception - If something goes wrong.
38       */
39      @Test
40      void returnsRepo() throws Exception {
41          MatcherAssert.assertThat(
42              "Value is null",
43              MkReferenceTest.reference().repo(),
44              Matchers.notNullValue()
45          );
46      }
47  
48      /**
49       * MkReference can fetch json.
50       * @throws Exception - If something goes wrong.
51       */
52      @Test
53      void fetchesJson() throws Exception {
54          final Reference ref = MkReferenceTest.reference();
55          final JsonObject json = ref.json();
56          MatcherAssert.assertThat(
57              "Values are not equal",
58              json.getString("ref"),
59              Matchers.is("refs/tags/hello")
60          );
61          MatcherAssert.assertThat(
62              "Values are not equal",
63              json.getString("sha"),
64              Matchers.is("testsha")
65          );
66      }
67  
68      /**
69       * MkReference should be able to patch itself.
70       * @throws Exception - If something goes wrong.
71       */
72      @Test
73      void patchesRef() throws Exception {
74          final Reference ref = MkReferenceTest.reference();
75          final JsonObject json = Json.createObjectBuilder()
76              .add("sha", "testshaPATCH")
77              .build();
78          ref.patch(json);
79          MatcherAssert.assertThat(
80              "Values are not equal",
81              ref.json().getString("sha"),
82              Matchers.is("testshaPATCH")
83          );
84      }
85  
86      /**
87       * Return a Reference for testing.
88       * @return Reference
89       */
90      private static Reference reference() throws IOException {
91          return new MkGitHub().randomRepo().git()
92              .references().create("refs/tags/hello", "testsha");
93      }
94  }