Changeset 18106 in josm for trunk/test/unit/org/openstreetmap/josm/gui
- Timestamp:
- 2021-08-01T21:21:38+02:00 (3 years ago)
- 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 5 5 import static com.github.tomakehurst.wiremock.client.WireMock.get; 6 6 import 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; 7 import static org.junit.jupiter.api.Assertions.assertEquals; 8 import static org.junit.jupiter.api.Assertions.assertNotNull; 9 import static org.junit.jupiter.api.Assertions.assertNull; 11 10 12 11 import java.net.CookieHandler; … … 15 14 import java.util.Collections; 16 15 17 import org.junit. Rule;18 import org.junit. Test;16 import org.junit.jupiter.api.Test; 17 import org.junit.jupiter.api.Timeout; 19 18 import org.openstreetmap.josm.data.oauth.OAuthParameters; 20 19 import org.openstreetmap.josm.data.oauth.OAuthToken; 21 20 import org.openstreetmap.josm.io.OsmTransferCanceledException; 22 import org.openstreetmap.josm.testutils.JOSMTestRules; 21 import org.openstreetmap.josm.testutils.annotations.BasicPreferences; 22 import org.openstreetmap.josm.testutils.annotations.BasicWiremock; 23 import org.openstreetmap.josm.testutils.annotations.HTTP; 23 24 24 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; 25 26 import com.github.tomakehurst.wiremock.junit.WireMockRule; 25 import com.github.tomakehurst.wiremock.WireMockServer; 27 26 28 27 /** 29 28 * Unit tests of {@link OsmOAuthAuthorizationClient} class. 30 29 */ 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 35 class OsmOAuthAuthorizationClientTest { 40 36 /** 41 37 * HTTP mock. 42 38 */ 43 @ Rule44 public WireMockRule wireMockRule = new WireMockRule(options().dynamicPort());39 @BasicWiremock 40 WireMockServer wireMockServer; 45 41 46 42 /** … … 50 46 */ 51 47 @Test 52 publicvoid testOsmOAuthAuthorizationClient() throws OsmTransferCanceledException, OsmOAuthAuthorizationException {48 void testOsmOAuthAuthorizationClient() throws OsmTransferCanceledException, OsmOAuthAuthorizationException { 53 49 // request token 54 wireMock Rule.stubFor(get(urlEqualTo("/oauth/request_token"))50 wireMockServer.stubFor(get(urlEqualTo("/oauth/request_token")) 55 51 .willReturn(aResponse().withStatus(200).withBody(String.join("&", 56 52 "oauth_token=entxUGuwRKV6KyVDF0OWScdGhbqXGMGmosXuiChR", 57 53 "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"))); 59 56 60 57 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"); 63 60 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"); 65 62 66 63 // access token 67 wireMock Rule.stubFor(get(urlEqualTo("/oauth/access_token"))64 wireMockServer.stubFor(get(urlEqualTo("/oauth/access_token")) 68 65 .willReturn(aResponse().withStatus(200).withBody(String.join("&", 69 66 "oauth_token=eGMGmosXuiChRntxUGuwRKV6KyVDF0OWScdGhbqX", … … 71 68 72 69 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"); 75 72 } 76 73 … … 82 79 */ 83 80 @Test 84 publicvoid testCookieHandlingMock() throws Exception {85 wireMock Rule.stubFor(get(urlEqualTo("/login?cookie_test=true"))81 void testCookieHandlingMock() throws Exception { 82 wireMockServer.stubFor(get(urlEqualTo("/login?cookie_test=true")) 86 83 .willReturn(aResponse() 87 84 .withStatus(200) … … 90 87 "name=\"authenticity_token\" " + 91 88 "value=\"fzp6CWJhp6Vns09re3s2Tw==\" />"))); 92 final OAuthParameters parameters = OAuthParameters.createDefault(wireMock Rule.url("/api"));89 final OAuthParameters parameters = OAuthParameters.createDefault(wireMockServer.url("/api")); 93 90 final OsmOAuthAuthorizationClient client = new OsmOAuthAuthorizationClient(parameters); 94 91 final OsmOAuthAuthorizationClient.SessionId sessionId = client.fetchOsmWebsiteSessionId(); 95 92 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"); 99 96 } 100 97 … … 106 103 */ 107 104 @Test 108 publicvoid testCookieHandlingCookieManager() throws Exception {105 void testCookieHandlingCookieManager() throws Exception { 109 106 // emulate Java Web Start behaviour 110 107 // see https://docs.oracle.com/javase/tutorial/deployment/doingMoreWithRIA/accessingCookies.html -
trunk/test/unit/org/openstreetmap/josm/gui/preferences/server/ApiUrlTestTaskTest.java
r17194 r18106 5 5 import static com.github.tomakehurst.wiremock.client.WireMock.get; 6 6 import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; 7 import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;8 7 import static org.hamcrest.CoreMatchers.containsString; 9 8 import static org.hamcrest.MatcherAssert.assertThat; 10 import static org.junit.Assert.assertFalse; 11 import static org.junit.Assert.assertTrue; 9 import static org.junit.jupiter.api.Assertions.assertFalse; 10 import static org.junit.jupiter.api.Assertions.assertThrows; 11 import static org.junit.jupiter.api.Assertions.assertTrue; 12 12 13 import javax.swing.JLabel; 13 14 import java.awt.Component; 14 15 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; 16 import org.junit.jupiter.api.Test; 17 import org.junit.jupiter.api.Timeout; 18 import org.openstreetmap.josm.testutils.annotations.BasicPreferences; 19 import org.openstreetmap.josm.testutils.annotations.BasicWiremock; 20 import org.openstreetmap.josm.testutils.annotations.HTTP; 21 21 import org.openstreetmap.josm.tools.Logging; 22 22 23 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; 24 25 import com.github.tomakehurst.wiremock.junit.WireMockRule; 23 import com.github.tomakehurst.wiremock.WireMockServer; 26 24 27 25 /** 28 26 * Unit tests of {@link ApiUrlTestTask} class. 29 27 */ 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 32 class ApiUrlTestTaskTest { 39 33 /** 40 34 * HTTP mock. 41 35 */ 42 @ Rule43 public WireMockRule wireMockRule = new WireMockRule(options().dynamicPort().usingFilesUnderDirectory(TestUtils.getTestDataRoot()));36 @BasicWiremock 37 WireMockServer wireMockServer; 44 38 45 39 private static final Component PARENT = new JLabel(); … … 48 42 * Unit test of {@link ApiUrlTestTask#ApiUrlTestTask} - null url. 49 43 */ 50 @Test (expected = IllegalArgumentException.class)51 publicvoid testNullApiUrl() {52 new ApiUrlTestTask(PARENT, null);44 @Test 45 void testNullApiUrl() { 46 assertThrows(IllegalArgumentException.class, () -> new ApiUrlTestTask(PARENT, null)); 53 47 } 54 48 … … 57 51 */ 58 52 @Test 59 publicvoid testNominalUrl() {60 ApiUrlTestTask task = new ApiUrlTestTask(PARENT, wireMock Rule.url("/__files/api"));53 void testNominalUrl() { 54 ApiUrlTestTask task = new ApiUrlTestTask(PARENT, wireMockServer.url("/__files/api")); 61 55 task.run(); 62 56 assertTrue(task.isSuccess()); … … 67 61 */ 68 62 @Test 69 publicvoid testAlertInvalidUrl() {63 void testAlertInvalidUrl() { 70 64 Logging.clearLastErrorAndWarnings(); 71 65 ApiUrlTestTask task = new ApiUrlTestTask(PARENT, "malformed url"); … … 80 74 */ 81 75 @Test 82 publicvoid testUnknownHost() {76 void testUnknownHost() { 83 77 Logging.clearLastErrorAndWarnings(); 84 78 ApiUrlTestTask task = new ApiUrlTestTask(PARENT, "http://unknown"); … … 93 87 */ 94 88 @Test 95 publicvoid testAlertInvalidServerResult() {89 void testAlertInvalidServerResult() { 96 90 Logging.clearLastErrorAndWarnings(); 97 wireMock Rule.stubFor(get(urlEqualTo("/does-not-exist/0.6/capabilities"))91 wireMockServer.stubFor(get(urlEqualTo("/does-not-exist/0.6/capabilities")) 98 92 .willReturn(aResponse().withStatus(404))); 99 93 100 ApiUrlTestTask task = new ApiUrlTestTask(PARENT, wireMock Rule.url("/does-not-exist"));94 ApiUrlTestTask task = new ApiUrlTestTask(PARENT, wireMockServer.url("/does-not-exist")); 101 95 task.run(); 102 96 assertFalse(task.isSuccess()); … … 109 103 */ 110 104 @Test 111 publicvoid testAlertInvalidCapabilities() {105 void testAlertInvalidCapabilities() { 112 106 Logging.clearLastErrorAndWarnings(); 113 ApiUrlTestTask task = new ApiUrlTestTask(PARENT, wireMock Rule.url("/__files/invalid_api"));107 ApiUrlTestTask task = new ApiUrlTestTask(PARENT, wireMockServer.url("/__files/invalid_api")); 114 108 task.run(); 115 109 assertFalse(task.isSuccess()); 116 110 assertThat(Logging.getLastErrorAndWarnings().toString(), containsString( 117 111 "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", wireMock Rule.url("/__files/invalid_api"))));112 .replace("XXX", wireMockServer.url("/__files/invalid_api")))); 119 113 } 120 114 }
Note:
See TracChangeset
for help on using the changeset viewer.