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.mock;
31  
32  import com.jcabi.github.Event;
33  import com.jcabi.github.Issue;
34  import com.jcabi.github.IssueLabels;
35  import com.jcabi.github.Label;
36  import com.jcabi.github.Repo;
37  import java.util.Collections;
38  import java.util.Iterator;
39  import org.hamcrest.MatcherAssert;
40  import org.hamcrest.Matchers;
41  import org.junit.Test;
42  
43  /**
44   * Test case for {@link MkIssueLabels}.
45   * @author Yegor Bugayenko (yegor256@gmail.com)
46   * @version $Id: c77b889de3bb70008d585bab01a62cb2f349fec9 $
47   */
48  public final class MkIssueLabelsTest {
49      /**
50       * Username of actor.
51       */
52      private static final String USER = "jeff";
53  
54      /**
55       * MkIssueLabels can list labels.
56       * @throws Exception If some problem inside
57       */
58      @Test
59      public void iteratesIssues() throws Exception {
60          final Repo repo = new MkGithub().randomRepo();
61          final String name = "bug";
62          repo.labels().create(name, "c0c0c0");
63          final Issue issue = repo.issues().create("title", "body");
64          issue.labels().add(Collections.singletonList(name));
65          MatcherAssert.assertThat(
66              issue.labels().iterate(),
67              Matchers.<Label>iterableWithSize(1)
68          );
69      }
70  
71      /**
72       * MkIssueLabels can create labels through Smart decorator.
73       * @throws Exception If some problem inside
74       */
75      @Test
76      public void createsLabelsThroughDecorator() throws Exception {
77          final Repo repo = new MkGithub().randomRepo();
78          final Issue issue = repo.issues().create("how are you?", "");
79          final String name = "task";
80          new IssueLabels.Smart(issue.labels()).addIfAbsent(name, "f0f0f0");
81          MatcherAssert.assertThat(
82              issue.labels().iterate(),
83              Matchers.<Label>iterableWithSize(1)
84          );
85      }
86  
87      /**
88       * MkIssueLabels creates a "labeled" event when a label is added.
89       * @throws Exception If some problem inside
90       */
91      @Test
92      public void addingLabelGeneratesEvent() throws Exception {
93          final Repo repo = new MkGithub().randomRepo();
94          final String name = "confirmed";
95          repo.labels().create(name, "663399");
96          final Issue issue = repo.issues().create("Titular", "Corpus");
97          issue.labels().add(Collections.singletonList(name));
98          MatcherAssert.assertThat(
99              issue.events(),
100             Matchers.<Event>iterableWithSize(1)
101         );
102         final Event.Smart labeled = new Event.Smart(
103             issue.events().iterator().next()
104         );
105         MatcherAssert.assertThat(
106             labeled.type(),
107             Matchers.equalTo(Event.LABELED)
108         );
109         MatcherAssert.assertThat(
110             labeled.author().login(),
111             Matchers.equalTo(USER)
112         );
113         MatcherAssert.assertThat(
114             labeled.repo(),
115             Matchers.equalTo(repo)
116         );
117         MatcherAssert.assertThat(
118             labeled.label().get().name(),
119             Matchers.equalTo(name)
120         );
121     }
122 
123     /**
124      * MkIssueLabels creates an "unlabeled" event when a label is removed.
125      * @throws Exception If some problem inside
126      */
127     @Test
128     public void removingLabelGeneratesEvent() throws Exception {
129         final Repo repo = new MkGithub().randomRepo();
130         final String name = "invalid";
131         repo.labels().create(name, "ee82ee");
132         final Issue issue = repo.issues().create("Rewrite", "Sound good?");
133         issue.labels().add(Collections.singletonList(name));
134         issue.labels().remove(name);
135         MatcherAssert.assertThat(
136             issue.events(),
137             Matchers.<Event>iterableWithSize(2)
138         );
139         final Iterator<Event> events = issue.events().iterator();
140         events.next();
141         final Event.Smart unlabeled = new Event.Smart(events.next());
142         MatcherAssert.assertThat(
143             unlabeled.type(),
144             Matchers.equalTo(Event.UNLABELED)
145         );
146         MatcherAssert.assertThat(
147             unlabeled.author().login(),
148             Matchers.equalTo(USER)
149         );
150         MatcherAssert.assertThat(
151             unlabeled.repo(),
152             Matchers.equalTo(repo)
153         );
154         MatcherAssert.assertThat(
155             unlabeled.label().get().name(),
156             Matchers.equalTo(name)
157         );
158     }
159 }