View Javadoc
1   /**
2    * Copyright (c) 2013-2023, 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 java.util.Arrays;
33  import java.util.Collections;
34  import javax.json.Json;
35  import org.hamcrest.MatcherAssert;
36  import org.hamcrest.Matchers;
37  import org.junit.Test;
38  import org.mockito.Mockito;
39  
40  /**
41   * Test case for {@link IssueLabels}.
42   * @author Yegor Bugayenko (yegor256@gmail.com)
43   * @version $Id: 074e3a3478bafd30a6965420ae7e9bbfd52b83fc $
44   * @checkstyle MultipleStringLiterals (500 lines)
45   */
46  public final class IssueLabelsTest {
47  
48      /**
49       * IssueLabels.Smart can fetch labels by color.
50       * @throws Exception If some problem inside
51       */
52      @Test
53      public void fetchesLabelsByColor() throws Exception {
54          final Label first = Mockito.mock(Label.class);
55          Mockito.doReturn(
56              Json.createObjectBuilder().add("color", "c0c0c0").build()
57          ).when(first).json();
58          final Label second = Mockito.mock(Label.class);
59          Mockito.doReturn(
60              Json.createObjectBuilder().add("color", "fefefe").build()
61          ).when(second).json();
62          final IssueLabels labels = Mockito.mock(IssueLabels.class);
63          Mockito.doReturn(Arrays.asList(first, second)).when(labels).iterate();
64          MatcherAssert.assertThat(
65              new IssueLabels.Smart(labels).findByColor("c0c0c0"),
66              Matchers.allOf(
67                  Matchers.<Label>iterableWithSize(1),
68                  Matchers.hasItems(first)
69              )
70          );
71      }
72  
73      /**
74       * IssueLabels.Smart can check label's existence by name.
75       */
76      @Test
77      public void checksLabelExistenceByName() {
78          final Label first = Mockito.mock(Label.class);
79          Mockito.doReturn("first").when(first).name();
80          final Label second = Mockito.mock(Label.class);
81          Mockito.doReturn("second").when(second).name();
82          final IssueLabels labels = Mockito.mock(IssueLabels.class);
83          Mockito.doReturn(Arrays.asList(first, second)).when(labels).iterate();
84          MatcherAssert.assertThat(
85              new IssueLabels.Smart(labels).contains("first"),
86              Matchers.is(true)
87          );
88          MatcherAssert.assertThat(
89              new IssueLabels.Smart(labels).contains("third"),
90              Matchers.is(false)
91          );
92      }
93  
94      /**
95       * IssueLabels.Smart can find label by name.
96       */
97      @Test
98      public void getsLabelByName() {
99          final Label first = Mockito.mock(Label.class);
100         Mockito.doReturn("a").when(first).name();
101         final Label second = Mockito.mock(Label.class);
102         Mockito.doReturn("b").when(second).name();
103         final IssueLabels labels = Mockito.mock(IssueLabels.class);
104         Mockito.doReturn(Arrays.asList(first, second)).when(labels).iterate();
105         MatcherAssert.assertThat(
106             new IssueLabels.Smart(labels).get("a").name(),
107             Matchers.equalTo("a")
108         );
109     }
110 
111     /**
112      * IssueLabels.Smart can throw when label is absent.
113      */
114     @Test(expected = IllegalArgumentException.class)
115     public void throwsWhenLabelIsAbsent() {
116         final IssueLabels labels = Mockito.mock(IssueLabels.class);
117         Mockito.doReturn(Collections.emptyList()).when(labels).iterate();
118         new IssueLabels.Smart(labels).get("something");
119     }
120 
121 }