|
16 | 16 |
|
17 | 17 | package com.marklogic.client.functionaltest; |
18 | 18 |
|
| 19 | +import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; |
19 | 20 | import static org.junit.Assert.*; |
20 | 21 |
|
21 | 22 | import java.io.IOException; |
| 23 | +import java.util.Calendar; |
22 | 24 | import java.util.HashMap; |
23 | 25 |
|
24 | 26 | import javax.xml.parsers.ParserConfigurationException; |
25 | 27 | import javax.xml.transform.TransformerException; |
26 | 28 |
|
| 29 | +import org.custommonkey.xmlunit.exceptions.XpathException; |
27 | 30 | import org.junit.After; |
28 | 31 | import org.junit.AfterClass; |
29 | 32 | import org.junit.Before; |
|
55 | 58 | import com.marklogic.client.io.StringHandle; |
56 | 59 | import com.marklogic.client.io.DocumentMetadataHandle.Capability; |
57 | 60 | import com.marklogic.client.query.MatchDocumentSummary; |
| 61 | +import com.marklogic.client.query.QueryDefinition; |
58 | 62 | import com.marklogic.client.query.QueryManager; |
59 | 63 | import com.marklogic.client.query.RawStructuredQueryDefinition; |
60 | 64 | import com.marklogic.client.query.QueryManager.QueryView; |
@@ -334,7 +338,8 @@ public void testBulkSearchSQDwithTransactionsandDOMHandle() throws Exception{ |
334 | 338 | finally{t.rollback();} |
335 | 339 |
|
336 | 340 | docMgr.search(qd, 1,results); |
337 | | - assertNull("Total search results after rollback are ",results.get().getElementsByTagNameNS("*", "response").item(0).getAttributes().getNamedItem("total")); |
| 341 | + System.out.println(convertXMLDocumentToString(results.get())); |
| 342 | + assertEquals("Total search results after rollback are ",results.get().getElementsByTagNameNS("*", "response").item(0).getAttributes().getNamedItem("total").getNodeValue(),"0"); |
338 | 343 |
|
339 | 344 | } |
340 | 345 | //This test is to verify RAW XML structured Query |
@@ -454,4 +459,119 @@ public void testBulkSearchRawJSONStrucQD() throws Exception{ |
454 | 459 | System.out.println(results.get().toString()); |
455 | 460 | assertEquals("Total search results before transaction rollback are ","204",results.get().get("total").asText()); |
456 | 461 | } |
| 462 | + /* Searching for boolean and string in XML element using value query. |
| 463 | + * Purpose: To validate QueryBuilder's new value methods (in 8.0) in XML document using an element. |
| 464 | + * |
| 465 | + * Load a file that has a boolean value in a XML attribute and use query def to search on that boolean value |
| 466 | + * |
| 467 | + * Methods used : value(StructuredQueryBuilder.TextIndex index, boolean) |
| 468 | + * value(StructuredQueryBuilder.TextIndex index, String) |
| 469 | + */ |
| 470 | + @Test |
| 471 | + public void testQueryBuilderValueWithBooleanAndString() throws XpathException, SAXException, IOException { |
| 472 | + |
| 473 | + String docId[] = {"play-persons.xml"}; |
| 474 | + |
| 475 | + TextDocumentManager docMgr = client.newTextDocumentManager(); |
| 476 | + QueryManager queryMgr = client.newQueryManager(); |
| 477 | + DocumentWriteSet writeset = docMgr.newWriteSet(); |
| 478 | + |
| 479 | + // Put meta-data |
| 480 | + |
| 481 | + DocumentMetadataHandle metadataHandle = new DocumentMetadataHandle(); |
| 482 | + metadataHandle.getCollections().addAll("my-collection1","my-collection2"); |
| 483 | + metadataHandle.getPermissions().add("app-user", Capability.UPDATE, Capability.READ); |
| 484 | + metadataHandle.getProperties().put("reviewed", true); |
| 485 | + metadataHandle.getProperties().put("myString", "foo"); |
| 486 | + metadataHandle.getProperties().put("myInteger", 10); |
| 487 | + metadataHandle.getProperties().put("myDecimal", 34.56678); |
| 488 | + metadataHandle.getProperties().put("myCalendar", Calendar.getInstance().get(Calendar.YEAR)); |
| 489 | + metadataHandle.setQuality(23); |
| 490 | + |
| 491 | + writeset.addDefault(metadataHandle); |
| 492 | + |
| 493 | + // Create a new document using StringHandle |
| 494 | + StringBuffer strBuf = new StringBuffer(); |
| 495 | + |
| 496 | + strBuf.append("<PLAY>"); |
| 497 | + strBuf.append("<TITLE>All's Well That Ends Well</TITLE>"); |
| 498 | + strBuf.append("<PERSONAE>"); |
| 499 | + strBuf.append("<TITLE>Dramatis Personae</TITLE>"); |
| 500 | + |
| 501 | + strBuf.append("<PGROUP>"); |
| 502 | + strBuf.append("<subgroup>true</subgroup>"); |
| 503 | + |
| 504 | + strBuf.append("<PERSONA>KING OF FRANCE</PERSONA>"); |
| 505 | + strBuf.append("<PERSONA>DUKE OF FLORENCE</PERSONA>"); |
| 506 | + strBuf.append("<PERSONA>BERTRAM, Count of Rousillon.</PERSONA>"); |
| 507 | + strBuf.append("<PERSONA>LAFEU, an old lord.</PERSONA>"); |
| 508 | + strBuf.append("</PGROUP>"); |
| 509 | + |
| 510 | + strBuf.append("<PGROUP>"); |
| 511 | + strBuf.append("<subgroup>false</subgroup>"); |
| 512 | + |
| 513 | + strBuf.append("<PERSONA>PAROLLES, a follower of Bertram.</PERSONA>"); |
| 514 | + strBuf.append("<PERSONA>A Page. </PERSONA>"); |
| 515 | + strBuf.append("</PGROUP>"); |
| 516 | + |
| 517 | + strBuf.append("<PGROUP>"); |
| 518 | + strBuf.append("<subgroup>false</subgroup>"); |
| 519 | + strBuf.append("<PERSONA>COUNTESS OF ROUSILLON, mother to Bertram. </PERSONA>"); |
| 520 | + strBuf.append("<PERSONA>HELENA, a gentlewoman protected by the Countess.</PERSONA>"); |
| 521 | + strBuf.append("<PERSONA>An old Widow of Florence. </PERSONA>"); |
| 522 | + strBuf.append("<PERSONA>DIANA, daughter to the Widow.</PERSONA>"); |
| 523 | + strBuf.append("</PGROUP>"); |
| 524 | + |
| 525 | + strBuf.append("<PGROUP>"); |
| 526 | + strBuf.append("<subgroup>false</subgroup>"); |
| 527 | + strBuf.append("<PERSONA>VIOLENTA</PERSONA>"); |
| 528 | + strBuf.append("<PERSONA>MARIANA</PERSONA>"); |
| 529 | + strBuf.append("<GRPDESCR>neighbours and friends to the Widow.</GRPDESCR>"); |
| 530 | + strBuf.append("</PGROUP>"); |
| 531 | + |
| 532 | + strBuf.append("<PERSONA>Lords, Officers, Soldiers, &c., French and Florentine.</PERSONA>"); |
| 533 | + strBuf.append("</PERSONAE>"); |
| 534 | + strBuf.append("</PLAY>"); |
| 535 | + |
| 536 | + writeset.add("/1/"+docId[0], new StringHandle().with(strBuf.toString())); |
| 537 | + docMgr.write(writeset); |
| 538 | + |
| 539 | + docMgr.write(writeset); |
| 540 | + |
| 541 | + // Search for the range with attribute value true in rangeRelativeBucketConstraintOpt.xml document. |
| 542 | + StructuredQueryBuilder qb = new StructuredQueryBuilder(); |
| 543 | + |
| 544 | + // Build an object that represents StructuredQueryBuilder.ElementAttribute for use in values method |
| 545 | + // that is of type StructuredQueryBuilder.TextIndex |
| 546 | + |
| 547 | + QueryDefinition qd = qb.value(qb.element("subgroup"), false); |
| 548 | + |
| 549 | + // Create handle for the result |
| 550 | + StringHandle resultsHandle = new StringHandle().withFormat(Format.XML); |
| 551 | + queryMgr.search(qd, resultsHandle); |
| 552 | + |
| 553 | + // Get the result |
| 554 | + String resultDoc = resultsHandle.get(); |
| 555 | + |
| 556 | + System.out.println(resultDoc); |
| 557 | + //Verify that search response has found 1 element attribute |
| 558 | + assertXpathEvaluatesTo("fn:doc(\"/1/play-persons.xml\")", "string(//*[local-name()='response']//*[local-name()='result']//@*[local-name()='path'])", resultDoc); |
| 559 | + assertXpathEvaluatesTo("3", "count(//*[local-name()='response']//*[local-name()='match'])", resultDoc); |
| 560 | + |
| 561 | + // Search for the following royal (XML ELEMENT) in all-well.xml document. |
| 562 | + StructuredQueryBuilder qbStr = new StructuredQueryBuilder(); |
| 563 | + QueryDefinition qdStr = qbStr.value(qbStr.element("PERSONA"), "KING OF FRANCE","DUKE OF FLORENCE", "BERTRAM, Count of Rousillon.","LAFEU, an old lord."); |
| 564 | + |
| 565 | + // Create handle for the result |
| 566 | + StringHandle resultsHandleStr = new StringHandle().withFormat(Format.XML); |
| 567 | + queryMgr.search(qdStr, resultsHandleStr); |
| 568 | + |
| 569 | + // Get the result |
| 570 | + String resultDocStr = resultsHandleStr.get(); |
| 571 | + |
| 572 | + System.out.println(resultDocStr); |
| 573 | + //Verify that search response has found 4 PERSONA elements under /PLAY/PERSONAE |
| 574 | + assertXpathEvaluatesTo("fn:doc(\"/1/play-persons.xml\")", "string(//*[local-name()='response']//*[local-name()='result']//@*[local-name()='path'])", resultDocStr); |
| 575 | + assertXpathEvaluatesTo("4", "count(//*[local-name()='response']//*[local-name()='match'])", resultDocStr); |
| 576 | + } |
457 | 577 | } |
0 commit comments