Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#972: Stars no longer depends on user/repo parameters. It now has its ow... #981

Merged
merged 2 commits into from
Jan 14, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/com/jcabi/github/RtRepo.java
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public Git git() {
@Override
@NotNull(message = "Stars is never NULL")
public Stars stars() {
return new RtStars(this.entry);
return new RtStars(this.entry, this);
}

@Override
Expand Down
43 changes: 24 additions & 19 deletions src/main/java/com/jcabi/github/RtStars.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@
package com.jcabi.github;

import com.jcabi.aspects.Immutable;
import com.jcabi.aspects.Loggable;
import com.jcabi.http.Request;
import com.jcabi.http.response.RestResponse;
import java.io.IOException;
import java.net.HttpURLConnection;
import javax.validation.constraints.NotNull;
import lombok.EqualsAndHashCode;
import org.hamcrest.Matchers;

/**
Expand All @@ -46,31 +48,44 @@
* @see <a href="https://developer.github.com/v3/activity/starring/">Starring API</a>
*/
@Immutable
@Loggable(Loggable.DEBUG)
@EqualsAndHashCode(of = { "owner", "request" })
final class RtStars implements Stars {

/**
* RESTful request.
*/
private final transient Request request;

/**
* Repository.
*/
private final transient Repo owner;

/**
* Public ctor.
* @param req Request
* @param repo Repository
*/
RtStars(final Request req) {
RtStars(final Request req, final Repo repo) {
final Coordinates coords = repo.coordinates();
this.request = req.uri()
.path("/user/starred")
.path(coords.user())
.path(coords.repo())
.back();
this.owner = repo;
}

@Override
@NotNull(message = "repository is never NULL")
public Repo repo() {
return this.owner;
}

@Override
public boolean starred(
@NotNull(message = "user can't be NULL") final String user,
@NotNull(message = "repo can't be NULL") final String repo
) throws IOException {
public boolean starred() throws IOException {
return this.request
.uri().path(user).path(repo)
.back()
.fetch().as(RestResponse.class)
.assertStatus(
Matchers.isOneOf(
Expand All @@ -81,27 +96,17 @@ public boolean starred(
}

@Override
public void star(
@NotNull(message = "user can't be NULL") final String user,
@NotNull(message = "repo can't be NULL") final String repo
) throws IOException {
public void star() throws IOException {
this.request
.method(Request.PUT)
.uri().path(user).path(repo)
.back()
.fetch().as(RestResponse.class)
.assertStatus(HttpURLConnection.HTTP_NO_CONTENT);
}

@Override
public void unstar(
@NotNull(message = "user can't be NULL") final String user,
@NotNull(message = "repo can't be NULL") final String repo
) throws IOException {
public void unstar() throws IOException {
this.request
.method(Request.DELETE)
.uri().path(user).path(repo)
.back()
.fetch().as(RestResponse.class)
.assertStatus(HttpURLConnection.HTTP_NO_CONTENT);
}
Expand Down
35 changes: 14 additions & 21 deletions src/main/java/com/jcabi/github/Stars.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,38 +43,31 @@
*/
@Immutable
public interface Stars {

/**
* Owner of them.
* @return Repo
*/
@NotNull(message = "repository is never NULL")
Repo repo();

/**
* Check if particular repo is starred by current user.
* @param user Repo owner login
* @param repo Repo name
* Check if repo is starred.
* @return True if repo is starred
* @throws IOException - If anything goes wrong.
*/
@NotNull(message = "starred is never NULL")
boolean starred(
@NotNull(message = "user can't be NULL") final String user,
@NotNull(message = "repo can't be NULL") final String repo
) throws IOException;
boolean starred() throws IOException;

/**
* Star particular repository by current user.
* @param user Repo owner login
* @param repo Repo name
* Star repository.
* @throws IOException - If anything goes wrong.
*/
void star(
@NotNull(message = "user can't be NULL") final String user,
@NotNull(message = "repo can't be NULL") final String repo
) throws IOException;
void star() throws IOException;

/**
* Unstar particular repository by current user.
* @param user Repo owner login
* @param repo Repo name
* Unstar repository.
* @throws IOException - If anything goes wrong.
*/
void unstar(
@NotNull(message = "user can't be NULL") final String user,
@NotNull(message = "repo can't be NULL") final String repo
) throws IOException;
void unstar() throws IOException;
}
22 changes: 9 additions & 13 deletions src/main/java/com/jcabi/github/mock/MkStars.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@

import com.jcabi.aspects.Immutable;
import com.jcabi.aspects.Loggable;
import com.jcabi.github.Repo;
import com.jcabi.github.Stars;
import javax.validation.constraints.NotNull;
import lombok.ToString;
import org.apache.commons.lang3.NotImplementedException;

Expand All @@ -51,26 +51,22 @@
@ToString
final class MkStars implements Stars {
@Override
public boolean starred(
@NotNull(message = "user can't be NULL") final String user,
@NotNull(message = "repo can't be NULL") final String repo
) {
public Repo repo() {
throw new NotImplementedException("MkStars.repo()");
}

@Override
public boolean starred() {
throw new NotImplementedException("MkStars.starred()");
}

@Override
public void star(
@NotNull(message = "user can't be NULL") final String user,
@NotNull(message = "repo can't be NULL") final String repo
) {
public void star() {
throw new NotImplementedException("MkStars.star()");
}

@Override
public void unstar(
@NotNull(message = "user can't be NULL") final String user,
@NotNull(message = "repo can't be NULL") final String repo
) {
public void unstar() {
throw new NotImplementedException("MkStars.unstar()");
}
}
12 changes: 5 additions & 7 deletions src/test/java/com/jcabi/github/RtStarsITCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,20 +85,18 @@ public static void tearDown() throws IOException {
*/
@Test
public void starsUnstarsChecksStar() throws IOException {
final String coordinates = repo.coordinates().repo();
final String user = repo.coordinates().user();
MatcherAssert.assertThat(
repo.stars().starred(user, coordinates),
repo.stars().starred(),
Matchers.equalTo(false)
);
repo.stars().star(user, coordinates);
repo.stars().star();
MatcherAssert.assertThat(
repo.stars().starred(user, coordinates),
repo.stars().starred(),
Matchers.equalTo(true)
);
repo.stars().unstar(user, coordinates);
repo.stars().unstar();
MatcherAssert.assertThat(
repo.stars().starred(user, coordinates),
repo.stars().starred(),
Matchers.equalTo(false)
);
}
Expand Down
45 changes: 33 additions & 12 deletions src/test/java/com/jcabi/github/RtStarsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.mockito.Mockito;

/**
* Test case for {@link RtStars}.
Expand All @@ -60,14 +61,19 @@ public void checkIfRepoStarred() throws Exception {
new MkAnswer.Simple(HttpURLConnection.HTTP_NO_CONTENT)
).next(new MkAnswer.Simple(HttpURLConnection.HTTP_NOT_FOUND))
.start();
final Stars stars = new RtStars(
new ApacheRequest(container.home())
final Stars starred = new RtStars(
new ApacheRequest(container.home()),
RtStarsTest.repo("someuser", "starredrepo")
);
MatcherAssert.assertThat(
stars.starred("someuser", "starredrepo"), Matchers.is(true)
starred.starred(), Matchers.is(true)
);
final Stars unstarred = new RtStars(
new ApacheRequest(container.home()),
RtStarsTest.repo("otheruser", "notstarredrepo")
);
MatcherAssert.assertThat(
stars.starred("otheruser", "notstarredrepo"), Matchers.is(false)
unstarred.starred(), Matchers.is(false)
);
}

Expand All @@ -81,13 +87,14 @@ public void starRepository() throws Exception {
final MkContainer container = new MkGrizzlyContainer().next(
new MkAnswer.Simple(HttpURLConnection.HTTP_NO_CONTENT)
).start();
final Stars stars = new RtStars(
new ApacheRequest(container.home())
);
final String user = "staruser";
final String repo = "starrepo";
final Stars stars = new RtStars(
new ApacheRequest(container.home()),
RtStarsTest.repo(user, repo)
);
try {
stars.star(user, repo);
stars.star();
final MkQuery query = container.take();
MatcherAssert.assertThat(
query.method(),
Expand Down Expand Up @@ -117,13 +124,14 @@ public void unstarRepository() throws Exception {
final MkContainer container = new MkGrizzlyContainer().next(
new MkAnswer.Simple(HttpURLConnection.HTTP_NO_CONTENT)
).start();
final Stars stars = new RtStars(
new ApacheRequest(container.home())
);
final String user = "unstaruser";
final String repo = "unstarrepo";
final Stars stars = new RtStars(
new ApacheRequest(container.home()),
RtStarsTest.repo(user, repo)
);
try {
stars.unstar(user, repo);
stars.unstar();
final MkQuery query = container.take();
MatcherAssert.assertThat(
query.method(),
Expand All @@ -142,4 +150,17 @@ public void unstarRepository() throws Exception {
container.stop();
}
}

/**
* Create and return repo for testing.
* @param user User
* @param reponame Repository
* @return Repo
*/
private static Repo repo(final String user, final String reponame) {
final Repo repo = Mockito.mock(Repo.class);
Mockito.doReturn(new Coordinates.Simple(user, reponame))
.when(repo).coordinates();
return repo;
}
}