Skip to content
This repository has been archived by the owner on Dec 5, 2020. It is now read-only.

Commit

Permalink
refactoring and more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
amihaiemil committed Sep 22, 2016
1 parent d046d4f commit b3ec2b3
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 53 deletions.
1 change: 1 addition & 0 deletions charles-rest/pom.xml
Expand Up @@ -53,6 +53,7 @@
</executions>
<configuration>
<excludes>
<exclude>**/com/amihaiemil/charles/filters/*</exclude>
<exclude>**/com/amihaiemil/charles/rest/*</exclude>
</excludes>
</configuration>
Expand Down
Expand Up @@ -86,38 +86,34 @@ public Brain(Logger logger, LogsLocation logsLoc, List<Language> langs) {
public Steps understand(Command com) throws IOException {
String authorLogin = com.authorLogin();
logger.info("Command author's login: " + authorLogin);
List<Step> steps = new LinkedList<Step>();
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!");
String unknown = String.format(
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(
Expand Down
26 changes: 15 additions & 11 deletions charles-rest/src/main/java/com/amihaiemil/charles/github/Steps.java
Expand Up @@ -25,8 +25,6 @@

package com.amihaiemil.charles.github;

import java.util.List;

import com.amihaiemil.charles.steps.Step;

/**
Expand All @@ -36,10 +34,11 @@
* @since 1.0.0
*/
public class Steps implements Step {

/**
* Steps to be performed.
*/
private List<Step> steps;
private Step steps;

/**
* Message to send in case some step fails.
Expand All @@ -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<Step> 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;
}

}
Expand Up @@ -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);
}

/**
Expand All @@ -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);

Expand Down
Expand Up @@ -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}
Expand All @@ -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<Comment> 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());
}
}

0 comments on commit b3ec2b3

Please sign in to comment.