View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2013-2025 Yegor Bugayenko
3    * SPDX-License-Identifier: MIT
4    */
5   package com.jcabi.github;
6   
7   import com.jcabi.http.mock.MkAnswer;
8   import com.jcabi.http.mock.MkContainer;
9   import com.jcabi.http.mock.MkGrizzlyContainer;
10  import com.jcabi.http.request.JdkRequest;
11  import jakarta.json.Json;
12  import jakarta.json.JsonValue;
13  import java.io.IOException;
14  import java.net.HttpURLConnection;
15  import org.hamcrest.MatcherAssert;
16  import org.hamcrest.Matchers;
17  import org.junit.jupiter.api.Test;
18  import org.junit.jupiter.api.extension.ExtendWith;
19  import org.mockito.Mockito;
20  
21  /**
22   * Test case for {@link RtAssignees}.
23   * @since 0.7
24   * @checkstyle MultipleStringLiterals (500 lines)
25   */
26  @ExtendWith(RandomPort.class)
27  final class RtAssigneesTest {
28  
29      @Test
30      void iteratesAssignees() throws IOException {
31          try (
32              MkContainer container = new MkGrizzlyContainer().next(
33                  new MkAnswer.Simple(
34                      HttpURLConnection.HTTP_OK,
35                      Json.createArrayBuilder()
36                          .add(RtAssigneesTest.json("octocat"))
37                          .add(RtAssigneesTest.json("dummy"))
38                          .build().toString()
39                  )
40              ).start(RandomPort.port())
41          ) {
42              final Assignees users = new RtAssignees(
43                  new JdkRequest(container.home()),
44                  RtAssigneesTest.repo()
45              );
46              MatcherAssert.assertThat(
47                  "Collection size is incorrect",
48                  users.iterate(),
49                  Matchers.iterableWithSize(2)
50              );
51          }
52      }
53  
54      @Test
55      void checkUserIsAssigneeForRepo() throws IOException {
56          try (
57              MkContainer container = new MkGrizzlyContainer().next(
58                  new MkAnswer.Simple(
59                      HttpURLConnection.HTTP_NO_CONTENT,
60                      Json.createArrayBuilder()
61                          .add(RtAssigneesTest.json("octocat2"))
62                          .add(RtAssigneesTest.json("dummy"))
63                          .build().toString()
64                  )
65              ).start(RandomPort.port())) {
66              final Assignees users = new RtAssignees(
67                  new JdkRequest(container.home()),
68                  RtAssigneesTest.repo()
69              );
70              MatcherAssert.assertThat(
71                  "Values are not equal",
72                  users.check("octocat2"),
73                  Matchers.equalTo(true)
74              );
75          }
76      }
77  
78      @Test
79      void checkUserIsNotAssigneeForRepo() throws IOException {
80          try (
81              MkContainer container = new MkGrizzlyContainer().next(
82                  new MkAnswer.Simple(
83                      HttpURLConnection.HTTP_NOT_FOUND,
84                      Json.createArrayBuilder()
85                          .add(RtAssigneesTest.json("octocat3"))
86                          .add(RtAssigneesTest.json("dummy"))
87                          .build().toString()
88                  )
89              ).start(RandomPort.port())
90          ) {
91              final Assignees users = new RtAssignees(
92                  new JdkRequest(container.home()),
93                  RtAssigneesTest.repo()
94              );
95              MatcherAssert.assertThat(
96                  "Values are not equal",
97                  users.check("octocat33"),
98                  Matchers.equalTo(false)
99              );
100         }
101     }
102 
103     /**
104      * Create and return JsonObject to test.
105      * @param login Username to login
106      * @return JsonObject
107      */
108     private static JsonValue json(final String login) {
109         return Json.createObjectBuilder()
110             .add("login", login)
111             .build();
112     }
113 
114     /**
115      * Create and return repo for testing.
116      * @return Repo
117      */
118     private static Repo repo() {
119         final Repo repo = Mockito.mock(Repo.class);
120         Mockito.doReturn(new Coordinates.Simple("test", "assignee"))
121             .when(repo).coordinates();
122         Mockito.doReturn(Mockito.mock(GitHub.class)).when(repo).github();
123         return repo;
124     }
125 }