Skip to content

Commit

Permalink
#360 Can now configure connection in mlUnitTest
Browse files Browse the repository at this point in the history
  • Loading branch information
rjrudin committed Jul 9, 2018
1 parent 1283da6 commit 38c5edb
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
33 changes: 31 additions & 2 deletions examples/unit-test-project/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
This project shows an example of using the ml-unit-test framework to run server-side tests within MarkLogic.

## Enabling ml-unit-test in an ml-gradle project

Using ml-unit-test requires two additions to the build.gradle file, as described below.

First, ml-gradle includes an "mlUnitTest" task, which depends on the ml-unit-test-client JAR file. ml-gradle does not
Expand All @@ -25,8 +27,10 @@ is a feature of ml-gradle for depending on packages of MarkLogic modules):
mlRestApi "com.marklogic:ml-unit-test-modules:0.11.1"
}

With those additions in place, the "mlUnitTest" task can be run. This task will use the value of mlTestRestPort to
determine which MarkLogic app server to connect to.
## Running unit tests

With the above additions in place, the "mlUnitTest" task can be run. This task will use the value of mlTestRestPort to
determine which MarkLogic app server to connect to - see below for how to customize this.

First, deploy the application:

Expand Down Expand Up @@ -56,3 +60,28 @@ You can also access the ml-unit-test REST endpoints directly:
And you can run the original UI test runner by going to:

- http://localhost:8135/test/default.xqy

## Configuring which server mlUnitTest connects to

Prior to ml-gradle 3.8.1, the mlUnitTest task will connect to mlTestRestPort if it's set, else mlRestPort.

Starting in release 3.8.1, you can configure which REST API server mlUnitTest will connect to. The mlUnitTest task now
exposes a property of type [DatabaseClientConfig](https://github.com/marklogic-community/ml-javaclient-util/blob/master/src/main/java/com/marklogic/client/ext/DatabaseClientConfig.java).
You can configure the properties of this object, and mlUnitTest will use it for creating a connection to MarkLogic.

Below is an example - note that you need to configure every property necessary for the type of connection you want, as
none of the properties of the DatabaseClientConfig have any default value:

mlUnitTest.databaseClientConfig.host = mlHost
mlUnitTest.databaseClientConfig.port = 8880 // probably a port that differs from mlRestPort and mlTestRestPort
mlUnitTest.databaseClientConfig.username = mlUsername
mlUnitTest.databaseClientConfig.password = mlPassword
// Other properties that can be set
// mlUnitTest.databaseClientConfig.securityContextType
// mlUnitTest.databaseClientConfig.database
// mlUnitTest.databaseClientConfig.sslContext
// mlUnitTest.databaseClientConfig.sslHostnameVerifier
// mlUnitTest.databaseClientConfig.certFile
// mlUnitTest.databaseClientConfig.certPassword
// mlUnitTest.databaseClientConfig.externalName
// mlUnitTest.databaseClientConfig.trustManager
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.marklogic.gradle.task.test

import com.marklogic.client.ext.DatabaseClientConfig
import com.marklogic.gradle.task.MarkLogicTask
import com.marklogic.test.unit.DefaultJUnitTestReporter
import com.marklogic.test.unit.JUnitTestSuite
Expand All @@ -13,6 +14,8 @@ import org.gradle.api.tasks.TaskAction
*/
class UnitTestTask extends MarkLogicTask {

DatabaseClientConfig databaseClientConfig = new DatabaseClientConfig()

@TaskAction
void runUnitTests() {
try {
Expand All @@ -21,8 +24,21 @@ class UnitTestTask extends MarkLogicTask {
def message = "This task requires the com.marklogic:marklogic-unit-test-client library to be a buildscript dependency"
throw new GradleException(message)
}

def appConfig = getAppConfig()
def client = appConfig.getTestRestPort() != null ? appConfig.newTestDatabaseClient() : appConfig.newDatabaseClient()

def client
if (databaseClientConfig.getPort() != null && databaseClientConfig.getPort() > 0) {
println "Constructing DatabaseClient based on settings in the databaseClientConfig task property"
client = appConfig.configuredDatabaseClientFactory.newDatabaseClient(databaseClientConfig)
} else if (appConfig.getTestRestPort() != null) {
println "Constructing DatabaseClient that will connect to port: " + appConfig.getTestRestPort()
client = appConfig.newTestDatabaseClient()
} else {
println "Constructing DatabaseClient that will connect to port: " + appConfig.getRestPort()
client = appConfig.newDatabaseClient()
}

try {
def testManager = new TestManager(client)

Expand Down

0 comments on commit 38c5edb

Please sign in to comment.