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.request.FakeRequest;
8   import jakarta.json.Json;
9   import java.io.IOException;
10  import java.util.Date;
11  import org.hamcrest.MatcherAssert;
12  import org.hamcrest.Matchers;
13  import org.junit.jupiter.api.Assertions;
14  import org.junit.jupiter.api.Test;
15  import org.mockito.Mockito;
16  
17  /**
18   * Test case for {@link Limit}.
19   * @since 0.1
20   * @checkstyle MultipleStringLiteralsCheck (100 lines)
21   */
22  final class LimitTest {
23  
24      @Test
25      void throwsWhenResourceIsAbsent() throws IOException {
26          final Limit limit = Mockito.mock(Limit.class);
27          final Limit.Throttled throttled = new Limit.Throttled(limit, 23);
28          Mockito.when(limit.json()).thenReturn(
29              Json.createObjectBuilder().add("absent", "absentValue").build()
30          );
31          Assertions.assertThrows(
32              IllegalStateException.class,
33              throttled::json,
34              "Should throw when resource is absent"
35          );
36      }
37  
38      /**
39       * Limit reset() method properly converts time.
40       * GitHub reset property is in seconds, but java.util.Date
41       * constructor assumes milliseconds.
42       */
43      @Test
44      void timeIsCreatedForReset() throws IOException {
45          // @checkstyle MagicNumberCheck (21 lines)
46          final RtLimit limit = new RtLimit(
47              Mockito.mock(GitHub.class),
48              new FakeRequest().withBody(
49                  Json.createObjectBuilder().add(
50                      "rate", Json.createObjectBuilder()
51                          .add("limit", 5000)
52                          .add("remaining", 4999)
53                          .add("reset", 1_372_700_873)
54                          .build()
55                  ).add(
56                      "resources", Json.createObjectBuilder().add(
57                          "core", Json.createObjectBuilder()
58                              .add("limit", 5000)
59                              .add("remaining", 4999)
60                              .add("reset", 1_372_700_873)
61                              .build()
62                      ).add(
63                          "search", Json.createObjectBuilder()
64                              .add("limit", 5000)
65                              .add("remaining", 4999)
66                              .add("reset", 1_372_700_873)
67                              .build()
68                      ).build()
69                  ).build().toString()
70              ),
71              "core"
72          );
73          final RtLimit.Smart smart = new RtLimit.Smart(limit);
74          // @checkstyle MagicNumberCheck (3 lines)
75          MatcherAssert.assertThat(
76              "Values are not equal",
77              smart.reset(),
78              Matchers.equalTo(new Date(1_372_700_873_000L))
79          );
80      }
81  
82  }