Java tutorial
/* * Copyright 2015 DuraSpace, Inc. * * Licensed 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.fcrepo.it; /** * An access exception has occurred. * * @author yinlinchen * @since 2016-04-06 */ import static java.lang.Integer.MAX_VALUE; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.net.URLEncoder; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpRequestBase; import org.apache.http.impl.client.HttpClientBuilder; import org.junit.Assert; import org.junit.Test; public class FusekiIT { public static String FEDORA_CONTEXT = System.getProperty("fcrepo.context"); private static String CARGO_PORT = "8080"; private static final String serverHost = "localhost"; private static HttpClient client = createClient(); private static HttpClient createClient() { return HttpClientBuilder.create().setMaxConnPerRoute(MAX_VALUE).setMaxConnTotal(MAX_VALUE).build(); } private static String getFedoraBaseUrl() { String endpoint = "/rest"; if (FEDORA_CONTEXT == null || FEDORA_CONTEXT.isEmpty()) { endpoint = "rest"; } return "http://" + serverHost + ":" + CARGO_PORT + "/" + FEDORA_CONTEXT + endpoint; } private static String getFusekiBaseUrl() { return "http://localhost:8080/fuseki"; } private static int getStatusCode(final HttpRequestBase request) throws IOException { try { return client.execute(request).getStatusLine().getStatusCode(); } finally { request.releaseConnection(); } } private static String getURIForPid(final String pid) { return getFedoraBaseUrl() + pid; } private static HttpResponse queryFuseki(final String query) throws IOException { final String queryUrl = getFusekiBaseUrl() + "/test/query?query=" + URLEncoder.encode(query, "UTF-8") + "&default-graph-uri=&output=csv&stylesheet="; return client.execute(new HttpGet(queryUrl)); } private static class FusekiResponse { final private List<List<String>> rows; public FusekiResponse(final String csvResponse) { final String[] rowArray = csvResponse.split("\\n"); this.rows = new ArrayList<List<String>>(); for (final String row : rowArray) { final ArrayList<String> rowList = new ArrayList<String>(); for (final String cell : row.split(",")) { rowList.add(cell.trim()); } this.rows.add(rowList); } } public List<String> getValues(final String header) { final List<String> results = new ArrayList<String>(); int columnIndex = -1; if (!rows.isEmpty()) { final List<String> headerRow = rows.get(0); for (int i = 0; i < headerRow.size(); i++) { if (headerRow.get(i).equals(header)) { columnIndex = i; } } if (columnIndex != -1) { for (int rowIndex = 1; rowIndex < rows.size(); rowIndex++) { results.add(rows.get(rowIndex).get(columnIndex)); } } } return results; } public List<List<String>> getRows() { return this.rows; } } @Test public void testFusekiIsRunning() throws IOException { Assert.assertEquals("Fuseki must be running!", 200, getStatusCode(new HttpGet(getFusekiBaseUrl()))); } private static final String pid101 = "/objects/101"; @Test public void testSparqlQueries1() throws IOException, InterruptedException { final String fusekiQuery1b = "prefix dc: <http://purl.org/dc/elements/1.1/>\n" + "select ?object ?title where { ?object dc:title ?title }\n" + "limit 1"; final ByteArrayOutputStream response1b = new ByteArrayOutputStream(); queryFuseki(fusekiQuery1b).getEntity().writeTo(response1b); Assert.assertEquals(Arrays.asList(new String[] { getURIForPid(pid101) }), new FusekiResponse(new String(response1b.toByteArray()).trim()).getValues("object")); } }