|
| 1 | +plugins { |
| 2 | + id 'net.saliman.properties' version '1.4.6' |
| 3 | + id 'com.marklogic.ml-data-hub' version '4.2.2' |
| 4 | + id 'java' |
| 5 | +} |
| 6 | + |
| 7 | +repositories { |
| 8 | + jcenter() |
| 9 | + mavenLocal() |
| 10 | +} |
| 11 | + |
| 12 | +dependencies { |
| 13 | + mlRestApi "com.marklogic:marklogic-unit-test-modules:0.13.develop" |
| 14 | + |
| 15 | + testCompile "com.marklogic:marklogic-junit:0.13.develop" |
| 16 | + testCompile "com.marklogic:marklogic-data-hub:4.2.2" |
| 17 | + |
| 18 | + // Needed by Gradle 4.6+ |
| 19 | + testRuntime "org.junit.jupiter:junit-jupiter-engine:5.3.0" |
| 20 | +} |
| 21 | + |
| 22 | +// Needed by Gradle 4.6+ - see https://www.petrikainulainen.net/programming/testing/junit-5-tutorial-running-unit-tests-with-gradle/ |
| 23 | +test { |
| 24 | + useJUnitPlatform() |
| 25 | +} |
| 26 | + |
| 27 | + |
| 28 | +/** |
| 29 | + * Tasks for setting up a test database and a test app server that mirror either your final or staging database and |
| 30 | + * app server, and then loading hub and user modules via the test app server so that REST options are accessible to it. |
| 31 | + * Depends on the following properties being set: |
| 32 | + * |
| 33 | + * - mlTestDbFilename = the name of the file to use for constructing a database - either final-database.json or staging-database.json |
| 34 | + * - mlTestDbName = the name for the test database |
| 35 | + * - mlTestServerFilename = the name of the file to use for constructing an app server - either final-server.json or staging-server.json |
| 36 | + * - mlTestServerName = the name of the test app server |
| 37 | + * - mlTestPort = the port to assign to the test app server |
| 38 | + */ |
| 39 | + |
| 40 | +task hubDeployTestDatabase(type: com.marklogic.gradle.task.MarkLogicTask) { |
| 41 | + doLast { |
| 42 | + println "Deploying a test database with name ${mlTestDbName} based on configuration file named ${mlTestDbFilename}" |
| 43 | + new DeployHubTestDatabaseCommand(hubConfig, mlTestDbFilename, mlTestDbName).execute(mlCommandContext) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +task hubDeployTestServer(type: com.marklogic.gradle.task.MarkLogicTask) { |
| 48 | + doLast { |
| 49 | + println "Deploying a test server with name ${mlTestServerName} and port ${mlTestPort}, connected to content database ${mlTestDbName}, based on configuration file named ${mlTestServerFilename}" |
| 50 | + new DeployHubTestServerCommand(mlTestServerFilename, mlTestServerName, Integer.parseInt(mlTestPort), mlTestDbName).execute(mlCommandContext); |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +task testDeploy { |
| 55 | + description = "Deploy a test database and a test server" |
| 56 | + dependsOn = ["hubDeployTestDatabase", "hubDeployTestServer"] |
| 57 | +} |
| 58 | +hubDeployTestServer.mustRunAfter hubDeployTestDatabase |
| 59 | + |
| 60 | +task hubUndeployTestResources(type: com.marklogic.gradle.task.MarkLogicTask) { |
| 61 | + description = "Undeploys the test server and database that were created via testDeploy" |
| 62 | + doLast { |
| 63 | + mlAdminManager.invokeActionRequiringRestart({ |
| 64 | + new com.marklogic.mgmt.resource.appservers.ServerManager(mlManageClient).deleteByIdField(mlTestServerName) |
| 65 | + return true |
| 66 | + }) |
| 67 | + new com.marklogic.mgmt.resource.databases.DatabaseManager(mlManageClient).deleteByName(mlTestDbName) |
| 68 | + } |
| 69 | +} |
| 70 | +mlUndeploy.dependsOn hubUndeployTestResources |
| 71 | + |
| 72 | +import com.fasterxml.jackson.databind.ObjectMapper |
| 73 | +import com.fasterxml.jackson.databind.node.ObjectNode |
| 74 | +import com.fasterxml.jackson.databind.node.TextNode |
| 75 | +import com.marklogic.hub.HubConfig |
| 76 | + |
| 77 | +import java.util.regex.Pattern |
| 78 | + |
| 79 | +class DeployHubTestDatabaseCommand extends com.marklogic.hub.deploy.commands.DeployHubDatabaseCommand { |
| 80 | + String testDatabaseName |
| 81 | + |
| 82 | + DeployHubTestDatabaseCommand(HubConfig config, String databaseFilename, String testDatabaseName) { |
| 83 | + super(config, null, databaseFilename) |
| 84 | + this.testDatabaseName = testDatabaseName |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + protected String copyFileToString(File f) { |
| 89 | + String payload = super.copyFileToString(f) |
| 90 | + ObjectNode node = new ObjectMapper().readTree(payload) |
| 91 | + node.set("database-name", new TextNode(testDatabaseName)) |
| 92 | + return node.toString() |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +class DeployHubTestServerCommand extends com.marklogic.hub.deploy.commands.DeployHubOtherServersCommand { |
| 97 | + String serverName |
| 98 | + int port |
| 99 | + String contentDatabaseName |
| 100 | + |
| 101 | + DeployHubTestServerCommand(String serverFilenamePattern, String serverName, int port, String contentDatabaseName) { |
| 102 | + super() |
| 103 | + setResourceFilenamesIncludePattern(Pattern.compile(serverFilenamePattern)) |
| 104 | + this.serverName = serverName |
| 105 | + this.port = port |
| 106 | + this.contentDatabaseName = contentDatabaseName |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + protected String copyFileToString(File f) { |
| 111 | + String payload = super.copyFileToString(f) |
| 112 | + ObjectNode node = new ObjectMapper().readTree(payload) |
| 113 | + node.set("server-name", new TextNode(serverName)) |
| 114 | + node.set("port", new TextNode(port + "")) |
| 115 | + node.set("content-database", new TextNode(contentDatabaseName)) |
| 116 | + return node.toString() |
| 117 | + } |
| 118 | +} |
0 commit comments