Skip to content

Instantly share code, notes, and snippets.

@rjrudin
Last active September 7, 2023 07:05
Show Gist options
  • Save rjrudin/ce347cd657b3768332c17641fdb12907 to your computer and use it in GitHub Desktop.
Save rjrudin/ce347cd657b3768332c17641fdb12907 to your computer and use it in GitHub Desktop.
Gradle tasks for setting up test resources in your MarkLogic Data Hub Framework 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 only works on DHF 3 projects, and it should work as well on 4.0.x projects.
// For 4.1 projects, see https://gist.github.com/rjrudin/df0ff09f5bff67833048e67f1ebaa5d6
/**
* 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 hubDeployTestResources".
*/
task hubDeployTestDatabase(type: com.marklogic.gradle.task.MarkLogicTask) {
doLast {
println "Deploying a test database with name ${mlTestDbName} based on configuration files 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 files named ${mlTestServerFilename}"
new DeployHubTestServerCommand(hubConfig, mlTestServerFilename, mlTestServerName, Integer.parseInt(mlTestPort), mlTestDbName).execute(mlCommandContext);
}
}
task hubLoadTestModules(type: com.marklogic.gradle.task.MarkLogicTask) {
description = "Load modules via the test REST server with a port defined by the mlTestPort property"
doLast {
// The staging properties are used for defining the connection for loading modules, so override
// additional properties (besides the port) here as needed
hubConfig.stagingPort = Integer.parseInt(mlTestPort)
// Need to set this so that final options are loaded into the test server to
hubConfig.finalPort = Integer.parseInt(mlTestPort)
new com.marklogic.hub.deploy.commands.LoadHubModulesCommand(hubConfig).execute(mlCommandContext)
new com.marklogic.hub.deploy.commands.LoadUserModulesCommand(hubConfig).execute(mlCommandContext)
}
}
// The timestamps file needs to be deleted so everything can be loaded, which ensures that REST options files are
// loaded via the test server
hubLoadTestModules.dependsOn mlDeleteModuleTimestampsFile
task hubDeployTestResources {
description = "Deploy a test database and a test server, and then load all of the modules via the test server so that REST options are available through it"
dependsOn = ["hubDeployTestDatabase", "hubDeployTestServer", "hubLoadTestModules"]
}
hubDeployTestServer.mustRunAfter hubDeployTestDatabase
hubLoadTestModules.mustRunAfter hubDeployTestServer
task hubUndeployTestResources(type: com.marklogic.gradle.task.MarkLogicTask) {
description = "Undeploys the test server and database that were created via hubDeployTestResources"
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.JsonNode
import com.fasterxml.jackson.databind.node.ObjectNode
import com.fasterxml.jackson.databind.node.TextNode
import com.marklogic.appdeployer.AppConfig
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, databaseFilename)
this.testDatabaseName = testDatabaseName
}
@Override
protected JsonNode mergeDatabaseFiles(AppConfig appConfig) {
ObjectNode objectNode = (ObjectNode) super.mergeDatabaseFiles(appConfig)
objectNode.set("database-name", new TextNode(testDatabaseName))
return objectNode
}
}
class DeployHubTestServerCommand extends com.marklogic.hub.deploy.commands.DeployHubServersCommand {
String serverName
int port
String contentDatabaseName
DeployHubTestServerCommand(HubConfig config, String serverFilenamePattern, String serverName, int port, String contentDatabaseName) {
super(config)
setResourceFilenamesIncludePattern(Pattern.compile(serverFilenamePattern))
this.serverName = serverName
this.port = port
this.contentDatabaseName = contentDatabaseName
}
@Override
protected JsonNode mergeServerFiles(File f) {
ObjectNode objectNode = (ObjectNode) super.mergeServerFiles(f)
objectNode.set("server-name", new TextNode(serverName))
objectNode.set("port", new TextNode(port + ""))
objectNode.set("content-database", new TextNode(contentDatabaseName))
return objectNode
}
}
@derms
Copy link

derms commented Aug 17, 2018

nice one @rjrudin

Btw one thing to consider if you want to create a test modules db also.

There is a file

/com.marklogic.hub/config.sjs

that contains

module.exports = {

STAGINGDATABASE: "data-hub-STAGING",

FINALDATABASE: "data-hub-FINAL",

TRACEDATABASE: "data-hub-TRACING",

JOBDATABASE: "data-hub-JOBS",

MODULESDATABASE: "data-hub-MODULES",

HUBVERSION: "3.0.0"

};

I think that it would need to be modified also

@rjrudin
Copy link
Author

rjrudin commented Sep 4, 2018

@derms I haven't run into an issue with that module yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment