|
| 1 | +package com.marklogic.hub.flow; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 4 | +import com.fasterxml.jackson.databind.node.ArrayNode; |
| 5 | +import com.fasterxml.jackson.databind.node.ObjectNode; |
| 6 | +import com.marklogic.appdeployer.command.temporal.DeployTemporalAxesCommand; |
| 7 | +import com.marklogic.appdeployer.command.temporal.DeployTemporalCollectionsCommand; |
| 8 | +import com.marklogic.appdeployer.impl.SimpleAppDeployer; |
| 9 | +import com.marklogic.client.io.DocumentMetadataHandle; |
| 10 | +import com.marklogic.client.io.JacksonHandle; |
| 11 | +import com.marklogic.hub.ApplicationConfig; |
| 12 | +import com.marklogic.hub.HubConfig; |
| 13 | +import com.marklogic.hub.HubTestBase; |
| 14 | +import com.marklogic.hub.flow.impl.FlowRunnerImpl; |
| 15 | +import com.marklogic.mgmt.resource.databases.DatabaseManager; |
| 16 | +import com.marklogic.mgmt.util.ObjectMapperFactory; |
| 17 | +import org.junit.jupiter.api.AfterEach; |
| 18 | +import org.junit.jupiter.api.Assertions; |
| 19 | +import org.junit.jupiter.api.BeforeEach; |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 22 | +import org.springframework.test.context.ContextConfiguration; |
| 23 | +import org.springframework.test.context.junit.jupiter.SpringExtension; |
| 24 | + |
| 25 | +import static com.marklogic.client.io.DocumentMetadataHandle.Capability.*; |
| 26 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 27 | + |
| 28 | +@ExtendWith(SpringExtension.class) |
| 29 | +@ContextConfiguration(classes = ApplicationConfig.class) |
| 30 | +public class TemporalWriteTest extends HubTestBase { |
| 31 | + private FlowRunner flowRunner; |
| 32 | + |
| 33 | + @BeforeEach |
| 34 | + void setUp() { |
| 35 | + runAsDataHubDeveloper(); |
| 36 | + clearStagingFinalAndJobDatabases(); |
| 37 | + installHubArtifacts(runAsDataHubDeveloper(), true); |
| 38 | + installProjectInFolder("test-projects/temporal-test"); |
| 39 | + addFieldAndIndexes(); |
| 40 | + new SimpleAppDeployer(new DeployTemporalAxesCommand(), new DeployTemporalCollectionsCommand()) |
| 41 | + .deploy(adminHubConfig.getAppConfig()); |
| 42 | + DocumentMetadataHandle meta = new DocumentMetadataHandle(); |
| 43 | + meta.getCollections().add("testTemporal"); |
| 44 | + meta.getPermissions().add("data-hub-operator", READ, UPDATE, EXECUTE); |
| 45 | + ObjectMapper mapper = new ObjectMapper(); |
| 46 | + ObjectNode sourceNode = mapper.createObjectNode(); |
| 47 | + sourceNode.put("content", "v1-content"); |
| 48 | + stagingClient.newJSONDocumentManager().write("/test.json", meta, new JacksonHandle(sourceNode)); |
| 49 | + } |
| 50 | + |
| 51 | + @AfterEach |
| 52 | + void tearDown(){ |
| 53 | + removeIndexesAndFields(); |
| 54 | + new DatabaseManager(runAsAdmin().getManageClient()).clearDatabase(HubConfig.DEFAULT_STAGING_SCHEMAS_DB_NAME); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + void writeTemporalFile() { |
| 59 | + flowRunner = new FlowRunnerImpl(runAsDataHubOperator()); |
| 60 | + final String flowName = "temporal-test"; |
| 61 | + flowRunner.runFlow(new FlowInputs(flowName)); |
| 62 | + flowRunner.awaitCompletion(); |
| 63 | + assertEquals(1, getDocCount(HubConfig.DEFAULT_STAGING_NAME, "temporal/test")); |
| 64 | + String updateTemporalDoc = "var temporal = require('/MarkLogic/temporal.xqy');\n" + |
| 65 | + "var root ={'envelope':{'headers':{}, 'triples':[], 'instance':{'content':'v2-content'}, 'attachments':null}};\n" + |
| 66 | + "declareUpdate();\n" + |
| 67 | + "temporal.documentInsert('temporal/test', '/temporal/ingestion/test.json', root, {permissions : [xdmp.permission('data-hub-operator', 'read'),xdmp.permission('data-hub-operator', 'update')]});"; |
| 68 | + |
| 69 | + try{ |
| 70 | + runAsDataHubOperator().newStagingClient().newServerEval().javascript(updateTemporalDoc).eval(); |
| 71 | + } |
| 72 | + catch (Exception e){ |
| 73 | + logger.error("Document update failed: ", e); |
| 74 | + Assertions.fail("After the step is run, a temporal document should have been created and it's update should not fail. " + |
| 75 | + "If it's not a temporal document, temporal update would fail"); |
| 76 | + } |
| 77 | + |
| 78 | + String deleteTemporalDoc = "var temporal = require('/MarkLogic/temporal.xqy');\n" + |
| 79 | + "declareUpdate();\n" + |
| 80 | + "temporal.documentDelete('temporal/test', '/temporal/ingestion/test.json');"; |
| 81 | + |
| 82 | + try{ |
| 83 | + runAsDataHubOperator().newStagingClient().newServerEval().javascript(deleteTemporalDoc).eval(); |
| 84 | + } |
| 85 | + catch (Exception e){ |
| 86 | + logger.error("Document deletion failed: ", e); |
| 87 | + Assertions.fail("dh-operator should be able to delete the document"); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + private void removeIndexesAndFields() { |
| 92 | + String indexesDeletion = " const admin = require('/MarkLogic/admin.xqy');\n" + |
| 93 | + " var config = admin.getConfiguration();\n" + |
| 94 | + " var dbid = xdmp.database('data-hub-STAGING');\n" + |
| 95 | + " var rangespec1 = admin.databaseRangeFieldIndex('dateTime', 'systemStart', '', fn.false());\n" + |
| 96 | + " var rangespec2 = admin.databaseRangeFieldIndex('dateTime', 'systemEnd', '', fn.false());\n" + |
| 97 | + " config = admin.databaseDeleteRangeFieldIndex(config, dbid, rangespec1);\n" + |
| 98 | + " config = admin.databaseDeleteRangeFieldIndex(config, dbid, rangespec2);\n" + |
| 99 | + " config = admin.databaseDeleteField(config, dbid, 'systemStart');\n" + |
| 100 | + " config = admin.databaseDeleteField(config, dbid, 'systemEnd');\n" + |
| 101 | + " admin.saveConfigurationWithoutRestart(config);"; |
| 102 | + runAsAdmin().newStagingClient().newServerEval().javascript(indexesDeletion).eval(); |
| 103 | + } |
| 104 | + |
| 105 | + private void addFieldAndIndexes() { |
| 106 | + ObjectNode db = getDatabaseProperties("data-hub-STAGING"); |
| 107 | + |
| 108 | + ObjectNode newNode = ObjectMapperFactory.getObjectMapper().createObjectNode(); |
| 109 | + newNode.put("database-name", "data-hub-STAGING"); |
| 110 | + newNode.set("field", db.get("field")); |
| 111 | + |
| 112 | + Object field = newNode.get("field"); |
| 113 | + ObjectNode systemStart = ((ArrayNode) field).addObject(); |
| 114 | + systemStart.put("field-name", "systemStart"); |
| 115 | + systemStart.put("metadata", true); |
| 116 | + ObjectNode systemEnd = ((ArrayNode) field).addObject(); |
| 117 | + systemEnd.put("field-name", "systemEnd"); |
| 118 | + systemEnd.put("metadata", true); |
| 119 | + |
| 120 | + |
| 121 | + ArrayNode indexes; |
| 122 | + if (db.has("range-field-index")) { |
| 123 | + indexes = (ArrayNode) db.get("range-field-index"); |
| 124 | + newNode.set("range-field-index", indexes); |
| 125 | + } else { |
| 126 | + indexes = newNode.putArray("range-field-index"); |
| 127 | + } |
| 128 | + ObjectNode systemStartIndex = indexes.addObject(); |
| 129 | + systemStartIndex.put("scalar-type", "dateTime"); |
| 130 | + systemStartIndex.put("field-name", "systemStart"); |
| 131 | + systemStartIndex.put("invalid-values", "reject"); |
| 132 | + systemStartIndex.put("range-value-positions", false); |
| 133 | + |
| 134 | + ObjectNode systemEndIndex = indexes.addObject(); |
| 135 | + systemEndIndex.put("scalar-type", "dateTime"); |
| 136 | + systemEndIndex.put("field-name", "systemEnd"); |
| 137 | + systemEndIndex.put("invalid-values", "reject"); |
| 138 | + systemEndIndex.put("range-value-positions", false); |
| 139 | + new DatabaseManager(adminHubConfig.getManageClient()).save(newNode.toString()); |
| 140 | + } |
| 141 | +} |
0 commit comments