Skip to content

Commit

Permalink
Merge pull request #69 from RediCloud/tests
Browse files Browse the repository at this point in the history
feat: test framework
  • Loading branch information
Suqatri authored Apr 9, 2024
2 parents bb5169e + 6a51f9e commit 3287321
Show file tree
Hide file tree
Showing 126 changed files with 1,172 additions and 316 deletions.
8 changes: 2 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ build/
!**/src/main/**/build/
!**/src/test/**/build/
redicloud-version.properties
/testing/
test-cloud/

### IntelliJ IDEA ###
.idea/
Expand All @@ -14,12 +16,6 @@ out/
!**/src/main/**/out/
!**/src/test/**/out/

### Test Files ###
/test/**/tmp/
/test/**/.libs/
/test/**/storage/
/test/**/*.jar

### Eclipse ###
.apt_generated
.classpath
Expand Down
24 changes: 14 additions & 10 deletions apis/base-api/src/main/kotlin/dev/redicloud/api/utils/CloudFiles.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,37 @@ val MINECRAFT_VERSIONS_FOLDER = CloudFile("versions", "storage", folder = true)
val TEMPLATE_FOLDER = CloudFile("templates", "storage", folder = true)
val DATABASE_JSON = CloudFile("database.json", "storage")
val CONNECTORS_FOLDER = CloudFile("connectors", "storage", folder = true)
val MODULE_FOLDER = CloudFile("modules", "storage", folder = true)
val MODULES_FOLDER = CloudFile("modules", "storage", folder = true)

fun toCloudFile(universalPath: String): File {
return File(CLOUD_PATH, universalPath)
}

