Search in sources :

Example 1 with WireMockServer

use of com.github.tomakehurst.wiremock.WireMockServer in project azure-tools-for-java by Microsoft.

the class SparkBatchRemoteDebugJobScenario method mockLivyService.

@Given("^setup a mock livy service for (.+) request '(.+)' to return '(.+)' with status code (\\d+)$")
public void mockLivyService(String action, String serviceUrl, String response, int statusCode) throws Throwable {
    URI mockUri = new URI(serviceUrl);
    int mockedPort = mockUri.getPort() == -1 ? 80 : mockUri.getPort();
    if (httpServerMock == null || httpServerMock.port() != mockedPort) {
        httpServerMock = new WireMockServer(wireMockConfig().bindAddress(mockUri.getHost()).port(mockedPort));
        httpServerMock.start();
    }
    configureFor(mockUri.getHost(), mockedPort);
    stubFor(request(action, urlEqualTo(serviceUrl.substring(mockUri.resolve("/").toString().length() - 1))).willReturn(aResponse().withStatus(statusCode).withBody(response)));
}
Also used : URI(java.net.URI) WireMockServer(com.github.tomakehurst.wiremock.WireMockServer) Given(cucumber.api.java.en.Given)

Example 2 with WireMockServer

use of com.github.tomakehurst.wiremock.WireMockServer in project maven-plugins by apache.

the class PmdReportTest method testFileURL.

public void testFileURL() throws Exception {
    FileUtils.copyDirectoryStructure(new File(getBasedir(), "src/test/resources/unit/default-configuration/jxr-files"), new File(getBasedir(), "target/test/unit/default-configuration/target/site"));
    File testPom = new File(getBasedir(), "src/test/resources/unit/default-configuration/default-configuration-plugin-config.xml");
    PmdReport mojo = (PmdReport) lookupMojo("pmd", testPom);
    // Additional test case for MPMD-174 (https://issues.apache.org/jira/browse/MPMD-174).
    WireMockServer mockServer = new WireMockServer(3456);
    mockServer.start();
    String sonarRuleset = IOUtils.toString(getClass().getClassLoader().getResourceAsStream("unit/default-configuration/rulesets/sonar-way-ruleset.xml"), StandardCharsets.UTF_8);
    String sonarMainPageHtml = IOUtils.toString(getClass().getClassLoader().getResourceAsStream("unit/default-configuration/rulesets/sonar-main-page.html"), StandardCharsets.UTF_8);
    final String sonarBaseUrl = "/profiles";
    final String sonarProfileUrl = sonarBaseUrl + "/export?format=pmd&language=java&name=Sonar%2520way";
    final String sonarExportRulesetUrl = "http://localhost:" + mockServer.port() + sonarProfileUrl;
    mockServer.stubFor(WireMock.get(WireMock.urlEqualTo(sonarBaseUrl)).willReturn(WireMock.aResponse().withStatus(200).withHeader("Content-Type", "text/html").withBody(sonarMainPageHtml)));
    mockServer.stubFor(WireMock.get(WireMock.urlEqualTo(sonarProfileUrl)).willReturn(WireMock.aResponse().withStatus(200).withHeader("Content-Type", "text/xml").withBody(sonarRuleset)));
    URL url = getClass().getClassLoader().getResource("rulesets/java/basic.xml");
    URL url2 = getClass().getClassLoader().getResource("rulesets/java/unusedcode.xml");
    URL url3 = getClass().getClassLoader().getResource("rulesets/java/imports.xml");
    mojo.setRulesets(new String[] { url.toString(), url2.toString(), url3.toString(), sonarExportRulesetUrl });
    mojo.execute();
    // check if the PMD files were generated
    File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/pmd.xml");
    assertTrue(FileUtils.fileExists(generatedFile.getAbsolutePath()));
    generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/basic.xml");
    assertTrue(FileUtils.fileExists(generatedFile.getAbsolutePath()));
    generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/imports.xml");
    assertTrue(FileUtils.fileExists(generatedFile.getAbsolutePath()));
    generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/unusedcode.xml");
    assertTrue(FileUtils.fileExists(generatedFile.getAbsolutePath()));
    generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/export_format_pmd_language_java_name_Sonar_2520way.xml");
    assertTrue(FileUtils.fileExists(generatedFile.getAbsolutePath()));
    generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/site/pmd.html");
    renderer(mojo, generatedFile);
    assertTrue(FileUtils.fileExists(generatedFile.getAbsolutePath()));
    // check if there's a link to the JXR files
    String str = readFile(new File(getBasedir(), "target/test/unit/default-configuration/target/site/pmd.html"));
    assertTrue(str.contains("/xref/def/configuration/App.html#L31"));
    assertTrue(str.contains("/xref/def/configuration/AppSample.html#L45"));
    mockServer.stop();
}
Also used : File(java.io.File) WireMockServer(com.github.tomakehurst.wiremock.WireMockServer) URL(java.net.URL)

Aggregations

WireMockServer (com.github.tomakehurst.wiremock.WireMockServer)2 Given (cucumber.api.java.en.Given)1 File (java.io.File)1 URI (java.net.URI)1 URL (java.net.URL)1