-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Confused, Conversation, Hello and GithubSteps + unit tests
- Loading branch information
1 parent
65f0d5a
commit 0234c93
Showing
10 changed files
with
412 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
Oops, something went wrong.