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.aspects.Tv;
33  import com.jcabi.github.Organization;
34  import com.jcabi.github.PublicMembers;
35  import com.jcabi.github.User;
36  import java.io.IOException;
37  import org.apache.commons.lang3.RandomStringUtils;
38  import org.hamcrest.MatcherAssert;
39  import org.hamcrest.Matchers;
40  import org.junit.Test;
41  
42  /**
43   * Test case for {@link MkPublicMembers}.
44   *
45   * @author Chris Rebert (github@chrisrebert.com)
46   * @version $Id: 6c25d065b700a3db5a6ef72aed9861f34a611773 $
47   */
48  public final class MkPublicMembersTest {
49      /**
50       * MkPublicMembers can fetch its organization.
51       * @throws Exception If some problem inside
52       */
53      @Test
54      public void fetchesOrg() throws Exception {
55          final Organization org = organization();
56          MatcherAssert.assertThat(
57              org.publicMembers().org().login(),
58              Matchers.equalTo(org.login())
59          );
60      }
61  
62      /**
63       * MkPublicMembers can publicize/conceal a member's membership.
64       * @throws Exception If some problem inside
65       */
66      @Test
67      public void changesPublicityOfMembershipOfUsers() throws Exception {
68          final MkOrganization org = organization();
69          final PublicMembers members = org.publicMembers();
70          final User user = org.github().users().get("johnny5");
71          org.addMember(user);
72          MatcherAssert.assertThat(
73              "Newly-added user is not a public member",
74              !members.contains(user)
75          );
76          members.publicize(user);
77          MatcherAssert.assertThat(
78              "User has been made a public member",
79              members.contains(user)
80          );
81          members.conceal(user);
82          MatcherAssert.assertThat(
83              "Concealed user is not a public member",
84              !members.contains(user)
85          );
86          members.publicize(user);
87          MatcherAssert.assertThat(
88              "User has been made a public member again",
89              members.contains(user)
90          );
91      }
92  
93      /**
94       * MkPublicMembers can check whether a user is a public member.
95       * @throws Exception If some problem inside
96       */
97      @Test
98      public void checkPublicMembership() throws Exception {
99          final MkOrganization org = organization();
100         final PublicMembers members = org.publicMembers();
101         final User user = org.github().users().get("agent99");
102         MatcherAssert.assertThat(
103             members.iterate(),
104             Matchers.emptyIterableOf(User.class)
105         );
106         org.addMember(user);
107         MatcherAssert.assertThat(
108             "The newly-added user is not a public member",
109             !members.contains(user)
110         );
111         members.publicize(user);
112         MatcherAssert.assertThat(
113             "The user has been made a public member",
114             members.contains(user)
115         );
116         MatcherAssert.assertThat(members.iterate(), Matchers.hasItem(user));
117         members.conceal(user);
118         MatcherAssert.assertThat(
119             "The concealed user is not a public member",
120             !members.contains(user)
121         );
122     }
123 
124     /**
125      * MkPublicMembers can iterate over all public members.
126      * @throws Exception If some problem inside
127      */
128     @Test
129     public void iteratesPublicMembers() throws Exception {
130         final MkOrganization org = organization();
131         final PublicMembers members = org.publicMembers();
132         final User user = org.github().users().get("jasmine");
133         MatcherAssert.assertThat(
134             members.iterate(),
135             Matchers.emptyIterableOf(User.class)
136         );
137         org.addMember(user);
138         MatcherAssert.assertThat(
139             members.iterate(),
140             Matchers.emptyIterableOf(User.class)
141         );
142         members.publicize(user);
143         MatcherAssert.assertThat(
144             members.iterate(),
145             Matchers.<User>iterableWithSize(1)
146         );
147         MatcherAssert.assertThat(members.iterate(), Matchers.hasItem(user));
148         members.conceal(user);
149         MatcherAssert.assertThat(
150             members.iterate(),
151             Matchers.emptyIterableOf(User.class)
152         );
153     }
154 
155     /**
156      * Get test organization.
157      * @return Organization
158      * @throws IOException If there is an I/O problem
159      */
160     private static MkOrganization organization() throws IOException {
161         return (MkOrganization) new MkOrganizations(
162             new MkStorage.InFile()
163         ).get(RandomStringUtils.randomAlphanumeric(Tv.TWENTY));
164     }
165 }