diff --git a/charles-rest/pom.xml b/charles-rest/pom.xml
index 5f38c88..603d5fa 100644
--- a/charles-rest/pom.xml
+++ b/charles-rest/pom.xml
@@ -53,6 +53,7 @@
+ **/com/amihaiemil/charles/filters/*
**/com/amihaiemil/charles/rest/*
diff --git a/charles-rest/src/main/java/com/amihaiemil/charles/github/Brain.java b/charles-rest/src/main/java/com/amihaiemil/charles/github/Brain.java
index cfd8c25..10a5cf8 100644
--- a/charles-rest/src/main/java/com/amihaiemil/charles/github/Brain.java
+++ b/charles-rest/src/main/java/com/amihaiemil/charles/github/Brain.java
@@ -86,25 +86,23 @@ public Brain(Logger logger, LogsLocation logsLoc, List langs) {
public Steps understand(Command com) throws IOException {
String authorLogin = com.authorLogin();
logger.info("Command author's login: " + authorLogin);
- List steps = new LinkedList();
+ Step steps;
CommandCategory category = this.categorizeCommand(com);
switch (category.type()) {
case "hello":
String hello = String.format(category.language().response("hello.comment"), authorLogin);
logger.info("Prepared response: " + hello);
- steps.add(
- new SendReply(
+ steps = new SendReply(
new TextReply(com, hello),
logger
- )
- );
+ );
break;
case "indexsite":
- steps.add(this.indexSteps(com, category, false));
+ steps = this.indexSteps(com, category, false);
break;
case "indexpage":
- steps.add(this.indexSteps(com, category, true));
+ steps = this.indexSteps(com, category, true);
break;
default:
logger.info("Unknwon command!");
@@ -112,12 +110,10 @@ public Steps understand(Command com) throws IOException {
category.language().response("unknown.comment"),
authorLogin);
logger.info("Prepared response: " + unknown);
- steps.add(
- new SendReply(
+ steps = new SendReply(
new TextReply(com, unknown),
this.logger
- )
- );
+ );
break;
}
return new Steps(
diff --git a/charles-rest/src/main/java/com/amihaiemil/charles/github/Steps.java b/charles-rest/src/main/java/com/amihaiemil/charles/github/Steps.java
index 1223e6e..71ecef2 100644
--- a/charles-rest/src/main/java/com/amihaiemil/charles/github/Steps.java
+++ b/charles-rest/src/main/java/com/amihaiemil/charles/github/Steps.java
@@ -25,8 +25,6 @@
package com.amihaiemil.charles.github;
-import java.util.List;
-
import com.amihaiemil.charles.steps.Step;
/**
@@ -36,10 +34,11 @@
* @since 1.0.0
*/
public class Steps implements Step {
+
/**
* Steps to be performed.
*/
- private List steps;
+ private Step steps;
/**
* Message to send in case some step fails.
@@ -51,24 +50,29 @@ public class Steps implements Step {
* @param steps Given steps.
* @param fm failure message in case any step fails.
*/
- public Steps(List steps, SendReply fm) {
+ public Steps(Step steps, SendReply fm) {
this.steps = steps;
this.failureMessage = fm;
}
+ /**
+ * Return the steps to perform.
+ * @return
+ */
+ public Step getStepsToPerform() {
+ return this.steps;
+ }
+
/**
* Perform all the given steps.
*/
@Override
public boolean perform() {
- for(Step s : steps) {
- if(s.perform()) {
- continue;
- }
- failureMessage.perform();
- return false;
+ if(steps.perform()) {
+ return true;
}
- return true;
+ failureMessage.perform();
+ return false;
}
}
diff --git a/charles-rest/src/test/java/com/amihaiemil/charles/github/BrainTestCase.java b/charles-rest/src/test/java/com/amihaiemil/charles/github/BrainTestCase.java
index ce97070..85c699d 100644
--- a/charles-rest/src/test/java/com/amihaiemil/charles/github/BrainTestCase.java
+++ b/charles-rest/src/test/java/com/amihaiemil/charles/github/BrainTestCase.java
@@ -54,18 +54,81 @@ public class BrainTestCase {
* @throws Exception if something goes wrong.
*/
@Test
- public void understandsCommand() throws Exception {
+ public void understandsHelloCommand() throws Exception {
Command com = this.mockCommand();
Language english = Mockito.mock(English.class);
Mockito.when(english.response("step.failure.comment")).thenReturn("failure on step");
- Mockito.when(english.response("hello.comment")).thenReturn("hi there, %s");
+ Mockito.when(english.response("hello.comment")).thenReturn("hi there");
Mockito.when(english.categorize(com)
).thenReturn(new CommandCategory("hello", english));
Brain br = new Brain(Mockito.mock(Logger.class), Mockito.mock(LogsLocation.class), Arrays.asList(english));
Steps steps = br.understand(com);
assertTrue(steps != null);
+ assertTrue(steps.getStepsToPerform() instanceof SendReply);
+ }
+
+ /**
+ * {@link Brain} can undestand an index site command.
+ * @throws Exception if something goes wrong.
+ */
+ @Test
+ public void understandsIndexSiteCommand() throws Exception {
+ Command com = this.mockCommand();
+
+ Language english = Mockito.mock(English.class);
+ Mockito.when(english.response("step.failure.comment")).thenReturn("failure on step");
+ Mockito.when(english.response("index.start.comment")).thenReturn("index start!");
+ Mockito.when(english.response("index.finished.comment")).thenReturn("index finished!");
+ Mockito.when(english.categorize(com)
+ ).thenReturn(new CommandCategory("indexsite", english));
+
+ Brain br = new Brain(Mockito.mock(Logger.class), Mockito.mock(LogsLocation.class), Arrays.asList(english));
+ Steps steps = br.understand(com);
+ assertTrue(steps != null);
+ assertTrue(steps.getStepsToPerform() instanceof IndexWithPreconditionCheck);
+ }
+
+ /**
+ * {@link Brain} can undestand an index page command.
+ * @throws Exception if something goes wrong.
+ */
+ @Test
+ public void understandsIndexPageCommand() throws Exception {
+ Command com = this.mockCommand();
+
+ Language english = Mockito.mock(English.class);
+ Mockito.when(english.response("step.failure.comment")).thenReturn("failure on step");
+ Mockito.when(english.response("index.start.comment")).thenReturn("index start!");
+ Mockito.when(english.response("index.finished.comment")).thenReturn("index finished!");
+ Mockito.when(english.categorize(com)
+ ).thenReturn(new CommandCategory("indexpage", english));
+
+ Brain br = new Brain(Mockito.mock(Logger.class), Mockito.mock(LogsLocation.class), Arrays.asList(english));
+ Steps steps = br.understand(com);
+ assertTrue(steps != null);
+ assertTrue(steps.getStepsToPerform() instanceof IndexWithPreconditionCheck);
+ }
+
+ /**
+ * {@link Brain} can see an unknown command.
+ * @throws Exception if something goes wrong.
+ */
+ @Test
+ public void uknownCommand() throws Exception {
+ Command com = this.mockCommand();
+
+ Language english = Mockito.mock(English.class);
+ Mockito.when(english.response("step.failure.comment")).thenReturn("failure on step");
+ Mockito.when(english.response("unknown.comment")).thenReturn("Unknown command!");
+ Mockito.when(english.categorize(com)
+ ).thenReturn(new CommandCategory("uknown", english));
+
+ Brain br = new Brain(Mockito.mock(Logger.class), Mockito.mock(LogsLocation.class), Arrays.asList(english));
+ Steps steps = br.understand(com);
+ assertTrue(steps != null);
+ assertTrue(steps.getStepsToPerform() instanceof SendReply);
}
/**
@@ -80,7 +143,7 @@ public Command mockCommand() throws IOException {
Issue issue = gh.repos().get(
new Coordinates.Simple("amihaiemil", "amihaiemil.github.io")
).issues().create("Test issue for commands", "test body");
- Comment c = issue.comments().post("@charlesmike hello there!");
+ Comment c = issue.comments().post("@charlesmike mock command for you!");
Command com = Mockito.mock(Command.class);
diff --git a/charles-rest/src/test/java/com/amihaiemil/charles/github/StepsTestCase.java b/charles-rest/src/test/java/com/amihaiemil/charles/github/StepsTestCase.java
index aa06f7e..7a86624 100644
--- a/charles-rest/src/test/java/com/amihaiemil/charles/github/StepsTestCase.java
+++ b/charles-rest/src/test/java/com/amihaiemil/charles/github/StepsTestCase.java
@@ -25,15 +25,26 @@
package com.amihaiemil.charles.github;
-import java.util.Arrays;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
-import org.junit.Test;
+import java.io.IOException;
+import java.util.List;
-import static org.junit.Assert.*;
+import javax.json.Json;
+import org.junit.Test;
import org.mockito.Mockito;
+import org.slf4j.Logger;
import com.amihaiemil.charles.steps.Step;
+import com.google.common.collect.Lists;
+import com.jcabi.github.Comment;
+import com.jcabi.github.Coordinates;
+import com.jcabi.github.Github;
+import com.jcabi.github.Issue;
+import com.jcabi.github.Repos.RepoCreate;
+import com.jcabi.github.mock.MkGithub;
/**
* Unit tests for {@link Steps}
@@ -47,43 +58,55 @@ public class StepsTestCase {
* Steps can perform 1 single step.
*/
@Test
- public void oneStepIsPerformed() {
+ public void stepsPerformOk() {
Step s = Mockito.mock(Step.class);
Mockito.when(s.perform()).thenReturn(true);
- Steps steps = new Steps(Arrays.asList(s), Mockito.mock(SendReply.class));
+ Steps steps = new Steps(s, Mockito.mock(SendReply.class));
assertTrue(steps.perform());
}
/**
* Steps can perform more steps.
+ * @throws Exception if something goes wrong.
*/
@Test
- public void moreStepsArePeformed() {
- Step s1 = Mockito.mock(Step.class);
- Mockito.when(s1.perform()).thenReturn(true);
- Step s2 = Mockito.mock(Step.class);
- Mockito.when(s2.perform()).thenReturn(true);
- Step s3 = Mockito.mock(Step.class);
- Mockito.when(s3.perform()).thenReturn(true);
+ public void stepsFail() throws Exception {
+ Command com = this.mockCommand();
+ Reply rep = new TextReply(com, "Error whene executig steps!");
+ SendReply sr = new SendReply(rep, Mockito.mock(Logger.class));
- Steps steps = new Steps(Arrays.asList(s1, s2, s3), Mockito.mock(SendReply.class));
- assertTrue(steps.perform());
- }
+ Step s = Mockito.mock(Step.class);
+ Mockito.when(s.perform()).thenReturn(false);
+
+ Steps steps = new Steps(s, sr);
+ assertFalse(steps.perform());
+ List comments = Lists.newArrayList(com.issue().comments().iterate());
+ assertTrue(comments.size() == 1);
+ assertTrue(
+ comments.get(0).json().getString("body").equals(
+ "> @charlesmike mock command\n\nError whene executig steps!"
+ )
+ );
+ }
+
/**
- * Steps stops performing the steps and returns false when 1 step fails.
- */
- @Test
- public void stopsPeformingWhenOneStepFails() {
- Step s1 = Mockito.mock(Step.class);
- Mockito.when(s1.perform()).thenReturn(true);
- Step s2 = Mockito.mock(Step.class);
- Mockito.when(s2.perform()).thenReturn(false);
- Step s3 = Mockito.mock(Step.class);
- Mockito.when(s3.perform()).thenReturn(true);
+ * Mock a command.
+ * @return The created Command.
+ * @throws IOException If something goes wrong.
+ */
+ public Command mockCommand() throws IOException {
+ Github gh = new MkGithub("amihaiemil");
+ RepoCreate repoCreate = new RepoCreate("amihaiemil.github.io", false);
+ gh.repos().create(repoCreate);
+ Issue issue = gh.repos().get(
+ new Coordinates.Simple("amihaiemil", "amihaiemil.github.io")
+ ).issues().create("Test issue for commands", "test body");
+ Command com = Mockito.mock(Command.class);
+ Mockito.when(com.issue()).thenReturn(issue);
+ Mockito.when(com.json()).thenReturn(Json.createObjectBuilder().add("body", "@charlesmike mock command").build());
+ return com;
+ }
- Steps steps = new Steps(Arrays.asList(s1, s2, s3), Mockito.mock(SendReply.class));
- assertFalse(steps.perform());
- }
}