View Javadoc
1   /**
2    * Copyright (c) 2013-2022, 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.UserEmails;
33  import java.util.Arrays;
34  import java.util.Collections;
35  import org.hamcrest.MatcherAssert;
36  import org.hamcrest.Matchers;
37  import org.junit.Test;
38  
39  /**
40   * Test case for {@link MkUserEmails}.
41   *
42   * @author Carlos Miranda (miranda.cma@gmail.com)
43   * @version $Id: a36d78863a19dcefd1e91851864f7c242ae21bca $
44   * @since 0.8
45   */
46  public final class MkUserEmailsTest {
47  
48      /**
49       * MkUserEmails should be able to add emails to a user.
50       *
51       * @throws Exception if a problem occurs.
52       */
53      @Test
54      public void canAddEmails() throws Exception {
55          final UserEmails emails = new MkGithub().users().add("john").emails();
56          final String email = "john@nowhere.com";
57          final Iterable<String> added = emails.add(
58              Collections.singleton(email)
59          );
60          MatcherAssert.assertThat(
61              added,
62              Matchers.allOf(
63                  Matchers.<String>iterableWithSize(1),
64                  Matchers.hasItems(email)
65              )
66          );
67      }
68  
69      /**
70       * MkUserEmails should be able to remove emails of a user.
71       *
72       * @throws Exception if a problem occurs.
73       */
74      @Test
75      public void canRemoveEmails() throws Exception {
76          final UserEmails emails = new MkGithub().users().add("joe").emails();
77          final String removed = "joe@nowhere.com";
78          final String retained = "joseph@somewhere.net";
79          emails.add(
80              Arrays.asList(
81                  new String[]{removed, retained}
82              )
83          );
84          emails.remove(Collections.singleton(removed));
85          MatcherAssert.assertThat(
86              emails.iterate(),
87              Matchers.allOf(
88                  Matchers.<String>iterableWithSize(1),
89                  Matchers.hasItems(retained),
90                  Matchers.not(Matchers.hasItems(removed))
91              )
92          );
93      }
94  
95      /**
96       * MkUserEmails should be able to iterate emails of a user.
97       *
98       * @throws Exception if a problem occurs.
99       */
100     @Test
101     public void canIterateEmails() throws Exception {
102         final UserEmails emails = new MkGithub().users().add("matt").emails();
103         final String[] added = new String[]{
104             "matt@none.org",
105             "matthew@somewhere.net",
106         };
107         emails.add(Arrays.asList(added));
108         MatcherAssert.assertThat(
109             emails.iterate(),
110             Matchers.allOf(
111                 Matchers.<String>iterableWithSize(2),
112                 Matchers.hasItems(added)
113             )
114         );
115     }
116 
117     /**
118      * MkUserEmails can be represented in JSON format.
119      *
120      * @throws Exception if a problem occurs.
121      */
122     @Test
123     public void canRepresentAsJson() throws Exception {
124         final UserEmails emails = new MkGithub().users().add("jeff").emails();
125         final String email = "jeff@something.net";
126         emails.add(Collections.singleton(email));
127         MatcherAssert.assertThat(
128             emails.json().getString("email"),
129             Matchers.is(email)
130         );
131     }
132 
133 }