|
8 | 8 | import com.marklogic.client.ext.DefaultConfiguredDatabaseClientFactory; |
9 | 9 | import com.marklogic.client.ext.batch.DataMovementBatchWriter; |
10 | 10 | import com.marklogic.client.ext.datamovement.job.DeleteCollectionsJob; |
11 | | -import com.marklogic.client.ext.spring.SpringDatabaseClientConfig; |
12 | 11 | import com.marklogic.client.impl.DocumentWriteOperationImpl; |
13 | 12 | import com.marklogic.client.io.DocumentMetadataHandle; |
14 | 13 | import com.marklogic.client.io.Format; |
15 | 14 | import com.marklogic.client.io.StringHandle; |
16 | | -import org.junit.After; |
17 | | -import org.junit.Assert; |
18 | | -import org.junit.Before; |
19 | | -import org.junit.runner.RunWith; |
20 | | -import org.slf4j.Logger; |
21 | | -import org.slf4j.LoggerFactory; |
| 15 | +import com.marklogic.junit5.spring.AbstractSpringMarkLogicTest; |
| 16 | +import org.junit.jupiter.api.AfterEach; |
| 17 | +import org.junit.jupiter.api.BeforeEach; |
22 | 18 | import org.springframework.beans.factory.annotation.Autowired; |
23 | | -import org.springframework.context.annotation.Bean; |
24 | | -import org.springframework.context.annotation.Configuration; |
25 | | -import org.springframework.context.annotation.Import; |
26 | | -import org.springframework.context.annotation.PropertySource; |
27 | | -import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; |
28 | | -import org.springframework.test.context.ContextConfiguration; |
29 | | -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
30 | 19 |
|
31 | 20 | import java.io.File; |
32 | 21 | import java.io.IOException; |
33 | 22 | import java.util.*; |
34 | 23 | import java.util.zip.ZipEntry; |
35 | 24 | import java.util.zip.ZipFile; |
36 | 25 |
|
37 | | -/** |
38 | | - * Abstract class for making it easier to test DMSDK listeners and consumers. |
39 | | - */ |
40 | | -@RunWith(SpringJUnit4ClassRunner.class) |
41 | | -@ContextConfiguration(classes = {TestConfig.class}) |
42 | | -public abstract class AbstractDataMovementTest extends Assert { |
43 | | - |
44 | | - protected final static String COLLECTION = "data-movement-test"; |
45 | | - |
46 | | - protected final static String FIRST_URI = "/test/dmsdk-test-1.xml"; |
47 | | - protected final static String SECOND_URI = "/test/dmsdk-test-2.xml"; |
48 | | - |
49 | | - protected QueryBatcherTemplate queryBatcherTemplate; |
50 | | - |
51 | | - protected Logger logger = LoggerFactory.getLogger(getClass()); |
52 | | - |
53 | | - @Autowired |
54 | | - protected DatabaseClientConfig clientConfig; |
55 | | - protected DatabaseClient client; |
56 | | - |
57 | | - protected ConfiguredDatabaseClientFactory configuredDatabaseClientFactory = new DefaultConfiguredDatabaseClientFactory(); |
58 | | - |
59 | | - protected DatabaseClient newClient() { |
60 | | - client = configuredDatabaseClientFactory.newDatabaseClient(clientConfig); |
61 | | - return client; |
62 | | - } |
63 | | - |
64 | | - @After |
65 | | - public void releaseClientOnTearDown() { |
66 | | - if (client != null) { |
67 | | - try { |
68 | | - client.release(); |
69 | | - } catch (Exception ex) { |
70 | | - // That's fine, the test probably released it already |
71 | | - } |
72 | | - } |
73 | | - } |
74 | | - |
75 | | - @Before |
76 | | - public void setupAbstractDataMovementTest() { |
77 | | - queryBatcherTemplate = new QueryBatcherTemplate(newClient("Documents")); |
78 | | - queryBatcherTemplate.setJobName("manage-collections-test"); |
79 | | - queryBatcherTemplate.setBatchSize(1); |
80 | | - queryBatcherTemplate.setThreadCount(2); |
81 | | - |
82 | | - queryBatcherTemplate.applyOnDocumentUris(new DeleteListener(), FIRST_URI, SECOND_URI); |
83 | | - new DeleteCollectionsJob(COLLECTION).run(client); |
84 | | - |
85 | | - writeDocuments(FIRST_URI, SECOND_URI); |
86 | | - } |
87 | | - |
88 | | - protected DatabaseClient newClient(String database) { |
89 | | - String currentDatabase = clientConfig.getDatabase(); |
90 | | - clientConfig.setDatabase(database); |
91 | | - client = configuredDatabaseClientFactory.newDatabaseClient(clientConfig); |
92 | | - clientConfig.setDatabase(currentDatabase); |
93 | | - return client; |
94 | | - } |
95 | | - |
96 | | - protected void writeDocuments(String... uris) { |
97 | | - List<DocumentWriteOperation> list = new ArrayList<>(); |
98 | | - DocumentMetadataHandle metadata = new DocumentMetadataHandle(); |
99 | | - metadata.getCollections().add(COLLECTION); |
100 | | - for (String uri : uris) { |
101 | | - list.add(new DocumentWriteOperationImpl(DocumentWriteOperation.OperationType.DOCUMENT_WRITE, uri, |
102 | | - metadata, new StringHandle("<test>" + uri + "</test>").withFormat(Format.XML))); |
103 | | - } |
104 | | - writeDocuments(list); |
105 | | - } |
106 | | - |
107 | | - protected void writeDocuments(List<DocumentWriteOperation> writeOperations) { |
108 | | - DataMovementBatchWriter writer = new DataMovementBatchWriter(client); |
109 | | - writer.initialize(); |
110 | | - writer.write(writeOperations); |
111 | | - writer.waitForCompletion(); |
112 | | - } |
113 | | - |
114 | | - protected void assertZipFileContainsEntryNames(File file, String... names) { |
115 | | - Set<String> entryNames = new HashSet<>(); |
116 | | - try { |
117 | | - ZipFile zipFile = new ZipFile(file); |
118 | | - try { |
119 | | - Enumeration<?> entries = zipFile.entries(); |
120 | | - while (entries.hasMoreElements()) { |
121 | | - ZipEntry zipEntry = (ZipEntry) entries.nextElement(); |
122 | | - entryNames.add(zipEntry.getName()); |
123 | | - } |
124 | | - } finally { |
125 | | - zipFile.close(); |
126 | | - } |
127 | | - } catch (IOException ie) { |
128 | | - throw new RuntimeException(ie); |
129 | | - } |
130 | | - |
131 | | - logger.info("Entry names: " + entryNames); |
132 | | - for (String name : names) { |
133 | | - assertTrue(entryNames.contains(name)); |
134 | | - } |
135 | | - assertEquals(names.length, entryNames.size()); |
136 | | - } |
| 26 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 27 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 28 | + |
| 29 | +public abstract class AbstractDataMovementTest extends AbstractSpringMarkLogicTest { |
| 30 | + |
| 31 | + protected final static String COLLECTION = "data-movement-test"; |
| 32 | + |
| 33 | + protected final static String FIRST_URI = "/test/dmsdk-test-1.xml"; |
| 34 | + protected final static String SECOND_URI = "/test/dmsdk-test-2.xml"; |
| 35 | + |
| 36 | + protected QueryBatcherTemplate queryBatcherTemplate; |
| 37 | + |
| 38 | + @Autowired |
| 39 | + protected DatabaseClientConfig clientConfig; |
| 40 | + protected DatabaseClient client; |
| 41 | + |
| 42 | + protected ConfiguredDatabaseClientFactory configuredDatabaseClientFactory = new DefaultConfiguredDatabaseClientFactory(); |
| 43 | + |
| 44 | + @AfterEach |
| 45 | + public void releaseClientOnTearDown() { |
| 46 | + if (client != null) { |
| 47 | + try { |
| 48 | + client.release(); |
| 49 | + } catch (Exception ex) { |
| 50 | + // That's fine, the test probably released it already |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + @BeforeEach |
| 56 | + public void setupAbstractDataMovementTest() { |
| 57 | + queryBatcherTemplate = new QueryBatcherTemplate(newClient("Documents")); |
| 58 | + queryBatcherTemplate.setJobName("manage-collections-test"); |
| 59 | + queryBatcherTemplate.setBatchSize(1); |
| 60 | + queryBatcherTemplate.setThreadCount(2); |
| 61 | + |
| 62 | + queryBatcherTemplate.applyOnDocumentUris(new DeleteListener(), FIRST_URI, SECOND_URI); |
| 63 | + new DeleteCollectionsJob(COLLECTION).run(client); |
| 64 | + |
| 65 | + writeDocuments(FIRST_URI, SECOND_URI); |
| 66 | + } |
| 67 | + |
| 68 | + protected DatabaseClient newClient(String database) { |
| 69 | + String currentDatabase = clientConfig.getDatabase(); |
| 70 | + clientConfig.setDatabase(database); |
| 71 | + client = configuredDatabaseClientFactory.newDatabaseClient(clientConfig); |
| 72 | + clientConfig.setDatabase(currentDatabase); |
| 73 | + return client; |
| 74 | + } |
| 75 | + |
| 76 | + protected void writeDocuments(String... uris) { |
| 77 | + List<DocumentWriteOperation> list = new ArrayList<>(); |
| 78 | + DocumentMetadataHandle metadata = new DocumentMetadataHandle(); |
| 79 | + metadata.getCollections().add(COLLECTION); |
| 80 | + for (String uri : uris) { |
| 81 | + list.add(new DocumentWriteOperationImpl(DocumentWriteOperation.OperationType.DOCUMENT_WRITE, uri, |
| 82 | + metadata, new StringHandle("<test>" + uri + "</test>").withFormat(Format.XML))); |
| 83 | + } |
| 84 | + writeDocuments(list); |
| 85 | + } |
| 86 | + |
| 87 | + protected void writeDocuments(List<DocumentWriteOperation> writeOperations) { |
| 88 | + DataMovementBatchWriter writer = new DataMovementBatchWriter(client); |
| 89 | + writer.initialize(); |
| 90 | + writer.write(writeOperations); |
| 91 | + writer.waitForCompletion(); |
| 92 | + } |
| 93 | + |
| 94 | + protected void assertZipFileContainsEntryNames(File file, String... names) { |
| 95 | + Set<String> entryNames = new HashSet<>(); |
| 96 | + try { |
| 97 | + ZipFile zipFile = new ZipFile(file); |
| 98 | + try { |
| 99 | + Enumeration<?> entries = zipFile.entries(); |
| 100 | + while (entries.hasMoreElements()) { |
| 101 | + ZipEntry zipEntry = (ZipEntry) entries.nextElement(); |
| 102 | + entryNames.add(zipEntry.getName()); |
| 103 | + } |
| 104 | + } finally { |
| 105 | + zipFile.close(); |
| 106 | + } |
| 107 | + } catch (IOException ie) { |
| 108 | + throw new RuntimeException(ie); |
| 109 | + } |
| 110 | + |
| 111 | + logger.info("Entry names: " + entryNames); |
| 112 | + for (String name : names) { |
| 113 | + assertTrue(entryNames.contains(name)); |
| 114 | + } |
| 115 | + assertEquals(names.length, entryNames.size()); |
| 116 | + } |
137 | 117 |
|
138 | 118 | } |
139 | | - |
140 | | -@Configuration |
141 | | -@Import(value = {SpringDatabaseClientConfig.class}) |
142 | | -@PropertySource("classpath:application.properties") |
143 | | -class TestConfig { |
144 | | - |
145 | | - /** |
146 | | - * Ensures that placeholders are replaced with property values |
147 | | - */ |
148 | | - @Bean |
149 | | - public static PropertySourcesPlaceholderConfigurer propertyPlaceHolderConfigurer() { |
150 | | - return new PropertySourcesPlaceholderConfigurer(); |
151 | | - } |
152 | | -} |
153 | | - |
0 commit comments