Changeset 17192 in josm for trunk/test/unit/org


Ignore:
Timestamp:
2020-10-13T21:36:06+02:00 (4 years ago)
Author:
simon04
Message:

see #15102 - see #16637 - get rid of real HTTP calls to https://api.openstreetmap.org in ApiUrlTestTaskTest, mock them

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/unit/org/openstreetmap/josm/gui/preferences/server/ApiUrlTestTaskTest.java

    r14119 r17192  
    22package org.openstreetmap.josm.gui.preferences.server;
    33
     4import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
     5import static com.github.tomakehurst.wiremock.client.WireMock.get;
     6import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
     7import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
     8import static org.hamcrest.CoreMatchers.containsString;
     9import static org.hamcrest.MatcherAssert.assertThat;
    410import static org.junit.Assert.assertFalse;
    511import static org.junit.Assert.assertTrue;
     
    1117import org.junit.Rule;
    1218import org.junit.Test;
    13 import org.openstreetmap.josm.spi.preferences.Config;
     19import org.openstreetmap.josm.TestUtils;
    1420import org.openstreetmap.josm.testutils.JOSMTestRules;
     21import org.openstreetmap.josm.tools.Logging;
    1522
    1623import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
     24
     25import com.github.tomakehurst.wiremock.junit.WireMockRule;
    1726
    1827/**
     
    2736    @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
    2837    public JOSMTestRules test = new JOSMTestRules().preferences().timeout(30000);
     38
     39    /**
     40     * HTTP mock.
     41     */
     42    @Rule
     43    public WireMockRule wireMockRule = new WireMockRule(options().dynamicPort().usingFilesUnderDirectory(TestUtils.getTestDataRoot()));
    2944
    3045    private static final Component PARENT = new JLabel();
     
    4358    @Test
    4459    public void testNominalUrl() {
    45         ApiUrlTestTask task = new ApiUrlTestTask(PARENT, Config.getUrls().getDefaultOsmApiUrl());
     60        ApiUrlTestTask task = new ApiUrlTestTask(PARENT, wireMockRule.url("/__files/osm_api"));
    4661        task.run();
    4762        assertTrue(task.isSuccess());
     
    5368    @Test
    5469    public void testAlertInvalidUrl() {
     70        Logging.clearLastErrorAndWarnings();
    5571        ApiUrlTestTask task = new ApiUrlTestTask(PARENT, "malformed url");
    5672        task.run();
    5773        assertFalse(task.isSuccess());
     74        assertThat(Logging.getLastErrorAndWarnings().toString(), containsString(
     75                "<html>'malformed url' is not a valid OSM API URL.<br>Please check the spelling and validate again.</html>"));
    5876    }
    5977
     
    6381    @Test
    6482    public void testUnknownHost() {
     83        Logging.clearLastErrorAndWarnings();
    6584        ApiUrlTestTask task = new ApiUrlTestTask(PARENT, "http://unknown");
    6685        task.run();
    6786        assertFalse(task.isSuccess());
     87        assertThat(Logging.getLastErrorAndWarnings().toString(), containsString(
     88                "java.net.UnknownHostException: unknown"));
    6889    }
    6990
     
    7394    @Test
    7495    public void testAlertInvalidServerResult() {
    75         ApiUrlTestTask task = new ApiUrlTestTask(PARENT, "http://www.openstreetmap.org");
     96        Logging.clearLastErrorAndWarnings();
     97        wireMockRule.stubFor(get(urlEqualTo("/does-not-exist/0.6/capabilities"))
     98                .willReturn(aResponse().withStatus(404)));
     99
     100        ApiUrlTestTask task = new ApiUrlTestTask(PARENT, wireMockRule.url("/does-not-exist"));
    76101        task.run();
    77102        assertFalse(task.isSuccess());
     103        assertThat(Logging.getLastErrorAndWarnings().toString(), containsString(
     104                "The server responded with the return code 404 instead of 200."));
    78105    }
    79106
     
    83110    @Test
    84111    public void testAlertInvalidCapabilities() {
    85         ApiUrlTestTask task = new ApiUrlTestTask(PARENT, "https://josm.openstreetmap.de/export/10979/josm/trunk/test/data/invalid_api");
     112        Logging.clearLastErrorAndWarnings();
     113        ApiUrlTestTask task = new ApiUrlTestTask(PARENT, wireMockRule.url("/__files/invalid_api"));
    86114        task.run();
    87115        assertFalse(task.isSuccess());
     116        assertThat(Logging.getLastErrorAndWarnings().toString(), containsString(
     117                "The OSM API server at 'XXX' did not return a valid response.<br>It is likely that 'XXX' is not an OSM API server."
     118                        .replace("XXX", wireMockRule.url("/__files/invalid_api"))));
    88119    }
    89120}
Note: See TracChangeset for help on using the changeset viewer.