Skip to content

Commit db51724

Browse files
Ajit GeorgeAjit George
authored andcommitted
Updated test for delete with multiple version
1 parent 2d2ec1c commit db51724

File tree

1 file changed

+96
-25
lines changed

1 file changed

+96
-25
lines changed

test-complete/src/test/java/com/marklogic/client/functionaltest/TestBiTempMetaValues.java

Lines changed: 96 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import com.marklogic.client.document.DocumentManager.Metadata;
4444
import com.marklogic.client.document.DocumentMetadataPatchBuilder;
4545
import com.marklogic.client.document.DocumentPage;
46+
import com.marklogic.client.document.DocumentRecord;
4647
import com.marklogic.client.document.JSONDocumentManager;
4748
import com.marklogic.client.document.XMLDocumentManager;
4849
import com.marklogic.client.io.DocumentMetadataHandle;
@@ -54,6 +55,7 @@
5455
import com.marklogic.client.query.QueryManager;
5556
import com.marklogic.client.query.StructuredQueryBuilder;
5657
import com.marklogic.client.query.StructuredQueryDefinition;
58+
import com.marklogic.client.query.StructuredQueryBuilder.TemporalOperator;
5759

5860
public class TestBiTempMetaValues extends BasicJavaClientREST {
5961

@@ -123,15 +125,15 @@ public static void tearDownAfterClass() throws Exception {
123125

124126
// Temporal collection needs to be delete before temporal axis associated
125127
// with it can be deleted
126-
ConnectedRESTQA.deleteElementRangeIndexTemporalCollection("Documents",
128+
ConnectedRESTQA.deleteElementRangeIndexTemporalCollection(dbName,
127129
temporalLsqtCollectionName);
128-
ConnectedRESTQA.deleteElementRangeIndexTemporalCollection("Documents",
130+
ConnectedRESTQA.deleteElementRangeIndexTemporalCollection(dbName,
129131
temporalCollectionName);
130-
ConnectedRESTQA.deleteElementRangeIndexTemporalCollection("Documents",
132+
ConnectedRESTQA.deleteElementRangeIndexTemporalCollection(dbName,
131133
bulktemporalCollectionName);
132-
ConnectedRESTQA.deleteElementRangeIndexTemporalAxis("Documents",
134+
ConnectedRESTQA.deleteElementRangeIndexTemporalAxis(dbName,
133135
axisValidName);
134-
ConnectedRESTQA.deleteElementRangeIndexTemporalAxis("Documents",
136+
ConnectedRESTQA.deleteElementRangeIndexTemporalAxis(dbName,
135137
axisSystemName);
136138
}
137139

@@ -705,24 +707,27 @@ public void testProtectDelete() throws Exception {
705707
ConnectedRESTQA.updateTemporalCollectionForLSQT(dbName,
706708
temporalLsqtCollectionName, true);
707709

708-
Calendar insertTime = DatatypeConverter.parseDateTime("2005-01-01T00:00:01");
709-
Calendar deleteTime = DatatypeConverter.parseDateTime("2005-01-01T00:00:11");
710+
Calendar insertTime = DatatypeConverter.parseDateTime("2001-01-01T00:00:11");
711+
Calendar deleteTime = DatatypeConverter.parseDateTime("2011-01-01T00:00:31");
712+
Calendar updateTime1 = DatatypeConverter.parseDateTime("2007-01-01T00:00:21");
713+
Calendar updateTime2 = DatatypeConverter.parseDateTime("2009-01-01T00:00:21");
714+
710715
DatabaseClient adminClient = getDatabaseClientOnDatabase("localhost", getRestServerPort(), dbName, "admin", "x", Authentication.DIGEST);
711716

712717
String docId = "javaSingleJSONDoc.json";
713-
JacksonDatabindHandle<ObjectNode> handle = getJSONDocumentHandle("2001-01-01T00:00:00",
714-
"2011-12-31T23:59:59",
718+
JacksonDatabindHandle<ObjectNode> handle = getJSONDocumentHandle("2001-01-01T00:00:00Z",
719+
"2011-12-30T23:59:59Z",
715720
"999 Skyway Park - JSON",
716721
docId
717722
);
718723

719-
JSONDocumentManager docMgr = writerClient.newJSONDocumentManager();
720-
JSONDocumentManager docMgrProtect = adminClient.newJSONDocumentManager();
721-
docMgr.write(docId, null, handle, null, null, temporalLsqtCollectionName, insertTime);
722-
724+
JSONDocumentManager docMgr = writerClient.newJSONDocumentManager();
725+
docMgr.write(docId, null, handle, null, null, temporalLsqtCollectionName, insertTime);
726+
//docMgr.newDescriptor(docId).setVersion(1000L);
727+
723728
//TODO: UPdate this test to include temporalDoucmentURi and delete one version of it and search and assert for that deleted version
724-
//Protect document for 30 sec from delete and update. Use Duration.
725-
docMgr.protect(docId, temporalLsqtCollectionName, ProtectionLevel.NODELETE, DatatypeFactory.newInstance().newDuration("PT30S"));
729+
//Protect document for 40 sec from delete and update. Use Duration.
730+
docMgr.protect(docId, temporalLsqtCollectionName, ProtectionLevel.NODELETE, DatatypeFactory.newInstance().newDuration("PT40S"));
726731

727732
StringBuilder str = new StringBuilder();
728733
try {
@@ -732,17 +737,73 @@ public void testProtectDelete() throws Exception {
732737
str.append(ex.getMessage());
733738
System.out.println("Exception when delete within 30 sec is " + str.toString());
734739
}
735-
assertTrue("Doc should not be deleted", str.toString().contains("The document javaSingleJSONDoc.json is protected noDelete"));
740+
assertTrue("Doc should not be deleted", str.toString().contains("The document javaSingleJSONDoc.json is protected noDelete"));
741+
str = null;
742+
// Added the word "Updated" to doc from 2007-01-01T00:00:00 to 2008-12-30T23:59:59
743+
JacksonDatabindHandle<ObjectNode> handleUpd = getJSONDocumentHandle(
744+
"2007-01-01T00:00:00Z", "2008-12-30T23:59:59Z",
745+
"1999 Skyway Park - Updated - JSON", docId);
736746

747+
// Can the document be updated with 40 sec with ProtectionLevel.NODELETE and 40 secs?
748+
docMgr.write(docId, null, handleUpd, null, null, temporalLsqtCollectionName, updateTime1);
749+
750+
// Remove the word "Updated" from 2008-12-31T00:00:00 to 2011-12-30T23:59:59
751+
JacksonDatabindHandle<ObjectNode> handleUpd2 = getJSONDocumentHandle(
752+
"2008-12-31T00:00:00Z", "2011-12-30T23:59:59Z",
753+
"1999 Skyway Park - JSON", docId);
754+
docMgr.write(docId, null, handleUpd2, null, null, temporalLsqtCollectionName, updateTime2);
755+
756+
// Search for the "Updated" term in doc and try to delete that document within 40 sec (should throw exception) and again delete that version after 40 secs.
757+
QueryManager queryMgr = writerClient.newQueryManager();
758+
StructuredQueryBuilder sqb = queryMgr.newStructuredQueryBuilder();
759+
760+
StructuredQueryDefinition termQuery = sqb.term("Updated");
761+
762+
StructuredQueryBuilder.Axis validAxis = sqb.axis(axisValidName);
763+
Calendar start1 = DatatypeConverter.parseDateTime("2007-02-01T00:00:00Z");
764+
Calendar end1 = DatatypeConverter.parseDateTime("2007-12-31T23:59:59Z");
765+
StructuredQueryBuilder.Period period1 = sqb.period(start1, end1);
766+
767+
StructuredQueryDefinition periodQuery = sqb.and(termQuery,
768+
sqb.temporalPeriodRange(validAxis, TemporalOperator.ALN_CONTAINS, period1));
769+
long startOffset = 1;
770+
DocumentPage termQueryResults = docMgr.search(periodQuery, startOffset);
771+
772+
long count = 0;
773+
String toDeleteURI = null;
774+
while (termQueryResults.hasNext()) {
775+
++count;
776+
DocumentRecord record = termQueryResults.next();
777+
System.out.println("URI = " + record.getUri());
778+
JacksonDatabindHandle<ObjectNode> recordHandle = new JacksonDatabindHandle<>(
779+
ObjectNode.class);
780+
record.getContent(recordHandle);
781+
System.out.println("Content = " + recordHandle.toString());
782+
if (recordHandle.toString().contains("Updated")) {
783+
toDeleteURI = record.getUri();
784+
}
785+
}
786+
str = new StringBuilder();
787+
// Delete this (searched) doc within 40 sec.
788+
try {
789+
docMgr.delete(toDeleteURI, null, temporalLsqtCollectionName,
790+
deleteTime);
791+
}
792+
catch(Exception ex) {
793+
str.append(ex.getMessage());
794+
System.out.println("Exception when delete within 40 sec is " + str.toString());
795+
}
796+
String docDelMessage = "The document " + toDeleteURI + " is protected noDelete";
797+
assertTrue("Doc should not be deleted", str.toString().contains(docDelMessage));
737798
// Sleep for 40 secs and try to delete the same docId.
738799
Thread.sleep(40000);
739800
docMgr.delete(docId, null, temporalLsqtCollectionName, deleteTime);
740801
Thread.sleep(5000);
741-
802+
// Make sure that bi temporal doc is not deleted.
742803
JSONDocumentManager jsonDocMgr = writerClient.newJSONDocumentManager();
743804
DocumentPage readResults = jsonDocMgr.read(docId);
744805
System.out.println("Number of results = " + readResults.size());
745-
// assertEquals("Wrong number of results", 0, readResults.size());
806+
assertEquals("Wrong number of results", 1, readResults.size());
746807
}
747808

748809
@Test
@@ -828,6 +889,7 @@ public void testProtectUpdateInTransaction() throws Exception {
828889

829890
JSONDocumentManager docMgr = writerClient.newJSONDocumentManager();
830891
Transaction t1 = writerClient.openTransaction();
892+
Transaction t2 = null;
831893
docMgr.write("javaSingleJSONDocV1.json", docId, null, handle, null, t1, temporalLsqtCollectionName, insertTime);
832894

833895
//Protect document for 30 sec from delete and update. Use Duration.
@@ -860,9 +922,11 @@ public void testProtectUpdateInTransaction() throws Exception {
860922

861923
StructuredQueryBuilder sqb = queryMgr.newStructuredQueryBuilder();
862924
StructuredQueryDefinition termQuery = sqb.collection(temporalLsqtCollectionName);
925+
t1.commit();
863926

864927
long start = 1;
865-
DocumentPage termQueryResults = docMgr.search(termQuery, start, t1);
928+
t2 = writerClient.openTransaction();
929+
DocumentPage termQueryResults = docMgr.search(termQuery, start, t2);
866930
System.out
867931
.println("Number of results = " + termQueryResults.getTotalSize());
868932
assertEquals("Wrong number of results", 4, termQueryResults.getTotalSize());
@@ -871,7 +935,8 @@ public void testProtectUpdateInTransaction() throws Exception {
871935
System.out.println("Exception when update within 30 sec is " + e.getMessage());
872936
}
873937
finally {
874-
t1.rollback();
938+
if (t2 != null)
939+
t2.rollback();
875940
}
876941
}
877942

@@ -910,10 +975,11 @@ public void testProtectUpdateInDiffTransactions() throws Exception {
910975
JacksonDatabindHandle<ObjectNode> handleUpd = getJSONDocumentHandle(
911976
"2003-01-01T00:00:00", "2008-12-31T23:59:59",
912977
"1999 Skyway Park - Updated - JSON", docId);
978+
913979
StringBuilder str = new StringBuilder();
914980
try {
915981
// Use t1 to write
916-
docMgr.write(docId, null, handleUpd, null, t1, temporalLsqtCollectionName, updateTime);
982+
docMgr.write(docId, null, handleUpd, null, t1, temporalLsqtCollectionName, updateTime);
917983
}
918984
catch(Exception ex) {
919985
str.append(ex.getMessage());
@@ -952,6 +1018,9 @@ public void testProtectUpdateInDiffTransactions() throws Exception {
9521018
* Write doc in T1 with transaction timeout 2 minutes
9531019
* Protect in T2 with transaction timeout of 30 secs and Protect duration of 30 sec.
9541020
* Update doc in T1 within 30 sec duration.
1021+
* Timeout t2 transaction.
1022+
* Should be available update in t1 transaction now.
1023+
* Commit and read.
9551024
*/
9561025
public void testProtectDiffTransactionsTimeouts() throws Exception {
9571026
Transaction t1 = null;
@@ -990,6 +1059,8 @@ public void testProtectDiffTransactionsTimeouts() throws Exception {
9901059
str.append(ex.getMessage());
9911060
System.out.println("Exception when update within 30 sec is " + str.toString());
9921061
}
1062+
// Time out t2 transaction.
1063+
Thread.sleep(40000);
9931064
}
9941065

9951066
catch(Exception ex) {
@@ -998,26 +1069,26 @@ public void testProtectDiffTransactionsTimeouts() throws Exception {
9981069
finally {
9991070
if (t1 != null) {
10001071
// Try to update when T2 has timed out
1001-
docMgr.write(docId, null, handleUpd, null, t1, temporalLsqtCollectionName, updateTime);
1072+
docMgr.write(docId, null, handleUpd, null, t1, temporalLsqtCollectionName, updateTime);
10021073
Thread.sleep(5000);
10031074

10041075
JSONDocumentManager jsonDocMgr = writerClient.newJSONDocumentManager();
10051076
DocumentPage readResults = jsonDocMgr.read(t1, docId);
10061077
System.out.println("Number of results = " + readResults.size());
10071078
assertEquals("Wrong number of results", 1, readResults.size());
1079+
t1.commit();
10081080

10091081
QueryManager queryMgr = writerClient.newQueryManager();
10101082

10111083
StructuredQueryBuilder sqb = queryMgr.newStructuredQueryBuilder();
10121084
StructuredQueryDefinition termQuery = sqb.collection(temporalLsqtCollectionName);
10131085

10141086
long start = 1;
1015-
DocumentPage termQueryResults = docMgr.search(termQuery, start, t1);
1087+
DocumentPage termQueryResults = docMgr.search(termQuery, start);
10161088
System.out
10171089
.println("Number of results = " + termQueryResults.getTotalSize());
10181090
assertEquals("Wrong number of results", 4, termQueryResults.getTotalSize());
1019-
}
1020-
t1.rollback();
1091+
}
10211092
}
10221093
}
10231094
@Test

0 commit comments

Comments
 (0)