Skip to content
This repository was archived by the owner on Aug 18, 2020. It is now read-only.

Commit 711c592

Browse files
author
Jonas
committed
Add testall plugin (see codeoverflow-org/chatoverflow#42 )
Deleted discord test (moved to testall plugin)
1 parent 01585b7 commit 711c592

File tree

7 files changed

+157
-109
lines changed

7 files changed

+157
-109
lines changed

discordtest/src/main/scala/discordTest.scala

Lines changed: 0 additions & 49 deletions
This file was deleted.
Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +0,0 @@
1-
import java.awt.Color
2-
3-
import org.codeoverflow.chatoverflow.api.io.dto.chat.discord.DiscordEmbed
4-
import org.codeoverflow.chatoverflow.api.io.event.chat.discord.{DiscordChatMessageDeleteEvent, DiscordChatMessageEditEvent, DiscordChatMessageReceiveEvent}
5-
import org.codeoverflow.chatoverflow.api.plugin.{PluginImpl, PluginManager}
6-
7-
import scala.collection.JavaConverters._
8-
9-
class discordTestPlugin(manager: PluginManager) extends PluginImpl(manager) {
10-
11-
private val discordInputReq = require.input.discordChat("reqDiscordIn", "Discord input", false)
12-
private val discordOutputReq = require.output.discordChat("reqDiscordOut", "Discord output", false)
13-
private val discordChannelReq = require.parameter.string("reqDiscordChannel", "The id of the channel to which the bot should connect", false)
14-
15-
override def setup(): Unit = {
16-
discordInputReq.get.setChannel(discordChannelReq.get.get)
17-
discordOutputReq.get.setChannel(discordChannelReq.get.get)
18-
println(s"Input connected to channel ${discordInputReq.get().getChannelId}")
19-
println(s"Output connected to channel ${discordOutputReq.get().getChannelId}")
20-
discordInputReq.get.registerChatMessageReceiveEventHandler(onMessage)
21-
discordInputReq.get.registerChatMessageEditEventHandler(onMessageEdit)
22-
discordInputReq.get.registerChatMessageDeleteEventHandler(onMessageDelete)
23-
discordOutputReq.get.sendChatMessage("Hey I'm working! \uD83C\uDF89")
24-
discordOutputReq.get().sendFile("../config/config.xml")
25-
discordOutputReq.get().sendFile("allowed_file.png")
26-
discordOutputReq.get().sendChatMessage(DiscordEmbed.Builder()
27-
.withColor(Color.RED)
28-
.withDescription("test")
29-
.withAuthor("skateShiny", null, "https://cdn.discordapp.com/emojis/496389587329875981.png?v=1")
30-
.build())
31-
println("Startet succesfully")
32-
}
33-
34-
def onMessage(event: DiscordChatMessageReceiveEvent): Unit = {
35-
if (event.getMessage.getMessage == "/messages") {
36-
var s = "**Recent messages:**\n"
37-
s += discordInputReq.get.getLastMessages(1000 * 60).asScala.filter(_.getAuthor.getId != discordTestPlugin.BOT_ID).mkString("\n")
38-
s += "\n\n**Recent private messages:**\n"
39-
s += discordInputReq.get().getLastPrivateMessages(1000 * 60).asScala.mkString("\n")
40-
discordOutputReq.get.sendChatMessage(s)
41-
}
42-
}
43-
44-
def onMessageEdit(event: DiscordChatMessageEditEvent): Unit = {
45-
println(s"Message #${event.getMessage.getId} was edited from '${event.getOldMessage.toString}' to '${event.getMessage.toString}'")
46-
}
47-
48-
def onMessageDelete(message: DiscordChatMessageDeleteEvent): Unit = {
49-
println(s"Message #${message.getMessage.getId} was deleted (content: $message)")
50-
}
51-
52-
override def loop(): Unit = {}
53-
54-
override def shutdown(): Unit = println("Plugin stopped")
55-
}
56-
57-
object discordTestPlugin {
58-
val BOT_ID = "572870096356376576"
59-
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
// GENERATED FILE USING THE CHAT OVERFLOW PLUGIN FRAMEWORK
22

3-
name := "discordTest"
3+
name := "testall"
44
version := "0.1"
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package org.codeoverflow.plugins.testall
2+
3+
import org.codeoverflow.chatoverflow.api.plugin.configuration.Requirement
4+
5+
abstract class test(private val plugin: testallPlugin, val requirements : Requirement[_]*) {
6+
7+
/**
8+
* Used on setup to determine if the testallPlugin should run this test.
9+
*
10+
* By default it only checks if the supplied requirements are set.
11+
*
12+
* Override to change this behaviour
13+
*
14+
* @return true if the test should be run
15+
*/
16+
def runTest: Boolean = requirements.forall(_.isSet)
17+
18+
/**
19+
* The name of the test for displaying in log
20+
*
21+
* @return the name
22+
*/
23+
def name: String
24+
25+
/**
26+
* Perform the tests that should be executed on startup
27+
*/
28+
def setup(): Unit
29+
30+
/**
31+
* Perform the tests that should be executed in a loop
32+
*/
33+
def loop(): Unit
34+
35+
/**
36+
* Perform the tests that should be executed at shutdown and close everything.
37+
*/
38+
def shutdown(): Unit
39+
40+
/**
41+
* Prints a log message on the console and saves the message for later inspection.
42+
*
43+
* @param message the message to show
44+
*/
45+
protected final def log(message: String): Unit = plugin.getManager.log(message)
46+
47+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.codeoverflow.plugins.testall
2+
3+
import org.codeoverflow.chatoverflow.api.plugin.{PluginImpl, PluginManager}
4+
import org.codeoverflow.plugins.testall.tests.discordtest
5+
6+
7+
class testallPlugin(manager: PluginManager) extends PluginImpl(manager) {
8+
9+
private val tests: Seq[test] = Seq(
10+
new discordtest(this,
11+
require.input.discordChat("reqDiscordIn", "Discord input", true),
12+
require.output.discordChat("reqDiscordOut", "Discord output", true),
13+
require.parameter.string("reqDiscordChannel", "The id of the channel to which the bot should connect", true)
14+
)
15+
)
16+
17+
private var running = Seq[test]()
18+
19+
override def setup(): Unit = {
20+
running = tests.filter(test => {
21+
log(s"${test.name}: ${if (test.runTest) "testing" else "skipping (not all requirements are set)"}")
22+
test.runTest
23+
})
24+
running.foreach(_.setup())
25+
}
26+
27+
override def loop(): Unit = running.foreach(_.loop())
28+
29+
override def shutdown(): Unit = running.foreach(_.shutdown())
30+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package org.codeoverflow.plugins.testall.tests
2+
3+
import java.awt.Color
4+
5+
import org.codeoverflow.chatoverflow.api.io.dto.chat.discord.{DiscordChatMessage, DiscordEmbed}
6+
import org.codeoverflow.chatoverflow.api.io.input.chat.DiscordChatInput
7+
import org.codeoverflow.chatoverflow.api.io.output.chat.DiscordChatOutput
8+
import org.codeoverflow.chatoverflow.api.io.parameter.StringParameter
9+
import org.codeoverflow.chatoverflow.api.plugin.configuration.Requirement
10+
import org.codeoverflow.plugins.testall.{test, testallPlugin}
11+
12+
import scala.collection.JavaConverters._
13+
14+
class discordtest(val plugin: testallPlugin,
15+
val in: Requirement[DiscordChatInput],
16+
val out: Requirement[DiscordChatOutput],
17+
val channel: Requirement[StringParameter]) extends test(plugin, in, out) {
18+
19+
override def name: String = "Discord test"
20+
21+
override def setup(): Unit = {
22+
in.get.setChannel(channel.get.get)
23+
out.get.setChannel(channel.get.get)
24+
println(s"Input connected to channel ${in.get().getChannelId}")
25+
println(s"Output connected to channel ${out.get().getChannelId}")
26+
in.get.registerMessageHandler(onMessage)
27+
in.get.registerMessageEditHandler(onMessageEdit)
28+
in.get.registerMessageDeleteHandler(onMessageDelete)
29+
out.get.sendChatMessage("Hey I'm working! \uD83C\uDF89")
30+
out.get().sendFile("../config/config.xml")
31+
out.get().sendFile("allowed_file.png")
32+
out.get().sendChatMessage(DiscordEmbed.Builder()
33+
.withColor(Color.RED)
34+
.withDescription("test")
35+
.withAuthor("skateShiny", null, "https://cdn.discordapp.com/emojis/496389587329875981.png?v=1")
36+
.build())
37+
log(s"$name started successfully")
38+
}
39+
40+
def onMessage(message: DiscordChatMessage): Unit = {
41+
if (message.getMessage == "/messages") {
42+
var s = "**Recent messages:**\n"
43+
s += in.get.getLastMessages(1000*60).asScala.mkString("\n")
44+
s += "\n\n**Recent private messages:**\n"
45+
s += in.get().getLastPrivateMessages(1000*60).asScala.mkString("\n")
46+
out.get.sendChatMessage(s)
47+
}
48+
}
49+
50+
51+
def onMessageEdit(oldMessage: DiscordChatMessage, newMessage: DiscordChatMessage): Unit = {
52+
log(s"$name: Message #${oldMessage.getId} was edited from '$oldMessage' to '$newMessage'")
53+
}
54+
55+
def onMessageDelete(message: DiscordChatMessage): Unit = {
56+
log(s"$name: Message #${message.getId} was deleted (content: $message)")
57+
}
58+
59+
override def loop(): Unit = {}
60+
61+
override def shutdown(): Unit = log(s"Stopped $name")
62+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import org.codeoverflow.chatoverflow.api.plugin.{Pluggable, Plugin, PluginManager}
2+
import org.codeoverflow.plugins.testall.testallPlugin
3+
4+
class testall extends Pluggable {
5+
6+
override def getName: String = "testall"
7+
8+
override def getAuthor: String = "joblo2213"
9+
10+
override def getDescription: String = "A plugin to simplify testing of all services"
11+
12+
override def getMajorAPIVersion: Int = 1
13+
14+
override def getMinorAPIVersion: Int = 0
15+
16+
override def createNewPluginInstance(manager: PluginManager): Plugin = new testallPlugin(manager)
17+
}

0 commit comments

Comments
 (0)