diff --git a/src/main/java/com/amihaiemil/charles/github/Action.java b/src/main/java/com/amihaiemil/charles/github/Action.java index 631a24c..4a39ca4 100644 --- a/src/main/java/com/amihaiemil/charles/github/Action.java +++ b/src/main/java/com/amihaiemil/charles/github/Action.java @@ -98,7 +98,7 @@ public void perform() { String commandBody = command.json().getString("body"); logger.info("Received command: " + commandBody); Steps steps = br.understand(command); - steps.perform(); + steps.perform(command, logger); } catch (IllegalArgumentException e) { logger.warn("No command found in the issue or the agent has already replied to the last command!"); } catch (IOException | IllegalStateException e) { diff --git a/src/main/java/com/amihaiemil/charles/github/AuthorOwnerCheck.java b/src/main/java/com/amihaiemil/charles/github/AuthorOwnerCheck.java index 3a7e120..6e594e7 100644 --- a/src/main/java/com/amihaiemil/charles/github/AuthorOwnerCheck.java +++ b/src/main/java/com/amihaiemil/charles/github/AuthorOwnerCheck.java @@ -39,16 +39,6 @@ */ public class AuthorOwnerCheck extends PreconditionCheckStep { - /** - * Given command; - */ - private Command com; - - /** - * Logger of the action. - */ - private Logger logger; - /** * Constructor. * @param com Command. @@ -57,12 +47,9 @@ public class AuthorOwnerCheck extends PreconditionCheckStep { * @param onFalse Step that should be performed next if the check is false. */ public AuthorOwnerCheck( - Command com, Logger logger, Step onTrue, Step onFalse ) { super(onTrue, onFalse); - this.com = com; - this.logger = logger; } /** @@ -70,17 +57,17 @@ public AuthorOwnerCheck( * @return true if the check is successful, false otherwise */ @Override - public void perform() { + public void perform(Command command, Logger logger) { logger.info("Checking ownership of the repo"); try { - String repoOwner = this.com.repo().json().getJsonObject("owner").getString("login"); - String author = this.com.authorLogin(); + String repoOwner = command.repo().json().getJsonObject("owner").getString("login"); + String author = command.authorLogin(); if(repoOwner.equals(author)) { logger.info("Commander is repo owner - OK"); - this.onTrue().perform(); + this.onTrue().perform(command, logger); } else { logger.warn("Commander is NOT repo owner"); - this.onFalse().perform(); + this.onFalse().perform(command, logger); } } catch (IOException ex) { logger.error("IOException when fetching repo owner from Github API", ex); diff --git a/src/main/java/com/amihaiemil/charles/github/Brain.java b/src/main/java/com/amihaiemil/charles/github/Brain.java index d25e943..3757623 100644 --- a/src/main/java/com/amihaiemil/charles/github/Brain.java +++ b/src/main/java/com/amihaiemil/charles/github/Brain.java @@ -92,10 +92,8 @@ public Steps understand(Command com) throws IOException { case "hello": String hello = String.format(category.language().response("hello.comment"), authorLogin); steps = new SendReply( - new TextReply(com, hello), - this.logger, - new Step.FinalStep(this.logger) - ); + new TextReply(com, hello), new Step.FinalStep() + ); break; case "indexsite": steps = this.withCommonChecks( @@ -104,7 +102,6 @@ public Steps understand(Command com) throws IOException { break; case "indexpage": steps = new PageHostedOnGithubCheck( - com, this.logger, this.withCommonChecks( com, category.language(), this.indexPageStep(com, category.language()) ), @@ -113,7 +110,6 @@ public Steps understand(Command com) throws IOException { break; case "indexsitemap": steps = new PageHostedOnGithubCheck( - com, this.logger, this.withCommonChecks( com, category.language(), this.indexSitemapStep(com, category.language()) @@ -127,14 +123,11 @@ public Steps understand(Command com) throws IOException { break; case "deleteindex": steps = new DeleteIndexCommandCheck( - com, this.logger, new IndexExistsCheck( - com.indexName(), this.logger, + com.indexName(), new AuthorOwnerCheck( - com, this.logger, this.deleteIndexStep(com, category.language()), new OrganizationAdminCheck( - com, this.logger, this.deleteIndexStep(com, category.language()), this.finalCommentStep( com, category.language(), @@ -161,14 +154,12 @@ public Steps understand(Command com) throws IOException { authorLogin); steps = new SendReply( new TextReply(com, unknown), - this.logger, - new Step.FinalStep(this.logger) + new Step.FinalStep() ); break; } return new Steps( steps, - this.logger, new SendReply( new TextReply( com, @@ -177,11 +168,7 @@ public Steps understand(Command com) throws IOException { com.authorLogin(), this.logsLoc.address() ) ), - this.logger, - new Step.FinalStep( - this.logger, - "[ERROR] Some step didn't execute properly." - ) + new Step.FinalStep("[ERROR] Some step didn't execute properly.") ) ); } @@ -202,11 +189,9 @@ public Step indexPageStep(Command com, Language lang) throws IOException { com.authorLogin(), this.logsLoc.address() ) - ), this.logger, + ), new IndexPage( - com, this.logger, new StarRepo( - com.issue().repo(), this.logger, this.finalCommentStep( com, lang, "index.finished.comment", com.authorLogin(), @@ -233,11 +218,9 @@ public Step indexSitemapStep(Command com, Language lang) throws IOException { com.authorLogin(), this.logsLoc.address() ) - ), this.logger, + ), new IndexSitemap( - com, this.logger, new StarRepo( - com.issue().repo(), this.logger, this.finalCommentStep( com, lang, "index.finished.comment", com.authorLogin(), @@ -264,11 +247,9 @@ public Step indexSiteStep(Command com, Language lang) throws IOException { com.authorLogin(), this.logsLoc.address() ) - ), this.logger, + ), new IndexSite( - com, this.logger, new StarRepo( - com.issue().repo(), this.logger, this.finalCommentStep( com, lang, "index.finished.comment", com.authorLogin(), @@ -288,7 +269,6 @@ public Step indexSiteStep(Command com, Language lang) throws IOException { */ public Step deleteIndexStep(Command com, Language lang) throws IOException { return new DeleteIndex( - com, logger, this.finalCommentStep( com, lang, "deleteindex.finished.comment", com.authorLogin(), @@ -331,22 +311,20 @@ private CommandCategory categorizeCommand(Command com) throws IOException { */ private Step withCommonChecks(Command com, Language lang, Step action) throws IOException { PreconditionCheckStep repoForkCheck = new RepoForkCheck( - com.repo().json(), this.logger, action, + com.repo().json(), action, this.finalCommentStep(com, lang, "denied.fork.comment", com.authorLogin()) ); PreconditionCheckStep authorOwnerCheck = new AuthorOwnerCheck( - com, this.logger, repoForkCheck, new OrganizationAdminCheck( - com, this.logger, repoForkCheck, this.finalCommentStep(com, lang, "denied.commander.comment", com.authorLogin()) ) ); PreconditionCheckStep repoNameCheck = new RepoNameCheck( - com.repo().json(), this.logger, authorOwnerCheck, + com.repo().json(), authorOwnerCheck, new GhPagesBranchCheck( - com, this.logger, authorOwnerCheck, + authorOwnerCheck, this.finalCommentStep(com, lang, "denied.name.comment", com.authorLogin()) ) ); @@ -370,8 +348,8 @@ private SendReply finalCommentStep( lang.response(messagekey), (Object[]) formatParts ) - ), this.logger, - new Step.FinalStep(this.logger) + ), + new Step.FinalStep() ); } diff --git a/src/main/java/com/amihaiemil/charles/github/Command.java b/src/main/java/com/amihaiemil/charles/github/Command.java index 8cdb22a..9ee0c66 100644 --- a/src/main/java/com/amihaiemil/charles/github/Command.java +++ b/src/main/java/com/amihaiemil/charles/github/Command.java @@ -171,7 +171,7 @@ public JsonObject authorOrgMembership() throws IOException { */ public CommandedRepo repo() throws IOException { if(this.repo == null) { - this.repo = new CommandedRepo(this.issue.repo());; + this.repo = new CommandedRepo(this.issue.repo()); } return this.repo; } diff --git a/src/main/java/com/amihaiemil/charles/github/DeleteIndex.java b/src/main/java/com/amihaiemil/charles/github/DeleteIndex.java index 8b63173..aec1ac6 100644 --- a/src/main/java/com/amihaiemil/charles/github/DeleteIndex.java +++ b/src/main/java/com/amihaiemil/charles/github/DeleteIndex.java @@ -40,15 +40,6 @@ */ public class DeleteIndex extends IntermediaryStep { - /** - * Command. - */ - private Command com; - - /** - * Action's logger. - */ - private Logger logger; /** * Constructor. @@ -56,23 +47,21 @@ public class DeleteIndex extends IntermediaryStep { * @param logger The action's logger * @param next The next step to take */ - public DeleteIndex(Command com, Logger logger, Step next) { + public DeleteIndex(Step next) { super(next); - this.com = com; - this.logger = logger; } @Override - public void perform() { - this.logger.info("Starting index deletion..."); + public void perform(Command command, Logger logger) { + logger.info("Starting index deletion..."); try { - new AmazonEsRepository(com.indexName()).delete(); + new AmazonEsRepository(command.indexName()).delete(); } catch (IOException e) { logger.error("Exception while deleting the index!", e); throw new IllegalStateException("Exception while deleting the index!" , e); } - this.logger.info("Index successfully deleted!"); - this.next().perform(); + logger.info("Index successfully deleted!"); + this.next().perform(command, logger); } } diff --git a/src/main/java/com/amihaiemil/charles/github/DeleteIndexCommandCheck.java b/src/main/java/com/amihaiemil/charles/github/DeleteIndexCommandCheck.java index fe27ad9..248716e 100644 --- a/src/main/java/com/amihaiemil/charles/github/DeleteIndexCommandCheck.java +++ b/src/main/java/com/amihaiemil/charles/github/DeleteIndexCommandCheck.java @@ -39,33 +39,18 @@ */ public class DeleteIndexCommandCheck extends PreconditionCheckStep { - /** - * Given command. - */ - private Command com; - - /** - * Logger. - */ - private Logger logger; - - public DeleteIndexCommandCheck( - Command com, Logger logger, - Step onTrue, Step onFalse - ) { + public DeleteIndexCommandCheck(Step onTrue, Step onFalse) { super(onTrue, onFalse); - this.com = com; - this.logger = logger; } @Override - public void perform() { + public void perform(Command command, Logger logger) { boolean passed = false; - String text = com.json().getString("body"); + String text = command.json().getString("body"); if(text.contains("`")) { String repoToBeDeleted = text.substring(text.indexOf('`') + 1, text.lastIndexOf('`')); try { - String repoName = com.repo().name(); + String repoName = command.repo().name(); passed = repoName.equals(repoToBeDeleted); } catch (IOException e) { logger.error("Exception when getting repo's name", e); @@ -73,9 +58,9 @@ public void perform() { } } if(passed) { - this.onTrue().perform(); + this.onTrue().perform(command, logger); } else { - this.onFalse().perform(); + this.onFalse().perform(command, logger); } } diff --git a/src/main/java/com/amihaiemil/charles/github/GhPagesBranchCheck.java b/src/main/java/com/amihaiemil/charles/github/GhPagesBranchCheck.java index 464b5ed..4842101 100644 --- a/src/main/java/com/amihaiemil/charles/github/GhPagesBranchCheck.java +++ b/src/main/java/com/amihaiemil/charles/github/GhPagesBranchCheck.java @@ -39,16 +39,6 @@ */ public class GhPagesBranchCheck extends PreconditionCheckStep { - /** - * Given command. - */ - private Command com; - - /** - * Logger. - */ - private Logger logger; - /** * Constructor. * @param com Given command. @@ -56,29 +46,24 @@ public class GhPagesBranchCheck extends PreconditionCheckStep { * @param onTrue Step that should be performed next if the check is true. * @param onFalse Step that should be performed next if the check is false. */ - public GhPagesBranchCheck( - Command com, Logger logger, - Step onTrue, Step onFalse - ) { + public GhPagesBranchCheck(Step onTrue, Step onFalse) { super(onTrue, onFalse); - this.com = com; - this.logger = logger; } /** * Perform this step. */ @Override - public void perform() { + public void perform(Command command, Logger logger) { logger.info("Checking whether the repository has a gh-pages branch..."); try { - if (com.repo().hasGhPagesBranch()) { + if (command.repo().hasGhPagesBranch()) { logger.info("The repo has a gh-pages branch - OK!"); - this.onTrue().perform(); + this.onTrue().perform(command, logger); } else { logger.info("The repo does NOT have a gh-pages branch"); - this.onFalse().perform(); + this.onFalse().perform(command, logger); } } catch (IOException e) { logger.error("Exception when checking if gh-pages branch exists", e); diff --git a/src/main/java/com/amihaiemil/charles/github/IndexExistsCheck.java b/src/main/java/com/amihaiemil/charles/github/IndexExistsCheck.java index 0a42e9a..f08f982 100644 --- a/src/main/java/com/amihaiemil/charles/github/IndexExistsCheck.java +++ b/src/main/java/com/amihaiemil/charles/github/IndexExistsCheck.java @@ -43,51 +43,36 @@ public final class IndexExistsCheck extends PreconditionCheckStep { */ private AwsEsRepository repo; - /** - * Action's logger. - */ - private Logger logger; - /** * Constructor. - * @param index Index name - * @param logger The action's logger * @param onTrue The step to perform on successful check. * @param onFalse the step to perform in unsuccessful check. */ - public IndexExistsCheck( - String index, Logger logger, - Step onTrue, Step onFalse - ) { - this(new AmazonEsRepository(index), logger, onTrue, onFalse); + public IndexExistsCheck(String index, Step onTrue, Step onFalse) { + this(new AmazonEsRepository(index), onTrue, onFalse); } /** * Constructor. * @param repo AWS repository to look into. - * @param logger The action's logger * @param onTrue The step to perform on successful check. * @param onFalse The step to perform in unsuccessful check. */ - public IndexExistsCheck( - AwsEsRepository repo, Logger logger, - Step onTrue, Step onFalse - ) { + public IndexExistsCheck(AwsEsRepository repo, Step onTrue, Step onFalse) { super(onTrue, onFalse); this.repo = repo; - this.logger = logger; } @Override - public void perform() { - this.logger.info("Checking if required index exists..."); + public void perform(Command command, Logger logger) { + logger.info("Checking if required index exists..."); boolean exists = this.repo.exists(); if(exists) { - this.logger.info("Index exists - Ok!"); - this.onTrue().perform(); + logger.info("Index exists - Ok!"); + this.onTrue().perform(command, logger); } else { - this.logger.warn("The required index does not exist! It may have been deleted already."); - this.onFalse().perform(); + logger.warn("The required index does not exist! It may have been deleted already."); + this.onFalse().perform(command, logger); } } diff --git a/src/main/java/com/amihaiemil/charles/github/IndexPage.java b/src/main/java/com/amihaiemil/charles/github/IndexPage.java index c483b39..0ef696e 100644 --- a/src/main/java/com/amihaiemil/charles/github/IndexPage.java +++ b/src/main/java/com/amihaiemil/charles/github/IndexPage.java @@ -46,31 +46,17 @@ */ public class IndexPage extends IndexStep { - /** - * Action's logger. - */ - private Logger logger; - - /** - * Command. - */ - private Command com; - /** * Ctor. - * @param com Given command. - * @param logger Logger. * @param next Next step to take. */ - public IndexPage(Command com, Logger logger, Step next) { + public IndexPage(Step next) { super(next); - this.com = com; - this.logger = logger; } @Override - public void perform() { - String link = this.getLink(); + public void perform(Command command, Logger logger) { + String link = this.getLink(command); logger.info("Indexing page " + link + " ..."); try { logger.info("Crawling the page..."); @@ -78,7 +64,7 @@ public void perform() { driver.get(link); WebPage snapshot = new SnapshotWebPage(new LiveWebPage(driver)); logger.info("Page crawled. Sending to aws..."); - new AmazonEsRepository(this.com.indexName()).export( + new AmazonEsRepository(command.indexName()).export( Arrays.asList(snapshot) ); logger.info("Page successfully sent to aws!"); @@ -90,7 +76,7 @@ public void perform() { "Exception while indexing the page" + link, e ); } - this.next().perform(); + this.next().perform(command, logger); } /** @@ -98,8 +84,8 @@ public void perform() { * link like [this](http://link.com/here/the/ling) . * @return String link. */ - private String getLink() { - String body = this.com.json().getString("body"); + private String getLink(Command command) { + String body = command.json().getString("body"); return body.substring(body.indexOf('(') + 1, body.indexOf(')')); } diff --git a/src/main/java/com/amihaiemil/charles/github/IndexSite.java b/src/main/java/com/amihaiemil/charles/github/IndexSite.java index d7302d7..b8dc84e 100644 --- a/src/main/java/com/amihaiemil/charles/github/IndexSite.java +++ b/src/main/java/com/amihaiemil/charles/github/IndexSite.java @@ -46,33 +46,21 @@ */ public class IndexSite extends IndexStep { - /** - * Command. - */ - private Command com; - - /** - * Action's logger. - */ - private Logger logger; - /** * Constructor. * @param com Command * @param logger The action's logger * @param next The next step to take */ - public IndexSite(Command com, Logger logger, Step next) { + public IndexSite(Step next) { super(next); - this.com = com; - this.logger = logger; } @Override - public void perform() { + public void perform(Command command, Logger logger) { try { logger.info("Starting to index the whole site..."); - this.graphCrawl().crawl(); + this.graphCrawl(command, logger).crawl(); logger.info("Indexing finished successfully!"); } catch ( DataExportException | @@ -82,7 +70,7 @@ public void perform() { logger.error("Exception while indexing the website!", e); throw new IllegalStateException("Exception while indexing the website", e); } - this.next().perform(); + this.next().perform(command, logger); } /** @@ -90,11 +78,11 @@ public void perform() { * @return RetriableWebCrawl * @throws IOException */ - public WebCrawl graphCrawl() throws IOException { - String repoName = this.com.repo().name(); + public WebCrawl graphCrawl(Command command, Logger logger) throws IOException { + String repoName = command.repo().name(); String siteIndexUrl; - if(this.com.repo().hasGhPagesBranch()) { - siteIndexUrl = "http://" + this.com.repo().ownerLogin() + ".github.io/" + repoName; + if(command.repo().hasGhPagesBranch()) { + siteIndexUrl = "http://" + command.repo().ownerLogin() + ".github.io/" + repoName; } else { siteIndexUrl = "http://" + repoName; } @@ -102,7 +90,7 @@ public WebCrawl graphCrawl() throws IOException { + " .The website will be crawled as a graph, going in-depth from the index page."); WebCrawl siteCrawl = new GraphCrawl( siteIndexUrl, this.phantomJsDriver(), new IgnoredPatterns(), - new AmazonEsRepository(this.com.indexName()), 20 + new AmazonEsRepository(command.indexName()), 20 ); return new RetriableCrawl(siteCrawl, 5); } diff --git a/src/main/java/com/amihaiemil/charles/github/IndexSitemap.java b/src/main/java/com/amihaiemil/charles/github/IndexSitemap.java index 53543f5..8ec0e07 100644 --- a/src/main/java/com/amihaiemil/charles/github/IndexSitemap.java +++ b/src/main/java/com/amihaiemil/charles/github/IndexSitemap.java @@ -43,38 +43,24 @@ */ public class IndexSitemap extends IndexStep { - /** - * Command. - */ - private Command com; - - /** - * Action's logger. - */ - private Logger logger; - /** * Constructor. - * @param com Command - * @param logger The action's logger * @param next The next step to take */ - public IndexSitemap(Command com, Logger logger, Step next) { + public IndexSitemap(Step next) { super(next); - this.com = com; - this.logger = logger; } @Override - public void perform() { - String link = this.getLink(); + public void perform(Command command, Logger logger) { + String link = this.getLink(command); try { logger.info("Indexing sitemap " + link + " ..."); WebCrawl sitemap = new RetriableCrawl( new SitemapXmlCrawl( this.phantomJsDriver(), new SitemapXmlOnline(link), - new AmazonEsRepository(this.com.indexName()), + new AmazonEsRepository(command.indexName()), 20 ), 5 @@ -89,7 +75,7 @@ public void perform() { "Exception while indexing the page" + link, e ); } - this.next().perform(); + this.next().perform(command, logger); } /** @@ -98,8 +84,8 @@ public void perform() { * [this](http://link.com/here/the/ling) . * @return String link. */ - private String getLink() { - String body = this.com.json().getString("body"); + private String getLink(Command command) { + String body = command.json().getString("body"); return body.substring(body.indexOf('(') + 1, body.indexOf(')')); } } diff --git a/src/main/java/com/amihaiemil/charles/github/IntermediaryStep.java b/src/main/java/com/amihaiemil/charles/github/IntermediaryStep.java index 307e5f2..ca61863 100644 --- a/src/main/java/com/amihaiemil/charles/github/IntermediaryStep.java +++ b/src/main/java/com/amihaiemil/charles/github/IntermediaryStep.java @@ -48,8 +48,6 @@ public IntermediaryStep(Step next) { this.nextStep = next; } - public abstract void perform(); - /** * Get the next step to perform. * @return Step. diff --git a/src/main/java/com/amihaiemil/charles/github/OrganizationAdminCheck.java b/src/main/java/com/amihaiemil/charles/github/OrganizationAdminCheck.java index 58cc4df..021241f 100644 --- a/src/main/java/com/amihaiemil/charles/github/OrganizationAdminCheck.java +++ b/src/main/java/com/amihaiemil/charles/github/OrganizationAdminCheck.java @@ -40,47 +40,29 @@ * */ public class OrganizationAdminCheck extends PreconditionCheckStep { - - /** - * The command. - */ - private Command com; - - /** - * Logger of the action. - */ - private Logger logger; - /** * Constructor. - * @param com Command. - * @param logger Action logger. * @param onTrue Step that should be performed next if the check is true. * @param onFalse Step that should be performed next if the check is false. */ - public OrganizationAdminCheck( - Command com, Logger logger, - Step onTrue, Step onFalse - ) { + public OrganizationAdminCheck(Step onTrue, Step onFalse) { super(onTrue, onFalse); - this.com = com; - this.logger = logger; } @Override - public void perform() { + public void perform(Command command, Logger logger) { try { logger.info("Checking if the author is an active admin of the organization ..."); - JsonObject membership = com.authorOrgMembership(); + JsonObject membership = command.authorOrgMembership(); String state = membership.getString("state", "statenotfound"); String role = membership.getString("role", "adminnotfound"); boolean passed = state.equals("active") && role.equals("admin"); if(!passed) { logger.warn("The author is NOT an active admin: [state: " + state + ", role: " + role +"]"); - this.onFalse().perform(); + this.onFalse().perform(command, logger); } else { logger.info("The author is an active admin, ok"); - this.onTrue().perform(); + this.onTrue().perform(command, logger); } } catch (IOException e) { logger.error("IOException when getting the organization membership!", e); diff --git a/src/main/java/com/amihaiemil/charles/github/PageHostedOnGithubCheck.java b/src/main/java/com/amihaiemil/charles/github/PageHostedOnGithubCheck.java index ba51104..a54ac27 100644 --- a/src/main/java/com/amihaiemil/charles/github/PageHostedOnGithubCheck.java +++ b/src/main/java/com/amihaiemil/charles/github/PageHostedOnGithubCheck.java @@ -38,49 +38,26 @@ */ public class PageHostedOnGithubCheck extends PreconditionCheckStep { - /** - * Command. - */ - private Command com; - - /** - * Link to the page. - */ - private String link; - - /** - * Action's logger; - */ - private Logger logger; - /** * Ctor - * @param com Command. - * @param link Page link. - * @param logger Logger. * @param onTrue Step that should be performed next if the check is true. * @param onFalse Step that should be performed next if the check is false. */ - public PageHostedOnGithubCheck( - Command com, Logger logger, - Step onTrue, Step onFalse - ) { + public PageHostedOnGithubCheck(Step onTrue, Step onFalse) { super(onTrue, onFalse); - this.com = com; - String comment = com.json().getString("body"); - this.link = comment.substring(comment.indexOf('(') + 1, comment.indexOf(')')); - this.logger = logger; } @Override - public void perform() { + public void perform(Command command, Logger logger) { try { - CommandedRepo repo = com.repo(); + String comment = command.json().getString("body"); + String link = comment.substring(comment.indexOf('(') + 1, comment.indexOf(')')); + CommandedRepo repo = command.repo(); String owner = repo.ownerLogin(); String expDomain; logger.info("Checking if the page belongs to the repo " + owner + "/" + repo.name()); - logger.info("Page link: " + this.link); - boolean ghPagesBranch = com.repo().hasGhPagesBranch(); + logger.info("Page link: " + link); + boolean ghPagesBranch = command.repo().hasGhPagesBranch(); if (ghPagesBranch) { expDomain = owner + ".github.io/" + repo.name(); logger.info("The repo has a gh-pages branch so the page link has to start with " + expDomain); @@ -88,13 +65,13 @@ public void perform() { expDomain = owner + ".github.io"; logger.info("The repo is a website repo so the page link has to start with " + expDomain); } - boolean passed = this.link.startsWith("http://" + expDomain) || this.link.startsWith("https://" + expDomain); + boolean passed = link.startsWith("http://" + expDomain) || link.startsWith("https://" + expDomain); if(!passed) { logger.warn("The given webpage is NOT part of this repository!"); - this.onFalse().perform(); + this.onFalse().perform(command, logger); } else { logger.info("The given webpage is part of this repository - Ok!"); - this.onTrue().perform(); + this.onTrue().perform(command, logger); } } catch (IOException ex) { logger.error("IOException when calling the Github API", ex); diff --git a/src/main/java/com/amihaiemil/charles/github/PreconditionCheckStep.java b/src/main/java/com/amihaiemil/charles/github/PreconditionCheckStep.java index 0c96cf5..052a15f 100644 --- a/src/main/java/com/amihaiemil/charles/github/PreconditionCheckStep.java +++ b/src/main/java/com/amihaiemil/charles/github/PreconditionCheckStep.java @@ -54,8 +54,6 @@ public PreconditionCheckStep(Step onTrue, Step onFalse) { this.onTrue = onTrue; this.onFalse = onFalse; } - - public abstract void perform(); /** * Step to perform on successful check diff --git a/src/main/java/com/amihaiemil/charles/github/RepoForkCheck.java b/src/main/java/com/amihaiemil/charles/github/RepoForkCheck.java index 13fb74a..8bf351c 100644 --- a/src/main/java/com/amihaiemil/charles/github/RepoForkCheck.java +++ b/src/main/java/com/amihaiemil/charles/github/RepoForkCheck.java @@ -43,24 +43,17 @@ public class RepoForkCheck extends PreconditionCheckStep { */ private JsonObject repo; - /** - * Logger. - */ - private Logger logger; - /** * Constructor. * @param repo Given repository json. - * @param logger Action logger. * @param onTrue Step that should be performed next if the check is true. * @param onFalse Step that should be performed next if the check is false. */ public RepoForkCheck( - JsonObject repo, Logger logger, Step onTrue, Step onFalse + JsonObject repo, Step onTrue, Step onFalse ) { super(onTrue, onFalse); this.repo = repo; - this.logger = logger; } /** @@ -68,15 +61,15 @@ public RepoForkCheck( * @returns true if the repo is NOT a fork, false otherwise. */ @Override - public void perform() { + public void perform(Command command, Logger logger) { logger.info("Checking whether the repository is a fork..."); - boolean fork = repo.getBoolean("fork"); + boolean fork = this.repo.getBoolean("fork"); if(fork) { logger.warn("Repository should NOT be a fork!"); - this.onFalse().perform(); + this.onFalse().perform(command, logger); } else { logger.info("Repository is not a fork - Ok!"); - this.onTrue().perform(); + this.onTrue().perform(command, logger); } } } diff --git a/src/main/java/com/amihaiemil/charles/github/RepoNameCheck.java b/src/main/java/com/amihaiemil/charles/github/RepoNameCheck.java index 0908b1a..423641e 100644 --- a/src/main/java/com/amihaiemil/charles/github/RepoNameCheck.java +++ b/src/main/java/com/amihaiemil/charles/github/RepoNameCheck.java @@ -44,23 +44,15 @@ public class RepoNameCheck extends PreconditionCheckStep { */ private JsonObject repo; - /** - * Action logger. - */ - private Logger logger; - - /** * Constructor. * @param repo Json repo. - * @param message For the commander in case this check fails. * @param onTrue Step that should be performed next if the check is true. * @param onFalse Step that should be performed next if the check is false. */ - public RepoNameCheck(JsonObject repo, Logger logger, Step onTrue, Step onFalse) { + public RepoNameCheck(JsonObject repo, Step onTrue, Step onFalse) { super(onTrue, onFalse); this.repo = repo; - this.logger = logger; } /** @@ -68,7 +60,7 @@ public RepoNameCheck(JsonObject repo, Logger logger, Step onTrue, Step onFalse) * @return true if the check is successful, false otherwise */ @Override - public void perform() { + public void perform(Command command, Logger logger) { logger.info("Checking repository name... "); String owner = this.repo.getJsonObject("owner").getString("login"); String expectedName = owner + ".github.io"; @@ -76,11 +68,11 @@ public void perform() { String name = this.repo.getString("name"); logger.info("Actual name: " + name); if(expectedName.equals(name)) { - logger.info("Repository name matchers - Ok"); - this.onTrue().perform(); + logger.info("Repository name matches - Ok"); + this.onTrue().perform(command, logger); } else { logger.warn("Repository name does not match the expected name"); - this.onFalse().perform(); + this.onFalse().perform(command, logger); } } } diff --git a/src/main/java/com/amihaiemil/charles/github/SendEmail.java b/src/main/java/com/amihaiemil/charles/github/SendEmail.java index 4b5a9cb..2edabfc 100644 --- a/src/main/java/com/amihaiemil/charles/github/SendEmail.java +++ b/src/main/java/com/amihaiemil/charles/github/SendEmail.java @@ -60,11 +60,6 @@ public class SendEmail extends IntermediaryStep { */ private Envelope env; - /** - * Logger. - */ - private Logger logger; - /** * Constructor. * @param to Recipient of this mail. @@ -79,7 +74,7 @@ public SendEmail(String to, String subject, String message) { new Step() { @Override - public void perform() { + public void perform(Command command, Logger logger) { //does nothing } } @@ -126,7 +121,6 @@ public SendEmail( .with(new StSubject(subject)) .with(new EnPlain(message)); } - this.logger = logger; } /** @@ -140,7 +134,7 @@ public SendEmail(Postman postman, Envelope env) { postman, env, LoggerFactory.getLogger(SendEmail.class), new Step() { @Override - public void perform() { + public void perform(Command command, Logger logger) { //does nothing next } } @@ -161,14 +155,13 @@ public SendEmail( super(next); this.postman = postman; this.env = env; - this.logger = logger; } /** * Send the email. */ @Override - public void perform() { + public void perform(Command command, Logger logger) { logger.info("Sending e-mail..."); if(this.postman == null) { logger.warn("Uninitialized postman (username and/or password missing). Cannot send email!"); @@ -177,7 +170,7 @@ public void perform() { try { this.postman.send(env); logger.info("E-mail sent successfully!"); - this.next().perform(); + this.next().perform(command, logger); } catch (IOException e) { logger.error("Error when sending the email " + e.getMessage(), e); throw new IllegalStateException(e); diff --git a/src/main/java/com/amihaiemil/charles/github/SendReply.java b/src/main/java/com/amihaiemil/charles/github/SendReply.java index 0c989ab..0f990b5 100644 --- a/src/main/java/com/amihaiemil/charles/github/SendReply.java +++ b/src/main/java/com/amihaiemil/charles/github/SendReply.java @@ -44,27 +44,20 @@ public class SendReply extends IntermediaryStep { */ private Reply rep; - /** - * Logger of the action. - */ - private Logger logger; - /** * Constructor * @param rep The reply to be sent. - * @param logger Action's logger. * @param next The next step to perform. */ public SendReply( - Reply rep, Logger logger, Step next + Reply rep, Step next ) { super(next); this.rep = rep; - this.logger = logger; } @Override - public void perform() { + public void perform(Command command, Logger logger) { try { logger.info("Sending comment..."); rep.send(); @@ -73,7 +66,7 @@ public void perform() { logger.error("IOException when sending the reply!", e); throw new IllegalStateException("IOException when sending the reply!" , e); } - this.next().perform(); + this.next().perform(command, logger); } } diff --git a/src/main/java/com/amihaiemil/charles/github/StarRepo.java b/src/main/java/com/amihaiemil/charles/github/StarRepo.java index 397bdac..761c226 100644 --- a/src/main/java/com/amihaiemil/charles/github/StarRepo.java +++ b/src/main/java/com/amihaiemil/charles/github/StarRepo.java @@ -40,45 +40,32 @@ * */ public class StarRepo extends IntermediaryStep { - - /** - * Repository to be starred. - */ - private Repo repo; - - /** - * Action logger. - */ - private Logger logger; /** * Constructor. - * @param repo Given repo. - * @param logger Action's logger. * @param next Next step to perform. */ - public StarRepo(Repo repo, Logger logger, Step next) { + public StarRepo(Step next) { super(next); - this.repo = repo; - this.logger = logger; } /** * Star the repository. * @return Always returns true, since it's not a critical step. */ - public void perform() { + public void perform(Command command, Logger logger) { try { - this.logger.info("Starring repository..."); - if(!this.repo.stars().starred()) { - this.repo.stars().star(); + logger.info("Starring repository..."); + Repo repo = command.issue().repo(); + if(!repo.stars().starred()) { + repo.stars().star(); } - this.logger.info("Repository starred!"); + logger.info("Repository starred!"); } catch (IOException e) { - this.logger.error("Error when starring repository: " + e.getMessage(), e); + logger.error("Error when starring repository: " + e.getMessage(), e); //We do not throw IllegalStateException here since starring the repo is not //a critical matter } - this.next().perform(); + this.next().perform(command, logger); } } diff --git a/src/main/java/com/amihaiemil/charles/github/Step.java b/src/main/java/com/amihaiemil/charles/github/Step.java index 7deb7a4..b3cd401 100644 --- a/src/main/java/com/amihaiemil/charles/github/Step.java +++ b/src/main/java/com/amihaiemil/charles/github/Step.java @@ -37,7 +37,13 @@ * */ public interface Step { - void perform(); + + /** + * Perform this step. + * @param command Command that triggered the action. + * @param logger The Action's logger. + */ + void perform(Command command, Logger logger); /** * Final step of the action. Just logs a line @@ -45,11 +51,6 @@ public interface Step { */ public final static class FinalStep implements Step { - /** - * Action's logger. - */ - private Logger logger; - /** * Message to log */ @@ -57,10 +58,9 @@ public final static class FinalStep implements Step { /** * Ctor without message for successful ending. - * @param logger Logger */ - public FinalStep(Logger logger) { - this(logger, "Finished action successfully!"); + public FinalStep() { + this("Finished action successfully!"); } /** @@ -68,13 +68,12 @@ public FinalStep(Logger logger) { * @param logger Logger. * @param message Message to log at the end of the action. */ - public FinalStep(Logger logger, String message) { - this.logger = logger; + public FinalStep(String message) { this.message = message; } - public void perform() { - this.logger.info(message); + public void perform(Command command, Logger logger) { + logger.info(message); } } @@ -97,7 +96,7 @@ public Fake(boolean pass) { } @Override - public void perform() { + public void perform(Command command, Logger logger) { if(!pass) { throw new IllegalStateException("Step should not have been executed!"); } diff --git a/src/main/java/com/amihaiemil/charles/github/Steps.java b/src/main/java/com/amihaiemil/charles/github/Steps.java index 314d466..66f9cb4 100644 --- a/src/main/java/com/amihaiemil/charles/github/Steps.java +++ b/src/main/java/com/amihaiemil/charles/github/Steps.java @@ -46,21 +46,14 @@ public class Steps implements Step { * Message to send in case some step fails. */ private SendReply failureMessage; - - /** - * Action's logger. - */ - private Logger logger; /** * Constructor. * @param steps Given steps. - * @param log Given logger. * @param fm failure message in case any step fails. */ - public Steps(Step steps, Logger log, SendReply fm) { + public Steps(Step steps, SendReply fm) { this.steps = steps; - this.logger = log; this.failureMessage = fm; } @@ -76,12 +69,12 @@ public Step getStepsToPerform() { * Perform all the given steps. */ @Override - public void perform() { + public void perform(Command command, Logger logger) { try { - this.steps.perform(); + this.steps.perform(command, logger); } catch (RuntimeException ex) { logger.error("A runtime exception occured", ex); - this.failureMessage.perform(); + this.failureMessage.perform(command, logger); } } diff --git a/src/test/java/com/amihaiemil/charles/github/AuthorOwnerCheckTestCase.java b/src/test/java/com/amihaiemil/charles/github/AuthorOwnerCheckTestCase.java index 70f8a1f..eecac9e 100644 --- a/src/test/java/com/amihaiemil/charles/github/AuthorOwnerCheckTestCase.java +++ b/src/test/java/com/amihaiemil/charles/github/AuthorOwnerCheckTestCase.java @@ -51,9 +51,9 @@ public class AuthorOwnerCheckTestCase { public void authorIsRepoOwner() throws Exception { Command com = this.mockCommand("amihaiemil", "amihaiemil", 0); AuthorOwnerCheck aoc = new AuthorOwnerCheck( - com, Mockito.mock(Logger.class), new Step.Fake(true), new Step.Fake(false) + new Step.Fake(true), new Step.Fake(false) ); - aoc.perform(); + aoc.perform(com, Mockito.mock(Logger.class)); } /** @@ -64,9 +64,9 @@ public void authorIsRepoOwner() throws Exception { public void authorIsNotRepoOwner() throws Exception { Command com = this.mockCommand("someone", "amihaiemil", 0); AuthorOwnerCheck aoc = new AuthorOwnerCheck( - com, Mockito.mock(Logger.class), new Step.Fake(false), new Step.Fake(true) + new Step.Fake(false), new Step.Fake(true) ); - aoc.perform(); + aoc.perform(com, Mockito.mock(Logger.class)); } /** diff --git a/src/test/java/com/amihaiemil/charles/github/DeleteIndexCommandCheckTestCase.java b/src/test/java/com/amihaiemil/charles/github/DeleteIndexCommandCheckTestCase.java index ad1484a..90042bb 100644 --- a/src/test/java/com/amihaiemil/charles/github/DeleteIndexCommandCheckTestCase.java +++ b/src/test/java/com/amihaiemil/charles/github/DeleteIndexCommandCheckTestCase.java @@ -56,9 +56,9 @@ public void repoNameEqualsActual() throws Exception { Mockito.when(com.repo()).thenReturn(crepo); DeleteIndexCommandCheck dc = new DeleteIndexCommandCheck( - com, Mockito.mock(Logger.class), new Step.Fake(true), new Step.Fake(false) + new Step.Fake(true), new Step.Fake(false) ); - dc.perform(); + dc.perform(com, Mockito.mock(Logger.class)); } /** @@ -78,9 +78,9 @@ public void repoNameNotEqualsActual() throws Exception { Mockito.when(com.repo()).thenReturn(crepo); DeleteIndexCommandCheck dc = new DeleteIndexCommandCheck( - com, Mockito.mock(Logger.class), new Step.Fake(false), new Step.Fake(true) + new Step.Fake(false), new Step.Fake(true) ); - dc.perform(); + dc.perform(com, Mockito.mock(Logger.class)); } /** @@ -100,13 +100,13 @@ public void missingBackApostrophes() throws Exception { Mockito.when(com.repo()).thenReturn(crepo); Step onTrue = Mockito.mock(Step.class); - Mockito.doThrow(new IllegalStateException("This step should not have been executed!")).when(onTrue).perform(); + Mockito.doThrow(new IllegalStateException("This step should not have been executed!")).when(onTrue).perform(Mockito.any(Command.class), Mockito.any(Logger.class)); Step onFalse = Mockito.mock(Step.class); - Mockito.doNothing().when(onFalse).perform(); + Mockito.doNothing().when(onFalse).perform(Mockito.any(Command.class), Mockito.any(Logger.class)); DeleteIndexCommandCheck dc = new DeleteIndexCommandCheck( - com, Mockito.mock(Logger.class), onTrue, onFalse + onTrue, onFalse ); - dc.perform(); + dc.perform(com, Mockito.mock(Logger.class)); } } diff --git a/src/test/java/com/amihaiemil/charles/github/GhPagesBranchCheckTestCase.java b/src/test/java/com/amihaiemil/charles/github/GhPagesBranchCheckTestCase.java index f617e9c..233f607 100644 --- a/src/test/java/com/amihaiemil/charles/github/GhPagesBranchCheckTestCase.java +++ b/src/test/java/com/amihaiemil/charles/github/GhPagesBranchCheckTestCase.java @@ -50,9 +50,9 @@ public void ghpagesBranchExists() throws Exception { Mockito.when(com.repo()).thenReturn(crepo); GhPagesBranchCheck gpc = new GhPagesBranchCheck( - com, Mockito.mock(Logger.class), new Step.Fake(true), new Step.Fake(false) + new Step.Fake(true), new Step.Fake(false) ); - gpc.perform(); + gpc.perform(com, Mockito.mock(Logger.class)); } /** @@ -67,9 +67,9 @@ public void ghpagesBranchDoesntExist() throws Exception { Mockito.when(com.repo()).thenReturn(crepo); GhPagesBranchCheck gpc = new GhPagesBranchCheck( - com, Mockito.mock(Logger.class), new Step.Fake(false), new Step.Fake(true) + new Step.Fake(false), new Step.Fake(true) ); - gpc.perform(); + gpc.perform(com, Mockito.mock(Logger.class)); } } diff --git a/src/test/java/com/amihaiemil/charles/github/IndexExistsCheckTestCase.java b/src/test/java/com/amihaiemil/charles/github/IndexExistsCheckTestCase.java index 9db7b0a..eca5b46 100644 --- a/src/test/java/com/amihaiemil/charles/github/IndexExistsCheckTestCase.java +++ b/src/test/java/com/amihaiemil/charles/github/IndexExistsCheckTestCase.java @@ -20,10 +20,9 @@ public final class IndexExistsCheckTestCase { public void indexExists() { IndexExistsCheck iec = new IndexExistsCheck( new AwsEsRepository.Fake(true), - Mockito.mock(Logger.class), new Step.Fake(true), new Step.Fake(false) ); - iec.perform(); + iec.perform(Mockito.mock(Command.class), Mockito.mock(Logger.class)); } /** @@ -33,9 +32,8 @@ public void indexExists() { public void indexDoesntExist() { IndexExistsCheck iec = new IndexExistsCheck( new AwsEsRepository.Fake(false), - Mockito.mock(Logger.class), new Step.Fake(false), new Step.Fake(true) ); - iec.perform(); + iec.perform(Mockito.mock(Command.class), Mockito.mock(Logger.class)); } } diff --git a/src/test/java/com/amihaiemil/charles/github/IndexPageaActionTestCase.java b/src/test/java/com/amihaiemil/charles/github/IndexPageaActionTestCase.java index ba87b4f..adf569a 100644 --- a/src/test/java/com/amihaiemil/charles/github/IndexPageaActionTestCase.java +++ b/src/test/java/com/amihaiemil/charles/github/IndexPageaActionTestCase.java @@ -27,7 +27,6 @@ import static org.junit.Assert.assertTrue; import java.io.IOException; -import java.util.Arrays; import java.util.List; import javax.json.Json; @@ -77,13 +76,12 @@ public void indexPageChecksAreSuccessfulOnBlogRepo() throws Exception { Mockito.doReturn(//replace the index step with a simple comment; we're just interested in the checks here, not the index step itself new SendReply( new TextReply(com, "index-page checks passed!"), - Mockito.mock(Logger.class), - new Step.FinalStep(Mockito.mock(Logger.class)) + new Step.FinalStep() ) ).when(spiedBrain).indexPageStep(com, eng); Step steps = spiedBrain.understand(com); - steps.perform(); + steps.perform(com, Mockito.mock(Logger.class)); List comments = Lists.newArrayList(com.issue().comments().iterate()); assertTrue(comments.size() == 2); @@ -119,21 +117,20 @@ public void indexPageChecksAreSuccessfulOnGhPagesRepo() throws Exception { Brain spiedBrain = Mockito.spy(br); Mockito.doReturn(//replace the index step with a simple comment; we're just interested in the checks here, not the index step itself new SendReply( - new TextReply(com, "index-page checks passed!"), - Mockito.mock(Logger.class), - new Step.FinalStep(Mockito.mock(Logger.class)) - ) + new TextReply(com, "index-page checks passed!"), + new Step.FinalStep() + ) ).when(spiedBrain).indexPageStep(com, eng); Step steps = spiedBrain.understand(com); - steps.perform(); + steps.perform(com, Mockito.mock(Logger.class)); List comments = Lists.newArrayList(com.issue().comments().iterate()); assertTrue(comments.size() == 2); assertTrue( - comments.get(0).json().getString("body").startsWith( - "@charlesmike index [this](" - ) + comments.get(0).json().getString("body").startsWith( + "@charlesmike index [this](" + ) ); assertTrue( comments.get(1).json().getString("body").endsWith( @@ -165,8 +162,7 @@ public void indexPageChecksAreSuccessfulOnGhPagesRepoUnderOrg() throws Exception Mockito.doReturn(//replace the index step with a simple comment; we're just interested in the checks here, not the index step itself new SendReply( new TextReply(com, "index-page checks passed!"), - Mockito.mock(Logger.class), - new Step.FinalStep(Mockito.mock(Logger.class)) + new Step.FinalStep() ) ).when(spiedBrain).indexPageStep(com, eng); @@ -177,18 +173,16 @@ public void indexPageChecksAreSuccessfulOnGhPagesRepoUnderOrg() throws Exception .build() ); - Step steps = spiedBrain.understand(com); - steps.perform(); + steps.perform(com, Mockito.mock(Logger.class)); List comments = Lists.newArrayList(com.issue().comments().iterate()); assertTrue(comments.size() == 2); assertTrue( - comments.get(0).json().getString("body").startsWith( - "@charlesmike index [this](" - ) + comments.get(0).json().getString("body").startsWith( + "@charlesmike index [this](" + ) ); - System.out.println( comments.get(1).json().getString("body")); assertTrue( comments.get(1).json().getString("body").endsWith( "\n\nindex-page checks passed!" @@ -219,8 +213,7 @@ public void indexPageChecksAreSuccessfulOnBlogRepoUnderOrg() throws Exception { Mockito.doReturn(//replace the index step with a simple comment; we're just interested in the checks here, not the index step itself new SendReply( new TextReply(com, "index-page checks passed!"), - Mockito.mock(Logger.class), - new Step.FinalStep(Mockito.mock(Logger.class)) + new Step.FinalStep() ) ).when(spiedBrain).indexPageStep(com, eng); @@ -233,14 +226,14 @@ public void indexPageChecksAreSuccessfulOnBlogRepoUnderOrg() throws Exception { Step steps = spiedBrain.understand(com); - steps.perform(); + steps.perform(com, Mockito.mock(Logger.class)); List comments = Lists.newArrayList(com.issue().comments().iterate()); assertTrue(comments.size() == 2); assertTrue( - comments.get(0).json().getString("body").startsWith( - "@charlesmike index [this](" - ) + comments.get(0).json().getString("body").startsWith( + "@charlesmike index [this](" + ) ); assertTrue( comments.get(1).json().getString("body").endsWith( @@ -260,6 +253,7 @@ public void indexPageChecksFailAuthorNotOwner() throws Exception { "notowner", "amihaiemil", "amihaiemil.github.io", false, false, "http://amihaiemil.github.io/page/to/index" ); + Logger logger = Mockito.mock(Logger.class); Language eng = this.mockEnglish(com); Brain br = new Brain( Mockito.mock(Logger.class), @@ -268,7 +262,7 @@ public void indexPageChecksFailAuthorNotOwner() throws Exception { ); Brain spiedBrain = Mockito.spy(br); IndexPage index = Mockito.mock(IndexPage.class); - Mockito.doThrow(new IllegalStateException("Should not have reached here!")).when(index).perform(); + Mockito.doThrow(new IllegalStateException("Should not have reached here!")).when(index).perform(com, logger); Mockito.doReturn(index).when(spiedBrain).indexPageStep(com, eng); Mockito.when(com.authorOrgMembership()).thenReturn( @@ -276,7 +270,7 @@ public void indexPageChecksFailAuthorNotOwner() throws Exception { ); Step steps = spiedBrain.understand(com); - steps.perform(); + steps.perform(com, logger); List comments = Lists.newArrayList(com.issue().comments().iterate()); assertTrue(comments.size() == 2); @@ -303,6 +297,7 @@ public void indexPageChecksFailRepoIsForked() throws Exception { "amihaiemil", "amihaiemil", "forkedrepo", true, true, "http://amihaiemil.github.io/forkedrepo/page/to/index" ); + Logger logger = Mockito.mock(Logger.class); Language eng = this.mockEnglish(com); Brain br = new Brain( Mockito.mock(Logger.class), @@ -311,11 +306,11 @@ public void indexPageChecksFailRepoIsForked() throws Exception { ); Brain spiedBrain = Mockito.spy(br); IndexPage index = Mockito.mock(IndexPage.class); - Mockito.doThrow(new IllegalStateException("Should not have reached here!")).when(index).perform(); + Mockito.doThrow(new IllegalStateException("Should not have reached here!")).when(index).perform(com, logger); Mockito.doReturn(index).when(spiedBrain).indexPageStep(com, eng); Step steps = spiedBrain.understand(com); - steps.perform(); + steps.perform(com, logger); List comments = Lists.newArrayList(com.issue().comments().iterate()); assertTrue(comments.size() == 2); @@ -344,6 +339,7 @@ public void indexPageChecksFailNoWebsite() throws Exception { "amihaiemil", "amihaiemil", "someRepoWithNoWebsite", false, false, "http://amihaiemil.github.io/page/to/index" ); + Logger logger = Mockito.mock(Logger.class); Language eng = this.mockEnglish(com); Brain br = new Brain( Mockito.mock(Logger.class), @@ -352,11 +348,11 @@ public void indexPageChecksFailNoWebsite() throws Exception { ); Brain spiedBrain = Mockito.spy(br); IndexPage index = Mockito.mock(IndexPage.class); - Mockito.doThrow(new IllegalStateException("Should not have reached here!")).when(index).perform(); + Mockito.doThrow(new IllegalStateException("Should not have reached here!")).when(index).perform(com, logger); Mockito.doReturn(index).when(spiedBrain).indexPageStep(com, eng); Step steps = spiedBrain.understand(com); - steps.perform(); + steps.perform(com, logger); List comments = Lists.newArrayList(com.issue().comments().iterate()); assertTrue(comments.size() == 2); @@ -385,6 +381,7 @@ public void indexPageChecksFailPageNotInTheRepo() throws Exception { "amihaiemil", "amihaiemil", "charles", false, true, "http://www.othersite.test/page/to/index" ); + Logger logger = Mockito.mock(Logger.class); Language eng = this.mockEnglish(com); Brain br = new Brain( Mockito.mock(Logger.class), @@ -393,11 +390,11 @@ public void indexPageChecksFailPageNotInTheRepo() throws Exception { ); Brain spiedBrain = Mockito.spy(br); IndexPage index = Mockito.mock(IndexPage.class); - Mockito.doThrow(new IllegalStateException("Should not have reached here!")).when(index).perform(); + Mockito.doThrow(new IllegalStateException("Should not have reached here!")).when(index).perform(com, logger); Mockito.doReturn(index).when(spiedBrain).indexPageStep(com, eng); Step steps = spiedBrain.understand(com); - steps.perform(); + steps.perform(com, logger); List comments = Lists.newArrayList(com.issue().comments().iterate()); assertTrue(comments.size() == 2); diff --git a/src/test/java/com/amihaiemil/charles/github/IndexSiteActionTestCase.java b/src/test/java/com/amihaiemil/charles/github/IndexSiteActionTestCase.java index 4b8252a..d8fe844 100644 --- a/src/test/java/com/amihaiemil/charles/github/IndexSiteActionTestCase.java +++ b/src/test/java/com/amihaiemil/charles/github/IndexSiteActionTestCase.java @@ -66,6 +66,7 @@ public void indexSiteChecksAreSuccessfulOnBlogRepo() throws Exception { "amihaiemil", "amihaiemil", "amihaiemil.github.io", false, false ); + Logger logger = Mockito.mock(Logger.class); Language eng = this.mockEnglish(com); Brain br = new Brain( Mockito.mock(Logger.class), @@ -76,13 +77,12 @@ public void indexSiteChecksAreSuccessfulOnBlogRepo() throws Exception { Mockito.doReturn(//replace the index step with a simple comment; we're just interested in the checks here, not the index step itself new SendReply( new TextReply(com, "Index-site checks passed!"), - Mockito.mock(Logger.class), - new Step.FinalStep(Mockito.mock(Logger.class)) + new Step.FinalStep() ) ).when(spiedBrain).indexSiteStep(com, eng); Step steps = spiedBrain.understand(com); - steps.perform(); + steps.perform(com, Mockito.mock(Logger.class)); List comments = Lists.newArrayList(com.issue().comments().iterate()); assertTrue(comments.size() == 2); @@ -119,13 +119,12 @@ public void indexSiteChecksAreSuccessfulOnGhPagesRepo() throws Exception { Mockito.doReturn(//replace the index step with a simple comment; we're just interested in the checks here, not the index step itself new SendReply( new TextReply(com, "Index-site checks passed!"), - Mockito.mock(Logger.class), - new Step.FinalStep(Mockito.mock(Logger.class)) + new Step.FinalStep() ) ).when(spiedBrain).indexSiteStep(com, eng); Step steps = spiedBrain.understand(com); - steps.perform(); + steps.perform(com, Mockito.mock(Logger.class)); List comments = Lists.newArrayList(com.issue().comments().iterate()); assertTrue(comments.size() == 2); @@ -164,8 +163,7 @@ public void indexSiteChecksAreSuccessfulOnGhPagesRepoUnderOrg() throws Exception Mockito.doReturn(//replace the index step with a simple comment; we're just interested in the checks here, not the index step itself new SendReply( new TextReply(com, "Index-site checks passed!"), - Mockito.mock(Logger.class), - new Step.FinalStep(Mockito.mock(Logger.class)) + new Step.FinalStep() ) ).when(spiedBrain).indexSiteStep(com, eng); @@ -178,7 +176,7 @@ public void indexSiteChecksAreSuccessfulOnGhPagesRepoUnderOrg() throws Exception Step steps = spiedBrain.understand(com); - steps.perform(); + steps.perform(com, Mockito.mock(Logger.class)); List comments = Lists.newArrayList(com.issue().comments().iterate()); assertTrue(comments.size() == 2); @@ -217,8 +215,7 @@ public void indexSiteChecksAreSuccessfulOnBlogRepoUnderOrg() throws Exception { Mockito.doReturn(//replace the index step with a simple comment; we're just interested in the checks here, not the index step itself new SendReply( new TextReply(com, "Index-site checks passed!"), - Mockito.mock(Logger.class), - new Step.FinalStep(Mockito.mock(Logger.class)) + new Step.FinalStep() ) ).when(spiedBrain).indexSiteStep(com, eng); @@ -231,7 +228,7 @@ public void indexSiteChecksAreSuccessfulOnBlogRepoUnderOrg() throws Exception { Step steps = spiedBrain.understand(com); - steps.perform(); + steps.perform(com, Mockito.mock(Logger.class)); List comments = Lists.newArrayList(com.issue().comments().iterate()); assertTrue(comments.size() == 2); @@ -258,6 +255,7 @@ public void indexSiteChecksFailAuthorNotOwner() throws Exception { "notowner", "amihaiemil", "amihaiemil.github.io", false, false ); + Logger logger = Mockito.mock(Logger.class); Language eng = this.mockEnglish(com); Brain br = new Brain( Mockito.mock(Logger.class), @@ -266,7 +264,7 @@ public void indexSiteChecksFailAuthorNotOwner() throws Exception { ); Brain spiedBrain = Mockito.spy(br); IndexStep index = Mockito.mock(IndexStep.class); - Mockito.doThrow(new IllegalStateException("Should not have reached here!")).when(index).perform(); + Mockito.doThrow(new IllegalStateException("Should not have reached here!")).when(index).perform(com, logger); Mockito.doReturn(index).when(spiedBrain).indexSiteStep(com, eng); Mockito.when(com.authorOrgMembership()).thenReturn( @@ -274,7 +272,7 @@ public void indexSiteChecksFailAuthorNotOwner() throws Exception { ); Step steps = spiedBrain.understand(com); - steps.perform(); + steps.perform(com, logger); List comments = Lists.newArrayList(com.issue().comments().iterate()); assertTrue(comments.size() == 2); @@ -301,6 +299,7 @@ public void indexSiteChecksFailRepoIsForked() throws Exception { "amihaiemil", "amihaiemil", "forkedrepo", true, true ); + Logger logger = Mockito.mock(Logger.class); Language eng = this.mockEnglish(com); Brain br = new Brain( Mockito.mock(Logger.class), @@ -309,11 +308,11 @@ public void indexSiteChecksFailRepoIsForked() throws Exception { ); Brain spiedBrain = Mockito.spy(br); IndexStep index = Mockito.mock(IndexStep.class); - Mockito.doThrow(new IllegalStateException("Should not have reached here!")).when(index).perform(); + Mockito.doThrow(new IllegalStateException("Should not have reached here!")).when(index).perform(com, logger); Mockito.doReturn(index).when(spiedBrain).indexSiteStep(com, eng); Step steps = spiedBrain.understand(com); - steps.perform(); + steps.perform(com, logger); List comments = Lists.newArrayList(com.issue().comments().iterate()); assertTrue(comments.size() == 2); @@ -341,6 +340,7 @@ public void indexSiteChecksFailNoWebsite() throws Exception { "amihaiemil", "amihaiemil", "someRepoWithNoWebsite", false, false ); + Logger logger = Mockito.mock(Logger.class); Language eng = this.mockEnglish(com); Brain br = new Brain( Mockito.mock(Logger.class), @@ -349,11 +349,11 @@ public void indexSiteChecksFailNoWebsite() throws Exception { ); Brain spiedBrain = Mockito.spy(br); IndexStep index = Mockito.mock(IndexStep.class); - Mockito.doThrow(new IllegalStateException("Should not have reached here!")).when(index).perform(); + Mockito.doThrow(new IllegalStateException("Should not have reached here!")).when(index).perform(com, logger); Mockito.doReturn(index).when(spiedBrain).indexSiteStep(com, eng); Step steps = spiedBrain.understand(com); - steps.perform(); + steps.perform(com, logger); List comments = Lists.newArrayList(com.issue().comments().iterate()); assertTrue(comments.size() == 2); @@ -395,10 +395,9 @@ private Command mockCommand( Issue agentIssue = new MkGithub(storage, "charlesmike") .repos().get(repoCoord).issues().get(issue.number()); - Command com = Mockito.mock(Command.class); - Mockito.when(com.json()).thenReturn(c.json()); - Mockito.when(com.issue()).thenReturn(agentIssue); + Mockito.when(com.json()).thenReturn(c.json()); + Mockito.when(com.issue()).thenReturn(agentIssue); Mockito.when(com.authorLogin()).thenReturn(commander); //we build our own json repo since the one returned by MkGithub doesn't map 1:1 with the expected and so the tests fail. @@ -409,12 +408,12 @@ private Command mockCommand( .build(); - CommandedRepo crepo = Mockito.mock(CommandedRepo.class); + CommandedRepo crepo = Mockito.mock(CommandedRepo.class); Mockito.when(crepo.json()).thenReturn(repo); Mockito.when(crepo.hasGhPagesBranch()).thenReturn(ghpages); - Mockito.when(com.repo()).thenReturn(crepo); - return com; + Mockito.when(com.repo()).thenReturn(crepo); + return com; } /** diff --git a/src/test/java/com/amihaiemil/charles/github/OrganizationAdminCheckTestCase.java b/src/test/java/com/amihaiemil/charles/github/OrganizationAdminCheckTestCase.java index 72065a8..acab4d9 100644 --- a/src/test/java/com/amihaiemil/charles/github/OrganizationAdminCheckTestCase.java +++ b/src/test/java/com/amihaiemil/charles/github/OrganizationAdminCheckTestCase.java @@ -54,9 +54,9 @@ public void authorOrganizationAdmin() throws Exception { Mockito.when(com.authorOrgMembership()).thenReturn(membership); OrganizationAdminCheck oac = new OrganizationAdminCheck( - com, Mockito.mock(Logger.class), new Step.Fake(true), new Step.Fake(false) + new Step.Fake(true), new Step.Fake(false) ); - oac.perform(); + oac.perform(com, Mockito.mock(Logger.class)); } /** @@ -69,9 +69,9 @@ public void membershipEndpointThrowsIOException() throws Exception { Mockito.when(com.authorOrgMembership()).thenThrow(new IOException()); OrganizationAdminCheck oac = new OrganizationAdminCheck( - com, Mockito.mock(Logger.class), Mockito.mock(Step.class), Mockito.mock(Step.class) + Mockito.mock(Step.class), Mockito.mock(Step.class) ); - oac.perform(); + oac.perform(com, Mockito.mock(Logger.class)); } /** @@ -83,16 +83,17 @@ public void membershipEndpointThrowsIOException() throws Exception { public void authorNotOrganizationAdmin() throws Exception { JsonObject membership = Json.createObjectBuilder().add("state", "active").add("role", "member").build(); Command com = this.mockCommand("someone", "orgName"); + Logger logger = Mockito.mock(Logger.class); Mockito.when(com.authorOrgMembership()).thenReturn(membership); Step onTrue = Mockito.mock(Step.class); - Mockito.doThrow(new IllegalStateException("This step should not have been executed!")).when(onTrue).perform(); + Mockito.doThrow(new IllegalStateException("This step should not have been executed!")).when(onTrue).perform(com, logger); Step onFalse = Mockito.mock(Step.class); - Mockito.doNothing().when(onFalse).perform(); + Mockito.doNothing().when(onFalse).perform(com, logger); OrganizationAdminCheck oac = new OrganizationAdminCheck( - com, Mockito.mock(Logger.class), new Step.Fake(false), new Step.Fake(true) + new Step.Fake(false), new Step.Fake(true) ); - oac.perform(); + oac.perform(com, logger); } /** * Mock a command for the unit tests. diff --git a/src/test/java/com/amihaiemil/charles/github/PageHostedOnGithubCheckTestCase.java b/src/test/java/com/amihaiemil/charles/github/PageHostedOnGithubCheckTestCase.java index 286894b..c2fdcc2 100644 --- a/src/test/java/com/amihaiemil/charles/github/PageHostedOnGithubCheckTestCase.java +++ b/src/test/java/com/amihaiemil/charles/github/PageHostedOnGithubCheckTestCase.java @@ -51,16 +51,20 @@ public class PageHostedOnGithubCheckTestCase { public void tellsValidLinkGhPages() throws IOException { PageHostedOnGithubCheck phgc = new PageHostedOnGithubCheck( + new Step.Fake(true), new Step.Fake(false) + ); + phgc.perform( this.mockCommand("amihaiemil", "myrepo", true, "http://amihaiemil.github.io/myrepo/stuff/page.html"), - Mockito.mock(Logger.class), new Step.Fake(true), new Step.Fake(false) + Mockito.mock(Logger.class) ); - phgc.perform(); PageHostedOnGithubCheck phgc2 = new PageHostedOnGithubCheck( - this.mockCommand("amihaiemil", "myrepo", true, "https://amihaiemil.github.io/myrepo/stuff/page.html"), - Mockito.mock(Logger.class), new Step.Fake(true), new Step.Fake(false) + new Step.Fake(true), new Step.Fake(false) + ); + phgc2.perform( + this.mockCommand("amihaiemil", "myrepo", true, "https://amihaiemil.github.io/myrepo/stuff/page.html"), + Mockito.mock(Logger.class) ); - phgc2.perform(); } /** @@ -72,16 +76,20 @@ public void tellsValidLinkGhPages() throws IOException { public void tellsInvalidLinkGhPages() throws IOException { PageHostedOnGithubCheck phgc = new PageHostedOnGithubCheck( + new Step.Fake(false), new Step.Fake(true) + ); + phgc.perform( this.mockCommand("amihaiemil", "myrepo", true, "http://domain.io/stuff/page.html"), - Mockito.mock(Logger.class), new Step.Fake(false), new Step.Fake(true) + Mockito.mock(Logger.class) ); - phgc.perform(); PageHostedOnGithubCheck phgc2 = new PageHostedOnGithubCheck( + new Step.Fake(false), new Step.Fake(true) + ); + phgc2.perform( this.mockCommand("amihaiemil", "myrepo", true, "ftp://amihaiemil.github.io/folder/stuff/page.html"), - Mockito.mock(Logger.class), new Step.Fake(false), new Step.Fake(true) + Mockito.mock(Logger.class) ); - phgc2.perform(); } /** @@ -91,18 +99,20 @@ public void tellsInvalidLinkGhPages() throws IOException { */ @Test public void tellsValidLink() throws IOException { - PageHostedOnGithubCheck phgc = new PageHostedOnGithubCheck( + new Step.Fake(true), new Step.Fake(false) + ); + phgc.perform( this.mockCommand("amihaiemil", "myrepo", false, "http://amihaiemil.github.io/myrepo/stuff/page.html"), - Mockito.mock(Logger.class), new Step.Fake(true), new Step.Fake(false) + Mockito.mock(Logger.class) ); - phgc.perform(); - PageHostedOnGithubCheck phgc2 = new PageHostedOnGithubCheck( - this.mockCommand("amihaiemil", "myrepo", false, "https://amihaiemil.github.io/myrepo/stuff/page.html"), - Mockito.mock(Logger.class), new Step.Fake(true), new Step.Fake(false) + new Step.Fake(true), new Step.Fake(false) + ); + phgc2.perform( + this.mockCommand("amihaiemil", "myrepo", false, "https://amihaiemil.github.io/myrepo/stuff/page.html"), + Mockito.mock(Logger.class) ); - phgc2.perform(); } /** @@ -114,17 +124,21 @@ public void tellsValidLink() throws IOException { public void tellsInvalidLink() throws IOException { PageHostedOnGithubCheck phgc = new PageHostedOnGithubCheck( - this.mockCommand("amihaiemil", "myrepo", false, "http://amihaiemil.github.io/stuff/page.html"), - Mockito.mock(Logger.class), new Step.Fake(true), new Step.Fake(false) + new Step.Fake(true), new Step.Fake(false) + ); + phgc.perform( + this.mockCommand("amihaiemil", "myrepo", false, "http://amihaiemil.github.io/stuff/page.html"), + Mockito.mock(Logger.class) ); - phgc.perform(); PageHostedOnGithubCheck phgc2 = new PageHostedOnGithubCheck( + new Step.Fake(false), new Step.Fake(true) + ); + phgc2.perform( this.mockCommand("amihaiemil", "myrepo", false, "ftp://amihaiemil.github.io/myrepo/stuff/page.html"), - Mockito.mock(Logger.class), new Step.Fake(false), new Step.Fake(true) + Mockito.mock(Logger.class) ); - phgc2.perform(); } /** diff --git a/src/test/java/com/amihaiemil/charles/github/RepoForkCheckTestCase.java b/src/test/java/com/amihaiemil/charles/github/RepoForkCheckTestCase.java index b4fab71..5b63b37 100644 --- a/src/test/java/com/amihaiemil/charles/github/RepoForkCheckTestCase.java +++ b/src/test/java/com/amihaiemil/charles/github/RepoForkCheckTestCase.java @@ -47,15 +47,18 @@ public class RepoForkCheckTestCase { @Test public void recognizesFork() { JsonObject repo = Json.createObjectBuilder().add("fork", true).build(); + Command com = Mockito.mock(Command.class); + Logger logger = Mockito.mock(Logger.class); + Step onTrue = Mockito.mock(Step.class); - Mockito.doThrow(new IllegalStateException("This step should not have been executed!")).when(onTrue).perform(); + Mockito.doThrow(new IllegalStateException("This step should not have been executed!")).when(onTrue).perform(com, logger); Step onFalse = Mockito.mock(Step.class); - Mockito.doNothing().when(onFalse).perform(); + Mockito.doNothing().when(onFalse).perform(com, logger); RepoForkCheck rfc = new RepoForkCheck( - repo, Mockito.mock(Logger.class), onTrue, onFalse + repo, onTrue, onFalse ); - rfc.perform(); + rfc.perform(com, logger); } /** @@ -64,14 +67,17 @@ public void recognizesFork() { @Test public void recognizesNotFork() { JsonObject repo = Json.createObjectBuilder().add("fork", false).build(); + Command com = Mockito.mock(Command.class); + Logger logger = Mockito.mock(Logger.class); + Step onTrue = Mockito.mock(Step.class); - Mockito.doNothing().when(onTrue).perform(); + Mockito.doNothing().when(onTrue).perform(com, logger); Step onFalse = Mockito.mock(Step.class); - Mockito.doThrow(new IllegalStateException("This step should not have been executed!")).when(onFalse).perform(); + Mockito.doThrow(new IllegalStateException("This step should not have been executed!")).when(onFalse).perform(com, logger); RepoForkCheck rfc = new RepoForkCheck( - repo, Mockito.mock(Logger.class), onTrue, onFalse + repo, onTrue, onFalse ); - rfc.perform(); + rfc.perform(com, logger); } } diff --git a/src/test/java/com/amihaiemil/charles/github/RepoNameCheckTestCase.java b/src/test/java/com/amihaiemil/charles/github/RepoNameCheckTestCase.java index a856513..d3e33b8 100644 --- a/src/test/java/com/amihaiemil/charles/github/RepoNameCheckTestCase.java +++ b/src/test/java/com/amihaiemil/charles/github/RepoNameCheckTestCase.java @@ -52,13 +52,14 @@ public class RepoNameCheckTestCase { */ @Test public void repoNameMatches() throws Exception { - RepoNameCheck rnc = new RepoNameCheck( - this.mockCommand("amihaiemil", "amihaiemil.github.io").issue().repo().json(), - Mockito.mock(Logger.class), - new Step.Fake(true), new Step.Fake(false) + this.mockCommand("amihaiemil", "amihaiemil.github.io").issue().repo().json(), + new Step.Fake(true), new Step.Fake(false) + ); + rnc.perform( + Mockito.mock(Command.class), + Mockito.mock(Logger.class) ); - rnc.perform(); } /** @@ -69,10 +70,9 @@ public void repoNameMatches() throws Exception { public void repoNameDoesntMatch() throws Exception { RepoNameCheck rnc = new RepoNameCheck( this.mockCommand("amihaiemil", "reponame").issue().repo().json(), - Mockito.mock(Logger.class), new Step.Fake(false), new Step.Fake(true) ); - rnc.perform(); + rnc.perform(Mockito.mock(Command.class), Mockito.mock(Logger.class)); } /** diff --git a/src/test/java/com/amihaiemil/charles/github/SendEmailTestCase.java b/src/test/java/com/amihaiemil/charles/github/SendEmailTestCase.java index 6bf3ef7..ff18ecd 100644 --- a/src/test/java/com/amihaiemil/charles/github/SendEmailTestCase.java +++ b/src/test/java/com/amihaiemil/charles/github/SendEmailTestCase.java @@ -35,6 +35,8 @@ import org.junit.After; import org.junit.Test; +import org.mockito.Mockito; +import org.slf4j.Logger; import com.amihaiemil.charles.github.SendEmail; import com.icegreen.greenmail.util.GreenMail; @@ -84,7 +86,7 @@ public void sendsEmaiLToSmtpServer() throws Exception { ); try { - se.perform(); + se.perform(Mockito.mock(Command.class), Mockito.mock(Logger.class)); final MimeMessage[] messages = server.getReceivedMessages(); assertTrue(messages.length == 1); for (final Message msg : messages) { @@ -115,7 +117,7 @@ public void sendsEmailFromGivenUser() throws Exception { SendEmail se = new SendEmail("amihaiemil@gmail.com", "hello", "hello, how are you?"); try { - se.perform(); + se.perform(Mockito.mock(Command.class), Mockito.mock(Logger.class)); final MimeMessage[] messages = server.getReceivedMessages(); assertTrue(messages.length == 1); for (final Message msg : messages) { @@ -134,7 +136,7 @@ public void sendsEmailFromGivenUser() throws Exception { @Test(expected = IllegalStateException.class) public void uninitializedPostman() { SendEmail se = new SendEmail("amihaiemil@gmail.com", "hello", "hello, how are you?"); - se.perform(); + se.perform(Mockito.mock(Command.class), Mockito.mock(Logger.class)); } /** diff --git a/src/test/java/com/amihaiemil/charles/github/SendReplyTestCase.java b/src/test/java/com/amihaiemil/charles/github/SendReplyTestCase.java index 95d16bf..d88676d 100644 --- a/src/test/java/com/amihaiemil/charles/github/SendReplyTestCase.java +++ b/src/test/java/com/amihaiemil/charles/github/SendReplyTestCase.java @@ -60,11 +60,10 @@ public void sendsComment() throws Exception { Command com = this.mockCommand(); Reply rep = new TextReply(com, "Hello there!"); SendReply sr = new SendReply( - rep, Mockito.mock(Logger.class), - Mockito.mock(Step.class) + rep, Mockito.mock(Step.class) ); - sr.perform(); + sr.perform(Mockito.mock(Command.class), Mockito.mock(Logger.class)); List comments = Lists.newArrayList(com.issue().comments().iterate()); assertTrue(comments.size() == 1); @@ -86,9 +85,8 @@ public void replySendFails() throws Exception { Mockito.doThrow(new IOException("This is expected, it's ok!")).when(rep).send(); try { new SendReply( - rep, Mockito.mock(Logger.class), - Mockito.mock(Step.class) - ).perform(); + rep, Mockito.mock(Step.class) + ).perform(Mockito.mock(Command.class), Mockito.mock(Logger.class)); fail("Expected ISE here, but was not thrown"); } catch (IllegalStateException ex) { assertTrue(ex.getMessage().equals("IOException when sending the reply!")); diff --git a/src/test/java/com/amihaiemil/charles/github/StarRepoTestCase.java b/src/test/java/com/amihaiemil/charles/github/StarRepoTestCase.java index 2f26f9b..b4b8808 100644 --- a/src/test/java/com/amihaiemil/charles/github/StarRepoTestCase.java +++ b/src/test/java/com/amihaiemil/charles/github/StarRepoTestCase.java @@ -35,6 +35,7 @@ import org.slf4j.Logger; import com.jcabi.github.Github; +import com.jcabi.github.Issue; import com.jcabi.github.Repo; import com.jcabi.github.Repos.RepoCreate; import com.jcabi.github.Stars; @@ -58,12 +59,20 @@ public void starsRepo() throws Exception { Logger logger = Mockito.mock(Logger.class); Mockito.doNothing().when(logger).info(Mockito.anyString()); Mockito.doThrow(new IllegalStateException("Unexpected error; test failed")).when(logger).error(Mockito.anyString()); - - Repo repo = this.mockGithubRepo(); - Step sr = new StarRepo(repo, logger, Mockito.mock(Step.class)); - assertFalse(repo.stars().starred()); - sr.perform(); - assertTrue(repo.stars().starred()); + + Github gh = new MkGithub("amihaiemil"); + Repo repo = gh.repos().create( + new RepoCreate("amihaiemil.github.io", false) + ); + Command com = Mockito.mock(Command.class); + Issue issue = Mockito.mock(Issue.class); + Mockito.when(issue.repo()).thenReturn(repo); + Mockito.when(com.issue()).thenReturn(issue); + + Step sr = new StarRepo(Mockito.mock(Step.class)); + assertFalse(com.issue().repo().stars().starred()); + sr.perform(com, logger); + assertTrue(com.issue().repo().stars().starred()); } /** @@ -76,12 +85,20 @@ public void starsRepoTwice() throws Exception { Mockito.doNothing().when(logger).info(Mockito.anyString()); Mockito.doThrow(new IllegalStateException("Unexpected error; test failed")).when(logger).error(Mockito.anyString()); - Repo repo = this.mockGithubRepo(); - Step sr = new StarRepo(repo, logger, Mockito.mock(Step.class)); - assertFalse(repo.stars().starred()); - sr.perform(); - sr.perform(); - assertTrue(repo.stars().starred()); + Github gh = new MkGithub("amihaiemil"); + Repo repo = gh.repos().create( + new RepoCreate("amihaiemil.github.io", false) + ); + Command com = Mockito.mock(Command.class); + Issue issue = Mockito.mock(Issue.class); + Mockito.when(issue.repo()).thenReturn(repo); + Mockito.when(com.issue()).thenReturn(issue); + + Step sr = new StarRepo(Mockito.mock(Step.class)); + assertFalse(com.issue().repo().stars().starred()); + sr.perform(com, logger); + sr.perform(com, logger); + assertTrue(com.issue().repo().stars().starred()); } /** @@ -106,19 +123,12 @@ public void repoStarringFails() throws IOException { Mockito.when(stars.starred()).thenReturn(false); Mockito.doThrow(new IOException()).when(stars).star(); Mockito.when(repo.stars()).thenReturn(stars); + Command com = Mockito.mock(Command.class); + Issue issue = Mockito.mock(Issue.class); + Mockito.when(issue.repo()).thenReturn(repo); + Mockito.when(com.issue()).thenReturn(issue); - StarRepo sr = new StarRepo(repo, logger, Mockito.mock(Step.class)); - sr.perform(); - } - /** - * Return a Github Repo mock for test. - * @return Repo. - * @throws Exception - */ - public Repo mockGithubRepo() throws Exception { - Github gh = new MkGithub("amihaiemil"); - return gh.repos().create( - new RepoCreate("amihaiemil.github.io", false) - ); + StarRepo sr = new StarRepo(Mockito.mock(Step.class)); + sr.perform(com, logger); } } diff --git a/src/test/java/com/amihaiemil/charles/github/StepsTestCase.java b/src/test/java/com/amihaiemil/charles/github/StepsTestCase.java index 492ee55..9002928 100644 --- a/src/test/java/com/amihaiemil/charles/github/StepsTestCase.java +++ b/src/test/java/com/amihaiemil/charles/github/StepsTestCase.java @@ -59,10 +59,9 @@ public class StepsTestCase { public void stepsPerformOk() { Steps steps = new Steps( Mockito.mock(Step.class), - Mockito.mock(Logger.class), Mockito.mock(SendReply.class) ); - steps.perform(); + steps.perform(Mockito.mock(Command.class), Mockito.mock(Logger.class)); } /** @@ -72,18 +71,18 @@ public void stepsPerformOk() { @Test public void stepsFail() throws Exception { Command com = this.mockCommand(); + Logger logger = Mockito.mock(Logger.class); Reply rep = new TextReply(com, "Error whene executig steps!"); SendReply sr = new SendReply( - rep, Mockito.mock(Logger.class), - Mockito.mock(Step.class) + rep, Mockito.mock(Step.class) ); Step s = Mockito.mock(Step.class); Mockito.doThrow(new IllegalStateException("for test")) - .when(s).perform(); + .when(s).perform(com, logger); - Steps steps = new Steps(s, Mockito.mock(Logger.class), sr); - steps.perform(); + Steps steps = new Steps(s, sr); + steps.perform(com, logger); List comments = Lists.newArrayList(com.issue().comments().iterate()); assertTrue(comments.size() == 1); diff --git a/src/test/java/com/amihaiemil/charles/github/TextReplyTestCase.java b/src/test/java/com/amihaiemil/charles/github/TextReplyTestCase.java index c8fc647..e0ccaca 100644 --- a/src/test/java/com/amihaiemil/charles/github/TextReplyTestCase.java +++ b/src/test/java/com/amihaiemil/charles/github/TextReplyTestCase.java @@ -87,9 +87,9 @@ public Command mockCommand(String msg) throws IOException { Command com = Mockito.mock(Command.class); - Mockito.when(com.json()).thenReturn(c.json()); - Mockito.when(com.issue()).thenReturn(issue); + Mockito.when(com.json()).thenReturn(c.json()); + Mockito.when(com.issue()).thenReturn(issue); - return com; + return com; } } diff --git a/src/test/java/com/amihaiemil/charles/rest/CharlesResourceTestCase.java b/src/test/java/com/amihaiemil/charles/rest/CharlesResourceTestCase.java index c07900d..e9dc180 100644 --- a/src/test/java/com/amihaiemil/charles/rest/CharlesResourceTestCase.java +++ b/src/test/java/com/amihaiemil/charles/rest/CharlesResourceTestCase.java @@ -24,28 +24,19 @@ */ package com.amihaiemil.charles.rest; -import static org.junit.Assert.assertTrue; - import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.net.ServerSocket; -import java.util.List; - import javax.servlet.http.HttpServletRequest; import javax.ws.rs.core.Response; - import org.apache.commons.io.IOUtils; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.junit.After; import org.junit.Test; import org.mockito.Mockito; - -import com.amihaiemil.charles.aws.AmazonEsSearch; -import com.amihaiemil.charles.aws.SearchQuery; -import com.amihaiemil.charles.github.Notification; import com.amihaiemil.charles.rest.model.SearchResultsPage; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; @@ -60,6 +51,7 @@ * @since 1.0.0 * */ +@SuppressWarnings("resource") public final class CharlesResourceTestCase { /** @@ -69,7 +61,7 @@ public final class CharlesResourceTestCase { @Test public void pagesAreDisplayed() throws IOException { int port = this.port(); - MkContainer awsEs = new MkGrizzlyContainer().next( + MkContainer awsEs = new MkGrizzlyContainer().next( new MkAnswer.Simple(this.readResource("esSearchResponse.json")) ).start(port); System.setProperty("aws.es.endpoint", "http://localhost:" + port + "/elasticsearch");