1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 package com.jcabi.github;
31
32 import com.jcabi.aspects.Tv;
33 import com.jcabi.http.request.FakeRequest;
34 import java.io.IOException;
35 import java.util.ArrayList;
36 import javax.json.Json;
37 import javax.json.JsonObject;
38 import org.hamcrest.MatcherAssert;
39 import org.hamcrest.Matchers;
40 import org.junit.Rule;
41 import org.junit.Test;
42 import org.junit.rules.ExpectedException;
43 import org.mockito.Mockito;
44
45
46
47
48
49
50
51 public final class IssueTest {
52
53
54
55
56
57 @Rule
58 @SuppressWarnings("deprecation")
59 public transient ExpectedException thrown = ExpectedException.none();
60
61
62
63
64
65 @Test
66 public void fetchesProperties() throws Exception {
67 final Issue issue = Mockito.mock(Issue.class);
68 Mockito.doReturn(
69 Json.createObjectBuilder()
70 .add("title", "this is some text \u20ac")
71 .add("body", "body of the issue")
72 .build()
73 ).when(issue).json();
74 final Issue.Smart smart = new Issue.Smart(issue);
75 MatcherAssert.assertThat(
76 smart.title(),
77 Matchers.notNullValue()
78 );
79 MatcherAssert.assertThat(
80 smart.body(),
81 Matchers.notNullValue()
82 );
83 }
84
85
86
87
88
89 @Test
90 public void detectsPullRequest() throws Exception {
91 final Issue issue = Mockito.mock(Issue.class);
92 Mockito.doReturn(
93 Json.createObjectBuilder().add(
94 "pull_request",
95 Json.createObjectBuilder().add(
96 "html_url", "http://ibm.com/pulls/3"
97 )
98 ).build()
99 ).when(issue).json();
100 final Pulls pulls = Mockito.mock(Pulls.class);
101 final Repo repo = Mockito.mock(Repo.class);
102 final Pull pull = Mockito.mock(Pull.class);
103 Mockito.doReturn(repo).when(issue).repo();
104 Mockito.doReturn(pulls).when(repo).pulls();
105 Mockito.when(pulls.get(Mockito.eq(Tv.THREE))).thenReturn(pull);
106 MatcherAssert.assertThat(
107 new Issue.Smart(issue).isPull(),
108 Matchers.is(true)
109 );
110 new Issue.Smart(issue).pull();
111 Mockito.verify(pulls).get(Tv.THREE);
112 }
113
114
115
116
117
118 @Test
119 public void detectsPullRequestAbsence() throws Exception {
120 final Issue issue = Mockito.mock(Issue.class);
121 Mockito.doReturn(
122 Json.createObjectBuilder().add(
123 "pull_request",
124 Json.createObjectBuilder().addNull("html_url")
125 ).build()
126 ).when(issue).json();
127 MatcherAssert.assertThat(
128 new Issue.Smart(issue).isPull(),
129 Matchers.is(false)
130 );
131 }
132
133
134
135
136
137 @Test
138 public void detectsFullPullRequestAbsence() throws Exception {
139 final Issue issue = Mockito.mock(Issue.class);
140 Mockito.doReturn(
141 Json.createObjectBuilder().build()
142 ).when(issue).json();
143 MatcherAssert.assertThat(
144 new Issue.Smart(issue).isPull(),
145 Matchers.is(false)
146 );
147 }
148
149
150
151
152
153 @Test
154 public void fetchLabelsRO() throws IOException {
155 final String name = "bug";
156 final JsonObject json = Json.createObjectBuilder().add(
157 "labels",
158 Json.createArrayBuilder().add(
159 Json.createObjectBuilder()
160 .add("name", name)
161 .add("color", "f29513")
162 )
163 ).build();
164 final Issue issue = new RtIssue(
165 new FakeRequest().withBody(json.toString()), this.repo(), 1
166 );
167 final IssueLabels labels = new Issue.Smart(issue).roLabels();
168 this.thrown.expect(UnsupportedOperationException.class);
169 labels.add(new ArrayList<>(0));
170 this.thrown.expect(UnsupportedOperationException.class);
171 labels.replace(new ArrayList<>(0));
172 this.thrown.expect(UnsupportedOperationException.class);
173 labels.remove(name);
174 this.thrown.expect(UnsupportedOperationException.class);
175 labels.clear();
176 final Label label = labels.iterate().iterator().next();
177 MatcherAssert.assertThat(label, Matchers.notNullValue());
178 this.thrown.expect(UnsupportedOperationException.class);
179 label.patch(Mockito.mock(JsonObject.class));
180 }
181
182
183
184
185
186 private Repo repo() {
187 final Repo repo = Mockito.mock(Repo.class);
188 final Coordinates coords = Mockito.mock(Coordinates.class);
189 Mockito.doReturn(coords).when(repo).coordinates();
190 Mockito.doReturn("user").when(coords).user();
191 Mockito.doReturn("repo").when(coords).repo();
192 return repo;
193 }
194 }