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.http.Request;
33 import com.jcabi.http.mock.MkAnswer;
34 import com.jcabi.http.mock.MkContainer;
35 import com.jcabi.http.mock.MkGrizzlyContainer;
36 import com.jcabi.http.mock.MkQuery;
37 import com.jcabi.http.request.ApacheRequest;
38 import java.net.HttpURLConnection;
39 import javax.ws.rs.core.UriBuilder;
40 import org.hamcrest.MatcherAssert;
41 import org.hamcrest.Matchers;
42 import org.junit.Rule;
43 import org.junit.Test;
44 import org.mockito.Mockito;
45
46
47
48
49
50
51
52
53 public final class RtStarsTest {
54
55
56
57
58
59 @Rule
60 public final transient RandomPort resource = new RandomPort();
61
62
63
64
65
66
67 @Test
68 public void checkIfRepoStarred() throws Exception {
69 try (
70 final MkContainer container = new MkGrizzlyContainer().next(
71 new MkAnswer.Simple(HttpURLConnection.HTTP_NO_CONTENT)
72 ).next(new MkAnswer.Simple(HttpURLConnection.HTTP_NOT_FOUND))
73 .start(this.resource.port())
74 ) {
75 final Stars starred = new RtStars(
76 new ApacheRequest(container.home()),
77 RtStarsTest.repo("someuser", "starredrepo")
78 );
79 MatcherAssert.assertThat(
80 starred.starred(), Matchers.is(true)
81 );
82 final Stars unstarred = new RtStars(
83 new ApacheRequest(container.home()),
84 RtStarsTest.repo("otheruser", "notstarredrepo")
85 );
86 MatcherAssert.assertThat(
87 unstarred.starred(), Matchers.is(false)
88 );
89 container.stop();
90 }
91 }
92
93
94
95
96
97
98 @Test
99 public void starRepository() throws Exception {
100 try (
101 final MkContainer container = new MkGrizzlyContainer().next(
102 new MkAnswer.Simple(HttpURLConnection.HTTP_NO_CONTENT)
103 ).start(this.resource.port());
104 ) {
105 final String user = "staruser";
106 final String repo = "starrepo";
107 final Stars stars = new RtStars(
108 new ApacheRequest(container.home()),
109 RtStarsTest.repo(user, repo)
110 );
111 stars.star();
112 final MkQuery query = container.take();
113 MatcherAssert.assertThat(
114 query.method(),
115 Matchers.equalTo(Request.PUT)
116 );
117 MatcherAssert.assertThat(
118 query.uri().getPath(),
119 Matchers.containsString(
120 UriBuilder.fromPath(user)
121 .path(repo)
122 .build()
123 .getPath()
124 )
125 );
126 container.stop();
127 }
128 }
129
130
131
132
133
134
135 @Test
136 public void unstarRepository() throws Exception {
137 try (
138 final MkContainer container = new MkGrizzlyContainer().next(
139 new MkAnswer.Simple(HttpURLConnection.HTTP_NO_CONTENT)
140 ).start(this.resource.port())
141 ) {
142 final String user = "unstaruser";
143 final String repo = "unstarrepo";
144 final Stars stars = new RtStars(
145 new ApacheRequest(container.home()),
146 RtStarsTest.repo(user, repo)
147 );
148 stars.unstar();
149 final MkQuery query = container.take();
150 MatcherAssert.assertThat(
151 query.method(),
152 Matchers.equalTo(Request.DELETE)
153 );
154 MatcherAssert.assertThat(
155 query.uri().getPath(),
156 Matchers.containsString(
157 UriBuilder.fromPath(user)
158 .path(repo)
159 .build()
160 .getPath()
161 )
162 );
163 container.stop();
164 }
165 }
166
167
168
169
170
171
172
173 private static Repo repo(final String user, final String reponame) {
174 final Repo repo = Mockito.mock(Repo.class);
175 Mockito.doReturn(new Coordinates.Simple(user, reponame))
176 .when(repo).coordinates();
177 return repo;
178 }
179 }