All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.inrupt.client.test.RdfMockService Maven / Gradle / Ivy

The newest version!
/*
 * Copyright Inrupt Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
 * Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package com.inrupt.client.test;

import static com.github.tomakehurst.wiremock.client.WireMock.*;

import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;

import java.util.Collections;
import java.util.Map;

/**
 * A {@link WireMockServer} based HTTP service used for testing RDF services.
 */
public class RdfMockService {

    private final WireMockServer wireMockServer;

    public RdfMockService() {
        wireMockServer = new WireMockServer(WireMockConfiguration.options()
                .dynamicPort());
    }

    public int getPort() {
        return wireMockServer.port();
    }

    private void setupMocks() {

        wireMockServer.stubFor(get(urlEqualTo("/oneTriple"))
                    .willReturn(aResponse()
                        .withStatus(200)
                        .withHeader("Content-Type", "text/turtle")
                        .withBody("   .")));

        wireMockServer.stubFor(post(urlEqualTo("/postOneTriple"))
                    .withRequestBody(matching(
                            ".*\\s+" +
                            "\\s+\"object\"\\s+\\..*"))
                    .withHeader("Content-Type", containing("text/turtle"))
                    .willReturn(aResponse()
                        .withStatus(204)));

        wireMockServer.stubFor(get(urlEqualTo("/example"))
                    .willReturn(aResponse()
                        .withStatus(200)
                        .withHeader("Content-Type", "text/turtle")
                        .withBody(getExampleTTL())));

        wireMockServer.stubFor(patch(urlEqualTo("/sparqlUpdate"))
                    .withHeader("Content-Type", containing("application/sparql-update"))
                    .withRequestBody(matching(
                            "INSERT DATA\\s+\\{\\s*\\s+" +
                            "\\s+\\s*\\.\\s*\\}\\s*"))
                    .willReturn(aResponse()
                        .withStatus(204)));
    }

    private String getExampleTTL() {
        return "" +
            "@prefix : ." +
            "\n @prefix foaf: ." +
            "\n @prefix schema: ." +
            "\n @prefix solid: ." +
            "\n @prefix space: ." +
            "\n" +
            "\n :me" +
            "\n    a schema:Person, foaf:Person;" +
            "\n    space:preferencesFile ;" +
            "\n    solid:privateTypeIndex ;" +
            "\n    solid:publicTypeIndex ;" +
            "\n    foaf:name \"Jane Doe\";" +
            "\n    solid:oidcIssuer .";
    }

    public Map start() {
        wireMockServer.start();

        setupMocks();

        return Collections.singletonMap("rdf_uri", wireMockServer.baseUrl());
    }

    public void stop() {
        wireMockServer.stop();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy