You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@jena.apache.org by afs <gi...@git.apache.org> on 2017/05/28 18:50:30 UTC

[GitHub] jena pull request #255: JENA-1350: Use Fuseki embedded server for testing

GitHub user afs opened a pull request:

    https://github.com/apache/jena/pull/255

    JENA-1350: Use Fuseki embedded server for testing

    

You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/afs/jena fuseki-for-testing

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/jena/pull/255.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #255
    
----
commit 92931cfffb9f221f3f7183f666d6b63a2149de6f
Author: Andy Seaborne <an...@apache.org>
Date:   2017-05-28T08:51:38Z

    Update the MIME types for RDF related syntaxes and file extensions.

commit 0a71419eca5e6a1045a811b01b3f0be9a8123e03
Author: Andy Seaborne <an...@apache.org>
Date:   2017-05-28T17:16:19Z

    Update RAT plugin to 0.12

commit a363223b270fb104896e9a2875141dfa12960488
Author: Andy Seaborne <an...@apache.org>
Date:   2017-05-28T17:19:31Z

    JENA-1350: Use embedded Fuseki for testing

----


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] jena pull request #255: JENA-1350: Use Fuseki embedded server for testing

Posted by asfgit <gi...@git.apache.org>.
Github user asfgit closed the pull request at:

    https://github.com/apache/jena/pull/255


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] jena pull request #255: JENA-1350: Use Fuseki embedded server for testing

