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 java.io.IOException;
13  import java.net.HttpURLConnection;
14  import org.hamcrest.MatcherAssert;
15  import org.hamcrest.Matchers;
16  import org.junit.jupiter.api.Test;
17  import org.junit.jupiter.api.extension.ExtendWith;
18  import org.mockito.Mockito;
19  
20  /**
21   * Test case for {@link RtEvent}.
22   * @since 0.6.1
23   * @checkstyle MultipleStringLiteralsCheck (100 lines)
24   */
25  @ExtendWith(RandomPort.class)
26  final class RtEventTest {
27  
28      /**
29       * The rule for skipping test if there's BindException.
30       * @checkstyle VisibilityModifierCheck (3 lines)
31       */
32      @Test
33      void canRetrieveOwnRepo() {
34          final Repo repo = RtEventTest.repo();
35          final RtEvent event = new RtEvent(new FakeRequest(), repo, 1);
36          MatcherAssert.assertThat(
37              "Assertion failed",
38              event.repo(),
39              Matchers.sameInstance(repo)
40          );
41      }
42  
43      @Test
44      void canRetrieveOwnNumber() {
45          final Repo repo = RtEventTest.repo();
46          final RtEvent event = new RtEvent(new FakeRequest(), repo, 2);
47          MatcherAssert.assertThat(
48              "Values are not equal",
49              event.number(),
50              Matchers.equalTo(2)
51          );
52      }
53  
54      @Test
55      void retrieveEventAsJson() throws IOException {
56          try (MkContainer container = new MkGrizzlyContainer().next(
57              new MkAnswer.Simple(
58                  HttpURLConnection.HTTP_OK,
59                  "{\"test\":\"events\"}"
60              )
61          ).start(RandomPort.port())) {
62              final RtEvent event = new RtEvent(
63                  new ApacheRequest(container.home()),
64                  RtEventTest.repo(),
65                  3
66              );
67              MatcherAssert.assertThat(
68                  "Values are not equal",
69                  event.json().getString("test"),
70                  Matchers.equalTo("events")
71              );
72          }
73      }
74  
75      @Test
76      void canCompareInstances() {
77          final RtEvent less = new RtEvent(new FakeRequest(), RtEventTest.repo(), 1);
78          final RtEvent greater = new RtEvent(new FakeRequest(), RtEventTest.repo(), 2);
79          MatcherAssert.assertThat(
80              "Value is not less than expected",
81              less.compareTo(greater), Matchers.lessThan(0)
82          );
83          MatcherAssert.assertThat(
84              "Value is not greater than expected",
85              greater.compareTo(less), Matchers.greaterThan(0)
86          );
87      }
88  
89      /**
90       * Create and return repo for testing.
91       * @return Repo
92       */
93      private static Repo repo() {
94          final Repo repo = Mockito.mock(Repo.class);
95          Mockito.doReturn(new Coordinates.Simple("test", "event"))
96              .when(repo).coordinates();
97          return repo;
98      }
99  
100 }