From b412e8abb211475a477cfb5ef8ea05e659517d4e Mon Sep 17 00:00:00 2001 From: Chris Rebert Date: Tue, 14 Apr 2015 02:28:54 -0700 Subject: [PATCH] Fix #1057 and add unit test --- .../com/jcabi/github/mock/MkComments.java | 20 ++++- .../com/jcabi/github/mock/MkCommentTest.java | 74 ++++++++++++++++++- 2 files changed, 87 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/jcabi/github/mock/MkComments.java b/src/main/java/com/jcabi/github/mock/MkComments.java index 3852d1cb7..d5e07ca3e 100644 --- a/src/main/java/com/jcabi/github/mock/MkComments.java +++ b/src/main/java/com/jcabi/github/mock/MkComments.java @@ -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(); diff --git a/src/test/java/com/jcabi/github/mock/MkCommentTest.java b/src/test/java/com/jcabi/github/mock/MkCommentTest.java index ee58b8d68..65115e054 100644 --- a/src/test/java/com/jcabi/github/mock/MkCommentTest.java +++ b/src/test/java/com/jcabi/github/mock/MkCommentTest.java @@ -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; @@ -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(), @@ -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); + } }