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

#561 added languages tweets #761

Merged
merged 2 commits into from
Jan 28, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 8 additions & 3 deletions src/main/java/com/rultor/agents/twitter/Tweets.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.jcabi.aspects.Tv;
import com.jcabi.github.Github;
import com.jcabi.github.Issue;
import com.jcabi.github.Language;
import com.jcabi.github.Repo;
import com.jcabi.log.Logger;
import com.jcabi.xml.XML;
Expand Down Expand Up @@ -107,6 +108,7 @@ public Iterable<Directive> process(final XML xml) throws IOException {
* @return Tweet text
* @throws IOException If fails
*/
@SuppressWarnings("PMD.InsufficientStringBufferDeclaration")
private static String tweet(final Repo.Smart repo, final String tag)
throws IOException {
final StringBuilder text = new StringBuilder(2 * Tv.HUNDRED);
Expand All @@ -120,10 +122,13 @@ private static String tweet(final Repo.Smart repo, final String tag)
)
);
}
return text.append(", ").append(tag)
text.append(", ").append(tag)
.append(" released https://github.com/")
.append(repo.coordinates())
.toString();
.append(repo.coordinates());
for (final Language lang : repo.languages()) {
text.append(String.format(" #%s", lang));
}
return text.toString();
}

}
108 changes: 104 additions & 4 deletions src/test/java/com/rultor/agents/twitter/TweetsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,21 @@
*/
package com.rultor.agents.twitter;

import com.google.common.collect.Lists;
import com.jcabi.github.Coordinates;
import com.jcabi.github.Github;
import com.jcabi.github.Issue;
import com.jcabi.github.Issues;
import com.jcabi.github.Language;
import com.jcabi.github.Repo;
import com.jcabi.github.Repos;
import com.jcabi.github.mock.MkGithub;
import com.rultor.spi.Agent;
import com.rultor.spi.Talk;
import java.io.IOException;
import java.util.List;
import javax.json.Json;
import javax.json.JsonObject;
import org.junit.Ignore;
import org.junit.Test;
import org.mockito.Matchers;
Expand All @@ -46,6 +56,10 @@
* @author Yegor Bugayenko ([email protected])
* @version $Id$
* @since 1.30
* @todo #561 When all the puzzles from
* https://github.com/jcabi/jcabi-github/issues/923 are implemented, remove

Choose a reason for hiding this comment

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

actually, jcabi/jcabi-github#923 is already implemented

* repo, issue, lang methods, and use MkGithub family in all the tests. Also
* enable postsTweet method.
*/
public final class TweetsTest {

Expand All @@ -60,6 +74,78 @@ public void postsTweet() throws Exception {
final Issue issue = repo.issues().create("", "");
final Twitter twitter = Mockito.mock(Twitter.class);
final Agent agent = new Tweets(repo.github(), twitter);
final Talk talk = this.talk(repo, issue);
agent.execute(talk);
Mockito.verify(twitter).post(
Matchers.contains("test")
);
}

/**
* Tweets can post a tweet with language tags.
* @throws Exception In case of error.
*/
@Test
public void postsTweetWithLanguages() throws Exception {
final Repo repo = this.repo();
final List<Language> langs = Lists.newArrayList(

Choose a reason for hiding this comment

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

we don't need mutability here. Consider using ImmutableList.of()

this.lang("Java"), this.lang("Python")
);
Mockito.when(repo.languages()).thenReturn(langs);
final Twitter twitter = Mockito.mock(Twitter.class);
final Agent agent = new Tweets(repo.github(), twitter);

Choose a reason for hiding this comment

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

looks like one-time variable

final Talk talk = this.talk(repo, this.issue(repo));
agent.execute(talk);
Mockito.verify(twitter).post(
Matchers.contains(
String.format("#%s #%s", langs.get(0), langs.get(1))
)
);
}

/**
* Create mock issue.
* @param repo Repo to use
* @return Mocked issue.
*/
private Issue issue(final Repo repo) {

Choose a reason for hiding this comment

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

why not static?

final Issues issues = Mockito.mock(Issues.class);
final Issue issue = Mockito.mock(Issue.class);
Mockito.when(issue.repo()).thenReturn(repo);
Mockito.when(issues.get(Mockito.anyInt())).thenReturn(issue);
Mockito.when(repo.issues()).thenReturn(issues);
return issue;
}

/**
* Create mock repo.
* @return Mocked repo
* @throws IOException In case of error
*/
private Repo repo() throws IOException {

Choose a reason for hiding this comment

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

same here

final Github github = Mockito.mock(Github.class);
final Repo repo = Mockito.mock(Repo.class);
final JsonObject rjson = Json.createObjectBuilder()
.add("description", "something").build();
Mockito.when(repo.json()).thenReturn(rjson);
Mockito.when(repo.github()).thenReturn(github);
final Repos repos = Mockito.mock(Repos.class);
Mockito.when(github.repos()).thenReturn(repos);
final Coordinates coords = new Coordinates.Simple("foo/bar");
Mockito.when(repo.coordinates()).thenReturn(coords);
Mockito.when(repos.get(Mockito.any(Coordinates.class)))
.thenReturn(repo);
return repo;
}

/**
* Creates a talk with repo and issue.
* @param repo Repo to use
* @param issue Issue to use
* @return Created Talk
* @throws IOException In case of error
*/
private Talk talk(final Repo repo, final Issue issue) throws IOException {
final Talk talk = new Talk.InFile();
talk.modify(
new Directives().xpath("/talk").add("wire")
Expand All @@ -73,10 +159,24 @@ public void postsTweet() throws Exception {
.add("type").set("release").up()
.add("args").add("arg").attr("name", "tag").set("1.7")
);
agent.execute(talk);
Mockito.verify(twitter).post(
Matchers.contains("test")
);
return talk;
}

/**
* Create mock language.
* @param name Name of the language
* @return Language created.
*/
private Language lang(final String name) {

Choose a reason for hiding this comment

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

and here

return new Language() {
@Override
public String name() {
return name;
}
@Override
public long bytes() {
return 0;
}
};
}
}