Skip to content

Commit a37b359

Browse files
committed
MLE-26420 First test using marklogic-junit5
This doesn't test incremental writes yet, but it will soon. Just want to get a "real" test in place that wipes data from the database before it starts. Also toying with a template approach for WriteBatcher, so made DataMovementManager Closeable which is a non-breaking change.
1 parent dcb8064 commit a37b359

File tree

4 files changed

+106
-6
lines changed

4 files changed

+106
-6
lines changed

marklogic-client-api/src/main/java/com/marklogic/client/datamovement/DataMovementManager.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.marklogic.client.io.marker.ContentHandle;
88
import com.marklogic.client.query.*;
99

10+
import java.io.Closeable;
1011
import java.util.Iterator;
1112

1213
/**
@@ -33,7 +34,15 @@
3334
* dataMovementManager.release();
3435
*}</pre>
3536
*/
36-
public interface DataMovementManager {
37+
public interface DataMovementManager extends Closeable {
38+
39+
/**
40+
* @since 8.1.0
41+
*/
42+
default void close() {
43+
release();
44+
}
45+
3746
/** Calls release() on all host-specific DatabaseClient instances (but not on
3847
* the DatabaseClient instance used to create this DataMovementManager
3948
* instance).
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright (c) 2010-2025 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved.
3+
*/
4+
package com.marklogic.client.test;
5+
6+
import com.marklogic.client.DatabaseClient;
7+
import com.marklogic.junit5.AbstractMarkLogicTest;
8+
9+
/**
10+
* Intended to be the base class for all future client API tests, as it properly prepares the database by deleting
11+
* documents from previous test runs that were not created as part of deploying the test app.
12+
*/
13+
public abstract class AbstractClientTest extends AbstractMarkLogicTest {
14+
15+
@Override
16+
protected final DatabaseClient getDatabaseClient() {
17+
return Common.newServerAdminClient();
18+
}
19+
20+
@Override
21+
protected final String getJavascriptForDeletingDocumentsBeforeTestRuns() {
22+
return """
23+
declareUpdate();
24+
cts.uris('', [], cts.notQuery(cts.collectionQuery(['test-data', 'temporal-collection'])))
25+
.toArray().forEach(item => xdmp.documentDelete(item))
26+
""";
27+
}
28+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright (c) 2010-2025 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved.
3+
*/
4+
package com.marklogic.client.test.datamovement;
5+
6+
import com.marklogic.client.DatabaseClient;
7+
import com.marklogic.client.datamovement.DataMovementManager;
8+
import com.marklogic.client.datamovement.WriteBatcher;
9+
import com.marklogic.client.io.DocumentMetadataHandle;
10+
import com.marklogic.client.io.StringHandle;
11+
import com.marklogic.client.test.AbstractClientTest;
12+
import com.marklogic.client.test.Common;
13+
import org.junit.jupiter.api.Test;
14+
15+
import java.util.concurrent.atomic.AtomicInteger;
16+
import java.util.function.Consumer;
17+
18+
import static org.junit.jupiter.api.Assertions.assertEquals;
19+
20+
class IncrementalWriteTest extends AbstractClientTest {
21+
22+
private static final DocumentMetadataHandle METADATA = new DocumentMetadataHandle()
23+
.withCollections("incremental-test")
24+
.withPermission("rest-reader", DocumentMetadataHandle.Capability.READ, DocumentMetadataHandle.Capability.UPDATE);
25+
26+
@Test
27+
void test() {
28+
AtomicInteger writtenCount = new AtomicInteger();
29+
30+
WriteBatcherTemplate template = new WriteBatcherTemplate(Common.newClient());
31+
32+
template.runWriteJob(writeBatcher -> writeBatcher
33+
.withThreadCount(1)
34+
.withBatchSize(10)
35+
.onBatchSuccess(batch -> writtenCount.addAndGet(batch.getItems().length)),
36+
37+
writeBatcher -> {
38+
for (int i = 1; i <= 20; i++) {
39+
String uri = "/incremental/test/doc-" + i + ".xml";
40+
String content = "<doc><docNum>" + i + "</docNum><text>This is document number " + i + "</text></doc>";
41+
writeBatcher.add(uri, METADATA, new StringHandle(content));
42+
}
43+
}
44+
);
45+
46+
assertEquals(20, writtenCount.get());
47+
}
48+
49+
// Experimenting with a template that gets rid of some annoying DMSDK boilerplate.
50+
private record WriteBatcherTemplate(DatabaseClient databaseClient) {
51+
52+
public void runWriteJob(Consumer<WriteBatcher> writeBatcherConfigurer, Consumer<WriteBatcher> writeBatcherUser) {
53+
try (DataMovementManager dmm = databaseClient.newDataMovementManager()) {
54+
WriteBatcher writeBatcher = dmm.newWriteBatcher();
55+
writeBatcherConfigurer.accept(writeBatcher);
56+
57+
dmm.startJob(writeBatcher);
58+
59+
writeBatcherUser.accept(writeBatcher);
60+
writeBatcher.awaitCompletion();
61+
62+
dmm.stopJob(writeBatcher);
63+
}
64+
}
65+
}
66+
}

marklogic-client-api/src/test/java/com/marklogic/client/test/rows/AbstractOpticUpdateTest.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.marklogic.client.row.RawPlanDefinition;
1919
import com.marklogic.client.row.RowManager;
2020
import com.marklogic.client.row.RowRecord;
21+
import com.marklogic.client.test.AbstractClientTest;
2122
import com.marklogic.client.test.Common;
2223
import org.junit.jupiter.api.AfterEach;
2324
import org.junit.jupiter.api.BeforeEach;
@@ -32,7 +33,7 @@
3233
import static org.junit.jupiter.api.Assertions.assertTrue;
3334

3435

35-
public abstract class AbstractOpticUpdateTest {
36+
public abstract class AbstractOpticUpdateTest extends AbstractClientTest {
3637

3738
private final static String XML_PREAMBLE = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
3839

@@ -42,10 +43,6 @@ public abstract class AbstractOpticUpdateTest {
4243

4344
@BeforeEach
4445
public void setup() {
45-
// Subclasses of this test are expected to only write URIs starting with /acme/ (which is used so that test
46-
// URIs show up near the top when exploring the database in qconsole), so delete all of them before running the
47-
// test to ensure a document doesn't already exist.
48-
Common.deleteUrisWithPattern("/acme/*");
4946
Common.client = Common.newClientBuilder().withUsername("writer-no-default-permissions").build();
5047
rowManager = Common.client.newRowManager().withUpdate(true);
5148
op = rowManager.newPlanBuilder();

0 commit comments

Comments
 (0)