1
2
3
4
5 package com.jcabi.github.mock;
6
7 import com.jcabi.github.Event;
8 import com.jcabi.github.Issue;
9 import com.jcabi.github.IssueLabels;
10 import com.jcabi.github.Repo;
11 import java.io.IOException;
12 import java.util.Collections;
13 import java.util.Iterator;
14 import org.hamcrest.MatcherAssert;
15 import org.hamcrest.Matchers;
16 import org.junit.jupiter.api.Test;
17
18
19
20
21
22 @SuppressWarnings("PMD.AvoidDuplicateLiterals")
23 final class MkIssueLabelsTest {
24
25
26
27 private static final String USER = "jeff";
28
29 @Test
30 void iteratesIssues() throws IOException {
31 final Repo repo = new MkGitHub().randomRepo();
32 final String name = "bug";
33 repo.labels().create(name, "c0c0c0");
34 final Issue issue = repo.issues().create("title", "body");
35 issue.labels().add(Collections.singletonList(name));
36 MatcherAssert.assertThat(
37 "Collection size is incorrect",
38 issue.labels().iterate(),
39 Matchers.iterableWithSize(1)
40 );
41 }
42
43 @Test
44 void createsLabelsThroughDecorator() throws IOException {
45 final Repo repo = new MkGitHub().randomRepo();
46 final Issue issue = repo.issues().create("how are you?", "");
47 final String name = "task";
48 new IssueLabels.Smart(issue.labels()).addIfAbsent(name, "f0f0f0");
49 MatcherAssert.assertThat(
50 "Collection size is incorrect",
51 issue.labels().iterate(),
52 Matchers.iterableWithSize(1)
53 );
54 }
55
56
57
58
59 @Test
60 void addingLabelGeneratesEvent() throws IOException {
61 final Repo repo = new MkGitHub().randomRepo();
62 final String name = "confirmed";
63 repo.labels().create(name, "663399");
64 final Issue issue = repo.issues().create("Titular", "Corpus");
65 issue.labels().add(Collections.singletonList(name));
66 MatcherAssert.assertThat(
67 "Collection size is incorrect",
68 issue.events(),
69 Matchers.iterableWithSize(1)
70 );
71 final Event.Smart labeled = new Event.Smart(
72 issue.events().iterator().next()
73 );
74 MatcherAssert.assertThat(
75 "Values are not equal",
76 labeled.type(),
77 Matchers.equalTo(Event.LABELED)
78 );
79 MatcherAssert.assertThat(
80 "Values are not equal",
81 labeled.author().login(),
82 Matchers.equalTo(MkIssueLabelsTest.USER)
83 );
84 MatcherAssert.assertThat(
85 "Values are not equal",
86 labeled.repo(),
87 Matchers.equalTo(repo)
88 );
89 MatcherAssert.assertThat(
90 "Values are not equal",
91 labeled.label().get().name(),
92 Matchers.equalTo(name)
93 );
94 }
95
96
97
98
99 @Test
100 void removingLabelGeneratesEvent() throws IOException {
101 final Repo repo = new MkGitHub().randomRepo();
102 final String name = "invalid";
103 repo.labels().create(name, "ee82ee");
104 final Issue issue = repo.issues().create("Rewrite", "Sound good?");
105 issue.labels().add(Collections.singletonList(name));
106 issue.labels().remove(name);
107 MatcherAssert.assertThat(
108 "Collection size is incorrect",
109 issue.events(),
110 Matchers.iterableWithSize(2)
111 );
112 final Iterator<Event> events = issue.events().iterator();
113 events.next();
114 final Event.Smart unlabeled = new Event.Smart(events.next());
115 MatcherAssert.assertThat(
116 "Values are not equal",
117 unlabeled.type(),
118 Matchers.equalTo(Event.UNLABELED)
119 );
120 MatcherAssert.assertThat(
121 "Values are not equal",
122 unlabeled.author().login(),
123 Matchers.equalTo(MkIssueLabelsTest.USER)
124 );
125 MatcherAssert.assertThat(
126 "Values are not equal",
127 unlabeled.repo(),
128 Matchers.equalTo(repo)
129 );
130 MatcherAssert.assertThat(
131 "Values are not equal",
132 unlabeled.label().get().name(),
133 Matchers.equalTo(name)
134 );
135 }
136 }