Skip to content

Commit

Permalink
Confused, Conversation, Hello and GithubSteps + unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
amihaiemil committed Oct 30, 2017
1 parent 65f0d5a commit 0234c93
Show file tree
Hide file tree
Showing 10 changed files with 412 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/main/java/co/comdor/Knowledge.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public interface Knowledge {
* order to fulfill it.
* @param mention Github mentioning comment.
* @return Steps to execute.
* @throws IOException If something goes wrong when calling Github.
*/
Steps start(final Mention mention) throws IOException;
}
2 changes: 1 addition & 1 deletion src/main/java/co/comdor/github/Confused.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public Steps start(final Mention mention) throws IOException {
final Step reply = new SendReply(
mention.language().response("unknown.comment")
);
return null;
return new GithubSteps(reply, mention);
}

}
12 changes: 6 additions & 6 deletions src/main/java/co/comdor/github/Conversation.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,29 +46,29 @@ public final class Conversation implements Knowledge {
/**
* The bot starts the conversation by saying "hello".
*/
private Knowledge hello;
private Knowledge followup;

/**
* Ctor.
* @param followup Followup of this conversation; what does it know to do next?
* @param followup What should it do after the conversation starts?
*/
public Conversation(final Knowledge followup) {
this(followup, new English());
}

/**
* Ctor.
* @param hello Start this conversation, say "hello".
* @param followup What should it do after the conversation starts?
* @param langs Languages that the bot speaks.
*/
public Conversation(final Knowledge hello, final Language... langs) {
this.hello = hello;
public Conversation(final Knowledge followup, final Language... langs) {
this.followup = followup;
this.languages = langs;
}

@Override
public Steps start(final Mention mention) throws IOException {
mention.understand(this.languages);
return this.hello.start(mention);
return this.followup.start(mention);
}
}
105 changes: 105 additions & 0 deletions src/main/java/co/comdor/github/GithubSteps.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/**
* Copyright (c) 2017, Mihai Emil Andronache
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1)Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2)Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3)Neither the name of comdor nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package co.comdor.github;

import co.comdor.Step;
import co.comdor.Steps;
import org.slf4j.Logger;

import java.io.IOException;

/**
* Steps to fulfill a mention, executed on Github.
* @author Mihai Andronache (amihaiemil@gmail.com)
* @version $Id$
* @since 0.0.1
*/
public final class GithubSteps implements Steps {

/**
* Steps to be performed.
*/
private Step steps;

/**
* Initial mention. The one that triggered everything.
*/
private Mention mention;

/**
* Message to send in case some step fails.
*/
private SendReply failureMessage;

/**
* Constructor.
* @param steps Steps to perform everything.
* @param mention Initial menton.
*/
public GithubSteps(final Step steps, final Mention mention) {
this(
steps, mention,
new SendReply(
mention.language().response("step.failure.comment"),
new Step.FinalStep("[ERROR] Some step didn't execute properly.")
)
);
}

/**
* Constructor.
* @param steps Steps to perform everything.
* @param mention Initial menton.
* @param failureMessage Reply sent in case of failure.
*/
public GithubSteps(
final Step steps, final Mention mention,
final SendReply failureMessage
) {
this.steps = steps;
this.mention = mention;
this.failureMessage = failureMessage;
}

/**
* Perform all the given steps.
* @param logger Action logger.
* @throws IOException If something goes wrong while calling Github.
*/
@Override
public void perform(final Logger logger) throws IOException {
try {
logger.info(
"Received command: " + this.mention.json().getString("body")
);
logger.info("Author login: " + this.mention.author());
this.steps.perform(this.mention, logger);
} catch (final IOException ex) {
logger.error("An exception occured, sending failure comment.", ex);
this.failureMessage.perform(this.mention, logger);
}
}
}
67 changes: 67 additions & 0 deletions src/main/java/co/comdor/github/Hello.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* Copyright (c) 2017, Mihai Emil Andronache
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1)Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2)Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3)Neither the name of comdor nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package co.comdor.github;

import co.comdor.Knowledge;
import co.comdor.Steps;

import java.io.IOException;

/**
* The bot knows how to respond to a 'hello' mention.
* @author Mihai Andronache (amihaiemil@gmail.com)
* @version $Id$
* @since 0.0.1
*/
public final class Hello implements Knowledge {

/**
* What do we do if it's not a 'hello' command?
*/
private Knowledge notHello;

/**
* Ctor.
* @param notHello What do we do if it's not a 'hello' command?
*/
public Hello(final Knowledge notHello) {
this.notHello = notHello;
}

@Override
public Steps start(final Mention com) throws IOException {
final Steps resolved;
if("hello".equalsIgnoreCase(com.type())) {
resolved = new GithubSteps(
new SendReply(com.language().response("hello.comment")),
com
);
} else {
resolved = this.notHello.start(com);
}
return resolved;
}
}
2 changes: 1 addition & 1 deletion src/main/java/co/comdor/github/LastMention.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public Language language() {
@Override
public void understand(final Language[] langs) throws IOException {
for(final Language spoken : langs) {
this.type = lang.categorize(this);
this.type = spoken.categorize(this);
if(!"unknown".equals(this.type)) {
this.lang = spoken;
break;
Expand Down
62 changes: 62 additions & 0 deletions src/test/java/co/comdor/github/ConfusedTestCase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* Copyright (c) 2017, Mihai Emil Andronache
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1)Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2)Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3)Neither the name of comdor nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package co.comdor.github;

import co.comdor.Knowledge;
import co.comdor.Steps;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.mockito.Mockito;

/**
* Unit tests for {@link Confused}
* @author Mihai Andronache (amihaiemil@gmail.com)
* @version $Id$
* @since 0.0.1
*/
public final class ConfusedTestCase {

/**
* Confused can start an 'unknown' command.
* @throws Exception If something goes wrong.
*/
@Test
public void startsUnknownCommand() throws Exception {
final Mention com = Mockito.mock(Mention.class);
Mockito.when(com.type()).thenReturn("unknown");
Mockito.when(com.language()).thenReturn(new English());

final Knowledge confused = new Confused();

Steps steps = confused.start(com);
MatcherAssert.assertThat(steps, Matchers.notNullValue());
MatcherAssert.assertThat(
steps instanceof GithubSteps, Matchers.is(true)
);

}
}
63 changes: 63 additions & 0 deletions src/test/java/co/comdor/github/ConversationTestCase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* Copyright (c) 2017, Mihai Emil Andronache
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1)Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2)Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3)Neither the name of comdor nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package co.comdor.github;

import co.comdor.Knowledge;
import co.comdor.Steps;
import org.junit.Test;
import org.mockito.Mockito;

import java.io.IOException;

/**
* Unit tests for {@link Conversation}
* @author Mihai Andronache (amihaiemil@gmail.com)
* @version $Id$
* @since 0.0.1
*/
public final class ConversationTestCase {

/**
* Conversation should try to understand the Mention before forwarding it
* to the next Knowledge.
* @throws Exception If something goes wrong.
*/
@Test
public void understandsBeforeForwarding() throws Exception {
final Language[] langs = {new English()};
final Conversation conv = new Conversation(
new Knowledge() {
@Override
public Steps start(final Mention mention) throws IOException {
Mockito.verify(mention).understand(langs);
return null;
}
},
langs
);
conv.start(Mockito.mock(Mention.class));
}
}
Loading

0 comments on commit 0234c93

Please sign in to comment.