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  package com.jcabi.github;
31  
32  import javax.json.Json;
33  import org.hamcrest.MatcherAssert;
34  import org.hamcrest.Matchers;
35  import org.junit.Test;
36  import org.mockito.Mockito;
37  
38  /**
39   * Test case for {@link PublicKey}.
40   *
41   * @author Carlos Miranda (miranda.cma@gmail.com)
42   * @version $Id: 3b4e9ef273fd66ad74fe654aa9bfe08a044a0d09 $
43   * @checkstyle MultipleStringLiterals (150 lines)
44   */
45  public final class PublicKeyTest {
46  
47      /**
48       * PublicKey.Smart can fetch the key value from PublicKey.
49       * @throws Exception If a problem occurs.
50       */
51      @Test
52      public void fetchesKey() throws Exception {
53          final PublicKey key = Mockito.mock(PublicKey.class);
54          final String value = "sha-rsa AAA...";
55          Mockito.doReturn(
56              Json.createObjectBuilder().add("key", value).build()
57          ).when(key).json();
58          MatcherAssert.assertThat(
59              new PublicKey.Smart(key).key(),
60              Matchers.is(value)
61          );
62      }
63  
64      /**
65       * PublicKey.Smart can update the key value of PublicKey.
66       * @throws Exception If a problem occurs.
67       */
68      @Test
69      public void updatesKey() throws Exception {
70          final PublicKey key = Mockito.mock(PublicKey.class);
71          final String value = "sha-rsa BBB...";
72          new PublicKey.Smart(key).key(value);
73          Mockito.verify(key).patch(
74              Json.createObjectBuilder().add("key", value).build()
75          );
76      }
77  
78      /**
79       * PublicKey.Smart can fetch URL property from PublicKey.
80       * @throws Exception If a problem occurs.
81       */
82      @Test
83      public void fetchesUrl() throws Exception {
84          final PublicKey key = Mockito.mock(PublicKey.class);
85          final String prop = "https://api.github.com/user/keys/1";
86          Mockito.doReturn(
87              Json.createObjectBuilder().add("url", prop).build()
88          ).when(key).json();
89          MatcherAssert.assertThat(
90              new PublicKey.Smart(key).url().toString(),
91              Matchers.is(prop)
92          );
93      }
94  
95      /**
96       * PublicKey.Smart can fetch title property from PublicKey.
97       * @throws Exception If a problem occurs.
98       */
99      @Test
100     public void fetchesTitle() throws Exception {
101         final PublicKey key = Mockito.mock(PublicKey.class);
102         final String prop = "octocat@octomac";
103         Mockito.doReturn(
104             Json.createObjectBuilder().add("title", prop).build()
105         ).when(key).json();
106         MatcherAssert.assertThat(
107             new PublicKey.Smart(key).title(),
108             Matchers.is(prop)
109         );
110     }
111 
112     /**
113      * PublicKey.Smart can update the title property of PublicKey.
114      * @throws Exception If a problem occurs.
115      */
116     @Test
117     public void updatesTitle() throws Exception {
118         final PublicKey key = Mockito.mock(PublicKey.class);
119         final String prop = "octocat@octomac";
120         new PublicKey.Smart(key).title(prop);
121         Mockito.verify(key).patch(
122             Json.createObjectBuilder().add("title", prop).build()
123         );
124     }
125 
126 }