Skip to content

Instantly share code, notes, and snippets.

@rjrudin
Last active April 2, 2019 17:13
Show Gist options
  • Save rjrudin/df0ff09f5bff67833048e67f1ebaa5d6 to your computer and use it in GitHub Desktop.
Save rjrudin/df0ff09f5bff67833048e67f1ebaa5d6 to your computer and use it in GitHub Desktop.
Gradle configuration for setting up test resources on a MarkLogic Data Hub Framework (DHF) 4.1 project
// Paste everything below this comment block into the bottom of the build.gradle file in your DHF project.
// Read the comments to see how this works and what properties you'll need to define.
// Note - this works on DHF 4.1 projects. See https://gist.github.com/rjrudin/ce347cd657b3768332c17641fdb12907 for
// DHF 3 and 4.0.x projects.
/**
* Tasks for setting up a test database and a test app server that mirror either your final or staging database and
* app server, and then loading hub and user modules via the test app server so that REST options are accessible to it.
* Depends on the following properties being set:
*
* - mlTestDbFilename = the name of the file to use for constructing a database - either final-database.json or staging-database.json
* - mlTestDbName = the name for the test database
* - mlTestServerFilename = the name of the file to use for constructing an app server - either final-server.json or staging-server.json
* - mlTestServerName = the name of the test app server
* - mlTestPort = the port to assign to the test app server
*
* Examples of setting these properties:
*
* mlTestDbName=data-hub-TEST
* mlTestDbFilename=final-database.json
* mlTestServerFilename=final-server.json
* mlTestServerName=data-hub-TEST
* mlTestPort=8015
*
* The easiest way to deploy the test resources is to run "gradle testDeploy" (assuming you've run mlDeploy already).
*/
task hubDeployTestDatabase(type: com.marklogic.gradle.task.MarkLogicTask) {
doLast {
println "Deploying a test database with name ${mlTestDbName} based on configuration file named ${mlTestDbFilename}"
new DeployHubTestDatabaseCommand(hubConfig, mlTestDbFilename, mlTestDbName).execute(mlCommandContext)
}
}
task hubDeployTestServer(type: com.marklogic.gradle.task.MarkLogicTask) {
doLast {
println "Deploying a test server with name ${mlTestServerName} and port ${mlTestPort}, connected to content database ${mlTestDbName}, based on configuration file named ${mlTestServerFilename}"
new DeployHubTestServerCommand(mlTestServerFilename, mlTestServerName, Integer.parseInt(mlTestPort), mlTestDbName).execute(mlCommandContext);
}
}
task testDeploy {
description = "Deploy a test database and a test server"
dependsOn = ["hubDeployTestDatabase", "hubDeployTestServer"]
}
hubDeployTestServer.mustRunAfter hubDeployTestDatabase
task hubUndeployTestResources(type: com.marklogic.gradle.task.MarkLogicTask) {
description = "Undeploys the test server and database that were created via testDeploy"
doLast {
mlAdminManager.invokeActionRequiringRestart({
new com.marklogic.mgmt.resource.appservers.ServerManager(mlManageClient).deleteByIdField(mlTestServerName)
return true
})
new com.marklogic.mgmt.resource.databases.DatabaseManager(mlManageClient).deleteByName(mlTestDbName)
}
}
mlUndeploy.dependsOn hubUndeployTestResources
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.node.ObjectNode
import com.fasterxml.jackson.databind.node.TextNode
import com.marklogic.hub.HubConfig
import java.util.regex.Pattern
class DeployHubTestDatabaseCommand extends com.marklogic.hub.deploy.commands.DeployHubDatabaseCommand {
String testDatabaseName
DeployHubTestDatabaseCommand(HubConfig config, String databaseFilename, String testDatabaseName) {
super(config, null, databaseFilename)
this.testDatabaseName = testDatabaseName
}
@Override
protected String copyFileToString(File f) {
String payload = super.copyFileToString(f)
ObjectNode node = new ObjectMapper().readTree(payload)
node.set("database-name", new TextNode(testDatabaseName))
return node.toString()
}
}
class DeployHubTestServerCommand extends com.marklogic.hub.deploy.commands.DeployHubOtherServersCommand {
String serverName
int port
String contentDatabaseName
DeployHubTestServerCommand(String serverFilenamePattern, String serverName, int port, String contentDatabaseName) {
super()
setResourceFilenamesIncludePattern(Pattern.compile(serverFilenamePattern))
this.serverName = serverName
this.port = port
this.contentDatabaseName = contentDatabaseName
}
@Override
protected String copyFileToString(File f) {
String payload = super.copyFileToString(f)
ObjectNode node = new ObjectMapper().readTree(payload)
node.set("server-name", new TextNode(serverName))
node.set("port", new TextNode(port + ""))
node.set("content-database", new TextNode(contentDatabaseName))
return node.toString()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment