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.ApacheRequest;
11  import com.jcabi.http.request.FakeRequest;
12  import jakarta.json.Json;
13  import java.io.IOException;
14  import java.net.HttpURLConnection;
15  import java.util.Collections;
16  import org.hamcrest.MatcherAssert;
17  import org.hamcrest.Matchers;
18  import org.junit.jupiter.api.Test;
19  import org.junit.jupiter.api.extension.ExtendWith;
20  
21  /**
22   * Test case for {@link RtUserEmails}.
23   * @since 0.1
24   */
25  @ExtendWith(RandomPort.class)
26  final class RtUserEmailsTest {
27  
28      /**
29       * The rule for skipping test if there's BindException.
30       * @checkstyle VisibilityModifierCheck (3 lines)
31       */
32      @Test
33      void fetchesEmails() throws IOException {
34          final String email = "test@email.com";
35          final UserEmails emails = new RtUserEmails(
36              new FakeRequest().withBody(
37                  Json.createArrayBuilder()
38                      .add(Json.createObjectBuilder().add("email", email))
39                      .build().toString()
40              )
41          );
42          MatcherAssert.assertThat(
43              "Values are not equal",
44              emails.iterate().iterator().next(), Matchers.equalTo(email)
45          );
46      }
47  
48      @Test
49      void addsEmails() throws IOException {
50          final String email = "test1@email.com";
51          final MkContainer container = new MkGrizzlyContainer().next(
52              new MkAnswer.Simple(
53                  HttpURLConnection.HTTP_CREATED,
54                  String.format("[{\"email\":\"%s\"}]", email)
55              )
56          );
57          container.start(RandomPort.port());
58          try {
59              final UserEmails emails = new RtUserEmails(
60                  new ApacheRequest(container.home())
61              );
62              MatcherAssert.assertThat(
63                  "Values are not equal",
64                  emails.add(Collections.singletonList(email)).iterator().next(),
65                  Matchers.equalTo(email)
66              );
67          } finally {
68              container.stop();
69          }
70      }
71  
72      @Test
73      void removesEmails() throws IOException {
74          final UserEmails emails = new RtUserEmails(
75              new FakeRequest().withStatus(HttpURLConnection.HTTP_NO_CONTENT)
76          );
77          emails.remove(Collections.singletonList("test2@email.com"));
78      }
79  
80  }