Skip to content

Commit fe0b3aa

Browse files
Ajit GeorgeAjit George
authored andcommitted
Tests changed for Git 362 and new tests for Git 361.
1 parent 4550830 commit fe0b3aa

File tree

1 file changed

+84
-34
lines changed

1 file changed

+84
-34
lines changed

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

Lines changed: 84 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
import com.marklogic.client.semantics.GraphManager;
6666
import com.marklogic.client.semantics.GraphPermissions;
6767
import com.marklogic.client.semantics.RDFMimeTypes;
68+
import com.marklogic.client.semantics.RDFTypes;
6869
import com.marklogic.client.semantics.SPARQLBindings;
6970
import com.marklogic.client.semantics.SPARQLQueryDefinition;
7071
import com.marklogic.client.semantics.SPARQLQueryManager;
@@ -831,32 +832,81 @@ public void testExecuteAsk() throws IOException, SAXException, ParserConfigurati
831832
assertTrue("Method testExecuteAsk result is incorrect. Expected true for empty ASK query", bAskEmpty);
832833
}
833834

834-
/* This test checks a simple SPARQL ASK results from named graph ina transaction.
835-
* The database should contain triples from foaf1.nt file. The data format in this file is XML.
835+
/* This test checks a simple SPARQL ASK results from named graph in a transaction.
836+
* The database contains triples from rdfxml1.rdf file. The data format in this file is RDFXML.
836837
*
837838
* The ASK query should be returning result in boolean.
838839
*
839840
*/
840841
@Test
841-
public void testExecuteAskTransaction() throws IOException, SAXException, ParserConfigurationException
842-
{
843-
System.out.println("In SPARQL Query Manager Test testExecuteAskTransaction method");
842+
public void testExecuteAskInTransactions() throws IOException, SAXException, ParserConfigurationException
843+
{
844+
System.out.println("In SPARQL Query Manager Test testExecuteAskInTransactions method");
844845
SPARQLQueryManager sparqlQmgr = writeclient.newSPARQLQueryManager();
845-
Transaction t = writeclient.openTransaction();
846-
847-
StringBuffer sparqlQuery = new StringBuffer().append("PREFIX foaf: <http://xmlns.com/foaf/0.1/>");
848-
sparqlQuery.append(newline) ;
849-
sparqlQuery.append("ASK { ?alum foaf:schoolHomepage <http://www.ucsb.edu/> }");
850-
846+
Transaction t1 = writeclient.openTransaction();
847+
Transaction t2 = writeclient.openTransaction();
848+
849+
StringBuffer sparqlQuery = new StringBuffer();
850+
sparqlQuery.append("ASK FROM <rdfxml> where { <http://example.org/kennedy/person1> <http://purl.org/dc/elements/1.1/title> \"Person\'s title\"@en }");
851+
851852
SPARQLQueryDefinition qdef = sparqlQmgr.newQueryDefinition(sparqlQuery.toString());
852-
boolean bAsk = sparqlQmgr.executeAsk(qdef, t);
853-
854-
// Verify result 1 value.
855-
System.out.println(bAsk);
856-
857-
assertTrue("Method testExecuteAskTransaction result is incorrect", bAsk);
853+
boolean bAskNoWrite = sparqlQmgr.executeAsk(qdef, t1);
854+
855+
// Verify result.
856+
System.out.println(bAskNoWrite);
857+
assertFalse("Method testExecuteAskInTransactions result is incorrect. No records should be returned.", bAskNoWrite);
858+
859+
// RDFXML "application/rdf+xml". Get the content into FileHandle
860+
File file = new File(datasource + "rdfxml1.rdf");
861+
862+
FileHandle filehandle = new FileHandle();
863+
filehandle.set(file);
864+
865+
// Create Graph manager
866+
GraphManager sparqlGmgr = writeclient.newGraphManager();
867+
// Write the triples in the doc into named graph.
868+
sparqlGmgr.write("rdfxml", filehandle.withMimetype(RDFMimeTypes.RDFXML), t1);
869+
870+
// Verify result in t1 transaction.
871+
boolean bAskInTransT1 = sparqlQmgr.executeAsk(qdef, t1);
872+
System.out.println(bAskInTransT1);
873+
assertTrue("Method testExecuteAskInTransactions result is incorrect. Records should be returned.", bAskInTransT1);
874+
875+
// Verify result in t2 transaction.
876+
boolean bAskInTransT2 = sparqlQmgr.executeAsk(qdef, t2);
877+
System.out.println(bAskInTransT2);
878+
assertFalse("Method testExecuteAskInTransactions result is incorrect. No Records should be returned.", bAskInTransT2);
879+
// Handle the transactions.
880+
t1.rollback();
881+
t2.rollback();
882+
883+
boolean bAskTransRolledback = sparqlQmgr.executeAsk(qdef);
884+
System.out.println(bAskTransRolledback);
885+
assertFalse("Method testExecuteAskInTransactions result is incorrect. No records should be returned.", bAskTransRolledback);
886+
887+
// After rollback. Open another transaction and verify ASK on that transaction.
888+
Transaction tAfterRollback = writeclient.openTransaction();
889+
// Write the triples in the doc into either named graph.
890+
sparqlGmgr.write("rdfxml", filehandle.withMimetype(RDFMimeTypes.RDFXML), tAfterRollback);
891+
tAfterRollback.commit();
892+
893+
boolean bAskAfterCommit = sparqlQmgr.executeAsk(qdef);
894+
System.out.println(bAskAfterCommit);
895+
assertTrue("Method testExecuteAskInTransactions result is incorrect. Records should be returned.", bAskAfterCommit);
896+
897+
// After commit. Open another transaction and verify ASK on that transaction.
898+
Transaction tAnother = writeclient.openTransaction();
899+
// Verify result.
900+
boolean bAskInAnotherTrans = sparqlQmgr.executeAsk(qdef, tAnother);
901+
System.out.println(bAskInAnotherTrans);
902+
assertFalse("Method testExecuteAskInTransactions result is incorrect. Records should be returned.", bAskInAnotherTrans);
903+
858904
// Handle the transaction.
859-
t.commit();
905+
tAnother.commit();
906+
t1 = null;
907+
t2 = null;
908+
tAfterRollback = null;
909+
tAnother = null;
860910
}
861911

862912
/* This test checks if Exceptions are throw when qdef is null.
@@ -1040,7 +1090,7 @@ public void testQueryBindingsOnString() throws IOException, SAXException, Parser
10401090

10411091
// Set up the String variable binding
10421092
SPARQLBindings bindings = qdef1.getBindings();
1043-
bindings.bind("firstname", "Ling", "string");
1093+
bindings.bind("firstname", "Ling", RDFTypes.STRING);
10441094
qdef1.setBindings(bindings);
10451095
// Parsing results using JsonNode.
10461096
JsonNode jsonStrResults = sparqlQmgr.executeSelect(qdef1, new JacksonHandle()).get();
@@ -1092,7 +1142,7 @@ public void testQueryWithBindingsOnString() throws IOException, SAXException, Pa
10921142
SPARQLQueryDefinition qdef1 = sparqlQmgr.newQueryDefinition(queryStr.toString());
10931143

10941144
// Set up the String variable binding
1095-
qdef1.withBinding("firstname", "Ling", "string");
1145+
qdef1.withBinding("firstname", "Ling", RDFTypes.STRING);
10961146
// Parsing results using JsonNode.
10971147
JsonNode jsonStrResults = sparqlQmgr.executeSelect(qdef1, new JacksonHandle()).get();
10981148
System.out.println(jsonStrResults);
@@ -1259,7 +1309,7 @@ public void testQueryBindingsOnInteger() throws IOException, SAXException, Parse
12591309

12601310
// Set up the String variable binding
12611311
SPARQLBindings bindings = qdef1.getBindings();
1262-
bindings.bind("cost","8","integer");
1312+
bindings.bind("cost","8", RDFTypes.INTEGER);
12631313
qdef1.setBindings(bindings);
12641314
// Parsing results using JsonNode.
12651315
JsonNode jsonStrResults = sparqlQmgr.executeSelect(qdef1, new JacksonHandle()).get();
@@ -1311,9 +1361,9 @@ public void testQueryBindingsAskOnStrings() throws IOException, SAXException, Pa
13111361

13121362
// Set up the String variable binding
13131363
SPARQLBindings bindings = qdef1.getBindings();
1314-
bindings.bind("firstname","John","string");
1364+
bindings.bind("firstname","John", RDFTypes.STRING);
13151365
qdef1.setBindings(bindings);
1316-
bindings.bind("firstname","Ling","string");
1366+
bindings.bind("firstname","Ling", RDFTypes.STRING);
13171367
qdef1.setBindings(bindings);
13181368
// Parsing results using JsonNode.
13191369
JsonNode jsonStrResults = sparqlQmgr.executeSelect(qdef1, new JacksonHandle()).get();
@@ -1366,7 +1416,7 @@ public void testQueryBindingsNullString() throws IOException, SAXException, Pars
13661416
String expectedException = "IllegalArgumentException";
13671417
String exception = "";
13681418
try {
1369-
bindings.bind("firstname", null,"string");
1419+
bindings.bind("firstname", null, RDFTypes.STRING);
13701420
} catch (Exception e) {
13711421
exception = e.toString();
13721422
}
@@ -1415,7 +1465,7 @@ public void testQueryBindingsDifferentVariableName() throws IOException, SAXExce
14151465

14161466
// Set up the String variable binding with wrong name
14171467
SPARQLBindings bindings = qdef1.getBindings();
1418-
bindings.bind("blahblah", "Ling","string");
1468+
bindings.bind("blahblah", "Ling", RDFTypes.STRING);
14191469
qdef1.setBindings(bindings);
14201470
// Parsing results using JsonNode.
14211471
JsonNode jsonStrResults = sparqlQmgr.executeSelect(qdef1, new JacksonHandle()).get();
@@ -1489,7 +1539,7 @@ public void testSparqlUpdateInsertDataBinding() throws IOException, SAXException
14891539

14901540
// Set up the integer variable binding
14911541
SPARQLBindings bindings = qdef.getBindings();
1492-
bindings.bind("playerid", "30","integer");
1542+
bindings.bind("playerid", "30",RDFTypes.INTEGER);
14931543
qdef.setBindings(bindings);
14941544

14951545
sparqlQmgr.executeUpdate(qdef);
@@ -1509,7 +1559,7 @@ public void testSparqlUpdateInsertDataBinding() throws IOException, SAXException
15091559
SPARQLQueryDefinition qdef2 = sparqlQmgr.newQueryDefinition(queryInsStr.toString());
15101560
// Set up the integer variable binding
15111561
SPARQLBindings bindings2 = qdef2.getBindings();
1512-
bindings2.bind("playerid", "30","integer");
1562+
bindings2.bind("playerid", "30", RDFTypes.INTEGER);
15131563
qdef2.setBindings(bindings2);
15141564
//Set the base URI. This gets concatented to the relative URI (BindingsGraph).
15151565
String baseuri2 = "http://qa.marklogic.com/qdef2/";
@@ -1530,7 +1580,7 @@ public void testSparqlUpdateInsertDataBinding() throws IOException, SAXException
15301580
SPARQLQueryDefinition qdefNull = sparqlQmgr.newQueryDefinition(queryInsStr.toString());
15311581
// Set up the integer variable binding
15321582
SPARQLBindings bindingsNull = qdefNull.getBindings();
1533-
bindingsNull.bind("playerid", "30","integer");
1583+
bindingsNull.bind("playerid", "30", RDFTypes.INTEGER);
15341584
qdefNull.setBindings(bindingsNull);
15351585
//Set the base URI. This gets concatented to the relative URI (BindingsGraph).
15361586
String baseuriNull = null;
@@ -1549,7 +1599,7 @@ public void testSparqlUpdateInsertDataBinding() throws IOException, SAXException
15491599

15501600
// Set up the integer variable binding
15511601
SPARQLBindings bindingsEmpty = qdefEmpty.getBindings();
1552-
bindingsEmpty.bind("playerid", "30", "integer");
1602+
bindingsEmpty.bind("playerid", "30", RDFTypes.INTEGER);
15531603
qdefEmpty.setBindings(bindingsEmpty);
15541604

15551605
// Set the base URI. This gets concatented to the relative URI
@@ -1574,7 +1624,7 @@ public void testSparqlUpdateInsertDataBinding() throws IOException, SAXException
15741624
SPARQLQueryDefinition qdefMB = sparqlQmgr.newQueryDefinition(queryInsStr.toString());
15751625
// Set up the integer variable binding
15761626
SPARQLBindings bindingsMB = qdefMB.getBindings();
1577-
bindingsMB.bind("playerid", "30","integer");
1627+
bindingsMB.bind("playerid", "30", RDFTypes.INTEGER);
15781628
qdefMB.setBindings(bindingsMB);
15791629
//Set the base URI. This gets concatented to the relative URI (BindingsGraph).
15801630
String baseuriMB = "http://qa.marklogic.com/qdef2/" + multibyteName + "/";
@@ -1634,7 +1684,7 @@ public void testQueryBindingsIncorrectDataType() throws IOException, SAXExceptio
16341684

16351685
// Set up the String variable binding with wrong data type.
16361686
SPARQLBindings bindings = qdef1.getBindings();
1637-
bindings.bind("firstname", "Ling","dateTimeStamp");
1687+
bindings.bind("firstname", "Ling", RDFTypes.DATETIME);
16381688
qdef1.setBindings(bindings);
16391689

16401690
String expectedException = "FailedRequestException";
@@ -1646,7 +1696,7 @@ public void testQueryBindingsIncorrectDataType() throws IOException, SAXExceptio
16461696
}
16471697
System.out.println("Exception thrown from testQueryBindingsIncorrectDataType is \n"+ exception);
16481698
assertTrue("Test testQueryBindingsIncorrectDataType method exception is not thrown", exception.contains(expectedException));
1649-
assertTrue("Message Incorrect", exception.contains("Invalid parameter: Bind variable type parameter requires XSD type"));
1699+
assertTrue("Message Incorrect", exception.contains("Invalid cast: \"Ling\" cast as xs:dateTime"));
16501700
}
16511701

16521702
/* This test verifies sparql update CREATE GRAPH, INSERT DATA and also validates SPARQL EXISTS.
@@ -2265,7 +2315,7 @@ public void testConstrainingQuery() throws IOException, SAXException, ParserConf
22652315
// Set up the String variable binding.
22662316
SPARQLBindings bindings = qdef.getBindings();
22672317

2268-
bindings.bind("firstname","Lei","string");
2318+
bindings.bind("firstname","Lei", RDFTypes.STRING);
22692319
qdef.setBindings(bindings);
22702320

22712321
// create query def.
@@ -2328,7 +2378,7 @@ public void testConstrainingQueryNull() throws IOException, SAXException, Parser
23282378
// Set up the String variable binding.
23292379
SPARQLBindings bindings = qdef.getBindings();
23302380

2331-
bindings.bind("firstname","Lei","string");
2381+
bindings.bind("firstname","Lei", RDFTypes.STRING);
23322382
qdef.setBindings(bindings);
23332383
qdef.setConstrainingQueryDefinition(null);
23342384
// Parsing results using JsonNode.

0 commit comments

Comments
 (0)