Skip to content

Commit

Permalink
JENA-2837: Reduce use of model APIs in Fuseki main testing
Browse files Browse the repository at this point in the history
  • Loading branch information
afs committed Nov 14, 2024
1 parent f391055 commit cf96347
Show file tree
Hide file tree
Showing 17 changed files with 186 additions and 169 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ public default Stream<Binding> stream() {

/**
* Normally a RowSet is processed until complete which implicitly closes any
* underlying resources. This "close" operation exists to explicitly do this in
* cases where it does onto automatically. There is no need to close RowSets
* underlying resources. This "close" operation exists to explicitly end use of the RowSet
* in cases where it does happen. There is no need to close RowSets
* normally - it is the {@link QueryExec} that should be closed.
*/
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ protected QueryExec createQueryExec(HttpAction action, Query query, DatasetGraph
return super.createQueryExec(action, query, target);

SecurityContext sCxt = DataAccessLib.getSecurityContext(action, dsg, requestUser);
// A QueryExecution for controlled access
// A query execution for controlled access
QueryExec qExec = sCxt.createQueryExec(query, target);
return qExec;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

import org.apache.jena.graph.Node;
import org.apache.jena.query.Query;
import org.apache.jena.query.QueryExecution;
import org.apache.jena.sparql.core.DatasetGraph;
import org.apache.jena.sparql.core.Quad;
import org.apache.jena.sparql.exec.QueryExec;
Expand Down Expand Up @@ -99,7 +98,7 @@ public QueryExec createQueryExec(Query query, DatasetGraph dsg) {

/**
* Apply a filter suitable for the TDB-backed {@link DatasetGraph}, to the {@link Context} of the
* {@link QueryExecution}. This does not modify the {@link DatasetGraph}.
* {@link QueryExec}. This does not modify the {@link DatasetGraph}.
*/
@Override
public void filterTDB(DatasetGraph dsg, QueryExec qExec) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ private Set<Node> graphs(DatasetGraph dsg, SecurityContext sCxt) {
assertSeen(visible);
}

// QueryExecution
// Query execution
private void filter_user(String user, Node ... expected) {
SecurityContext sCxt = reg.get(user);
Set<Node> visible = subjects(testdsg, queryAll, sCxt);
Expand Down Expand Up @@ -241,7 +241,7 @@ private void filter_user(String user, Node ... expected) {
assertSeen(visible);
}

// QueryExecution w/ Union default graph
// Query execution w/ union default graph
private void filter_union_user(String user, Node ... expected) {
SecurityContext sCxt = reg.get(user);
Set<Node> visible;
Expand Down Expand Up @@ -289,56 +289,56 @@ private void filter_union_user(String user, Node ... expected) {
}


// Graph/Model
@Test public void query_model_userNone() {
query_model_user(testdsg, dsg->dsg.getDefaultGraph(), "userNone");
// Graph
@Test public void query_graph_userNone() {
query_graph_user(testdsg, dsg->dsg.getDefaultGraph(), "userNone");
}

@Test public void query_model_userDft() {
query_model_user(testdsg, dsg->dsg.getDefaultGraph(), "userDft", s0);
@Test public void query_graph_userDft() {
query_graph_user(testdsg, dsg->dsg.getDefaultGraph(), "userDft", s0);
}

@Test public void query_model_user0() {
query_model_user(testdsg, dsg->dsg.getDefaultGraph(), "user0", s0);
@Test public void query_graph_user0() {
query_graph_user(testdsg, dsg->dsg.getDefaultGraph(), "user0", s0);
}

@Test public void query_model_user1() {
query_model_user(testdsg, dsg->dsg.getDefaultGraph(), "user1", s0);
@Test public void query_graph_user1() {
query_graph_user(testdsg, dsg->dsg.getDefaultGraph(), "user1", s0);
}

@Test public void query_model_user2() {
query_model_user(testdsg, dsg->dsg.getDefaultGraph(), "user2");
@Test public void query_graph_user2() {
query_graph_user(testdsg, dsg->dsg.getDefaultGraph(), "user2");
}

@Test public void query_model_ng_userNone() {
query_model_user(testdsg, dsg->dsg.getGraph(g1), "userNone");
@Test public void query_graph_ng_userNone() {
query_graph_user(testdsg, dsg->dsg.getGraph(g1), "userNone");
}

@Test public void query_model_ng_user11() {
query_model_user(testdsg, dsg->dsg.getGraph(g1), "user1", s1);
@Test public void query_graph_ng_user11() {
query_graph_user(testdsg, dsg->dsg.getGraph(g1), "user1", s1);
}

@Test public void query_model_ng_user21() {
query_model_user(testdsg, dsg->dsg.getGraph(g1), "user2", s1);
@Test public void query_graph_ng_user21() {
query_graph_user(testdsg, dsg->dsg.getGraph(g1), "user2", s1);
}

@Test public void query_model_ng_user12() {
query_model_user(testdsg, dsg->dsg.getGraph(g2), "user1");
@Test public void query_graph_ng_user12() {
query_graph_user(testdsg, dsg->dsg.getGraph(g2), "user1");
}

@Test public void query_model_ng_user22() {
query_model_user(testdsg, dsg->dsg.getGraph(g2), "user2", s2);
@Test public void query_graph_ng_user22() {
query_graph_user(testdsg, dsg->dsg.getGraph(g2), "user2", s2);
}

@Test public void query_model_userXa() {
query_model_user(testdsg, dsg->dsg.getDefaultGraph(), "userX");
@Test public void query_graph_userXa() {
query_graph_user(testdsg, dsg->dsg.getDefaultGraph(), "userX");
}

@Test public void query_model_userXb() {
query_model_user(testdsg, dsg->dsg.getGraph(g1), "userX");
@Test public void query_graph_userXb() {
query_graph_user(testdsg, dsg->dsg.getGraph(g1), "userX");
}

private void query_model_user(DatasetGraph dsg, Function<DatasetGraph, Graph> graphChoice, String user, Node ... expected) {
private void query_graph_user(DatasetGraph dsg, Function<DatasetGraph, Graph> graphChoice, String user, Node ... expected) {
SecurityContext sCxt = reg.get(user);
Set<Node> visible = subjects(dsg, graphChoice, queryDft, sCxt);
assertSeen(visible, expected);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,11 +306,11 @@ private static void abortSilent(HttpAction action) {
*/
protected abstract void validateQuery(HttpAction action, Query query);

/** Create the {@link QueryExecution} for this operation.
/** Create the {@link QueryExec} for this operation.
* @param action
* @param query
* @param dataset
* @return QueryExecution
* @return QueryExec
*/
protected QueryExec createQueryExec(HttpAction action, Query query, DatasetGraph dataset) {
QueryExecDatasetBuilder builder = QueryExec.newBuilder()
Expand Down Expand Up @@ -368,16 +368,16 @@ private static long chooseTimeout(long setupTimeout, long protocolTimeout) {
return setupTimeout;
}

/** Perform the {@link QueryExecution} once.
/** Perform the {@link QueryExec} once.
* @param action
* @param queryExecution
* @param requestQuery Original query; queryExecution query may have been modified.
* @param queryExec
* @param requestQuery Original query; the QueryExec query may have been modified.
* @param queryStringLog Informational string created from the initial query.
* @return
*/
protected QueryExecResult executeQuery(HttpAction action, QueryExec queryExecution, Query requestQuery, String queryStringLog) {
protected QueryExecResult executeQuery(HttpAction action, QueryExec queryExec, Query requestQuery, String queryStringLog) {
if ( requestQuery.isSelectType() ) {
RowSet rs = queryExecution.select();
RowSet rs = queryExec.select();

// Force some query execution now.
// If the timeout-first-row goes off, the output stream has not
Expand All @@ -393,23 +393,23 @@ protected QueryExecResult executeQuery(HttpAction action, QueryExec queryExecuti
}

if ( requestQuery.isConstructType() ) {
DatasetGraph dataset = queryExecution.constructDataset();
DatasetGraph dataset = queryExec.constructDataset();
return new QueryExecResult(dataset);
}

if ( requestQuery.isDescribeType() ) {
Graph graph = queryExecution.describe();
Graph graph = queryExec.describe();
return new QueryExecResult(graph);
}

if ( requestQuery.isAskType() ) {
boolean b = queryExecution.ask();
boolean b = queryExec.ask();
return new QueryExecResult(b);
}

if ( requestQuery.isJsonType() ) {
Iterator<JsonObject> jsonIterator = queryExecution.execJsonItems();
//JsonArray jsonArray = queryExecution.execJson();
Iterator<JsonObject> jsonIterator = queryExec.execJsonItems();
//JsonArray jsonArray = queryExec.execJson();
return new QueryExecResult(jsonIterator);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,21 @@

import java.util.function.Consumer;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import org.apache.jena.atlas.lib.StrUtils;
import org.apache.jena.atlas.web.HttpException;
import org.apache.jena.fuseki.test.HttpTest;
import org.apache.jena.graph.Graph;
import org.apache.jena.query.Dataset;
import org.apache.jena.query.DatasetFactory;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.rdfconnection.RDFConnection;
import org.apache.jena.graph.Node;
import org.apache.jena.graph.NodeFactory;
import org.apache.jena.rdflink.RDFLink;
import org.apache.jena.sparql.core.DatasetGraph;
import org.apache.jena.sparql.core.DatasetGraphFactory;
import org.apache.jena.sparql.engine.http.QueryExceptionHTTP;
import org.apache.jena.sparql.sse.SSE;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

/** Tests for .add("/ds", dsg, false) */
public class TestFusekiStdReadOnlySetup {
Expand All @@ -44,22 +43,20 @@ public class TestFusekiStdReadOnlySetup {
private static FusekiServer server = null;
private static int port;

private static Model data;
private static Dataset dataset;
private static Graph data;
private static DatasetGraph dataset;

private static String URL;

@BeforeClass
public static void beforeClass() {
Graph graph = SSE.parseGraph(StrUtils.strjoinNL
data = SSE.parseGraph(StrUtils.strjoinNL
("(graph"
," (:s :p 1)"
,")"));
data = ModelFactory.createModelForGraph(graph);

DatasetGraph dsgData = DatasetGraphFactory.create();
dsgData.add(SSE.parseQuad("(:g :s :p 2 )"));
dataset = DatasetFactory.wrap(dsgData);
dataset = DatasetGraphFactory.create();
dataset.add(SSE.parseQuad("(:g :s :p 2 )"));

DatasetGraph dsg = DatasetGraphFactory.createTxnMem();

Expand Down Expand Up @@ -95,12 +92,12 @@ public void stdSetup_endpoint_3() {

@Test
public void stdSetup_endpoint_4() {
exec(URL, "/get", conn -> conn.fetch());
exec(URL, "/get", conn -> conn.get());
}

@Test
public void stdSetup_endpoint_5() {
exec(URL, "/data", conn -> conn.fetch());
exec(URL, "/data", conn -> conn.get());
}

@Test
Expand Down Expand Up @@ -133,22 +130,23 @@ public void stdSetup_dataset_2() {

@Test
public void stdSetup_dataset_3() {
exec(URL, conn -> conn.fetch());
exec(URL, conn -> conn.get());
}

@Test
public void stdSetup_dataset_4() {
exec(URL, conn -> conn.fetchDataset());
exec(URL, conn -> conn.getDataset());
}

@Test
public void stdSetup_dataset_5() {
HttpTest.expect405(() -> exec(URL, conn -> conn.put("http://example", data)) );
Node gn = NodeFactory.createURI("http://example");
HttpTest.expect405(() -> exec(URL, conn -> conn.put(gn, data)) );
}

@Test
public void stdSetup_dataset_6() {
// Read-only: Bad POST - only queies can be POST'ed => bad media type.
// Read-only: Bad POST - only queries can be POST'ed => bad media type.
HttpTest.expect405(() -> exec(URL, conn -> conn.putDataset(dataset)) );
}

Expand All @@ -170,14 +168,14 @@ public void stdSetup_dataset_6() {
}

@Test public void stdSetup_endpoint_bad_5() {
HttpTest.expect404( () -> exec(URL+"2", "", (RDFConnection conn)->conn.queryAsk("ASK{}")) );
HttpTest.expect404( () -> exec(URL+"2", "", conn->conn.queryAsk("ASK{}")) );
}

private static void exec(String url, Consumer<RDFConnection> action) {
private static void exec(String url, Consumer<RDFLink> action) {
execEx(url, null, action);
}

private static void exec(String url, String ep, Consumer<RDFConnection> action) {
private static void exec(String url, String ep, Consumer<RDFLink> action) {
try {
execEx(url, ep, action);
} catch (HttpException ex) {
Expand All @@ -187,7 +185,7 @@ private static void exec(String url, String ep, Consumer<RDFConnection> action)
}
}

private static void execEx(String url, String ep, Consumer<RDFConnection> action) {
private static void execEx(String url, String ep, Consumer<RDFLink> action) {
String dest;
if ( ep == null || ep.isEmpty() ) {
dest = url;
Expand All @@ -198,7 +196,7 @@ private static void execEx(String url, String ep, Consumer<RDFConnection> action
ep = ep.substring(1);
dest = url+ep;
}
try ( RDFConnection conn = RDFConnection.connect(dest) ) {
try ( RDFLink conn = RDFLink.connect(dest) ) {
action.accept(conn);
}
}
Expand Down
Loading

0 comments on commit cf96347

Please sign in to comment.