Skip to content

Commit

Permalink
yegor256#536 fixing implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
krzyk authored and krzyk committed Sep 28, 2014
1 parent c54e68d commit 6fd46de
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 37 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,7 @@
<systemPropertyVariables combine.children="append">
<tomcat.home>http://localhost:${tomcat.port}</tomcat.home>
</systemPropertyVariables>
<reuseForks>false</reuseForks>
</configuration>
<executions>
<execution>
Expand Down
74 changes: 45 additions & 29 deletions src/main/java/com/rultor/dynamo/DyTalks.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.jcabi.aspects.Cacheable;
import com.jcabi.aspects.Immutable;
import com.jcabi.aspects.Tv;
Expand All @@ -63,6 +64,7 @@
@Immutable
@ToString
@EqualsAndHashCode(of = "region")
@SuppressWarnings("PMD.TooManyMethods")
public final class DyTalks implements Talks {

/**
Expand Down Expand Up @@ -239,41 +241,55 @@ public Talk apply(final Item input) {
@Cacheable
@Override
public Iterable<Talk> recent() {
return Iterables.filter(
Iterables.transform(
this.region.table(DyTalks.TBL)
.frame()
.through(
new QueryValve()
.withIndexName(DyTalks.IDX_ACTIVE)
.withScanIndexForward(false)
.withConsistentRead(false)
.withLimit(Tv.TWENTY)
.withSelect(Select.ALL_PROJECTED_ATTRIBUTES)
)
.where(DyTalks.ATTR_ACTIVE, Boolean.toString(false)),
new Function<Item, Talk>() {
@Override
public Talk apply(final Item input) {
return new DyTalk(input);
return Lists.newArrayList(
Iterables.filter(
Iterables.transform(
this.region.table(DyTalks.TBL)
.frame()
.through(
new QueryValve()
.withIndexName(DyTalks.IDX_ACTIVE)
.withScanIndexForward(false)
.withConsistentRead(false)
.withLimit(Tv.TWENTY)
.withSelect(Select.ALL_PROJECTED_ATTRIBUTES)
)
.where(
DyTalks.ATTR_ACTIVE, Boolean.toString(false)
),
new Function<Item, Talk>() {
@Override
public Talk apply(final Item input) {
return new DyTalk(input);
}
}
}
),
new Predicate<Talk>() {
@Override
public boolean apply(final Talk talk) {
try {
return !talk.read().nodes(
"/talk[@public='true']"
).isEmpty();
} catch (final IOException ex) {
throw new IllegalStateException(ex);
),
new Predicate<Talk>() {
@Override
public boolean apply(final Talk talk) {
try {
return !talk.read().nodes(
"/talk[@public='true']"
).isEmpty();
} catch (final IOException ex) {
throw new IllegalStateException(ex);
}
}
}
}
)
);
}

/**
* Method for unit test to clean recent() cache.
* @todo #536 Remove this method and PMD.TooManyMethods when
* jcabi-aspects#91 is resolved.
*/
@Cacheable.FlushBefore
public void flush() {
// intentionally left empty
}

@Override
public Iterable<Talk> siblings(final String repo, final Date since) {
return Iterables.transform(
Expand Down
54 changes: 46 additions & 8 deletions src/test/java/com/rultor/dynamo/DyTalksITCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@
import java.util.Date;
import java.util.concurrent.TimeUnit;
import org.hamcrest.CustomMatcher;
import org.hamcrest.Description;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.hamcrest.TypeSafeMatcher;
import org.junit.Assume;
import org.junit.Ignore;
import org.junit.Test;
Expand Down Expand Up @@ -83,44 +85,46 @@ public void listsRecentTalks() throws Exception {
final Talks talks = new DyTalks(
this.dynamo(), new MkSttc().counters().get("")
);
((DyTalks) talks).flush();
final String name = "yegor256/rultor#529";
talks.create("a/b", name);
final Talk talk = talks.get(name);
talk.active(false);
MatcherAssert.assertThat(
talks.recent().iterator().next().name(),
Matchers.equalTo(name)
talks.recent(),
Matchers.hasItem(new DyTalksITCase.TalkMatcher(name))
);
}

/**
* DyTalks caches talks.
* @throws Exception If some problem inside
* @todo #536 For some strange reason Cacheable is not being applied to
* DyTalks, when it starts working enable this test.
*/
@Test
@Ignore
public void cachesRecentTalks() throws Exception {
final Talks talks = new DyTalks(
this.dynamo(), new MkSttc().counters().get("")
);
final String first = "krzyk/rultor#562";
((DyTalks) talks).flush();
final String first = "krzyk1/rultor#562";
final String repo = "some/other";
talks.create(repo, first);
final Talk talk = talks.get(first);
talk.active(false);
MatcherAssert.assertThat(
talks.recent(),
Matchers.<Talk>iterableWithSize(1)
Matchers.hasItem(new DyTalksITCase.TalkMatcher(first))
);
final String second = "krzyk/rultor#562#2";
final String second = "krzyk2/rultor#562#2";
talks.create(repo, second);
final Talk talking = talks.get(second);
talking.active(false);
MatcherAssert.assertThat(
talks.recent(),
Matchers.<Talk>iterableWithSize(1)
Matchers.not(
Matchers.hasItem(new DyTalksITCase.TalkMatcher(second))
)
);
}

Expand Down Expand Up @@ -209,4 +213,38 @@ private Region dynamo() {
);
}

/**
* Matcher for Talks.
*/
@SuppressWarnings("PMD.CallSuperInConstructor")
private static final class TalkMatcher extends TypeSafeMatcher<Talk> {
/**
* Name of the talk.
*/
private final transient String name;

/**
* Constructor.
* @param nam Name of the talk.
*/
public TalkMatcher(final String nam) {
this.name = nam;
}

@Override
public boolean matchesSafely(final Talk talk) {
try {
return talk.name().equals(this.name);
} catch (final IOException ex) {
throw new IllegalStateException(ex);
}
}

@Override
public void describeTo(final Description description) {
description.appendText(
String.format("Talk '%s' not found", this.name)
);
}
}
}

0 comments on commit 6fd46de

Please sign in to comment.