|
| 1 | +package com.marklogic.client.test.rows; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.JsonNode; |
| 4 | +import com.marklogic.client.io.JacksonHandle; |
| 5 | +import com.marklogic.client.io.StringHandle; |
| 6 | +import com.marklogic.client.row.RawQueryDSLPlan; |
| 7 | +import com.marklogic.client.test.Common; |
| 8 | +import org.junit.jupiter.api.Test; |
| 9 | + |
| 10 | +import java.util.Iterator; |
| 11 | + |
| 12 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 13 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 14 | + |
| 15 | +public class ResultRowsWithTimestampTest extends AbstractOpticUpdateTest { |
| 16 | + |
| 17 | + private final static String NEW_MUSICIAN_JSON = "{\n" + |
| 18 | + " \"musician\": {\n" + |
| 19 | + " \"lastName\": \"Smith\",\n" + |
| 20 | + " \"firstName\": \"Jane\",\n" + |
| 21 | + " \"dob\": \"1901-08-04\"\n" + |
| 22 | + " }\n" + |
| 23 | + "}"; |
| 24 | + |
| 25 | + @Test |
| 26 | + void testResultRowsWithPointInTimeQueryTimestamp() { |
| 27 | + final RawQueryDSLPlan plan = rowManager.newRawQueryDSLPlan(new StringHandle("op.fromView('opticUnitTest', 'musician')")); |
| 28 | + |
| 29 | + JacksonHandle result = new JacksonHandle(); |
| 30 | + JsonNode doc = rowManager.resultDoc(plan, result).get(); |
| 31 | + assertEquals(4, doc.get("rows").size(), "Expecting the 4 musicians loaded by test-app to exist"); |
| 32 | + |
| 33 | + final long serverTimestamp = result.getServerTimestamp(); |
| 34 | + assertTrue(serverTimestamp > 0, "Unexpected timestamp: " + serverTimestamp); |
| 35 | + |
| 36 | + // Insert a new musician, which will bump up the server timestamp |
| 37 | + String uri = "/acme/newMusician.json"; |
| 38 | + Common.client.newJSONDocumentManager().write(uri, newDefaultMetadata(), new StringHandle(NEW_MUSICIAN_JSON)); |
| 39 | + |
| 40 | + doc = rowManager.resultDoc(plan, new JacksonHandle()).get(); |
| 41 | + assertEquals(5, doc.get("rows").size(), "Should now get 5 musician rows due to the 5th row being added by " + |
| 42 | + "inserting the new musician doc"); |
| 43 | + |
| 44 | + // Now verify a point-in-time query works |
| 45 | + result = new JacksonHandle(); |
| 46 | + result.setPointInTimeQueryTimestamp(serverTimestamp); |
| 47 | + doc = rowManager.resultDoc(plan, result).get(); |
| 48 | + assertEquals(4, doc.get("rows").size(), "Only 4 rows should be returned since the query should have been " + |
| 49 | + "run at a server timestamp prior to the newMusician doc being inserted."); |
| 50 | + |
| 51 | + // And verify point-in-time works when using resultRows too |
| 52 | + result = new JacksonHandle(); |
| 53 | + result.setPointInTimeQueryTimestamp(serverTimestamp); |
| 54 | + Iterator<JacksonHandle> rows = rowManager.resultRows(plan, result).iterator(); |
| 55 | + int count = 0; |
| 56 | + while (rows.hasNext()) { |
| 57 | + rows.next(); |
| 58 | + count++; |
| 59 | + } |
| 60 | + assertEquals(4, count, "resultRows should honor the point-in-time timestamp, just like resultDoc does"); |
| 61 | + } |
| 62 | +} |
0 commit comments