|
36 | 36 | import com.marklogic.client.expression.CtsQueryBuilder; |
37 | 37 | import com.marklogic.client.query.*; |
38 | 38 | import com.marklogic.client.type.CtsQueryExpr; |
| 39 | +import com.marklogic.client.util.EditableNamespaceContext; |
39 | 40 | import org.junit.After; |
40 | 41 | import org.junit.AfterClass; |
41 | 42 | import org.junit.BeforeClass; |
@@ -102,6 +103,30 @@ public static void setUpBeforeClass() throws Exception { |
102 | 103 |
|
103 | 104 | setupJavaRESTServer(dbName, fNames[0], restServerName, restServerPort); |
104 | 105 | setupAppServicesConstraint(dbName); |
| 106 | + String[][] namespacePaths = { |
| 107 | + // { prefix, namespace-uri } |
| 108 | + // If there is a need to add additional fields, then add them to |
| 109 | + // the end |
| 110 | + // of each array |
| 111 | + // and pass empty strings ("") into an array where the |
| 112 | + // additional field |
| 113 | + // does not have a value. |
| 114 | + // For example : as in namespace, collections below. |
| 115 | + { "ns1", "http://www.example1.com" }, |
| 116 | + { "ns2", "http://www.example2.com" }, |
| 117 | + { "nsdate", "http://purl.org/dc/elements/1.1/" } |
| 118 | + // Add new namespace Paths as an array below. |
| 119 | + }; |
| 120 | + // Insert the namespaces path |
| 121 | + addPathNamespace(dbName, namespacePaths); |
| 122 | + |
| 123 | + // Add additional range path indices with namespaces. |
| 124 | + String[][] rangePaths = { |
| 125 | + {"int","/ns1:root/ns1:popularity","","ignore","false"}, |
| 126 | + {"string","/ns2:root/ns2:status","http://marklogic.com/collation/","ignore","false"}, |
| 127 | + {"date","//nsdate:date","","ignore","false"} |
| 128 | + }; |
| 129 | + addRangePathIndex(dbName, rangePaths); |
105 | 130 |
|
106 | 131 | createUserRolesWithPrevilages("test-eval", "xdbc:eval", "xdbc:eval-in", "xdmp:eval-in", "any-uri", "xdbc:invoke"); |
107 | 132 | createRESTUser("eval-user", "x", "test-eval", "rest-admin", "rest-writer", "rest-reader", "rest-extension-user", "manage-user"); |
@@ -2274,7 +2299,6 @@ public void testUTF8InUri() throws Exception |
2274 | 2299 | DataMovementManager dmManagerTmp = null; |
2275 | 2300 |
|
2276 | 2301 | try { |
2277 | | - System.out.println("Running testUTF8InUri"); |
2278 | 2302 |
|
2279 | 2303 | String[] filenames = { "constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml" }; |
2280 | 2304 | String combinedQueryFileName = "combinedQueryOptionJSON.json"; |
@@ -2368,4 +2392,144 @@ public void testUTF8InUri() throws Exception |
2368 | 2392 | clearDB(); |
2369 | 2393 | } |
2370 | 2394 | } |
| 2395 | + |
| 2396 | + // Verify namespaces in query. Refer to Git Issue 1283. |
| 2397 | + |
| 2398 | + @Test |
| 2399 | + public void testPathNameSpacesInQuery() throws Exception { |
| 2400 | + System.out.println("Running testPathNameSpacesInQuery"); |
| 2401 | + DatabaseClient clientTmp = null; |
| 2402 | + DataMovementManager dmManagerTmp = null; |
| 2403 | + |
| 2404 | + try { |
| 2405 | + // Insert docs |
| 2406 | + StringBuilder doc1 = new StringBuilder(); |
| 2407 | + doc1.append("<root xmlns=\"http://www.example1.com\">"); |
| 2408 | + doc1.append("<title>Vannevar Bush</title>"); |
| 2409 | + doc1.append("<popularity>5</popularity>"); |
| 2410 | + doc1.append("<id>0011</id>"); |
| 2411 | + doc1.append("<date xmlns=\"http://purl.org/dc/elements/1.1/\">2005-01-01</date>"); |
| 2412 | + doc1.append("<price xmlns=\"http://cloudbank.com\" amt=\"0.1\"/>"); |
| 2413 | + doc1.append("<p>Vannevar Bush wrote an article for The Atlantic Monthly</p>"); |
| 2414 | + doc1.append("<status>active</status>"); |
| 2415 | + doc1.append("<g-elem-point>12,5</g-elem-point>"); |
| 2416 | + doc1.append("<g-elem-child-parent><g-elem-child-point>12,5</g-elem-child-point></g-elem-child-parent>"); |
| 2417 | + doc1.append("<g-elem-pair><lat>12</lat><long>5</long></g-elem-pair>"); |
| 2418 | + doc1.append("<g-attr-pair lat=\"12\" long=\"5\"/>"); |
| 2419 | + doc1.append("</root>"); |
| 2420 | + |
| 2421 | + StringBuilder doc2 = new StringBuilder(); |
| 2422 | + doc2.append("<root xmlns=\"http://www.example2.com\">"); |
| 2423 | + doc2.append("<title>The Bush article</title>"); |
| 2424 | + doc2.append("<popularity>4</popularity>"); |
| 2425 | + doc2.append("<id>0012</id>"); |
| 2426 | + doc2.append("<date xmlns=\"http://purl.org/dc/elements/1.1/\">2006-02-02</date>"); |
| 2427 | + doc2.append("<price xmlns=\"http://cloudbank.com\" amt=\"0.12\"/>"); |
| 2428 | + doc2.append("<p>The Bush article described a device called a Memex.</p>"); |
| 2429 | + doc2.append("<status>active</status>"); |
| 2430 | + doc2.append("</root>"); |
| 2431 | + |
| 2432 | + StringBuilder doc3 = new StringBuilder(); |
| 2433 | + doc3.append("<root xmlns=\"http://www.example2.com\">"); |
| 2434 | + doc3.append("<title>For 1945</title>"); |
| 2435 | + doc3.append("<popularity>3</popularity>"); |
| 2436 | + doc3.append("<id>0113</id>"); |
| 2437 | + doc3.append("<date xmlns=\"http://purl.org/dc/elements/1.1/\">2007-03-03</date>"); |
| 2438 | + doc3.append("<price xmlns=\"http://cloudbank.com\" amt=\"1.22\"/>"); |
| 2439 | + doc3.append("<p>For 1945, the thoughts expressed in The Atlantic Monthly were groundbreaking.</p>"); |
| 2440 | + doc3.append("<status>pending</status>"); |
| 2441 | + doc3.append("</root>"); |
| 2442 | + |
| 2443 | + StringBuilder doc4 = new StringBuilder(); |
| 2444 | + doc4.append("<root xmlns=\"http://www.example2.com\">"); |
| 2445 | + doc4.append("<title>Vannevar served</title>"); |
| 2446 | + doc4.append("<popularity>5</popularity>"); |
| 2447 | + doc4.append("<id>0024</id>"); |
| 2448 | + doc4.append("<date xmlns=\"http://purl.org/dc/elements/1.1/\">2008-04-04</date>"); |
| 2449 | + doc4.append("<price xmlns=\"http://cloudbank.com\" amt=\"12.34\"/>"); |
| 2450 | + doc4.append("<p>Vannevar served as a prominent policymaker and public intellectual.</p>"); |
| 2451 | + doc4.append("<status>active</status>"); |
| 2452 | + doc4.append("</root>"); |
| 2453 | + |
| 2454 | + StringBuilder doc5 = new StringBuilder(); |
| 2455 | + doc5.append("<root xmlns=\"http://www.example2.com\">"); |
| 2456 | + doc5.append("<title>The memex</title>"); |
| 2457 | + doc5.append("<popularity>5</popularity>"); |
| 2458 | + doc5.append("<id>0026</id>"); |
| 2459 | + doc5.append("<date xmlns=\"http://purl.org/dc/elements/1.1/\">2009-05-05</date>"); |
| 2460 | + doc5.append("<price xmlns=\"http://cloudbank.com\" amt=\"123.45\"/>"); |
| 2461 | + doc5.append("<p>The Memex, unfortunately, had no automated search feature.</p>"); |
| 2462 | + doc5.append("<status>pending</status>"); |
| 2463 | + doc5.append("</root>"); |
| 2464 | + |
| 2465 | + clientTmp = getDatabaseClient("eval-user", "x", getConnType()); |
| 2466 | + dmManager = clientTmp.newDataMovementManager(); |
| 2467 | + WriteBatcher batcher = dmManager.newWriteBatcher(); |
| 2468 | + dmManager.startJob(batcher); |
| 2469 | + batcher.add("/testdoc/doc1,xml", new StringHandle(doc1.toString())); |
| 2470 | + batcher.add("/testdoc/doc2.xml", new StringHandle(doc2.toString())); |
| 2471 | + batcher.add("/testdoc/doc3.xml", new StringHandle(doc3.toString())); |
| 2472 | + batcher.add("/testdoc/doc4.xml", new StringHandle(doc4.toString())); |
| 2473 | + batcher.add("/testdoc/doc5.xml", new StringHandle(doc5.toString())); |
| 2474 | + |
| 2475 | + batcher.flushAndWait(); |
| 2476 | + QueryManager queryManager = clientTmp.newQueryManager(); |
| 2477 | + |
| 2478 | + StringBuilder resultUris = new StringBuilder(); |
| 2479 | + StringBuilder failStr = new StringBuilder(); |
| 2480 | + AtomicInteger docCnt = new AtomicInteger(0); |
| 2481 | + |
| 2482 | + StructuredQueryBuilder queryBuilder = queryManager.newStructuredQueryBuilder(); |
| 2483 | + EditableNamespaceContext namespaceContext = new EditableNamespaceContext(); |
| 2484 | + namespaceContext.put("nsdate", "http://purl.org/dc/elements/1.1/"); |
| 2485 | + namespaceContext.put("ns1", "http://www.example1.com"); |
| 2486 | + namespaceContext.put("ns2", "http://www.example2.com"); |
| 2487 | + queryBuilder.setNamespaces(namespaceContext); |
| 2488 | + |
| 2489 | + StructuredQueryDefinition qd = queryBuilder.range( |
| 2490 | + queryBuilder.pathIndex("//nsdate:date"), |
| 2491 | + "xs:date", StructuredQueryBuilder.Operator.GT, "2007-01-01"); |
| 2492 | + |
| 2493 | + QueryBatcher qb = dmManager.newQueryBatcher(qd) |
| 2494 | + .onUrisReady(batch -> { |
| 2495 | + System.out.println("Items: " + Arrays.asList(batch.getItems())); |
| 2496 | + for(String s:batch.getItems()) { |
| 2497 | + resultUris.append(s); |
| 2498 | + resultUris.append("|"); |
| 2499 | + docCnt.incrementAndGet(); |
| 2500 | + } |
| 2501 | + }) |
| 2502 | + .onQueryFailure(failure -> { |
| 2503 | + System.out.println("Failure: " + failure.getMessage()); |
| 2504 | + failStr.append(failure.getMessage()); |
| 2505 | + }); |
| 2506 | + try { |
| 2507 | + dmManager.startJob(qb); |
| 2508 | + qb.awaitCompletion(); |
| 2509 | + } catch (Exception e) { |
| 2510 | + System.out.println("Exceptions thrown from Query Batcher job"); |
| 2511 | + } |
| 2512 | + finally { |
| 2513 | + dmManager.stopJob(qb); |
| 2514 | + int ndocs = docCnt.get(); |
| 2515 | + if (! failStr.toString().isEmpty()) { |
| 2516 | + fail("QueryBatcher failed to query required docs."); |
| 2517 | + } |
| 2518 | + assertEquals("Number of docs returned incorrect", 3, ndocs); |
| 2519 | + String res = resultUris.toString(); |
| 2520 | + assertTrue("Doc 1 returned incorrect", res.contains("/testdoc/doc3.xml")); |
| 2521 | + assertTrue("Doc 2 returned incorrect", res.contains("/testdoc/doc4.xml")); |
| 2522 | + assertTrue("Doc 3 returned incorrect", res.contains("/testdoc/doc5.xml")); |
| 2523 | + } |
| 2524 | + |
| 2525 | + } catch (Exception e) { |
| 2526 | + System.out.println("Exceptions thrown from testPathNameSpacesInQuery"); |
| 2527 | + System.out.println(e.getMessage()); |
| 2528 | + fail("testPathNameSpacesInQuery mathod failed"); |
| 2529 | + } finally { |
| 2530 | + clientTmp.release(); |
| 2531 | + clearDB(); |
| 2532 | + } |
| 2533 | + |
| 2534 | + } |
2371 | 2535 | } |
0 commit comments