/**
 * 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;

import java.io.IOException ;
import java.util.Objects ;

import org.apache.http.HttpResponse ;
import org.apache.http.StatusLine ;
import org.apache.http.client.HttpClient ;
import org.apache.http.client.methods.HttpOptions ;
import org.apache.http.client.methods.HttpUriRequest ;
import org.apache.http.impl.client.SystemDefaultHttpClient ;
import org.apache.http.protocol.HttpContext ;
import org.apache.http.util.EntityUtils ;
import org.apache.jena.atlas.web.HttpException ;
import org.apache.jena.riot.web.HttpNames ;
import org.apache.jena.riot.web.HttpResponseHandler ;
import org.apache.jena.riot.web.HttpResponseLib ;
import org.apache.jena.web.HttpSC ;
import org.junit.Assert ;

public class FusekiTest {

    /** Check whether str is a comma separated list of expected (unordered) */
    public static void assertStringList(String str, String... expected) {
        str = str.replace(" ", "") ;
        String[] x = str.split(",") ;
        int count = 0 ;
        for ( String ex : expected ) {
            Assert.assertTrue("Got: "+str+" - Does not contain "+ex, containsStr(ex, x)) ;
        }
        for ( String s : x ) {
            Assert.assertTrue("Got: "+str+" - Not expected "+s, containsStr(s, expected)) ;
        }
    }

    /** Is v in the list of strings? */
    public static boolean containsStr(String v, String[] strings) {
        for ( String s: strings ) {
            if ( Objects.equals(v, s)) 
                return true ;
        }
        return false ;
    }

    /** Do an HTTP Options. */
    public static String execOptions(String url) {
        try {
            // Prepare and execute
            HttpResponseHandler handler = HttpResponseLib.nullResponse ;
            HttpClient httpClient = new SystemDefaultHttpClient() ;
            HttpUriRequest request = new HttpOptions(url) ;
            HttpResponse response = httpClient.execute(request, (HttpContext)null);
            
            // Response
            StatusLine statusLine = response.getStatusLine();
            int statusCode = statusLine.getStatusCode();
            if (HttpSC.isClientError(statusCode) || HttpSC.isServerError(statusCode)) {
                // Error responses can have bodies so it is important to clear up.
                String contentPayload = "" ;
                if ( response.getEntity() != null )
                    contentPayload = EntityUtils.toString(response.getEntity()) ;
                throw new HttpException(statusLine.getStatusCode(), statusLine.getReasonPhrase(), contentPayload);
            }
            if (handler != null)
                handler.handle(url, response);
            return response.getFirstHeader(HttpNames.hAllow).getValue() ;
        } catch (IOException ex) {
            throw new HttpException(ex);
        }
    }
    
    public static void exec404(Runnable action) {
        execWithHttpException(HttpSC.NOT_FOUND_404, action) ;
    }
    
    public static void execWithHttpException(int statusCode, Runnable action) { 
        try {
            action.run();
            Assert.fail("Expected HttpException") ;
        } catch (HttpException ex) {
            // -1 : any status code in HttpException 
            if ( statusCode > 0 )
                Assert.assertEquals(statusCode, ex.getResponseCode()) ;
                
        }
    }
}


@@ -969,51 +969,8 @@
 e ;%0A
-import org.apache.http.client.HttpClient ;%0A
 impo
@@ -1107,29 +1107,73 @@
 .client.
-SystemDefault
+CloseableHttpClient ;%0Aimport org.apache.http.impl.client.
 HttpClie
@@ -1166,32 +1166,39 @@
 lient.HttpClient
+Builder
  ;%0Aimport org.ap
@@ -2427,34 +2427,16 @@
  url) %7B%0A
-        try %7B%0A    
         
@@ -2458,36 +2458,32 @@
 execute%0A        
-   
- 
 HttpResponseHand
@@ -2527,24 +2527,25 @@
 ponse ;%0A
+%0A
         
     Http
@@ -2532,28 +2532,39 @@
  ;%0A%0A        
-    
+try ( Closeable
 HttpClient h
@@ -2579,39 +2579,46 @@
 t = 
-new SystemDefaultHttpClient() ;
+HttpClientBuilder.create().build() ) %7B
 %0A   
@@ -2758,28 +2758,16 @@
 )null);%0A
-            
 %0A       
@@ -3561,32 +3561,33 @@
 OException ex) %7B
+ 
 %0A            thr

Switch ot using http client 4.5 preferred builder.