Skip to content

Commit 2c768f1

Browse files
rjrudinjamesagardner
authored andcommitted
Merged marklogic-junit library into marklogic-unit-test (#66)
Bumped version to 0.13.develop. Once this is ready for 0.13.0 or 1.0.0, will need to update the references to 0.13.develop. Also made a fix in marklogic-unit-test-client/build.gradle so that createHttpCredentials isn't executed when any task is run, but only when it's run. See the README file in the marklogic-junit directory for more information.
1 parent 9b8932e commit 2c768f1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+2324
-12
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
group=com.marklogic
2-
version=0.12.0
2+
version=0.13.develop

marklogic-junit/README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
## Easy JUnit 5 testing with MarkLogic
2+
3+
Want to write JUnit 5 tests that verify the behavior of endpoints in [MarkLogic](https://www.marklogic.com/),
4+
including applications using the [Data Hub Framework](https://marklogic.github.io/marklogic-data-hub/)?
5+
This library makes that as simple as possible by providing the following support:
6+
7+
1. Connect to MarkLogic with the [MarkLogic Java Client](https://developer.marklogic.com/products/java) by reusing
8+
configuration you've already defined in your project
9+
1. Clear your test database before a test run so it always runs in a known state
10+
1. Easily read and make assertions on JSON and XML documents, including support for XPath-based assertions
11+
1. Easily integrate [marklogic-unit-test](https://github.com/marklogic-community/marklogic-unit-test) tests into a JUnit test suite
12+
13+
Below is a simple example of a JUnit test that writes a couple documents, runs a search on them, and then reads them
14+
back and verifies the contents of each document:
15+
16+
```
17+
public class SearchTest extends AbstractSpringMarkLogicTest {
18+
19+
@Test
20+
public void twoDocuments() {
21+
getDatabaseClient().newXMLDocumentManager().write("/test/1.xml", new StringHandle("<message>Hello world</message>"));
22+
getDatabaseClient().newJSONDocumentManager().write("/test/2.json", new StringHandle("{\"message\":\"Hello world\"}"));
23+
24+
QueryManager queryManager = getDatabaseClient().newQueryManager();
25+
SearchHandle searchHandle = queryManager.search(queryManager.newStringDefinition(), new SearchHandle());
26+
assertEquals(2, searchHandle.getTotalResults());
27+
28+
XmlNode xml = readXmlDocument("/test/1.xml");
29+
xml.assertElementValue("/message", "Hello world");
30+
31+
JsonNode json = readJsonDocument("/test/2.json");
32+
assertEquals("Hello world", json.get("message").asText());
33+
}
34+
}
35+
```
36+
37+
## Getting started on an ml-gradle project
38+
39+
If you'd like to use marklogic-junit on a regular ml-gradle project (not a DHF project), then
40+
start with the ml-gradle example project to see a working example with instructions on how to get started.
41+
42+
## Getting started on a Data Hub Framework project
43+
44+
If you're working on a Data Hub Framework (DHF) project and you're like to start writing JUnit tests to verify your application
45+
features, then check out the DHF example project to see a working example with instructions on how to get started.
46+

marklogic-junit/build.gradle

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
plugins {
2+
id "java"
3+
id "maven-publish"
4+
id "com.jfrog.bintray" version "1.8.0"
5+
id "com.github.jk1.dependency-license-report" version "0.3.11"
6+
id "net.saliman.properties" version "1.4.6"
7+
}
8+
9+
sourceCompatibility = "1.8"
10+
targetCompatibility = "1.8"
11+
12+
repositories {
13+
jcenter()
14+
}
15+
16+
dependencies {
17+
compile project(":marklogic-unit-test-client")
18+
compile "com.marklogic:ml-javaclient-util:3.11.0"
19+
compile "jaxen:jaxen:1.1.6"
20+
compile "org.junit.jupiter:junit-jupiter-api:5.4.1"
21+
compile "org.junit.jupiter:junit-jupiter-params:5.4.1"
22+
compile "org.springframework:spring-context:5.0.8.RELEASE"
23+
compile "org.springframework:spring-test:5.0.8.RELEASE"
24+
25+
// Support for DHF is provided, but a client must specify their own version of the DHF library to use
26+
compileOnly ("com.marklogic:marklogic-data-hub:4.2.2") {
27+
exclude module: "ml-javaclient-util"
28+
}
29+
30+
// Needed by Gradle 4.6+ - see https://www.petrikainulainen.net/programming/testing/junit-5-tutorial-running-unit-tests-with-gradle/
31+
testRuntime "org.junit.jupiter:junit-jupiter-engine:5.4.1"
32+
33+
// Forcing Spring to use logback instead of commons-logging
34+
testRuntime "ch.qos.logback:logback-classic:1.1.8"
35+
testRuntime group: "org.slf4j", name: "jcl-over-slf4j", version: "1.7.22"
36+
testRuntime group: "org.slf4j", name: "slf4j-api", version: "1.7.22"
37+
}
38+
39+
// Needed by Gradle 4.6+ - see https://www.petrikainulainen.net/programming/testing/junit-5-tutorial-running-unit-tests-with-gradle/
40+
test {
41+
useJUnitPlatform()
42+
}
43+
44+
task sourcesJar(type: Jar, dependsOn: classes) {
45+
classifier "sources"
46+
from sourceSets.main.allJava
47+
}
48+
49+
publishing {
50+
publications {
51+
mainJava(MavenPublication) {
52+
from components.java
53+
artifactId "marklogic-junit"
54+
}
55+
sourcesJava(MavenPublication) {
56+
from components.java
57+
artifactId "marklogic-junit"
58+
artifact sourcesJar
59+
}
60+
}
61+
}
62+
63+
if (project.hasProperty("myBintrayUser")) {
64+
bintray {
65+
user = myBintrayUser
66+
key = myBintrayKey
67+
publications = ["mainJava", "sourcesJava"]
68+
pkg {
69+
repo = "maven"
70+
name = project.name
71+
licenses = ["Apache-2.0"]
72+
vcsUrl = "https://github.com/marklogic-community/" + project.name + ".git"
73+
version {
74+
name = project.version
75+
released = new Date()
76+
}
77+
}
78+
}
79+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.gradle
2+
out
3+
build
4+
*.iml
5+
gradle-local.properties
6+
src/main
7+
gradle.properties
8+
.tmp
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
This project shows an example of using both marklogic-junit and marklogic-unit-test within a
2+
Data Hub Framework version 4 project.
3+
4+
To try this out locally, first initialize the DHF project:
5+
6+
./gradlew hubInit
7+
8+
Then add the following lines to gradle.properties:
9+
10+
```
11+
mlModulePaths=src/main/ml-modules,src/test/ml-modules
12+
mlTestDbName=data-hub-TEST
13+
mlTestDbFilename=final-database.json
14+
mlTestServerFilename=final-server.json
15+
mlTestServerName=data-hub-TEST
16+
mlTestPort=8015
17+
mlTestUsername=someuser
18+
mlTestPassword=someuser's password
19+
```
20+
21+
You can now deploy both the normal DHF application and the test resources defined in build.gradle via:
22+
23+
./gradlew mlDeploy testDeploy
24+
25+
There are two marklogic-unit-test test modules under ./src/main/ml-modules/root/test. You can run these
26+
via Gradle:
27+
28+
./gradlew test
29+
30+
And Gradle should report "BUILD SUCCESSFUL" as none of the tests should fail.
31+
32+
Or import this project into your favorite IDE and execute "RunDataHubUnitTestsTest". Each test module
33+
will be executed as a separate JUnit test.
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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+
}
Binary file not shown.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
distributionBase=GRADLE_USER_HOME
2+
distributionPath=wrapper/dists
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10-bin.zip
4+
zipStoreBase=GRADLE_USER_HOME
5+
zipStorePath=wrapper/dists

0 commit comments

Comments
 (0)