Changeset 17075 in josm for trunk/test/unit/org
- Timestamp:
- 2020-09-29T23:10:10+02:00 (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/test/unit/org/openstreetmap/josm/data/cache/JCSCachedTileLoaderJobTest.java
r16913 r17075 2 2 package org.openstreetmap.josm.data.cache; 3 3 4 import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; 5 import static com.github.tomakehurst.wiremock.client.WireMock.anyUrl; 6 import static com.github.tomakehurst.wiremock.client.WireMock.get; 7 import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor; 8 import static com.github.tomakehurst.wiremock.client.WireMock.head; 9 import static com.github.tomakehurst.wiremock.client.WireMock.headRequestedFor; 10 import static com.github.tomakehurst.wiremock.client.WireMock.status; 11 import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; 4 12 import static org.junit.Assert.assertArrayEquals; 5 13 import static org.junit.Assert.assertEquals; … … 24 32 import org.openstreetmap.josm.tools.Logging; 25 33 26 import com.github.tomakehurst.wiremock.client.WireMock;27 34 import com.github.tomakehurst.wiremock.core.WireMockConfiguration; 28 35 import com.github.tomakehurst.wiremock.junit.WireMockRule; … … 120 127 public void testStatusCodes() throws IOException, InterruptedException { 121 128 doTestStatusCode(200); 122 // can't test for 3xx, as httpstat.us redirects finally to 200 page123 129 doTestStatusCode(401); 124 130 doTestStatusCode(402); … … 156 162 157 163 private void doTestStatusCode(int responseCode) throws IOException { 164 tileServer.stubFor(get(urlEqualTo("/httpstat/" + responseCode)).willReturn(aResponse().withStatus(responseCode))); 158 165 TestCachedTileLoaderJob job = getStatusLoaderJob(responseCode); 159 166 Listener listener = submitJob(job); … … 194 201 createEntryAttributes(expires, 200, testStart, "eTag") 195 202 ); 196 createHeadGetStub( WireMock.urlEqualTo("/test"), expires, testStart, "eTag", "mock entry");203 createHeadGetStub(urlEqualTo("/test"), expires, testStart, "eTag", "mock entry"); 197 204 198 205 TestCachedTileLoaderJob job = new TestCachedTileLoaderJob(tileServer.url("/test"), "test"); 199 206 Listener listener = submitJob(job, false); 200 tileServer.verify(0, WireMock.getRequestedFor(WireMock.anyUrl()));207 tileServer.verify(0, getRequestedFor(anyUrl())); 201 208 assertArrayEquals("cached entry".getBytes(StandardCharsets.UTF_8), listener.data); 202 209 } … … 215 222 createEntryAttributes(expires, 200, testStart + expires, "eTag") 216 223 ); 217 createHeadGetStub( WireMock.urlEqualTo("/test"), expires, testStart, "eTag", "mock entry");224 createHeadGetStub(urlEqualTo("/test"), expires, testStart, "eTag", "mock entry"); 218 225 219 226 TestCachedTileLoaderJob job = new TestCachedTileLoaderJob(tileServer.url("/test"), "test"); 220 227 Listener listener = submitJob(job, true); 221 tileServer.verify(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/test")));228 tileServer.verify(1, getRequestedFor(urlEqualTo("/test"))); 222 229 assertArrayEquals("mock entry".getBytes(StandardCharsets.UTF_8), listener.data); 223 230 } … … 231 238 public void testSettingMinimumExpiryWhenNoExpires() throws IOException { 232 239 long testStart = System.currentTimeMillis(); 233 tileServer.stubFor( 234 WireMock.get(WireMock.urlEqualTo("/test")) 235 .willReturn(WireMock.aResponse() 236 .withBody("mock entry") 237 ) 238 ); 240 tileServer.stubFor(get(urlEqualTo("/test")).willReturn(aResponse().withBody("mock entry"))); 239 241 240 242 TestCachedTileLoaderJob job = new TestCachedTileLoaderJob(tileServer.url("/test"), "test"); 241 243 Listener listener = submitJob(job, false); 242 tileServer.verify(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/test")));244 tileServer.verify(1, getRequestedFor(urlEqualTo("/test"))); 243 245 244 246 assertTrue("Cache entry expiration is " + (listener.attributes.getExpirationTime() - testStart) + " which is not larger than " + … … 265 267 long testStart = System.currentTimeMillis(); 266 268 long expires = TimeUnit.DAYS.toSeconds(1); 267 tileServer.stubFor( 268 WireMock.get(WireMock.urlEqualTo("/test")) 269 .willReturn(WireMock.aResponse() 269 tileServer.stubFor(get(urlEqualTo("/test")) 270 .willReturn(aResponse() 270 271 .withHeader("Cache-control", "max-age=" + expires) 271 272 .withBody("mock entry") … … 275 276 TestCachedTileLoaderJob job = new TestCachedTileLoaderJob(tileServer.url("/test"), "test"); 276 277 Listener listener = submitJob(job, false); 277 tileServer.verify(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/test")));278 tileServer.verify(1, getRequestedFor(urlEqualTo("/test"))); 278 279 279 280 assertTrue("Cache entry expiration is " + (listener.attributes.getExpirationTime() - testStart) + " which is not larger than " + … … 301 302 int minimumExpiryTimeSeconds = (int) (JCSCachedTileLoaderJob.DEFAULT_EXPIRE_TIME / 2); 302 303 303 createHeadGetStub( WireMock.urlEqualTo("/test"), (JCSCachedTileLoaderJob.DEFAULT_EXPIRE_TIME / 10), testStart, "eTag", "mock entry");304 createHeadGetStub(urlEqualTo("/test"), (JCSCachedTileLoaderJob.DEFAULT_EXPIRE_TIME / 10), testStart, "eTag", "mock entry"); 304 305 305 306 TestCachedTileLoaderJob job = new TestCachedTileLoaderJob(tileServer.url("/test"), "test", minimumExpiryTimeSeconds); 306 307 Listener listener = submitJob(job, false); 307 tileServer.verify(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/test")));308 tileServer.verify(1, getRequestedFor(urlEqualTo("/test"))); 308 309 assertArrayEquals("mock entry".getBytes(StandardCharsets.UTF_8), listener.data); 309 310 … … 332 333 int minimumExpiryTimeSeconds = (int) (JCSCachedTileLoaderJob.DEFAULT_EXPIRE_TIME * 2); 333 334 334 createHeadGetStub( WireMock.urlEqualTo("/test"), (JCSCachedTileLoaderJob.DEFAULT_EXPIRE_TIME / 10), testStart, "eTag", "mock entry");335 createHeadGetStub(urlEqualTo("/test"), (JCSCachedTileLoaderJob.DEFAULT_EXPIRE_TIME / 10), testStart, "eTag", "mock entry"); 335 336 336 337 TestCachedTileLoaderJob job = new TestCachedTileLoaderJob(tileServer.url("/test"), "test", minimumExpiryTimeSeconds); 337 338 Listener listener = submitJob(job, false); 338 tileServer.verify(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/test")));339 tileServer.verify(1, getRequestedFor(urlEqualTo("/test"))); 339 340 assertArrayEquals("mock entry".getBytes(StandardCharsets.UTF_8), listener.data); 340 341 … … 368 369 int minimumExpiryTimeSeconds = 0; 369 370 370 tileServer.stubFor( 371 WireMock.get(WireMock.urlEqualTo("/test")) 372 .willReturn(WireMock.aResponse() 371 tileServer.stubFor(get(urlEqualTo("/test")) 372 .willReturn(aResponse() 373 373 .withHeader("Expires", TestUtils.getHTTPDate(testStart + (JCSCachedTileLoaderJob.DEFAULT_EXPIRE_TIME / 10))) 374 374 .withHeader("Cache-Control", "max-age=" + … … 377 377 ) 378 378 ); 379 tileServer.stubFor( 380 WireMock.head(WireMock.urlEqualTo("/test")) 381 .willReturn(WireMock.aResponse() 379 tileServer.stubFor(head(urlEqualTo("/test")) 380 .willReturn(aResponse() 382 381 .withHeader("Expires", TestUtils.getHTTPDate(testStart + (JCSCachedTileLoaderJob.DEFAULT_EXPIRE_TIME / 10))) 383 382 .withHeader("Cache-Control", "max-age=" + … … 387 386 TestCachedTileLoaderJob job = new TestCachedTileLoaderJob(tileServer.url("/test"), "test", minimumExpiryTimeSeconds); 388 387 Listener listener = submitJob(job, false); 389 tileServer.verify(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/test")));388 tileServer.verify(1, getRequestedFor(urlEqualTo("/test"))); 390 389 assertArrayEquals("mock entry".getBytes(StandardCharsets.UTF_8), listener.data); 391 390 … … 410 409 * @throws IOException exception 411 410 */ 412 413 411 @Test 414 412 public void testMaxAgeVsSMaxAge() throws IOException { … … 416 414 int minimumExpiryTimeSeconds = 0; 417 415 418 419 tileServer.stubFor( 420 WireMock.get(WireMock.urlEqualTo("/test")) 421 .willReturn(WireMock.aResponse() 416 tileServer.stubFor(get(urlEqualTo("/test")) 417 .willReturn(aResponse() 422 418 .withHeader("Cache-Control", "" + 423 419 "max-age=" + TimeUnit.MILLISECONDS.toSeconds((JCSCachedTileLoaderJob.DEFAULT_EXPIRE_TIME / 10)) + "," + … … 427 423 ) 428 424 ); 429 tileServer.stubFor( 430 WireMock.head(WireMock.urlEqualTo("/test")) 431 .willReturn(WireMock.aResponse() 425 tileServer.stubFor(head(urlEqualTo("/test")) 426 .willReturn(aResponse() 432 427 .withHeader("Cache-Control", "" + 433 428 "max-age=" + TimeUnit.MILLISECONDS.toSeconds((JCSCachedTileLoaderJob.DEFAULT_EXPIRE_TIME / 10)) + "," + … … 437 432 TestCachedTileLoaderJob job = new TestCachedTileLoaderJob(tileServer.url("/test"), "test", minimumExpiryTimeSeconds); 438 433 Listener listener = submitJob(job, false); 439 tileServer.verify(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/test"))); 440 assertArrayEquals("mock entry".getBytes(StandardCharsets.UTF_8), listener.data); 441 434 tileServer.verify(1, getRequestedFor(urlEqualTo("/test"))); 435 assertArrayEquals("mock entry".getBytes(StandardCharsets.UTF_8), listener.data); 442 436 443 437 assertTrue("Cache entry expiration is " + (listener.attributes.getExpirationTime() - testStart) + " which is not larger than " + … … 453 447 } 454 448 455 456 449 /** 457 450 * Check if verifying cache entries using HEAD requests work properly … … 468 461 ); 469 462 470 tileServer.stubFor( 471 WireMock.get(WireMock.urlEqualTo("/test")) 472 .willReturn(WireMock.aResponse() 463 tileServer.stubFor(get(urlEqualTo("/test")) 464 .willReturn(aResponse() 473 465 .withHeader("Expires", TestUtils.getHTTPDate(testStart + expires)) 474 466 .withHeader("Last-Modified", Long.toString(testStart)) … … 477 469 ) 478 470 ); 479 tileServer.stubFor( 480 WireMock.head(WireMock.urlEqualTo("/test")) 481 .willReturn(WireMock.aResponse() 471 tileServer.stubFor(head(urlEqualTo("/test")) 472 .willReturn(aResponse() 482 473 .withHeader("Expires", TestUtils.getHTTPDate(testStart + expires)) 483 474 .withHeader("Last-Modified", Long.toString(testStart)) … … 488 479 TestCachedTileLoaderJob job = new TestCachedTileLoaderJob(tileServer.url("/test"), "test"); 489 480 Listener listener = submitJob(job, false); // cache entry is expired, no need to force refetch 490 tileServer.verify(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/test")));481 tileServer.verify(1, getRequestedFor(urlEqualTo("/test"))); 491 482 assertArrayEquals("mock entry".getBytes(StandardCharsets.UTF_8), listener.data); 492 483 493 484 // cache entry should be retrieved from cache 494 485 listener = submitJob(job, false); 495 tileServer.verify(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/test")));486 tileServer.verify(1, getRequestedFor(urlEqualTo("/test"))); 496 487 assertArrayEquals("mock entry".getBytes(StandardCharsets.UTF_8), listener.data); 497 488 … … 503 494 504 495 // because cache entry is invalid - HEAD request shall be made 505 tileServer.verify(0, WireMock.headRequestedFor(WireMock.urlEqualTo("/test"))); // no head requests were made until now496 tileServer.verify(0, headRequestedFor(urlEqualTo("/test"))); // no head requests were made until now 506 497 listener = submitJob(job, false); 507 tileServer.verify(1, WireMock.headRequestedFor(WireMock.urlEqualTo("/test"))); // verify head requests were made508 tileServer.verify(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/test"))); // verify no more get requests were made498 tileServer.verify(1, headRequestedFor(urlEqualTo("/test"))); // verify head requests were made 499 tileServer.verify(1, getRequestedFor(urlEqualTo("/test"))); // verify no more get requests were made 509 500 assertArrayEquals("mock entry".getBytes(StandardCharsets.UTF_8), listener.data); 510 501 assertTrue(listener.attributes.getExpirationTime() >= testStart + expires); … … 512 503 // cache entry should be retrieved from cache 513 504 listener = submitJob(job, false); // cache entry is expired, no need to force refetch 514 tileServer.verify(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/test")));515 tileServer.verify(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/test")));505 tileServer.verify(1, getRequestedFor(urlEqualTo("/test"))); 506 tileServer.verify(1, getRequestedFor(urlEqualTo("/test"))); 516 507 assertArrayEquals("mock entry".getBytes(StandardCharsets.UTF_8), listener.data); 517 508 } … … 531 522 ); 532 523 533 tileServer.stubFor( 534 WireMock.get(WireMock.urlEqualTo("/test")) 535 .willReturn(WireMock.status(304) 524 tileServer.stubFor(get(urlEqualTo("/test")) 525 .willReturn(status(304) 536 526 .withHeader("Expires", TestUtils.getHTTPDate(testStart + expires)) 537 527 .withHeader("Last-Modified", Long.toString(testStart)) … … 542 532 TestCachedTileLoaderJob job = new TestCachedTileLoaderJob(tileServer.url("/test"), "test"); 543 533 Listener listener = submitJob(job, false); 544 tileServer.verify(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/test")));534 tileServer.verify(1, getRequestedFor(urlEqualTo("/test"))); 545 535 assertArrayEquals("cached dummy".getBytes(StandardCharsets.UTF_8), listener.data); 546 536 assertTrue(testStart + expires <= listener.attributes.getExpirationTime()); 547 537 submitJob(job, false); 548 tileServer.verify(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/test"))); // no more requests were made538 tileServer.verify(1, getRequestedFor(urlEqualTo("/test"))); // no more requests were made 549 539 } 550 540 551 541 private void createHeadGetStub(UrlPattern url, long expires, long lastModified, String eTag, String body) { 552 tileServer.stubFor( 553 WireMock.get(url) 554 .willReturn(WireMock.aResponse() 542 tileServer.stubFor(get(url) 543 .willReturn(aResponse() 555 544 .withHeader("Expires", TestUtils.getHTTPDate(lastModified + expires)) 556 545 .withHeader("Last-Modified", Long.toString(lastModified)) … … 559 548 ) 560 549 ); 561 tileServer.stubFor( 562 WireMock.head(url) 563 .willReturn(WireMock.aResponse() 550 tileServer.stubFor(head(url) 551 .willReturn(aResponse() 564 552 .withHeader("Expires", TestUtils.getHTTPDate(lastModified + expires)) 565 553 .withHeader("Last-Modified", Long.toString(lastModified)) … … 567 555 ) 568 556 ); 569 }570 571 private CacheEntryAttributes createEntryAttributes(long maxAge, int responseCode, String eTag) {572 long validTo = maxAge + System.currentTimeMillis();573 return createEntryAttributes(maxAge, responseCode, validTo, eTag);574 557 } 575 558 … … 583 566 } 584 567 585 private staticTestCachedTileLoaderJob getStatusLoaderJob(int responseCode) {586 return new TestCachedTileLoaderJob( "http://httpstat.us/" + responseCode, "key_" + responseCode);568 private TestCachedTileLoaderJob getStatusLoaderJob(int responseCode) { 569 return new TestCachedTileLoaderJob(tileServer.url("/httpstat/" + responseCode), "key_" + responseCode); 587 570 } 588 571
Note:
See TracChangeset
for help on using the changeset viewer.