Ignore:
Timestamp:
2021-08-01T21:21:38+02:00 (3 years ago)
Author:
Don-vip
Message:

fix #21150 - Add JUnit5 annotation for WireMockServer (patch by taylor.smock)

Location:
trunk/test/unit/org/openstreetmap/josm/gui
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/unit/org/openstreetmap/josm/gui/oauth/OsmOAuthAuthorizationClientTest.java

    r17196 r18106  
    55import static com.github.tomakehurst.wiremock.client.WireMock.get;
    66import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
    7 import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
    8 import static org.junit.Assert.assertEquals;
    9 import static org.junit.Assert.assertNotNull;
    10 import static org.junit.Assert.assertNull;
     7import static org.junit.jupiter.api.Assertions.assertEquals;
     8import static org.junit.jupiter.api.Assertions.assertNotNull;
     9import static org.junit.jupiter.api.Assertions.assertNull;
    1110
    1211import java.net.CookieHandler;
     
    1514import java.util.Collections;
    1615
    17 import org.junit.Rule;
    18 import org.junit.Test;
     16import org.junit.jupiter.api.Test;
     17import org.junit.jupiter.api.Timeout;
    1918import org.openstreetmap.josm.data.oauth.OAuthParameters;
    2019import org.openstreetmap.josm.data.oauth.OAuthToken;
    2120import org.openstreetmap.josm.io.OsmTransferCanceledException;
    22 import org.openstreetmap.josm.testutils.JOSMTestRules;
     21import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
     22import org.openstreetmap.josm.testutils.annotations.BasicWiremock;
     23import org.openstreetmap.josm.testutils.annotations.HTTP;
    2324
    24 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
    25 
    26 import com.github.tomakehurst.wiremock.junit.WireMockRule;
     25import com.github.tomakehurst.wiremock.WireMockServer;
    2726
    2827/**
    2928 * Unit tests of {@link OsmOAuthAuthorizationClient} class.
    3029 */
    31 public class OsmOAuthAuthorizationClientTest {
    32 
    33     /**
    34      * Setup tests
    35      */
    36     @Rule
    37     @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
    38     public JOSMTestRules test = new JOSMTestRules().timeout(20000);
    39 
     30@Timeout(20)
     31@BasicWiremock
     32// Needed for OAuthParameters
     33@BasicPreferences
     34@HTTP
     35class OsmOAuthAuthorizationClientTest {
    4036    /**
    4137     * HTTP mock.
    4238     */
    43     @Rule
    44     public WireMockRule wireMockRule = new WireMockRule(options().dynamicPort());
     39    @BasicWiremock
     40    WireMockServer wireMockServer;
    4541
    4642    /**
     
    5046     */
    5147    @Test
    52     public void testOsmOAuthAuthorizationClient() throws OsmTransferCanceledException, OsmOAuthAuthorizationException {
     48    void testOsmOAuthAuthorizationClient() throws OsmTransferCanceledException, OsmOAuthAuthorizationException {
    5349        // request token
    54         wireMockRule.stubFor(get(urlEqualTo("/oauth/request_token"))
     50        wireMockServer.stubFor(get(urlEqualTo("/oauth/request_token"))
    5551                .willReturn(aResponse().withStatus(200).withBody(String.join("&",
    5652                        "oauth_token=entxUGuwRKV6KyVDF0OWScdGhbqXGMGmosXuiChR",
    5753                        "oauth_token_secret=nsBD2Hr5lLGDUeNoh3SnLaGsUV1TiPYM4qUr7tPB"))));
    58         OsmOAuthAuthorizationClient client = new OsmOAuthAuthorizationClient(OAuthParameters.createDefault(wireMockRule.url("/api")));
     54        OsmOAuthAuthorizationClient client = new OsmOAuthAuthorizationClient(OAuthParameters.createDefault(
     55                wireMockServer.url("/api")));
    5956
    6057        OAuthToken requestToken = client.getRequestToken(null);
    61         assertEquals("requestToken.key", "entxUGuwRKV6KyVDF0OWScdGhbqXGMGmosXuiChR", requestToken.getKey());
    62         assertEquals("requestToken.secret", "nsBD2Hr5lLGDUeNoh3SnLaGsUV1TiPYM4qUr7tPB", requestToken.getSecret());
     58        assertEquals("entxUGuwRKV6KyVDF0OWScdGhbqXGMGmosXuiChR", requestToken.getKey(), "requestToken.key");
     59        assertEquals("nsBD2Hr5lLGDUeNoh3SnLaGsUV1TiPYM4qUr7tPB", requestToken.getSecret(), "requestToken.secret");
    6360        String url = client.getAuthoriseUrl(requestToken);
    64         assertEquals("url", wireMockRule.url("/oauth/authorize?oauth_token=entxUGuwRKV6KyVDF0OWScdGhbqXGMGmosXuiChR"), url);
     61        assertEquals(wireMockServer.url("/oauth/authorize?oauth_token=entxUGuwRKV6KyVDF0OWScdGhbqXGMGmosXuiChR"), url, "url");
    6562
    6663        // access token
    67         wireMockRule.stubFor(get(urlEqualTo("/oauth/access_token"))
     64        wireMockServer.stubFor(get(urlEqualTo("/oauth/access_token"))
    6865                .willReturn(aResponse().withStatus(200).withBody(String.join("&",
    6966                        "oauth_token=eGMGmosXuiChRntxUGuwRKV6KyVDF0OWScdGhbqX",
     
    7168
    7269        OAuthToken accessToken = client.getAccessToken(null);
    73         assertEquals("accessToken.key", "eGMGmosXuiChRntxUGuwRKV6KyVDF0OWScdGhbqX", accessToken.getKey());
    74         assertEquals("accessToken.secret", "nsBUeNor7tPh3SHr5lLaGsGDUD2PYMV1TinL4qUB", accessToken.getSecret());
     70        assertEquals("eGMGmosXuiChRntxUGuwRKV6KyVDF0OWScdGhbqX", accessToken.getKey(), "accessToken.key");
     71        assertEquals("nsBUeNor7tPh3SHr5lLaGsGDUD2PYMV1TinL4qUB", accessToken.getSecret(), "accessToken.secret");
    7572    }
    7673
     
    8279     */
    8380    @Test
    84     public void testCookieHandlingMock() throws Exception {
    85         wireMockRule.stubFor(get(urlEqualTo("/login?cookie_test=true"))
     81    void testCookieHandlingMock() throws Exception {
     82        wireMockServer.stubFor(get(urlEqualTo("/login?cookie_test=true"))
    8683                .willReturn(aResponse()
    8784                        .withStatus(200)
     
    9087                        "name=\"authenticity_token\" " +
    9188                        "value=\"fzp6CWJhp6Vns09re3s2Tw==\" />")));
    92         final OAuthParameters parameters = OAuthParameters.createDefault(wireMockRule.url("/api"));
     89        final OAuthParameters parameters = OAuthParameters.createDefault(wireMockServer.url("/api"));
    9390        final OsmOAuthAuthorizationClient client = new OsmOAuthAuthorizationClient(parameters);
    9491        final OsmOAuthAuthorizationClient.SessionId sessionId = client.fetchOsmWebsiteSessionId();
    9592        assertNotNull(sessionId);
    96         assertEquals("sessionId.id", "7fe8e2ea36c6b803cb902301b28e0a", sessionId.id);
    97         assertEquals("sessionId.token", "fzp6CWJhp6Vns09re3s2Tw==", sessionId.token);
    98         assertNull("sessionId.userName", sessionId.userName);
     93        assertEquals("7fe8e2ea36c6b803cb902301b28e0a", sessionId.id, "sessionId.id");
     94        assertEquals("fzp6CWJhp6Vns09re3s2Tw==", sessionId.token, "sessionId.token");
     95        assertNull(sessionId.userName, "sessionId.userName");
    9996    }
    10097
     
    106103     */
    107104    @Test
    108     public void testCookieHandlingCookieManager() throws Exception {
     105    void testCookieHandlingCookieManager() throws Exception {
    109106        // emulate Java Web Start behaviour
    110107        // see https://docs.oracle.com/javase/tutorial/deployment/doingMoreWithRIA/accessingCookies.html
  • trunk/test/unit/org/openstreetmap/josm/gui/preferences/server/ApiUrlTestTaskTest.java

    r17194 r18106  
    55import static com.github.tomakehurst.wiremock.client.WireMock.get;
    66import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
    7 import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
    87import static org.hamcrest.CoreMatchers.containsString;
    98import static org.hamcrest.MatcherAssert.assertThat;
    10 import static org.junit.Assert.assertFalse;
    11 import static org.junit.Assert.assertTrue;
     9import static org.junit.jupiter.api.Assertions.assertFalse;
     10import static org.junit.jupiter.api.Assertions.assertThrows;
     11import static org.junit.jupiter.api.Assertions.assertTrue;
    1212
     13import javax.swing.JLabel;
    1314import java.awt.Component;
    1415
    15 import javax.swing.JLabel;
    16 
    17 import org.junit.Rule;
    18 import org.junit.Test;
    19 import org.openstreetmap.josm.TestUtils;
    20 import org.openstreetmap.josm.testutils.JOSMTestRules;
     16import org.junit.jupiter.api.Test;
     17import org.junit.jupiter.api.Timeout;
     18import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
     19import org.openstreetmap.josm.testutils.annotations.BasicWiremock;
     20import org.openstreetmap.josm.testutils.annotations.HTTP;
    2121import org.openstreetmap.josm.tools.Logging;
    2222
    23 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
    24 
    25 import com.github.tomakehurst.wiremock.junit.WireMockRule;
     23import com.github.tomakehurst.wiremock.WireMockServer;
    2624
    2725/**
    2826 * Unit tests of {@link ApiUrlTestTask} class.
    2927 */
    30 public class ApiUrlTestTaskTest {
    31 
    32     /**
    33      * Setup tests
    34      */
    35     @Rule
    36     @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
    37     public JOSMTestRules test = new JOSMTestRules().preferences().timeout(30000);
    38 
     28@Timeout(30)
     29@BasicPreferences
     30@BasicWiremock
     31@HTTP
     32class ApiUrlTestTaskTest {
    3933    /**
    4034     * HTTP mock.
    4135     */
    42     @Rule
    43     public WireMockRule wireMockRule = new WireMockRule(options().dynamicPort().usingFilesUnderDirectory(TestUtils.getTestDataRoot()));
     36    @BasicWiremock
     37    WireMockServer wireMockServer;
    4438
    4539    private static final Component PARENT = new JLabel();
     
    4842     * Unit test of {@link ApiUrlTestTask#ApiUrlTestTask} - null url.
    4943     */
    50     @Test(expected = IllegalArgumentException.class)
    51     public void testNullApiUrl() {
    52         new ApiUrlTestTask(PARENT, null);
     44    @Test
     45    void testNullApiUrl() {
     46        assertThrows(IllegalArgumentException.class, () -> new ApiUrlTestTask(PARENT, null));
    5347    }
    5448
     
    5751     */
    5852    @Test
    59     public void testNominalUrl() {
    60         ApiUrlTestTask task = new ApiUrlTestTask(PARENT, wireMockRule.url("/__files/api"));
     53    void testNominalUrl() {
     54        ApiUrlTestTask task = new ApiUrlTestTask(PARENT, wireMockServer.url("/__files/api"));
    6155        task.run();
    6256        assertTrue(task.isSuccess());
     
    6761     */
    6862    @Test
    69     public void testAlertInvalidUrl() {
     63    void testAlertInvalidUrl() {
    7064        Logging.clearLastErrorAndWarnings();
    7165        ApiUrlTestTask task = new ApiUrlTestTask(PARENT, "malformed url");
     
    8074     */
    8175    @Test
    82     public void testUnknownHost() {
     76    void testUnknownHost() {
    8377        Logging.clearLastErrorAndWarnings();
    8478        ApiUrlTestTask task = new ApiUrlTestTask(PARENT, "http://unknown");
     
    9387     */
    9488    @Test
    95     public void testAlertInvalidServerResult() {
     89    void testAlertInvalidServerResult() {
    9690        Logging.clearLastErrorAndWarnings();
    97         wireMockRule.stubFor(get(urlEqualTo("/does-not-exist/0.6/capabilities"))
     91        wireMockServer.stubFor(get(urlEqualTo("/does-not-exist/0.6/capabilities"))
    9892                .willReturn(aResponse().withStatus(404)));
    9993
    100         ApiUrlTestTask task = new ApiUrlTestTask(PARENT, wireMockRule.url("/does-not-exist"));
     94        ApiUrlTestTask task = new ApiUrlTestTask(PARENT, wireMockServer.url("/does-not-exist"));
    10195        task.run();
    10296        assertFalse(task.isSuccess());
     
    109103     */
    110104    @Test
    111     public void testAlertInvalidCapabilities() {
     105    void testAlertInvalidCapabilities() {
    112106        Logging.clearLastErrorAndWarnings();
    113         ApiUrlTestTask task = new ApiUrlTestTask(PARENT, wireMockRule.url("/__files/invalid_api"));
     107        ApiUrlTestTask task = new ApiUrlTestTask(PARENT, wireMockServer.url("/__files/invalid_api"));
    114108        task.run();
    115109        assertFalse(task.isSuccess());
    116110        assertThat(Logging.getLastErrorAndWarnings().toString(), containsString(
    117111                "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"))));
     112                        .replace("XXX", wireMockServer.url("/__files/invalid_api"))));
    119113    }
    120114}
Note: See TracChangeset for help on using the changeset viewer.