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 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: 501438c74ca06d469d846c0bec67d175c34206ec $
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       * @throws Exception If some problem inside
76       */
77      @Test
78      public void checksLabelExistenceByName() throws Exception {
79          final Label first = Mockito.mock(Label.class);
80          Mockito.doReturn("first").when(first).name();
81          final Label second = Mockito.mock(Label.class);
82          Mockito.doReturn("second").when(second).name();
83          final IssueLabels labels = Mockito.mock(IssueLabels.class);
84          Mockito.doReturn(Arrays.asList(first, second)).when(labels).iterate();
85          MatcherAssert.assertThat(
86              new IssueLabels.Smart(labels).contains("first"),
87              Matchers.is(true)
88          );
89          MatcherAssert.assertThat(
90              new IssueLabels.Smart(labels).contains("third"),
91              Matchers.is(false)
92          );
93      }
94  
95      /**
96       * IssueLabels.Smart can find label by name.
97       * @throws Exception If some problem inside
98       */
99      @Test
100     public void getsLabelByName() throws Exception {
101         final Label first = Mockito.mock(Label.class);
102         Mockito.doReturn("a").when(first).name();
103         final Label second = Mockito.mock(Label.class);
104         Mockito.doReturn("b").when(second).name();
105         final IssueLabels labels = Mockito.mock(IssueLabels.class);
106         Mockito.doReturn(Arrays.asList(first, second)).when(labels).iterate();
107         MatcherAssert.assertThat(
108             new IssueLabels.Smart(labels).get("a").name(),
109             Matchers.equalTo("a")
110         );
111     }
112 
113     /**
114      * IssueLabels.Smart can throw when label is absent.
115      * @throws Exception If some problem inside
116      */
117     @Test(expected = IllegalArgumentException.class)
118     public void throwsWhenLabelIsAbsent() throws Exception {
119         final IssueLabels labels = Mockito.mock(IssueLabels.class);
120         Mockito.doReturn(Collections.emptyList()).when(labels).iterate();
121         new IssueLabels.Smart(labels).get("something");
122     }
123 
124 }