Posted by afs <gi...@git.apache.org>.
Github user afs commented on a diff in the pull request:

    https://github.com/apache/jena/pull/255#discussion_r118848329
  
    --- Diff: jena-fuseki2/jena-fuseki-embedded/src/test/java/org/apache/jena/fuseki/embedded/FusekiTestAuth.java ---
    @@ -0,0 +1,182 @@
    +/**
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + *     http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.jena.fuseki.embedded;
    +
    +import static org.apache.jena.fuseki.server.FusekiEnv.choosePort;
    +
    +import java.util.Objects;
    +
    +import org.apache.jena.atlas.web.HttpException;
    +import org.apache.jena.fuseki.FusekiException;
    +import org.apache.jena.sparql.core.DatasetGraph ;
    +import org.apache.jena.sparql.core.DatasetGraphFactory;
    +import org.apache.jena.web.HttpSC;
    +import org.eclipse.jetty.security.*;
    +import org.eclipse.jetty.security.authentication.BasicAuthenticator;
    +import org.eclipse.jetty.util.security.Constraint;
    +import org.eclipse.jetty.util.security.Credential;
    +import org.eclipse.jetty.util.security.Password;
    +import org.junit.Assert;
    +
    +/**
    + * Testing HTTP athentication.
    + * <p>
    + * {@code FusekiAuth} provides helper code for before/after (any of suite/class/test).
    + * The pattern of usage is:
    + * <pre>
    + * 
    + * &#64;BeforeClass
    + * public static void beforeClassAuth() {
    + *     SecurityHandler sh = FusekiAuth.makeSimpleSecurityHandler("/*", "USER", "PASSWORD");
    + *     FusekiAuth.setupServer(true, sh);
    + * }
    + * 
    + * &#64;AfterClass
    + * public static void afterClassAuth() {
    + *     FusekiAuth.teardownServer();
    + *     // Clear up any pooled connections.
    + *     HttpOp.setDefaultHttpClient(HttpOp.createPoolingHttpClient());
    + * }
    + * 
    + * &#64;Test
    + * public void myAuthTest() {
    + *     BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
    + *     Credentials credentials = new UsernamePasswordCredentials("USER", "PASSWORD");
    + *     credsProvider.setCredentials(AuthScope.ANY, credentials);
    + *     HttpClient client = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
    + *     try (TypedInputStream in = HttpOp.execHttpGet(ServerCtl.urlDataset(), "* /*", client, null)) {}
    + * }
    + * 
    + * &#64;Test
    + * public void myAuthTestRejected() {
    + *     BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
    + *     Credentials credentials = new UsernamePasswordCredentials("USER", "PASSWORD");
    + *     credsProvider.setCredentials(AuthScope.ANY, credentials);
    + *     HttpClient client = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
    + *     try (TypedInputStream in = HttpOp.execHttpGet(ServerCtl.urlDataset(), "* /*", client, null)) {}
    + *     catch (HttpException ex) {
    + *         throw assertAuthHttpException(ex);
    + *     }
    + * }
    + * </pre>
    + * 
    + * {@code @BeforeClass} can be {@code @Before} but server stop-start is expensive so a
    + * large test suite may end up quite slow.
    + */
    +public class FusekiTestAuth {
    +    private static int currentPort = choosePort() ;
    +    
    +    public static int port() {
    +        return currentPort ;
    +    }
    +    
    +    static boolean CLEAR_DSG_DIRECTLY = true ;
    +    static private DatasetGraph dsgTesting ;
    +    
    +    // Abstraction that runs a SPARQL server for tests.
    +    public static final String urlRoot()            { return "http://localhost:"+port()+"/" ; }
    +    public static final String datasetPath()        { return "/dataset" ; }
    +    public static final String urlDataset()         { return "http://localhost:"+port()+datasetPath() ; }
    +    public static final DatasetGraph getDataset()   { return dsgTesting ; }
    +    
    +    public static final String serviceUpdate()      { return "http://localhost:"+port()+datasetPath()+"/update" ; } 
    +    public static final String serviceQuery()       { return "http://localhost:"+port()+datasetPath()+"/query" ; }
    +    public static final String serviceGSP()         { return "http://localhost:"+port()+datasetPath()+"/data" ; }
    +    
    +    private static FusekiEmbeddedServer server ;
    +    
    +    public static void setupServer(boolean updateable, SecurityHandler sh) {
    +        // Clean datasets.
    +        dsgTesting = DatasetGraphFactory.createTxnMem();
    +        server = FusekiEmbeddedServer.create()
    +            .add(datasetPath(), dsgTesting)
    --- End diff --
    
    Speed but also for clear-up.  No files get left on disk to interfere with other tests; especially if the server takes awhile to stop properly and release all its resources.
    
    We could add `void setupServer(boolean updateable, DatasetGraph dsg, SecurityHandler sh)` for the general case.
    
    `FusekiTestServer` is also a in-memory transactional dataset and there it is much more difficult to inject a specific implementation because the lifecycle is driven by JUnit.
    
    The example on the JIRA shows it is not necessary to use these helper classes.  They are helpful when a lot of small tests are run, ensuring a balance between careful clean-up and speed.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] jena pull request #255: JENA-1350: Use Fuseki embedded server for testing

Posted by ajs6f <gi...@git.apache.org>.
Github user ajs6f commented on a diff in the pull request:

    https://github.com/apache/jena/pull/255#discussion_r118848106
  
    --- Diff: jena-fuseki2/jena-fuseki-embedded/src/test/java/org/apache/jena/fuseki/embedded/FusekiTestAuth.java ---
    @@ -0,0 +1,182 @@
    +/**
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + *     http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.jena.fuseki.embedded;
    +
    +import static org.apache.jena.fuseki.server.FusekiEnv.choosePort;
    +
    +import java.util.Objects;
    +
    +import org.apache.jena.atlas.web.HttpException;
    +import org.apache.jena.fuseki.FusekiException;
    +import org.apache.jena.sparql.core.DatasetGraph ;
    +import org.apache.jena.sparql.core.DatasetGraphFactory;
    +import org.apache.jena.web.HttpSC;
    +import org.eclipse.jetty.security.*;
    +import org.eclipse.jetty.security.authentication.BasicAuthenticator;
    +import org.eclipse.jetty.util.security.Constraint;
    +import org.eclipse.jetty.util.security.Credential;
    +import org.eclipse.jetty.util.security.Password;
    +import org.junit.Assert;
    +
    +/**
    + * Testing HTTP athentication.
    + * <p>
    + * {@code FusekiAuth} provides helper code for before/after (any of suite/class/test).
    + * The pattern of usage is:
    + * <pre>
    + * 
    + * &#64;BeforeClass
    + * public static void beforeClassAuth() {
    + *     SecurityHandler sh = FusekiAuth.makeSimpleSecurityHandler("/*", "USER", "PASSWORD");
    + *     FusekiAuth.setupServer(true, sh);
    + * }
    + * 
    + * &#64;AfterClass
    + * public static void afterClassAuth() {
    + *     FusekiAuth.teardownServer();
    + *     // Clear up any pooled connections.
    + *     HttpOp.setDefaultHttpClient(HttpOp.createPoolingHttpClient());
    + * }
    + * 
    + * &#64;Test
    + * public void myAuthTest() {
    + *     BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
    + *     Credentials credentials = new UsernamePasswordCredentials("USER", "PASSWORD");
    + *     credsProvider.setCredentials(AuthScope.ANY, credentials);
    + *     HttpClient client = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
    + *     try (TypedInputStream in = HttpOp.execHttpGet(ServerCtl.urlDataset(), "* /*", client, null)) {}
    + * }
    + * 
    + * &#64;Test
    + * public void myAuthTestRejected() {
    + *     BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
    + *     Credentials credentials = new UsernamePasswordCredentials("USER", "PASSWORD");
    + *     credsProvider.setCredentials(AuthScope.ANY, credentials);
    + *     HttpClient client = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
    + *     try (TypedInputStream in = HttpOp.execHttpGet(ServerCtl.urlDataset(), "* /*", client, null)) {}
    + *     catch (HttpException ex) {
    + *         throw assertAuthHttpException(ex);
    + *     }
    + * }
    + * </pre>
    + * 
    + * {@code @BeforeClass} can be {@code @Before} but server stop-start is expensive so a
    + * large test suite may end up quite slow.
    + */
    +public class FusekiTestAuth {
    +    private static int currentPort = choosePort() ;
    +    
    +    public static int port() {
    +        return currentPort ;
    +    }
    +    
    +    static boolean CLEAR_DSG_DIRECTLY = true ;
    +    static private DatasetGraph dsgTesting ;
    +    
    +    // Abstraction that runs a SPARQL server for tests.
    +    public static final String urlRoot()            { return "http://localhost:"+port()+"/" ; }
    +    public static final String datasetPath()        { return "/dataset" ; }
    +    public static final String urlDataset()         { return "http://localhost:"+port()+datasetPath() ; }
    +    public static final DatasetGraph getDataset()   { return dsgTesting ; }
    +    
    +    public static final String serviceUpdate()      { return "http://localhost:"+port()+datasetPath()+"/update" ; } 
    +    public static final String serviceQuery()       { return "http://localhost:"+port()+datasetPath()+"/query" ; }
    +    public static final String serviceGSP()         { return "http://localhost:"+port()+datasetPath()+"/data" ; }
    +    
    +    private static FusekiEmbeddedServer server ;
    +    
    +    public static void setupServer(boolean updateable, SecurityHandler sh) {
    +        // Clean datasets.
    +        dsgTesting = DatasetGraphFactory.createTxnMem();
    +        server = FusekiEmbeddedServer.create()
    +            .add(datasetPath(), dsgTesting)
    --- End diff --
    
    Looks like all of the test server instances will be in-memory datasets, which is great for speed. Will there be any places where the test server instances touch the disk?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---