|
1 | 1 | package sample; |
2 | 2 |
|
3 | | -import javax.xml.namespace.QName; |
4 | | - |
5 | | -import org.junit.Test; |
6 | | -import org.springframework.test.context.ContextConfiguration; |
7 | | -import org.springframework.test.context.TestExecutionListeners; |
8 | | - |
9 | 3 | import com.marklogic.client.document.XMLDocumentManager; |
10 | 4 | import com.marklogic.client.io.DocumentMetadataHandle; |
11 | 5 | import com.marklogic.client.io.DocumentMetadataHandle.DocumentProperties; |
12 | 6 | import com.marklogic.client.io.Format; |
13 | 7 | import com.marklogic.client.io.StringHandle; |
14 | | -import com.marklogic.junit.spring.AbstractSpringTest; |
15 | | -import com.marklogic.junit.spring.ModulesLoaderTestExecutionListener; |
16 | | -import com.marklogic.junit.spring.ModulesPath; |
17 | | - |
18 | | -@ContextConfiguration(classes = { TestConfig.class }) |
19 | | -@TestExecutionListeners(value = { ModulesLoaderTestExecutionListener.class }) |
20 | | -@ModulesPath(baseDir = "src/main/ml-modules") |
21 | | -public class CpfTest extends AbstractSpringTest { |
22 | | - |
23 | | - @Test |
24 | | - public void test() { |
25 | | - // URI of the document we'll insert |
26 | | - final String uri = "/cpf-example/test.xml"; |
| 8 | +import com.marklogic.junit5.spring.AbstractSpringMarkLogicTest; |
| 9 | +import org.junit.jupiter.api.Test; |
27 | 10 |
|
28 | | - // Insert a document into the collection that we have a CPF pipeline attached to |
29 | | - XMLDocumentManager mgr = getClient().newXMLDocumentManager(); |
30 | | - DocumentMetadataHandle metadata = new DocumentMetadataHandle(); |
31 | | - metadata.withCollections("cpf-test"); |
32 | | - mgr.write(uri, metadata, new StringHandle("<test/>").withFormat(Format.XML)); |
33 | | - |
34 | | - // Wait for the task server to finish processing requests |
35 | | - waitForTaskServerRequestsToFinish(); |
36 | | - |
37 | | - // Verify that a property was set on our test document by the CPF action |
38 | | - DocumentProperties props = mgr.readMetadata(uri, new DocumentMetadataHandle()).getProperties(); |
39 | | - assertEquals("The sample-prop property should have been set by the CPF action", "Hello from the CPF action", |
40 | | - props.get(new QName("http://marklogic.com/sample", "sample-prop"))); |
41 | | - } |
42 | | - |
43 | | - /** |
44 | | - * TODO Move this into ml-junit for reuse. |
45 | | - */ |
46 | | - private void waitForTaskServerRequestsToFinish() { |
47 | | - String xquery = "declare namespace ss = 'http://marklogic.com/xdmp/status/server'; "; |
48 | | - xquery += "xdmp:server-status(xdmp:hosts(), xdmp:server('TaskServer'))/ss:request-statuses/ss:request-status"; |
49 | | - |
50 | | - final int MAX_TRIES = 100; |
51 | | - final int SLEEP_TIME = 500; |
| 11 | +import javax.xml.namespace.QName; |
52 | 12 |
|
53 | | - logger.info("Waiting for task server requests to finish..."); |
54 | | - for (int i = 0; i < MAX_TRIES; i++) { |
55 | | - String result = getXccTemplate().executeAdhocQuery(xquery); |
56 | | - if (result == null || result.trim().length() == 0) { |
57 | | - break; |
58 | | - } |
59 | | - try { |
60 | | - Thread.sleep(SLEEP_TIME); |
61 | | - } catch (Exception e) { |
62 | | - throw new RuntimeException("Unexpected error from sleeping while waiting for task server to finish", e); |
63 | | - } |
64 | | - } |
65 | | - logger.info("Task server no longer has any outstanding requests"); |
66 | | - } |
| 13 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 14 | + |
| 15 | +public class CpfTest extends AbstractSpringMarkLogicTest { |
| 16 | + |
| 17 | + @Test |
| 18 | + public void test() { |
| 19 | + // URI of the document we'll insert |
| 20 | + final String uri = "/cpf-example/test.xml"; |
| 21 | + |
| 22 | + // Insert a document into the collection that we have a CPF pipeline attached to |
| 23 | + XMLDocumentManager mgr = getDatabaseClient().newXMLDocumentManager(); |
| 24 | + DocumentMetadataHandle metadata = new DocumentMetadataHandle(); |
| 25 | + metadata.withCollections("cpf-test"); |
| 26 | + mgr.write(uri, metadata, new StringHandle("<test/>").withFormat(Format.XML)); |
| 27 | + |
| 28 | + // Wait for the task server to finish processing requests |
| 29 | + waitForTaskServerRequestsToFinish(); |
| 30 | + |
| 31 | + // Verify that a property was set on our test document by the CPF action |
| 32 | + DocumentProperties props = mgr.readMetadata(uri, new DocumentMetadataHandle()).getProperties(); |
| 33 | + assertEquals("Hello from the CPF action", props.get(new QName("http://marklogic.com/sample", "sample-prop")), |
| 34 | + "The sample-prop property should have been set by the CPF action"); |
| 35 | + } |
| 36 | + |
| 37 | + private void waitForTaskServerRequestsToFinish() { |
| 38 | + String xquery = "declare namespace ss = 'http://marklogic.com/xdmp/status/server'; "; |
| 39 | + xquery += "xdmp:server-status(xdmp:hosts(), xdmp:server('TaskServer'))/ss:request-statuses/ss:request-status"; |
| 40 | + |
| 41 | + final int MAX_TRIES = 100; |
| 42 | + final int SLEEP_TIME = 500; |
| 43 | + |
| 44 | + logger.info("Waiting for task server requests to finish..."); |
| 45 | + for (int i = 0; i < MAX_TRIES; i++) { |
| 46 | + String result = getDatabaseClient().newServerEval().xquery(xquery).evalAs(String.class); |
| 47 | + if (result == null || result.trim().length() == 0) { |
| 48 | + break; |
| 49 | + } |
| 50 | + try { |
| 51 | + Thread.sleep(SLEEP_TIME); |
| 52 | + } catch (Exception e) { |
| 53 | + throw new RuntimeException("Unexpected error from sleeping while waiting for task server to finish", e); |
| 54 | + } |
| 55 | + } |
| 56 | + logger.info("Task server no longer has any outstanding requests"); |
| 57 | + } |
67 | 58 | } |
0 commit comments