fun toUniversalPath(file: File): String {
fun toUniversalPath(file: File, forceSeperator: String = File.separator): String {
val path = file.absolutePath.replace(CLOUD_PATH, "")
return if (path.startsWith(File.separator)) path.replaceFirst(File.separator, "") else path
.replace(File.separator, forceSeperator)
return if (path.startsWith(forceSeperator)) {
path.replaceFirst(forceSeperator, "")
} else path
}

class CloudFile(val name: String, val parent: String = "", val folder: Boolean = false) {

fun getCloudPath(): String {
fun getCloudPath(cloudFolder: File? = null, separator: String = File.separator): String {
val prefixPath = cloudFolder?.absolutePath ?: CLOUD_PATH
return if (parent.isEmpty()) {
CLOUD_PATH + File.separator + name
prefixPath + separator + name
}else {
CLOUD_PATH + File.separator + parent + File.separator + name
prefixPath + separator + parent + separator + name
}
}

fun getFile(): File {
return File(getCloudPath())
fun getFile(cloudFolder: File? = null): File {
return File(getCloudPath(cloudFolder))
}

fun createIfNotExists(): File {
val file = getFile()
fun createIfNotExists(cloudFolder: File? = null): File {
val file = getFile(cloudFolder)
if (file.exists()) return file
if (!file.parentFile.exists()) file.parentFile.mkdirs()
if (folder) {
Expand Down
23 changes: 9 additions & 14 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ allprojects {
dependency(BuildDependencies.KHTTP)
dependency(BuildDependencies.KOTLIN_REFLECT)
dependency(BuildDependencies.GUICE)

testImplementation(BuildDependencies.DOCKER_TEST_CONTAINERS)
testImplementation(BuildDependencies.GSON)
testImplementation(BuildDependencies.LOGBACK_CORE)
testImplementation(BuildDependencies.LOGBACK_CLASSIC)
testImplementation(project(":utils"))
testImplementation(project(":apis:base-api"))
testImplementation(project(":database"))
testImplementation(project(":services:node-service"))
}

tasks {
Expand Down Expand Up @@ -90,18 +99,4 @@ allprojects {
}
}

}

tasks.register("buildCloudAndCopy") {
project.allprojects.forEach {
if (it == it.rootProject) return@forEach
try {
val task = it.tasks.named("buildAndCopy")
dependsOn(task)
println("Project ${it.name} has ${task.name} task!")
}catch (_: UnknownDomainObjectException) {
println("Project ${it.name} has no buildAndCopy task! Use default build task instead.")
dependsOn(it.tasks.named("build"))
}
}
}
1 change: 1 addition & 0 deletions buildSrc/src/main/kotlin/BuildDependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ object BuildDependencies {
const val JLINE_CONSOLE = "org.jline:jline-console:3.25.1"
const val JLINE_JANSI = "org.jline:jline-terminal-jansi:3.25.1"

const val DOCKER_TEST_CONTAINERS = "org.testcontainers:testcontainers:1.19.7"
const val BCPROV = "org.bouncycastle:bcprov-jdk15on:1.70"
const val BCPKIX = "org.bouncycastle:bcpkix-jdk15on:1.70"
}
Expand Down
28 changes: 0 additions & 28 deletions connectors/bukkit-connector/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -44,34 +44,6 @@ dependencies {
shade(project(":connectors:bukkit-legacy"))
}

tasks.register("buildAndCopy") {
dependsOn(tasks.named("build"))
doLast {
val outputJar = Builds.getOutputFileName(project) + ".jar"
val original = File(project.buildDir.resolve("libs"), outputJar)
val outputJarFile = File(project.buildDir.resolve("libs"), "${original.nameWithoutExtension}-local.jar")
original.renameTo(outputJarFile)
if (original.exists()) {
original.delete()
}
for (i in 1..Builds.testNodes) {
val id = if (i in 1..9) "0$i" else i.toString()
val path = File(Builds.getTestDirPath(project, "node$id"), "storage/connectors")
if (!path.exists()) {
path.mkdirs()
}
val targetJar = File(path, outputJarFile.name)
if (targetJar.exists()) {
targetJar.delete()
}
project.copy {
from(outputJarFile)
into(path)
}
}
}
}

val shadowModJar by tasks.creating(ShadowJar::class) {
archiveFileName.set(Builds.getOutputFileName(project) + "-shadow.jar")

Expand Down
28 changes: 0 additions & 28 deletions connectors/bungeecord-connector/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -35,31 +35,3 @@ dependencies {

compileOnly(BuildDependencies.BUNGEECORD_API)
}

tasks.register("buildAndCopy") {
dependsOn(tasks.named("build"))
doLast {
val outputJar = Builds.getOutputFileName(project) + ".jar"
val original = File(project.buildDir.resolve("libs"), outputJar)
val outputJarFile = File(project.buildDir.resolve("libs"), "${original.nameWithoutExtension}-local.jar")
original.renameTo(outputJarFile)
if (original.exists()) {
original.delete()
}
for (i in 1..Builds.testNodes) {
val id = if (i in 1..9) "0$i" else i.toString()
val path = File(Builds.getTestDirPath(project, "node$id"), "storage/connectors")
if (!path.exists()) {
path.mkdirs()
}
val targetJar = File(path, outputJarFile.name)
if (targetJar.exists()) {
targetJar.delete()
}
project.copy {
from(outputJarFile)
into(path)
}
}
}
}
29 changes: 0 additions & 29 deletions connectors/minestom-connector/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -46,35 +46,6 @@ dependencies {
shade(project(":server-factories:remote-server-factory"))
shade(project(":apis:connector-api"))
shade(BuildDependencies.CLOUD_LIBLOADER_BOOTSTRAP)

compileOnly(BuildDependencies.MINESTOM_API)
compileOnly(BuildDependencies.MINESTOM_EXTENSIONS)
}

tasks.register("buildAndCopy") {
dependsOn(tasks.named("build"))
doLast {
val outputJar = Builds.getOutputFileName(project) + ".jar"
val original = File(project.buildDir.resolve("libs"), outputJar)
val outputJarFile = File(project.buildDir.resolve("libs"), "${original.nameWithoutExtension}-local.jar")
original.renameTo(outputJarFile)
if (original.exists()) {
original.delete()
}
for (i in 1..Builds.testNodes) {
val id = if (i in 1..9) "0$i" else i.toString()
val path = File(Builds.getTestDirPath(project, "node$id"), "storage/connectors")
if (!path.exists()) {
path.mkdirs()
}
val targetJar = File(path, outputJarFile.name)
if (targetJar.exists()) {
targetJar.delete()
}
project.copy {
from(outputJarFile)
into(path)
}
}
}
}
28 changes: 0 additions & 28 deletions connectors/velocity-connector/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -40,31 +40,3 @@ dependencies {
compileOnly(BuildDependencies.VELOCITY_API)
kapt(BuildDependencies.VELOCITY_API)
}

tasks.register("buildAndCopy") {
dependsOn(tasks.named("build"))
doLast {
val outputJar = Builds.getOutputFileName(project) + ".jar"
val original = File(project.buildDir.resolve("libs"), outputJar)
val outputJarFile = File(project.buildDir.resolve("libs"), "${original.nameWithoutExtension}-local.jar")
original.renameTo(outputJarFile)
if (original.exists()) {
original.delete()
}
for (i in 1..Builds.testNodes) {
val id = if (i in 1..9) "0$i" else i.toString()
val path = File(Builds.getTestDirPath(project, "node$id"), "storage/connectors")
if (!path.exists()) {
path.mkdirs()
}
val targetJar = File(path, outputJarFile.name)
if (targetJar.exists()) {
targetJar.delete()
}
project.copy {
from(outputJarFile)
into(path)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,15 @@ class DatabaseConnection(
.setCodec(GsonCodec)
if (config.isCluster()) {
val clusterConfig = redissonConfig.useClusterServers()
.setClientName(serviceId.toName()).also {
if(config.password.isNotEmpty()) it.setPassword(config.password)
.setClientName(serviceId.toName())
.also {
if(!config.password.isNullOrEmpty()) {
it.setPassword(config.password)
}
if (!config.username.isNullOrEmpty()) {
it.setUsername(config.username)
}
}
if (!config.username.isNullOrEmpty()) {
clusterConfig.setUsername(config.username)
}
config.nodes.forEach { node ->
clusterConfig.addNodeAddress(node.toConnectionString())
}
Expand All @@ -70,8 +73,11 @@ class DatabaseConnection(
.setConnectionPoolSize(connectionPoolSize)
.setSubscriptionConnectionPoolSize(subscriptionConnectionPoolSize)
.setConnectionMinimumIdleSize(connectionMinimumIdleSize)
.setSubscriptionConnectionMinimumIdleSize(subscriptionConnectionMinimumIdleSize).also {
if(config.password.isNotEmpty()) it.setPassword(config.password)
.setSubscriptionConnectionMinimumIdleSize(subscriptionConnectionMinimumIdleSize)
.also {
if(!config.password.isNullOrEmpty()) {
it.setPassword(config.password)
}
}
if (!config.username.isNullOrEmpty()) {
singleConfig.setUsername(config.username)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import java.io.File

data class DatabaseConfiguration(
val username: String? = "",
val password: String = "",
val password: String? = "",
val nodes: List<DatabaseNode>,
val databaseId: Int = 0
) {
Expand Down
28 changes: 0 additions & 28 deletions examples/example-module/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,3 @@ dependencies {

compileOnly(BuildDependencies.SPIGOT_API)
}

tasks.register("buildAndCopy") {
dependsOn(tasks.named("build"))
doLast {
val outputJar = Builds.getOutputFileName(project) + ".jar"
val original = File(project.buildDir.resolve("libs"), outputJar)
val outputJarFile = File(project.buildDir.resolve("libs"), "${Builds.getOutputModuleFileName(project)}.jar")
original.renameTo(outputJarFile)
if (original.exists()) {
original.delete()
}
for (i in 1..Builds.testNodes) {
val id = if (i in 1..9) "0$i" else i.toString()
val path = File(Builds.getTestDirPath(project, "node$id"), "storage/modules")
if (!path.exists()) {
path.mkdirs()
}
val targetJar = File(path, outputJarFile.name)
if (targetJar.exists()) {
targetJar.delete()
}
project.copy {
from(outputJarFile)
into(path)
}
}
}
}
28 changes: 0 additions & 28 deletions examples/example-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,4 @@ dependencies {
// compileOnly("dev.redicloud:api:<cloud-version>")

compileOnly(BuildDependencies.SPIGOT_API)
}

tasks.register("buildAndCopy") {
dependsOn(tasks.named("build"))
doLast {
val outputJar = Builds.getOutputFileName(project) + ".jar"
val original = File(project.buildDir.resolve("libs"), outputJar)
val outputJarFile = File(project.buildDir.resolve("libs"), "${original.nameWithoutExtension}-local.jar")
original.renameTo(outputJarFile)
if (original.exists()) {
original.delete()
}
for (i in 1..Builds.testNodes) {
val id = if (i in 1..9) "0$i" else i.toString()
val path = File(Builds.getTestDirPath(project, "node$id"), "storage/templates/example/bukkit/plugins")
if (!path.exists()) {
path.mkdirs()
}
val targetJar = File(path, outputJarFile.name)
if (targetJar.exists()) {
targetJar.delete()
}
project.copy {
from(outputJarFile)
into(path)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.google.inject.name.Named
import dev.redicloud.api.modules.*
import dev.redicloud.api.service.ServiceId
import dev.redicloud.api.utils.CloudInjectable
import dev.redicloud.api.utils.MODULE_FOLDER
import dev.redicloud.api.utils.MODULES_FOLDER
import dev.redicloud.api.utils.injector
import dev.redicloud.api.version.ICloudServerVersionType
import dev.redicloud.commands.api.SUGGESTERS
Expand Down Expand Up @@ -176,7 +176,7 @@ class ModuleHandler(

fun detectModules() = lock.withLock {
moduleFiles.clear()
MODULE_FOLDER.getFile().listFiles()?.filter {
MODULES_FOLDER.getFile().listFiles()?.filter {
it.isFile && it.extension == "jar"
}?.filter {
val jarFile = JarFile(it)
Expand Down
Loading

0 comments on commit 3287321

Please sign in to comment.