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

#409 User now being passed to DyRepos #528

Merged
merged 5 commits into from Mar 20, 2015
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/main/java/com/thindeck/MnBase.java
Expand Up @@ -138,6 +138,10 @@ public Repo add(final String name) {
public Iterable<Repo> iterate() {
return Collections.<Repo>singleton(new MnBase.FakeRepo());
}
@Override
public User user() {
throw new UnsupportedOperationException("#user()");
}
}

/**
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/thindeck/api/Repo.java
Expand Up @@ -43,6 +43,9 @@
* @author Yegor Bugayenko (yegor@tpc2.com)
* @version $Id$
* @since 0.1
* @todo #409:30min Let's add a new method User user(). It should return the
* User whom the Repo is associated to. Let's also add implementations in the
* classes that implement this interface.
*/
@Immutable
public interface Repo {
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/com/thindeck/api/Repos.java
Expand Up @@ -74,4 +74,11 @@ public interface Repos {
@NotNull(message = "iterable of repos can't be null")
Iterable<Repo> iterate();

/**
* The owner of this.
* @return Owner of the repos.
*/
@NotNull(message = "user of repos can't be null")
User user();

}
22 changes: 22 additions & 0 deletions src/main/java/com/thindeck/api/User.java
Expand Up @@ -31,6 +31,7 @@

import com.jcabi.aspects.Immutable;
import com.jcabi.urn.URN;
import java.net.URISyntaxException;
import javax.validation.constraints.NotNull;

/**
Expand All @@ -42,6 +43,27 @@
*/
@Immutable
public interface User {
/**
* Default User.
*/
User DEFAULT = new User() {
@Override
public URN urn() {
try {
return new URN("urn:thindeck:DEFAULT");
} catch (final URISyntaxException ex) {
throw new IllegalStateException(ex);
}
}
@Override
public Repos repos() {
throw new UnsupportedOperationException();
}
@Override
public Usage usage() {
throw new UnsupportedOperationException();
}
};

/**
* URN.
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/thindeck/api/mock/MkRepos.java
Expand Up @@ -32,6 +32,7 @@
import com.jcabi.aspects.Immutable;
import com.thindeck.api.Repo;
import com.thindeck.api.Repos;
import com.thindeck.api.User;
import java.util.Collections;
import lombok.EqualsAndHashCode;
import lombok.ToString;
Expand Down Expand Up @@ -62,4 +63,9 @@ public Repo add(final String name) {
public Iterable<Repo> iterate() {
return Collections.<Repo>singleton(new MkRepo());
}

@Override
public User user() {
return new MkUser();
}
}
25 changes: 25 additions & 0 deletions src/main/java/com/thindeck/dynamo/DyRepos.java
Expand Up @@ -39,6 +39,7 @@
import com.jcabi.dynamo.Region;
import com.thindeck.api.Repo;
import com.thindeck.api.Repos;
import com.thindeck.api.User;
import java.io.IOException;
import java.util.Iterator;
import lombok.EqualsAndHashCode;
Expand All @@ -58,12 +59,27 @@ public final class DyRepos implements Repos {
*/
private final transient Region region;

/**
* Owner of these.
*/
private final transient User owner;

/**
* Constructor.
* @param rgn Region
*/
public DyRepos(final Region rgn) {
this(rgn, User.DEFAULT);
}

/**
* Constructor.
* @param rgn Region
* @param usr The user of the Repos
*/
public DyRepos(final Region rgn, final User usr) {
this.region = rgn;
this.owner = usr;
}

@Override
Expand All @@ -89,6 +105,10 @@ public Repo add(final String name) {
}
}

//@todo #409:30min We should include the User as a filter to the repos that
// will be fetched. If the Default user is specified, we should return all
// repos. Otherwise, only the repos of that specific user should be
// contained in the Iterable.
@Override
public Iterable<Repo> iterate() {
return Iterables.transform(
Expand All @@ -108,6 +128,11 @@ public Repo apply(final Item input) {
);
}

@Override
public User user() {
return this.owner;
}

/**
* Get iterator over repos with the name specified.
* If this repo exists, the iterator's set will contain 1 element.
Expand Down
9 changes: 1 addition & 8 deletions src/main/java/com/thindeck/dynamo/DyUser.java
Expand Up @@ -44,13 +44,6 @@
*
* @author Krzysztof Krason (Krzysztof.Krason@gmail.com)
* @version $Id$
* @todo #374:30min At the moment, the repos() method returns all Repos associated
* with the Dynamo region, and this is definitely incorrect. I think we need to
* refactor DyRepos in order to include a user criteria (probably URN) in its
* constructor, and have DyUser pass it so that it will only return the
* associated criteria. I'm not completely sure about this design, feel free to
* implement something else if you think it's wrong. The intuition behind it is
* that we should only get the repos associated with the current user.
*/
@EqualsAndHashCode
@ToString
Expand Down Expand Up @@ -90,7 +83,7 @@ public URN urn() {

@Override
public Repos repos() {
return new DyRepos(this.item.frame().table().region());
return new DyRepos(this.item.frame().table().region(), this);
}

@Override
Expand Down
27 changes: 27 additions & 0 deletions src/test/java/com/thindeck/dynamo/DyReposTest.java
Expand Up @@ -36,11 +36,13 @@
import com.jcabi.dynamo.mock.MkRegion;
import com.thindeck.api.Repo;
import com.thindeck.api.Repos;
import com.thindeck.api.User;
import java.io.IOException;
import java.util.Iterator;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.mockito.Mockito;

/**
* Tests for {@link DyRepos}.
Expand Down Expand Up @@ -127,6 +129,31 @@ public void iteratesOverMultipleRepos() throws IOException {
MatcherAssert.assertThat(repos.hasNext(), Matchers.is(false));
}

/**
* DyRepos can return the default user if the user was not specified.
* @throws Exception If something goes wrong.
*/
@Test
public void returnsDefaultUser() throws Exception {
MatcherAssert.assertThat(
new DyRepos(Mockito.mock(Region.class)).user(),
Matchers.is(User.DEFAULT)
);
}

/**
* DyRepos can return the specified user if the user was not specified.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment is not correct, it should be DyRepos can return the specified user., without the rest.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, thanks! Silly mistake...

* @throws Exception If something goes wrong.
*/
@Test
public void returnsSpecifiedUser() throws Exception {
final User user = Mockito.mock(User.class);
MatcherAssert.assertThat(
new DyRepos(Mockito.mock(Region.class), user).user(),
Matchers.is(user)
);
}

/**
* Create region with repos.
* @param names Names of the repos.
Expand Down
26 changes: 26 additions & 0 deletions src/test/java/com/thindeck/dynamo/DyUserTest.java
Expand Up @@ -99,6 +99,32 @@ public void canGetRepos() throws Exception {
);
}

/**
* DyUser can get itself from the Repos that it returns.
* @throws Exception If something goes wrong
*/
@Test
public void getsReposForItself() throws Exception {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

more like: getsItselfFromRepos

final Region region = new MkRegion(
new H2Data().with(
DyRepo.TBL,
new String[] {DyRepo.ATTR_NAME},
new String[] {DyRepo.ATTR_UPDATED}
)
);
final Table table = region.table(DyRepo.TBL);
table.put(
new Attributes()
.with(DyRepo.ATTR_NAME, "test-user-repo")
.with(DyRepo.ATTR_UPDATED, System.currentTimeMillis())
);
final User user = new DyUser(table.frame().iterator().next());
MatcherAssert.assertThat(
user.repos().user(),
Matchers.is(user)
);
}

/**
* DyUser can return usage info.
* @throws Exception If something goes wrong.
Expand Down