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 com.jcabi.http.mock.MkAnswer;
33 import com.jcabi.http.mock.MkContainer;
34 import com.jcabi.http.mock.MkGrizzlyContainer;
35 import com.jcabi.http.request.ApacheRequest;
36 import java.net.HttpURLConnection;
37 import javax.json.Json;
38 import javax.json.JsonObject;
39 import org.hamcrest.MatcherAssert;
40 import org.hamcrest.Matchers;
41 import org.junit.Rule;
42 import org.junit.Test;
43 import org.mockito.Mockito;
44
45 /**
46 * Test case for {@link RtUsers}.
47 *
48 * @author Giang Le (giang@vn-smartsolutions.com)
49 * @version $Id: b5c8c8402d37841c154ec9170f4af153ed3cc58d $
50 */
51 public final class RtUsersTest {
52
53 /**
54 * The rule for skipping test if there's BindException.
55 * @checkstyle VisibilityModifierCheck (3 lines)
56 */
57 @Rule
58 public final transient RandomPort resource = new RandomPort();
59
60 /**
61 * RtUsers can iterate users.
62 * @throws Exception if there is any error
63 */
64 @Test
65 public void iterateUsers() throws Exception {
66 final String identifier = "1";
67 final MkContainer container = new MkGrizzlyContainer().next(
68 new MkAnswer.Simple(
69 HttpURLConnection.HTTP_OK,
70 Json.createArrayBuilder()
71 .add(RtUsersTest.json("octocat", identifier))
72 .add(RtUsersTest.json("dummy", "2"))
73 .build().toString()
74 )
75 ).start(this.resource.port());
76 final Users users = new RtUsers(
77 Mockito.mock(Github.class),
78 new ApacheRequest(container.home())
79 );
80 MatcherAssert.assertThat(
81 users.iterate(identifier),
82 Matchers.<User>iterableWithSize(2)
83 );
84 container.stop();
85 }
86
87 /**
88 * RtUsers can get a single user.
89 *
90 * @throws Exception if there is any error
91 */
92 @Test
93 public void getSingleUser() throws Exception {
94 final String login = "mark";
95 final MkContainer container = new MkGrizzlyContainer().next(
96 new MkAnswer.Simple(
97 HttpURLConnection.HTTP_OK,
98 RtUsersTest.json(login, "3").toString()
99 )
100 ).start(this.resource.port());
101 final Users users = new RtUsers(
102 Mockito.mock(Github.class),
103 new ApacheRequest(container.home())
104 );
105 MatcherAssert.assertThat(
106 users.get(login).login(),
107 Matchers.equalTo(login)
108 );
109 container.stop();
110 }
111
112 /**
113 * RtUsers can get a current user.
114 *
115 * @throws Exception if there is any error
116 */
117 @Test
118 public void getCurrentUser() throws Exception {
119 final String login = "kendy";
120 final MkContainer container = new MkGrizzlyContainer().next(
121 new MkAnswer.Simple(
122 HttpURLConnection.HTTP_OK,
123 RtUsersTest.json(login, "4").toString()
124 )
125 ).start(this.resource.port());
126 final Users users = new RtUsers(
127 Mockito.mock(Github.class),
128 new ApacheRequest(container.home())
129 );
130 MatcherAssert.assertThat(
131 users.self().login(),
132 Matchers.equalTo(login)
133 );
134 container.stop();
135 }
136
137 /**
138 * Create and return JsonObject to test.
139 * @param login Username to login
140 * @param identifier User Id
141 * @return JsonObject
142 */
143 private static JsonObject json(
144 final String login, final String identifier
145 ) {
146 return Json.createObjectBuilder()
147 .add("id", Integer.valueOf(identifier))
148 .add("login", login)
149 .build();
150 }
151 }