Skip to content

Commit

Permalink
Merge remote branch 'fork/fix-1057'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Apr 15, 2015
2 parents 02d1b1b + b412e8a commit 7a7075c
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 7 deletions.
20 changes: 17 additions & 3 deletions src/main/java/com/jcabi/github/mock/MkComments.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,29 @@ public Comment post(
this.storage.lock();
final int number;
try {
final String timestamp = new Github.Time().toString();
number = 1 + this.storage.xml()
.nodes("//comment/number").size();
this.storage.apply(
new Directives().xpath(this.xpath()).add("comment")
.add("number").set(Integer.toString(number)).up()
.add("url")
.set(
String.format(
// @checkstyle LineLength (1 line)
"https://api.jcabi-github.invalid/repos/%s/%s/issues/comments/%d",
this.repo.user(),
this.repo.repo(),
number
)
)
.up()
.add("body").set(text).up()
.add("user").add("login").set(this.self).up()
.add("created_at").set(new Github.Time().toString()).up()
.add("updated_at").set(new Github.Time().toString())
.add("user")
.add("login").set(this.self).up()
.up()
.add("created_at").set(timestamp).up()
.add("updated_at").set(timestamp)
);
} finally {
this.storage.unlock();
Expand Down
74 changes: 70 additions & 4 deletions src/test/java/com/jcabi/github/mock/MkCommentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@
*/
package com.jcabi.github.mock;

import com.jcabi.aspects.Tv;
import com.jcabi.github.Comment;
import com.jcabi.github.Coordinates;
import java.net.URL;
import java.util.Date;
import javax.json.Json;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
Expand All @@ -43,14 +46,13 @@
* @version $Id$
*/
public final class MkCommentTest {

/**
* MkComment can change body.
* @throws Exception If some problem inside
*/
@Test
public void changesBody() throws Exception {
final Comment comment = this.comment();
final Comment comment = this.comment("hey buddy");
new Comment.Smart(comment).body("hello, this is a new body");
MatcherAssert.assertThat(
new Comment.Smart(comment).body(),
Expand Down Expand Up @@ -89,15 +91,79 @@ public void canCompareInstances() throws Exception {
);
}

/**
* MkComment should store all its data properly.
* We should get the proper data back when accessing its properties.
* @throws Exception when a problem occurs.
*/
@Test
public void dataStoredProperly() throws Exception {
final String cmt = "what's up?";
final long before = MkCommentTest.now();
final Comment comment = this.comment(cmt);
final long after = MkCommentTest.now();
MatcherAssert.assertThat(
comment.number(),
Matchers.greaterThan(0)
);
final Comment.Smart smart = new Comment.Smart(comment);
MatcherAssert.assertThat(
smart.issue().number(),
Matchers.greaterThan(0)
);
MatcherAssert.assertThat(
smart.author().login(),
Matchers.equalTo("jeff")
);
MatcherAssert.assertThat(
smart.body(),
Matchers.equalTo(cmt)
);
MatcherAssert.assertThat(
smart.url(),
Matchers.equalTo(
new URL(
// @checkstyle LineLength (1 line)
"https://api.jcabi-github.invalid/repos/jeff/test/issues/comments/1"
)
)
);
MatcherAssert.assertThat(
smart.createdAt().getTime(),
Matchers.greaterThanOrEqualTo(before)
);
MatcherAssert.assertThat(
smart.createdAt().getTime(),
Matchers.lessThanOrEqualTo(after)
);
MatcherAssert.assertThat(
smart.updatedAt().getTime(),
Matchers.greaterThanOrEqualTo(before)
);
MatcherAssert.assertThat(
smart.updatedAt().getTime(),
Matchers.lessThanOrEqualTo(after)
);
}

/**
* Create a comment to work with.
* @param text Text of comment
* @return Comment just created
* @throws Exception If some problem inside
*/
private Comment comment() throws Exception {
private Comment comment(final String text) throws Exception {
return new MkGithub().repos().create(
Json.createObjectBuilder().add("name", "test").build()
).issues().create("hey", "how are you?").comments().post("what's up?");
).issues().create("hey", "how are you?").comments().post(text);
}

/**
* Obtains the current time.
* @return Current time (in milliseconds since epoch) truncated to the nearest second
*/
private static long now() {
final long sinceepoch = new Date().getTime();
return sinceepoch - (sinceepoch % Tv.THOUSAND);
}
}

0 comments on commit 7a7075c

Please sign in to comment.