View Javadoc
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  
31  package com.jcabi.github.mock;
32  
33  import com.jcabi.github.Reference;
34  import javax.json.Json;
35  import javax.json.JsonObject;
36  import org.hamcrest.MatcherAssert;
37  import org.hamcrest.Matchers;
38  import org.junit.Test;
39  
40  /**
41   * Testcase for {@link MkReference}.
42   * @author Mihai Andronache (amihaiemil@gmail.com)
43   * @version $Id: 7775973c013fb0c99c7ca6cf0cd5e73f7f42f23d $
44   * @checkstyle MultipleStringLiterals (500 lines)
45   */
46  public final class MkReferenceTest {
47  
48      /**
49       * MkReference can return its name.
50       * @throws Exception - If something goes wrong.
51       */
52      @Test
53      public void returnsName() throws Exception {
54          MatcherAssert.assertThat(
55              this.reference().ref(),
56              Matchers.is("refs/tags/hello")
57          );
58      }
59  
60      /**
61       * MkReference can return its owner.
62       * @throws Exception - If something goes wrong.
63       */
64      @Test
65      public void returnsRepo() throws Exception {
66          MatcherAssert.assertThat(
67              this.reference().repo(),
68              Matchers.notNullValue()
69          );
70      }
71  
72      /**
73       * MkReference can fetch json.
74       * @throws Exception - If something goes wrong.
75       */
76      @Test
77      public void fetchesJson() throws Exception {
78          final Reference ref = this.reference();
79          final JsonObject json = ref.json();
80          MatcherAssert.assertThat(
81              json.getString("ref"),
82              Matchers.is("refs/tags/hello")
83          );
84          MatcherAssert.assertThat(
85              json.getString("sha"),
86              Matchers.is("testsha")
87          );
88      }
89  
90      /**
91       * MkReference should be able to patch itself.
92       * @throws Exception - If something goes wrong.
93       */
94      @Test
95      public void patchesRef() throws Exception {
96          final Reference ref = this.reference();
97          final JsonObject json = Json.createObjectBuilder()
98              .add("sha", "testshaPATCH")
99              .build();
100         ref.patch(json);
101         MatcherAssert.assertThat(
102             ref.json().getString("sha"),
103             Matchers.is("testshaPATCH")
104         );
105     }
106 
107     /**
108      * Return a Reference for testing.
109      * @return Reference
110      * @throws Exception - if something goes wrong.
111      */
112     private Reference reference() throws Exception {
113         return new MkGithub().randomRepo().git()
114             .references().create("refs/tags/hello", "testsha");
115     }
116 }