Skip to content

Commit fe7234d

Browse files
georgeajitgeorgeajit
authored andcommitted
#1283 - Sanity check QA tests
1 parent acbd6e2 commit fe7234d

File tree

2 files changed

+189
-4
lines changed

2 files changed

+189
-4
lines changed

marklogic-client-api-functionaltests/src/test/java/com/marklogic/client/datamovement/functionaltests/StringQueryHostBatcherTest.java

Lines changed: 165 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import com.marklogic.client.expression.CtsQueryBuilder;
3737
import com.marklogic.client.query.*;
3838
import com.marklogic.client.type.CtsQueryExpr;
39+
import com.marklogic.client.util.EditableNamespaceContext;
3940
import org.junit.After;
4041
import org.junit.AfterClass;
4142
import org.junit.BeforeClass;
@@ -102,6 +103,30 @@ public static void setUpBeforeClass() throws Exception {
102103

103104
setupJavaRESTServer(dbName, fNames[0], restServerName, restServerPort);
104105
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);
105130

106131
createUserRolesWithPrevilages("test-eval", "xdbc:eval", "xdbc:eval-in", "xdmp:eval-in", "any-uri", "xdbc:invoke");
107132
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
22742299
DataMovementManager dmManagerTmp = null;
22752300

22762301
try {
2277-
System.out.println("Running testUTF8InUri");
22782302

22792303
String[] filenames = { "constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml" };
22802304
String combinedQueryFileName = "combinedQueryOptionJSON.json";
@@ -2368,4 +2392,144 @@ public void testUTF8InUri() throws Exception
23682392
clearDB();
23692393
}
23702394
}
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+
}
23712535
}

marklogic-client-api-functionaltests/src/test/java/com/marklogic/client/functionaltest/ConnectedRESTQA.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1514,6 +1514,30 @@ public static void addRangePathIndex(String dbName, String[][] rangePaths) throw
15141514
setDatabaseProperties(dbName, "range-path-index", childNode);
15151515
}
15161516

1517+
public static void addPathNamespace(String dbName, String[][] pathNamespace) throws Exception {
1518+
ObjectMapper mapper = new ObjectMapper();
1519+
ObjectNode childNode = mapper.createObjectNode();
1520+
ArrayNode childArray = mapper.createArrayNode();
1521+
1522+
int nRowsLen = pathNamespace.length;
1523+
int j = 0;
1524+
for (int i = 0; i < nRowsLen; i++) {
1525+
ObjectNode childNodeObject = mapper.createObjectNode();
1526+
childNodeObject.put("prefix", pathNamespace[i][j++]);
1527+
childNodeObject.put("namespace-uri", pathNamespace[i][j++]);
1528+
/*
1529+
* if new field elements are to be added, then: 1) Increment value
1530+
* of j 2) add them below here using
1531+
* childNodeObject.put("FIELD-NAME", rangePaths[i][j++]);
1532+
*/
1533+
1534+
childArray.add(childNodeObject);
1535+
j = 0;
1536+
}
1537+
childNode.putArray("path-namespace").addAll(childArray);
1538+
setDatabaseProperties(dbName, "path-namespace", childNode);
1539+
}
1540+
15171541
public static void addGeospatialElementIndexes(String dbName, String localname, String namespace,
15181542
String coordinateSystem, String pointFormat, boolean rangeValuePositions, String invalidValues)
15191543
throws Exception {
@@ -1857,12 +1881,9 @@ public static void setupAppServicesConstraint(String dbName) throws Exception {
18571881

18581882
enableCollectionLexicon(dbName);
18591883
enableWordLexicon(dbName);
1860-
18611884
// Insert the range indices
18621885
addRangeElementIndex(dbName, rangeElements);
1863-
18641886
enableTrailingWildcardSearches(dbName);
1865-
18661887
// Insert the path range indices
18671888
addRangePathIndex(dbName, rangePaths);
18681889
}

0 commit comments

Comments
 (0)