View Javadoc
1   /**
2    * Copyright (c) 2013-2022, 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.github.Limit.Throttled;
33  import com.jcabi.http.request.FakeRequest;
34  import java.util.Date;
35  import javax.json.Json;
36  import org.hamcrest.MatcherAssert;
37  import org.hamcrest.Matchers;
38  import org.junit.Test;
39  import org.mockito.Mockito;
40  
41  /**
42   * Test case for {@link Limit}.
43   *
44   * @author Tomas Colombo (tomas.colombo@rollasolution.com)
45   * @version $Id: b116d7d06379a7eb8afdf2e1b9a2ca60c3f73e3f $
46   * @checkstyle MultipleStringLiteralsCheck (100 lines)
47   */
48  public final class LimitTest {
49  
50      /**
51       * Limit can throw exception when resource is absent.
52       *
53       * @throws Exception if some problem inside
54       */
55      @Test(expected = IllegalStateException.class)
56      public void throwsWhenResourceIsAbsent() throws Exception {
57          final Limit limit = Mockito.mock(Limit.class);
58          final Throttled throttled = new Throttled(limit, 23);
59          Mockito.when(limit.json()).thenReturn(
60              Json.createObjectBuilder().add("absent", "absentValue").build()
61          );
62          throttled.json();
63      }
64  
65      /**
66       * Limit reset() method properly converts time.
67       * GitHub reset property is in seconds, but java.util.Date
68       * constructor assumes miliseconds.
69       *
70       * @throws Exception if some problem inside
71       */
72      @Test
73      public void timeIsCreatedForReset() throws Exception {
74          // @checkstyle MagicNumberCheck (21 lines)
75          final RtLimit limit = new RtLimit(
76              Mockito.mock(Github.class),
77              new FakeRequest().withBody(
78                  Json.createObjectBuilder().add(
79                      "rate", Json.createObjectBuilder()
80                          .add("limit", 5000)
81                          .add("remaining", 4999)
82                          .add("reset", 1372700873)
83                          .build()
84                  ).add(
85                      "resources", Json.createObjectBuilder().add(
86                          "core", Json.createObjectBuilder()
87                              .add("limit", 5000)
88                              .add("remaining", 4999)
89                              .add("reset", 1372700873)
90                              .build()
91                      ).add(
92                          "search", Json.createObjectBuilder()
93                              .add("limit", 5000)
94                              .add("remaining", 4999)
95                              .add("reset", 1372700873)
96                              .build()
97                      ).build()
98                  ).build().toString()
99              ),
100             "core"
101         );
102         final RtLimit.Smart smart = new RtLimit.Smart(limit);
103         // @checkstyle MagicNumberCheck (3 lines)
104         MatcherAssert.assertThat(
105             smart.reset(),
106             Matchers.equalTo(new Date(1372700873000L))
107         );
108     }
109 
110 }