|
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 |
|
| 22 | +import java.io.File; |
21 | 23 | import java.io.IOException; |
| 24 | +import java.util.Calendar; |
22 | 25 | import java.util.HashMap; |
23 | 26 |
|
24 | 27 | import javax.xml.parsers.ParserConfigurationException; |
25 | 28 | import javax.xml.transform.TransformerException; |
26 | 29 |
|
| 30 | +import org.custommonkey.xmlunit.exceptions.XpathException; |
27 | 31 | import org.junit.After; |
28 | 32 | import org.junit.AfterClass; |
29 | 33 | import org.junit.Before; |
|
47 | 51 | import com.marklogic.client.document.XMLDocumentManager; |
48 | 52 | import com.marklogic.client.io.DOMHandle; |
49 | 53 | import com.marklogic.client.io.DocumentMetadataHandle; |
| 54 | +import com.marklogic.client.io.FileHandle; |
50 | 55 | import com.marklogic.client.io.Format; |
51 | 56 | import com.marklogic.client.io.JacksonHandle; |
52 | 57 | import com.marklogic.client.io.SearchHandle; |
53 | 58 | import com.marklogic.client.io.StringHandle; |
54 | 59 | import com.marklogic.client.io.DocumentMetadataHandle.Capability; |
55 | 60 | import com.marklogic.client.query.MatchDocumentSummary; |
56 | 61 | import com.marklogic.client.query.MatchLocation; |
| 62 | +import com.marklogic.client.query.QueryDefinition; |
57 | 63 | import com.marklogic.client.query.QueryManager; |
| 64 | +import com.marklogic.client.query.StructuredQueryBuilder; |
58 | 65 | import com.marklogic.client.query.QueryManager.QueryView; |
59 | 66 | import com.marklogic.client.query.StringQueryDefinition; |
60 | 67 |
|
@@ -393,5 +400,121 @@ public void testBulkSearchSQDwithTransactionsandDOMHandle() throws Exception{ |
393 | 400 | assertNull("Total search results after rollback are ",results.get().getElementsByTagNameNS("*", "response").item(0).getAttributes().getNamedItem("total")); |
394 | 401 |
|
395 | 402 | } |
| 403 | + |
| 404 | + |
| 405 | + /* Searching for boolean and string in XML element using value query. |
| 406 | + * Purpose: To validate QueryBuilder's new value methods (in 8.0) in XML document using an element. |
| 407 | + * |
| 408 | + * Load a file that has a boolean value in a XML attribute and use query def to search on that boolean value |
| 409 | + * |
| 410 | + * Methods used : value(StructuredQueryBuilder.TextIndex index, boolean) |
| 411 | + * value(StructuredQueryBuilder.TextIndex index, String) |
| 412 | + */ |
| 413 | + @Test |
| 414 | + public void testQueryBuilderValueWithBooleanAndString() throws XpathException, SAXException, IOException { |
| 415 | + |
| 416 | + String docId[] = {"play-persons.xml"}; |
| 417 | + |
| 418 | + TextDocumentManager docMgr = client.newTextDocumentManager(); |
| 419 | + QueryManager queryMgr = client.newQueryManager(); |
| 420 | + DocumentWriteSet writeset = docMgr.newWriteSet(); |
| 421 | + |
| 422 | + // Put meta-data |
| 423 | + |
| 424 | + DocumentMetadataHandle metadataHandle = new DocumentMetadataHandle(); |
| 425 | + metadataHandle.getCollections().addAll("my-collection1","my-collection2"); |
| 426 | + metadataHandle.getPermissions().add("app-user", Capability.UPDATE, Capability.READ); |
| 427 | + metadataHandle.getProperties().put("reviewed", true); |
| 428 | + metadataHandle.getProperties().put("myString", "foo"); |
| 429 | + metadataHandle.getProperties().put("myInteger", 10); |
| 430 | + metadataHandle.getProperties().put("myDecimal", 34.56678); |
| 431 | + metadataHandle.getProperties().put("myCalendar", Calendar.getInstance().get(Calendar.YEAR)); |
| 432 | + metadataHandle.setQuality(23); |
| 433 | + |
| 434 | + writeset.addDefault(metadataHandle); |
| 435 | + |
| 436 | + // Create a new document using StringHandle |
| 437 | + StringBuffer strBuf = new StringBuffer(); |
| 438 | + |
| 439 | + strBuf.append("<PLAY>"); |
| 440 | + strBuf.append("<TITLE>All's Well That Ends Well</TITLE>"); |
| 441 | + strBuf.append("<PERSONAE>"); |
| 442 | + strBuf.append("<TITLE>Dramatis Personae</TITLE>"); |
| 443 | + |
| 444 | + strBuf.append("<PGROUP>"); |
| 445 | + strBuf.append("<subgroup>true</subgroup>"); |
| 446 | + |
| 447 | + strBuf.append("<PERSONA>KING OF FRANCE</PERSONA>"); |
| 448 | + strBuf.append("<PERSONA>DUKE OF FLORENCE</PERSONA>"); |
| 449 | + strBuf.append("<PERSONA>BERTRAM, Count of Rousillon.</PERSONA>"); |
| 450 | + strBuf.append("<PERSONA>LAFEU, an old lord.</PERSONA>"); |
| 451 | + strBuf.append("</PGROUP>"); |
| 452 | + |
| 453 | + strBuf.append("<PGROUP>"); |
| 454 | + strBuf.append("<subgroup>false</subgroup>"); |
| 455 | + |
| 456 | + strBuf.append("<PERSONA>PAROLLES, a follower of Bertram.</PERSONA>"); |
| 457 | + strBuf.append("<PERSONA>A Page. </PERSONA>"); |
| 458 | + strBuf.append("</PGROUP>"); |
| 459 | + |
| 460 | + strBuf.append("<PGROUP>"); |
| 461 | + strBuf.append("<subgroup>false</subgroup>"); |
| 462 | + strBuf.append("<PERSONA>COUNTESS OF ROUSILLON, mother to Bertram. </PERSONA>"); |
| 463 | + strBuf.append("<PERSONA>HELENA, a gentlewoman protected by the Countess.</PERSONA>"); |
| 464 | + strBuf.append("<PERSONA>An old Widow of Florence. </PERSONA>"); |
| 465 | + strBuf.append("<PERSONA>DIANA, daughter to the Widow.</PERSONA>"); |
| 466 | + strBuf.append("</PGROUP>"); |
| 467 | + |
| 468 | + strBuf.append("<PGROUP>"); |
| 469 | + strBuf.append("<subgroup>false</subgroup>"); |
| 470 | + strBuf.append("<PERSONA>VIOLENTA</PERSONA>"); |
| 471 | + strBuf.append("<PERSONA>MARIANA</PERSONA>"); |
| 472 | + strBuf.append("<GRPDESCR>neighbours and friends to the Widow.</GRPDESCR>"); |
| 473 | + strBuf.append("</PGROUP>"); |
| 474 | + |
| 475 | + strBuf.append("<PERSONA>Lords, Officers, Soldiers, &c., French and Florentine.</PERSONA>"); |
| 476 | + strBuf.append("</PERSONAE>"); |
| 477 | + strBuf.append("</PLAY>"); |
| 478 | + |
| 479 | + writeset.add("/1/"+docId[0], new StringHandle().with(strBuf.toString())); |
| 480 | + docMgr.write(writeset); |
| 481 | + |
| 482 | + docMgr.write(writeset); |
| 483 | + |
| 484 | + // Search for the range with attribute value true in rangeRelativeBucketConstraintOpt.xml document. |
| 485 | + StructuredQueryBuilder qb = new StructuredQueryBuilder(); |
| 486 | + |
| 487 | + // Build an object that represents StructuredQueryBuilder.ElementAttribute for use in values method |
| 488 | + // that is of type StructuredQueryBuilder.TextIndex |
| 489 | + |
| 490 | + QueryDefinition qd = qb.value(qb.element("subgroup"), false); |
| 491 | + |
| 492 | + // Create handle for the result |
| 493 | + StringHandle resultsHandle = new StringHandle().withFormat(Format.XML); |
| 494 | + queryMgr.search(qd, resultsHandle); |
| 495 | + |
| 496 | + // Get the result |
| 497 | + String resultDoc = resultsHandle.get(); |
| 498 | + |
| 499 | + System.out.println(resultDoc); |
| 500 | + //Verify that search response has found 1 element attribute |
| 501 | + assertXpathEvaluatesTo("fn:doc(\"/1/play-persons.xml\")", "string(//*[local-name()='response']//*[local-name()='result']//@*[local-name()='path'])", resultDoc); |
| 502 | + assertXpathEvaluatesTo("3", "count(//*[local-name()='response']//*[local-name()='match'])", resultDoc); |
| 503 | + |
| 504 | + // Search for the following royal (XML ELEMENT) in all-well.xml document. |
| 505 | + StructuredQueryBuilder qbStr = new StructuredQueryBuilder(); |
| 506 | + QueryDefinition qdStr = qbStr.value(qbStr.element("PERSONA"), "KING OF FRANCE","DUKE OF FLORENCE", "BERTRAM, Count of Rousillon.","LAFEU, an old lord."); |
| 507 | + |
| 508 | + // Create handle for the result |
| 509 | + StringHandle resultsHandleStr = new StringHandle().withFormat(Format.XML); |
| 510 | + queryMgr.search(qdStr, resultsHandleStr); |
| 511 | + |
| 512 | + // Get the result |
| 513 | + String resultDocStr = resultsHandleStr.get(); |
| 514 | + |
| 515 | + System.out.println(resultDocStr); |
| 516 | + //Verify that search response has found 4 PERSONA elements under /PLAY/PERSONAE |
| 517 | + assertXpathEvaluatesTo("fn:doc(\"/1/play-persons.xml\")", "string(//*[local-name()='response']//*[local-name()='result']//@*[local-name()='path'])", resultDocStr); |
| 518 | + assertXpathEvaluatesTo("4", "count(//*[local-name()='response']//*[local-name()='match'])", resultDocStr); |
| 519 | + } |
396 | 520 | } |
397 | | - |
|
0 commit comments