diff --git a/src/main/java/co/comdor/SocialSteps.java b/src/main/java/co/comdor/SocialSteps.java new file mode 100644 index 0000000..bc42f4b --- /dev/null +++ b/src/main/java/co/comdor/SocialSteps.java @@ -0,0 +1,54 @@ +/** + * Copyright (c) 2017, Mihai Emil Andronache + * All rights reserved. + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1)Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2)Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3)Neither the name of comdor nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +package co.comdor; + +import co.comdor.github.Mention; + +/** + * After executing all the steps which are needed to fulfill an action, + * the bot performs a few more social, cosmetic steps, like starring the repo, + * following the user on Github, tweeting etc. + * + * These steps should be executted all the time, in the end of the Steps + * pyramid. + * + * @author Mihai Andronache (amihaiemil@gmail.com) + * @version $Id$ + * @since 0.0.2 + */ + +public interface SocialSteps extends Step { + + /** + * Notice that this overriden perform does not throw IOException. + * This is because we are not treating social steps failures, they are only + * cosmetic, not affecting the business at all. + * @param mention Mention which triggered everything. + * @param log Log of the Action. + */ + @Override + void perform(final Mention mention, final Log log); +} diff --git a/src/main/java/co/comdor/Step.java b/src/main/java/co/comdor/Step.java index 2e5b68e..36a50a6 100644 --- a/src/main/java/co/comdor/Step.java +++ b/src/main/java/co/comdor/Step.java @@ -26,7 +26,6 @@ package co.comdor; import co.comdor.github.Mention; -import org.slf4j.Logger; import java.io.IOException; /** @@ -44,7 +43,7 @@ public interface Step { * @throws IOException If there is anything wrong in the communication * with Github. */ - void perform(final Mention mention, final Logger log) throws IOException; + void perform(final Mention mention, final Log log) throws IOException; /** * Final step in the chain. @@ -73,8 +72,8 @@ public FinalStep(final String message) { } @Override - public void perform(final Mention mention, final Logger log) { - log.info(this.message); + public void perform(final Mention mention, final Log log) { + log.logger().info(this.message); } } @@ -97,7 +96,7 @@ public Fake(final boolean pass) { } @Override - public void perform(final Mention mention, final Logger log) { + public void perform(final Mention mention, final Log log) { if(!this.pass) { throw new IllegalStateException( "Step should not have been executed!" diff --git a/src/main/java/co/comdor/Steps.java b/src/main/java/co/comdor/Steps.java index b0c69bd..de5b262 100644 --- a/src/main/java/co/comdor/Steps.java +++ b/src/main/java/co/comdor/Steps.java @@ -25,7 +25,6 @@ */ package co.comdor; -import org.slf4j.Logger; import java.io.IOException; /** @@ -42,5 +41,6 @@ public interface Steps { * @throws IOException If there is anything wrong in the communication * with Github. */ - void perform(final Logger log) throws IOException; + void perform(final Log log) throws IOException; + } diff --git a/src/main/java/co/comdor/github/FollowUser.java b/src/main/java/co/comdor/github/FollowUser.java index b67fbcc..06c481f 100644 --- a/src/main/java/co/comdor/github/FollowUser.java +++ b/src/main/java/co/comdor/github/FollowUser.java @@ -30,7 +30,7 @@ import com.jcabi.http.Request; import java.io.IOException; import java.net.HttpURLConnection; -import org.slf4j.Logger; +import co.comdor.Log; /** * Step where the bot follows the Github user. @@ -50,25 +50,25 @@ public FollowUser(final Step next) { @Override public void perform( - final Mention mention, final Logger log + final Mention mention, final Log log ) throws IOException { final String author = mention.author(); final Request follow = mention.issue().repo().github().entry() .uri().path("/user/following/").path(author).back() .method("PUT"); - log.info("Following Github user " + author + " ..."); + log.logger().info("Following Github user " + author + " ..."); try { final int status = follow.fetch().status(); if(status != HttpURLConnection.HTTP_NO_CONTENT) { - log.error( + log.logger().error( "User follow status response is " + status + " . Should have been 204 (NO CONTENT)" ); } else { - log.info("Followed user " + author + " ."); + log.logger().info("Followed user " + author + " ."); } } catch (final IOException ex) { - log.warn("IOException while trying to follow the user."); + log.logger().warn("IOException while trying to follow the user."); } this.next().perform(mention, log); } diff --git a/src/main/java/co/comdor/github/GithubAction.java b/src/main/java/co/comdor/github/GithubAction.java index 0b972b0..65c890e 100644 --- a/src/main/java/co/comdor/github/GithubAction.java +++ b/src/main/java/co/comdor/github/GithubAction.java @@ -28,6 +28,7 @@ import co.comdor.Action; import co.comdor.Log; import co.comdor.LogFile; +import co.comdor.SocialSteps; import co.comdor.Steps; import co.comdor.WebLog; import com.jcabi.github.Issue; @@ -52,6 +53,12 @@ public final class GithubAction implements Action { */ private Issue issue; + /** + * Social steps to be executed in the end, after the bot finishes everything + * else. + */ + private SocialSteps social; + /** * Log of this action. Each Github action should be logged in its own file, * since we want to let the user inspect the logs sometimes. @@ -61,11 +68,16 @@ public final class GithubAction implements Action { /** * Ctor. * @param issue Github Issue which triggered this action. + * @param social Social steps that the bot executes after the he fulfills + * the triggering Mention. * @throws IOException If there is any IO problem (e.g. writing files, * communicating with Github etc). */ - public GithubAction(final Issue issue) throws IOException { + public GithubAction( + final Issue issue, final SocialSteps social + ) throws IOException { this.issue = issue; + this.social = social; this.id = UUID.randomUUID().toString(); this.log = new WebLog( new LogFile( @@ -89,8 +101,10 @@ public void perform() { new Confused() ) ); - final Steps steps = talk.start(new LastMention(this.issue)); - steps.perform(this.log.logger()); + final Mention mention = new LastMention(this.issue); + final Steps steps = talk.start(mention); + steps.perform(this.log); + this.social.perform(mention, this.log); } catch (final IllegalArgumentException iae) { this.log.logger().warn( "No command found in the issue or the agent has already" diff --git a/src/main/java/co/comdor/github/GithubSocialSteps.java b/src/main/java/co/comdor/github/GithubSocialSteps.java new file mode 100644 index 0000000..4991ca8 --- /dev/null +++ b/src/main/java/co/comdor/github/GithubSocialSteps.java @@ -0,0 +1,63 @@ +/** + * Copyright (c) 2017, Mihai Emil Andronache + * All rights reserved. + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1)Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2)Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3)Neither the name of comdor nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +package co.comdor.github; + +import co.comdor.SocialSteps; +import co.comdor.Step; +import co.comdor.Log; +import java.io.IOException; + +/** + * Github social steps. Follow the user and star the repository. + * @todo #34:30min StarRepo and FollowUser are each tested separately, but some + * tests for this class would also be useful. + */ +public final class GithubSocialSteps implements SocialSteps { + + /** + * Social steps. + */ + private final Step social; + + /** + * Ctor. + */ + public GithubSocialSteps() { + this.social = new StarRepo( + new FollowUser( + new Step.FinalStep("Starred the repo and followed the user.") + ) + ); + } + + @Override + public void perform(final Mention mention, final Log log) { + try { + this.social.perform(mention, log); + } catch (final IOException ioe){} + } + +} diff --git a/src/main/java/co/comdor/github/GithubSteps.java b/src/main/java/co/comdor/github/GithubSteps.java index 7487a3d..e61e9ab 100644 --- a/src/main/java/co/comdor/github/GithubSteps.java +++ b/src/main/java/co/comdor/github/GithubSteps.java @@ -27,7 +27,7 @@ import co.comdor.Step; import co.comdor.Steps; -import org.slf4j.Logger; +import co.comdor.Log; import java.io.IOException; @@ -43,63 +43,47 @@ public final class GithubSteps implements Steps { * Steps to be performed. */ private Step steps; - + /** * Initial mention. The one that triggered everything. */ private Mention mention; - /** - * Message to send in case some step fails. - */ - private SendReply failureMessage; - /** * Constructor. * @param steps Steps to perform everything. * @param mention Initial menton. */ public GithubSteps(final Step steps, final Mention mention) { - this( - steps, mention, - new SendReply( - mention.language().response("step.failure.comment"), - new Step.FinalStep("[ERROR] Some step didn't execute properly.") - ) - ); - } - - /** - * Constructor. - * @param steps Steps to perform everything. - * @param mention Initial menton. - * @param failureMessage Reply sent in case of failure. - */ - public GithubSteps( - final Step steps, final Mention mention, - final SendReply failureMessage - ) { this.steps = steps; this.mention = mention; - this.failureMessage = failureMessage; } /** * Perform all the given steps. - * @param logger Action logger. + * @param log Action logger. * @throws IOException If something goes wrong while calling Github. */ @Override - public void perform(final Logger logger) throws IOException { + public void perform(final Log log) throws IOException { try { - logger.info( + log.logger().info( "Received command: " + this.mention.json().getString("body") ); - logger.info("Author login: " + this.mention.author()); - this.steps.perform(this.mention, logger); + log.logger().info("Author login: " + this.mention.author()); + this.steps.perform(this.mention, log); } catch (final IOException ex) { - logger.error("An exception occured, sending failure comment.", ex); - this.failureMessage.perform(this.mention, logger); + log.logger().error( + "Some step did not execute properly, sending failure reply.", ex + ); + this.mention.reply( + String.format( + this.mention.language().response("steps.failure.comment"), + log.location() + ) + ); } + } + } diff --git a/src/main/java/co/comdor/github/SendReply.java b/src/main/java/co/comdor/github/SendReply.java index 7f2146b..d7881ce 100644 --- a/src/main/java/co/comdor/github/SendReply.java +++ b/src/main/java/co/comdor/github/SendReply.java @@ -26,7 +26,7 @@ package co.comdor.github; import co.comdor.IntermediaryStep; import co.comdor.Step; -import org.slf4j.Logger; +import co.comdor.Log; import java.io.IOException; /** @@ -64,12 +64,12 @@ public SendReply(final String rep, final Step next) { @Override public void perform( - final Mention mention, final Logger log + final Mention mention, final Log log ) throws IOException { try { mention.reply(this.rep); } catch (final IOException ex) { - log.error("IOException when sending a reply!", ex); + log.logger().error("IOException when sending a reply!", ex); throw new IOException("IOException when sending a reply!", ex); } this.next().perform(mention, log); diff --git a/src/main/java/co/comdor/github/StarRepo.java b/src/main/java/co/comdor/github/StarRepo.java index 144dfc0..6c33886 100644 --- a/src/main/java/co/comdor/github/StarRepo.java +++ b/src/main/java/co/comdor/github/StarRepo.java @@ -27,9 +27,9 @@ import co.comdor.IntermediaryStep; import co.comdor.Step; +import co.comdor.Log; import com.jcabi.github.Repo; import java.io.IOException; -import org.slf4j.Logger; /** * Step where the bot stars the Github repository. @@ -49,17 +49,17 @@ public StarRepo(final Step next) { @Override public void perform( - final Mention mention, final Logger log + final Mention mention, final Log log ) throws IOException { try { - log.info("Starring repository..."); + log.logger().info("Starring repository..."); final Repo repo = mention.issue().repo(); if(!repo.stars().starred()) { repo.stars().star(); } - log.info("Repository starred!"); + log.logger().info("Repository starred!"); } catch (final IOException ex) { - log.warn( + log.logger().warn( "IOException when starring repository, could not star the repo." ); } diff --git a/src/main/java/co/comdor/rest/NotificationsResource.java b/src/main/java/co/comdor/rest/NotificationsResource.java index b70c243..8b5aaec 100644 --- a/src/main/java/co/comdor/rest/NotificationsResource.java +++ b/src/main/java/co/comdor/rest/NotificationsResource.java @@ -27,6 +27,7 @@ import co.comdor.Action; import co.comdor.github.GithubAction; +import co.comdor.github.GithubSocialSteps; import co.comdor.rest.model.Notification; import co.comdor.rest.model.Notifications; import co.comdor.rest.model.SimplifiedNotifications; @@ -203,7 +204,8 @@ private boolean handleNotifications(final Notifications notifications) { new Coordinates.Simple( notification.repoFullName() ) - ).issues().get(notification.issueNumber()) + ).issues().get(notification.issueNumber()), + new GithubSocialSteps() ) ); } diff --git a/src/main/resources/responses_en.properties b/src/main/resources/responses_en.properties index d3257f6..17d3025 100644 --- a/src/main/resources/responses_en.properties +++ b/src/main/resources/responses_en.properties @@ -1,3 +1,6 @@ hello.comment=Hi there! I'm only version `0.0.1`, so I can't do much yet. Follow me and [my website](http://comdor.co) for updates on the next version. unknown.comment=Sorry, I do not understand what you mean. Check the [documentation](http://comdor.co/doc.html) for more details. + +steps.failure.comment=Oops, some steps failed when executing your request. [Here](%s) are the logs.\n\n\ + Please, try again and if the error persists, report an issue [here](https://www.github.com/amihaiemil/comdor). \ No newline at end of file diff --git a/src/test/java/co/comdor/github/FollowUserTestCase.java b/src/test/java/co/comdor/github/FollowUserTestCase.java index e071f1e..c14381a 100644 --- a/src/test/java/co/comdor/github/FollowUserTestCase.java +++ b/src/test/java/co/comdor/github/FollowUserTestCase.java @@ -26,6 +26,7 @@ package co.comdor.github; import co.comdor.Step; +import co.comdor.Log; import com.jcabi.github.Github; import com.jcabi.github.Issue; import com.jcabi.github.Repo; @@ -67,15 +68,17 @@ public void fullowsUserSuccessfuly() throws Exception { Mockito.when(com.issue().repo().github().entry()) .thenReturn(new ApacheRequest("http://localhost:" + port + "/")); - final Logger logger = Mockito.mock(Logger.class); + final Log log = Mockito.mock(Log.class); + final Logger slf4j = Mockito.mock(Logger.class); + Mockito.when(log.logger()).thenReturn(slf4j); try { - new FollowUser(new Step.Fake(true)).perform(com, logger); + new FollowUser(new Step.Fake(true)).perform(com, log); - Mockito.verify(logger).info( + Mockito.verify(slf4j).info( "Following Github user " + com.author() + " ..." ); - Mockito.verify(logger).info("Followed user " + com.author() + " ."); + Mockito.verify(slf4j).info("Followed user " + com.author() + " ."); final MkQuery request = github.take(); MatcherAssert.assertThat( request.uri().toString(), @@ -101,15 +104,18 @@ public void differentResponseStatus() throws Exception { final Mention com = this.mockMention(); Mockito.when(com.issue().repo().github().entry()) .thenReturn(new ApacheRequest("http://localhost:" + port + "/")); - final Logger logger = Mockito.mock(Logger.class); + final Log log = Mockito.mock(Log.class); + final Logger slf4j = Mockito.mock(Logger.class); + Mockito.when(log.logger()).thenReturn(slf4j); + try { - new FollowUser(new Step.Fake(true)).perform(com, logger); + new FollowUser(new Step.Fake(true)).perform(com, log); - Mockito.verify(logger).info( + Mockito.verify(slf4j).info( "Following Github user " + com.author() + " ..." ); - Mockito.verify(logger).error( + Mockito.verify(slf4j).error( "User follow status response is " + HttpURLConnection.HTTP_INTERNAL_ERROR + " . Should have been 204 (NO CONTENT)" @@ -136,14 +142,16 @@ public void serverIsDown() throws Exception { .thenReturn( new ApacheRequest("http://localhost:" + this.port() + "/") ); - final Logger logger = Mockito.mock(Logger.class); - - new FollowUser(new Step.Fake(true)).perform(com, logger); + final Log log = Mockito.mock(Log.class); + final Logger slf4j = Mockito.mock(Logger.class); + Mockito.when(log.logger()).thenReturn(slf4j); + + new FollowUser(new Step.Fake(true)).perform(com, log); - Mockito.verify(logger).info( + Mockito.verify(slf4j).info( "Following Github user " + com.author() + " ..." ); - Mockito.verify(logger).warn( + Mockito.verify(slf4j).warn( "IOException while trying to follow the user." ); } diff --git a/src/test/java/co/comdor/github/GithubActionTestCase.java b/src/test/java/co/comdor/github/GithubActionTestCase.java index 9bbbaa2..c652eb1 100644 --- a/src/test/java/co/comdor/github/GithubActionTestCase.java +++ b/src/test/java/co/comdor/github/GithubActionTestCase.java @@ -26,6 +26,7 @@ package co.comdor.github; import co.comdor.Action; +import co.comdor.SocialSteps; import com.google.common.collect.Lists; import com.jcabi.github.*; import com.jcabi.github.mock.MkGithub; @@ -61,9 +62,10 @@ public void actionsExecuteConcurrently() throws Exception { final Issue issue1 = this.githubIssue("amihaiemil", "@comdor hello there"); final Issue issue2 = this.githubIssue("jeff", "@comdor hello"); final Issue issue3 = this.githubIssue("vlad", "@comdor, hello"); - final Action ac1 = new GithubAction(issue1); - final Action ac2 = new GithubAction(issue2); - final Action ac3 = new GithubAction(issue3); + final SocialSteps social = Mockito.mock(SocialSteps.class); + final Action ac1 = new GithubAction(issue1, social); + final Action ac2 = new GithubAction(issue2, social); + final Action ac3 = new GithubAction(issue3, social); final ExecutorService executorService = Executors.newFixedThreadPool(5); final List futures = new ArrayList(); @@ -95,7 +97,7 @@ public void actionsExecuteConcurrently() throws Exception { MatcherAssert.assertThat( commentsWithReply3.get(1).json().getString("body"), Matchers.equalTo(expectedReply3) - ); + ); } /** @@ -118,7 +120,7 @@ public void genericErrorReplyIsSent() throws Exception { .thenReturn(comments) .thenReturn(issue.comments()); - final Action action = new GithubAction(failing); + final Action action = new GithubAction(failing, Mockito.mock(SocialSteps.class)); action.perform(); final List commentsWithReply = Lists.newArrayList(issue.comments().iterate()); diff --git a/src/test/java/co/comdor/github/GithubStepsTestCase.java b/src/test/java/co/comdor/github/GithubStepsTestCase.java index fcd2aa1..27b90c3 100644 --- a/src/test/java/co/comdor/github/GithubStepsTestCase.java +++ b/src/test/java/co/comdor/github/GithubStepsTestCase.java @@ -25,6 +25,7 @@ */ package co.comdor.github; +import co.comdor.Log; import java.io.IOException; import javax.json.Json; @@ -58,16 +59,17 @@ public void logsAndExecutesSteps() throws Exception { Mockito.when(comment.author()).thenReturn("amihaiemil"); final Step toExecute = Mockito.mock(Step.class); - final Steps steps = new GithubSteps( - toExecute, comment, new SendReply("some failure message") - ); + final Steps steps = new GithubSteps(toExecute, comment); + + final Log log = Mockito.mock(Log.class); + final Logger slf4j = Mockito.mock(Logger.class); + Mockito.when(log.logger()).thenReturn(slf4j); - final Logger logger = Mockito.mock(Logger.class); - steps.perform(logger); + steps.perform(log); - Mockito.verify(logger).info("Received command: @comdor run"); - Mockito.verify(logger).info("Author login: amihaiemil"); - Mockito.verify(toExecute).perform(comment, logger); + Mockito.verify(slf4j).info("Received command: @comdor run"); + Mockito.verify(slf4j).info("Author login: amihaiemil"); + Mockito.verify(toExecute).perform(comment, log); } /** @@ -78,20 +80,27 @@ toExecute, comment, new SendReply("some failure message") @Test public void stepsThrowIOException() throws Exception { final Mention comment = Mockito.mock(Mention.class); + Mockito.when(comment.language()).thenReturn(new English()); + Mockito.when(comment.author()).thenReturn("amihaiemil"); Mockito.when(comment.json()).thenReturn( Json.createObjectBuilder().add("body", "@comdor run").build() ); - final Logger logger = Mockito.mock(Logger.class); + final Log log = Mockito.mock(Log.class); + Mockito.when(log.logger()).thenReturn(Mockito.mock(Logger.class)); + Mockito.when(log.location()).thenReturn("/path/to/123.log"); final Step toExecute = Mockito.mock(Step.class); Mockito.doThrow( new IOException("Intented IOException") - ).when(toExecute).perform(comment, logger); - final Steps steps = new GithubSteps( - toExecute, comment, new SendReply("some failure message") - ); + ).when(toExecute).perform(comment, log); + final Steps steps = new GithubSteps(toExecute, comment); - steps.perform(logger); - Mockito.verify(comment).reply("some failure message"); + steps.perform(log); + Mockito.verify(comment).reply( + String.format( + new English().response("steps.failure.comment"), + log.location() + ) + ); } } diff --git a/src/test/java/co/comdor/github/SendReplyTestCase.java b/src/test/java/co/comdor/github/SendReplyTestCase.java index d57da05..5483fa1 100644 --- a/src/test/java/co/comdor/github/SendReplyTestCase.java +++ b/src/test/java/co/comdor/github/SendReplyTestCase.java @@ -26,12 +26,13 @@ package co.comdor.github; import co.comdor.Step; +import co.comdor.Log; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.junit.Test; import org.mockito.Mockito; -import org.slf4j.Logger; import java.io.IOException; +import org.slf4j.Logger; /** * Unit tests for {@link SendReply} @@ -52,7 +53,7 @@ public void replySentOk() throws Exception { Mockito.doNothing().when(mention).reply(message); new SendReply( message, new Step.Fake(true) - ).perform(mention, Mockito.mock(Logger.class)); + ).perform(mention, Mockito.mock(Log.class)); } /** @@ -65,10 +66,12 @@ public void replyFails() throws Exception { final String message = "hello"; Mockito.doThrow(new IOException("This is expected, it's ok!")) .when(mention).reply(message); + final Log log = Mockito.mock(Log.class); + Mockito.when(log.logger()).thenReturn(Mockito.mock(Logger.class)); try { new SendReply( message, new Step.Fake(false) - ).perform(mention, Mockito.mock(Logger.class)); + ).perform(mention, log); } catch (final IOException ex) { MatcherAssert.assertThat( ex.getMessage(), diff --git a/src/test/java/co/comdor/github/StarRepoTestCase.java b/src/test/java/co/comdor/github/StarRepoTestCase.java index 5817daa..8318320 100644 --- a/src/test/java/co/comdor/github/StarRepoTestCase.java +++ b/src/test/java/co/comdor/github/StarRepoTestCase.java @@ -26,6 +26,7 @@ package co.comdor.github; import co.comdor.Step; +import co.comdor.Log; import com.jcabi.github.Github; import com.jcabi.github.Issue; import com.jcabi.github.Stars; @@ -53,12 +54,14 @@ public final class StarRepoTestCase { */ @Test public void starsRepo() throws Exception { - final Logger logger = Mockito.mock(Logger.class); - Mockito.doNothing().when(logger).info(Mockito.anyString()); + final Log log = Mockito.mock(Log.class); + final Logger slf4j = Mockito.mock(Logger.class); + Mockito.doNothing().when(slf4j).info(Mockito.anyString()); Mockito.doThrow( new IllegalStateException("Unexpected error; test failed") - ).when(logger).error(Mockito.anyString()); - + ).when(slf4j).error(Mockito.anyString()); + Mockito.when(log.logger()).thenReturn(slf4j); + final Github gh = new MkGithub("amihaiemil"); final Repo repo = gh.repos().create( new Repos.RepoCreate("amihaiemil.github.io", false) @@ -73,7 +76,7 @@ public void starsRepo() throws Exception { com.issue().repo().stars().starred(), Matchers.is(false) ); - star.perform(com, logger); + star.perform(com, log); MatcherAssert.assertThat( com.issue().repo().stars().starred(), Matchers.is(true) @@ -86,11 +89,13 @@ public void starsRepo() throws Exception { */ @Test public void starsRepoTwice() throws Exception { - final Logger logger = Mockito.mock(Logger.class); - Mockito.doNothing().when(logger).info(Mockito.anyString()); + final Log log = Mockito.mock(Log.class); + final Logger slf4j = Mockito.mock(Logger.class); + Mockito.doNothing().when(slf4j).info(Mockito.anyString()); Mockito.doThrow( new IllegalStateException("Unexpected error; test failed") - ).when(logger).error(Mockito.anyString()); + ).when(slf4j).error(Mockito.anyString()); + Mockito.when(log.logger()).thenReturn(slf4j); final Github gh = new MkGithub("amihaiemil"); final Repo repo = gh.repos().create( @@ -106,8 +111,8 @@ public void starsRepoTwice() throws Exception { com.issue().repo().stars().starred(), Matchers.is(false) ); - star.perform(com, logger); - star.perform(com, logger); + star.perform(com, log); + star.perform(com, log); MatcherAssert.assertThat( com.issue().repo().stars().starred(), Matchers.is(true) @@ -124,8 +129,10 @@ public void starsRepoTwice() throws Exception { * @throws IOException If something goes wrong. */ public void repoStarringFails() throws IOException { - final Logger logger = Mockito.mock(Logger.class); - Mockito.doNothing().when(logger).info(Mockito.anyString()); + final Log log = Mockito.mock(Log.class); + final Logger slf4j = Mockito.mock(Logger.class); + Mockito.doNothing().when(slf4j).info(Mockito.anyString()); + Mockito.when(log.logger()).thenReturn(slf4j); final Repo repo = Mockito.mock(Repo.class); final Stars stars = Mockito.mock(Stars.class); @@ -139,8 +146,8 @@ public void repoStarringFails() throws IOException { Mockito.when(com.issue()).thenReturn(issue); final StarRepo star = new StarRepo(Mockito.mock(Step.class)); - star.perform(com, logger); - Mockito.verify(logger).warn( + star.perform(com, log); + Mockito.verify(slf4j).warn( "IOException when starring repository, could not star the repo." ); }