[ 
https://issues.apache.org/jira/browse/JENA-1350?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16027943#comment-16027943
 ] 

ASF GitHub Bot commented on JENA-1350:
--------------------------------------

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.


> Embedded Fuseki for testing usage
> ---------------------------------
>
>                 Key: JENA-1350
>                 URL: https://issues.apache.org/jira/browse/JENA-1350
>             Project: Apache Jena
>          Issue Type: Improvement
>            Reporter: Andy Seaborne
>            Assignee: Andy Seaborne
>
> Provide {{ServerCtl}} (see the full server and Fuseki1) for test cycle 
> support.
> Enable the possibility of security in embedded Fuseki for testing purposes.



--
This message was sent by Atlassian JIRA
(v6.3.15#6346)

Reply via email to