Changeset 17075 in josm for trunk


Ignore:
Timestamp:
2020-09-29T23:10:10+02:00 (4 years ago)
Author:
Don-vip
Message:

see #15102 - see #16637 - get rid of real HTTP calls to http://httpstat.us in unit tests, mock them

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/unit/org/openstreetmap/josm/data/cache/JCSCachedTileLoaderJobTest.java

    r16913 r17075  
    22package org.openstreetmap.josm.data.cache;
    33
     4import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
     5import static com.github.tomakehurst.wiremock.client.WireMock.anyUrl;
     6import static com.github.tomakehurst.wiremock.client.WireMock.get;
     7import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor;
     8import static com.github.tomakehurst.wiremock.client.WireMock.head;
     9import static com.github.tomakehurst.wiremock.client.WireMock.headRequestedFor;
     10import static com.github.tomakehurst.wiremock.client.WireMock.status;
     11import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
    412import static org.junit.Assert.assertArrayEquals;
    513import static org.junit.Assert.assertEquals;
     
    2432import org.openstreetmap.josm.tools.Logging;
    2533
    26 import com.github.tomakehurst.wiremock.client.WireMock;
    2734import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
    2835import com.github.tomakehurst.wiremock.junit.WireMockRule;
     
    120127    public void testStatusCodes() throws IOException, InterruptedException {
    121128        doTestStatusCode(200);
    122         // can't test for 3xx, as httpstat.us redirects finally to 200 page
    123129        doTestStatusCode(401);
    124130        doTestStatusCode(402);
     
    156162
    157163    private void doTestStatusCode(int responseCode) throws IOException {
     164        tileServer.stubFor(get(urlEqualTo("/httpstat/" + responseCode)).willReturn(aResponse().withStatus(responseCode)));
    158165        TestCachedTileLoaderJob job = getStatusLoaderJob(responseCode);
    159166        Listener listener = submitJob(job);
     
    194201                createEntryAttributes(expires, 200, testStart, "eTag")
    195202                );
    196         createHeadGetStub(WireMock.urlEqualTo("/test"), expires, testStart, "eTag", "mock entry");
     203        createHeadGetStub(urlEqualTo("/test"), expires, testStart, "eTag", "mock entry");
    197204
    198205        TestCachedTileLoaderJob job = new TestCachedTileLoaderJob(tileServer.url("/test"), "test");
    199206        Listener listener = submitJob(job, false);
    200         tileServer.verify(0, WireMock.getRequestedFor(WireMock.anyUrl()));
     207        tileServer.verify(0, getRequestedFor(anyUrl()));
    201208        assertArrayEquals("cached entry".getBytes(StandardCharsets.UTF_8), listener.data);
    202209    }
     
    215222                createEntryAttributes(expires, 200, testStart + expires, "eTag")
    216223                );
    217         createHeadGetStub(WireMock.urlEqualTo("/test"), expires, testStart, "eTag", "mock entry");
     224        createHeadGetStub(urlEqualTo("/test"), expires, testStart, "eTag", "mock entry");
    218225
    219226        TestCachedTileLoaderJob job = new TestCachedTileLoaderJob(tileServer.url("/test"), "test");
    220227        Listener listener = submitJob(job, true);
    221         tileServer.verify(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/test")));
     228        tileServer.verify(1, getRequestedFor(urlEqualTo("/test")));
    222229        assertArrayEquals("mock entry".getBytes(StandardCharsets.UTF_8), listener.data);
    223230    }
     
    231238    public void testSettingMinimumExpiryWhenNoExpires() throws IOException {
    232239        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")));
    239241
    240242        TestCachedTileLoaderJob job = new TestCachedTileLoaderJob(tileServer.url("/test"), "test");
    241243        Listener listener = submitJob(job, false);
    242         tileServer.verify(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/test")));
     244        tileServer.verify(1, getRequestedFor(urlEqualTo("/test")));
    243245
    244246        assertTrue("Cache entry expiration is " + (listener.attributes.getExpirationTime() - testStart) + " which is not larger than " +
     
    265267        long testStart = System.currentTimeMillis();
    266268        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()
    270271                        .withHeader("Cache-control", "max-age=" + expires)
    271272                        .withBody("mock entry")
     
    275276        TestCachedTileLoaderJob job = new TestCachedTileLoaderJob(tileServer.url("/test"), "test");
    276277        Listener listener = submitJob(job, false);
    277         tileServer.verify(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/test")));
     278        tileServer.verify(1, getRequestedFor(urlEqualTo("/test")));
    278279
    279280        assertTrue("Cache entry expiration is " + (listener.attributes.getExpirationTime() - testStart) + " which is not larger than " +
     
    301302        int minimumExpiryTimeSeconds = (int) (JCSCachedTileLoaderJob.DEFAULT_EXPIRE_TIME / 2);
    302303
    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");
    304305
    305306        TestCachedTileLoaderJob job = new TestCachedTileLoaderJob(tileServer.url("/test"), "test", minimumExpiryTimeSeconds);
    306307        Listener listener = submitJob(job, false);
    307         tileServer.verify(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/test")));
     308        tileServer.verify(1, getRequestedFor(urlEqualTo("/test")));
    308309        assertArrayEquals("mock entry".getBytes(StandardCharsets.UTF_8), listener.data);
    309310
     
    332333        int minimumExpiryTimeSeconds = (int) (JCSCachedTileLoaderJob.DEFAULT_EXPIRE_TIME * 2);
    333334
    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");
    335336
    336337        TestCachedTileLoaderJob job = new TestCachedTileLoaderJob(tileServer.url("/test"), "test", minimumExpiryTimeSeconds);
    337338        Listener listener = submitJob(job, false);
    338         tileServer.verify(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/test")));
     339        tileServer.verify(1, getRequestedFor(urlEqualTo("/test")));
    339340        assertArrayEquals("mock entry".getBytes(StandardCharsets.UTF_8), listener.data);
    340341
     
    368369        int minimumExpiryTimeSeconds = 0;
    369370
    370         tileServer.stubFor(
    371                 WireMock.get(WireMock.urlEqualTo("/test"))
    372                 .willReturn(WireMock.aResponse()
     371        tileServer.stubFor(get(urlEqualTo("/test"))
     372                .willReturn(aResponse()
    373373                        .withHeader("Expires", TestUtils.getHTTPDate(testStart + (JCSCachedTileLoaderJob.DEFAULT_EXPIRE_TIME / 10)))
    374374                        .withHeader("Cache-Control", "max-age=" +
     
    377377                        )
    378378                );
    379         tileServer.stubFor(
    380                 WireMock.head(WireMock.urlEqualTo("/test"))
    381                 .willReturn(WireMock.aResponse()
     379        tileServer.stubFor(head(urlEqualTo("/test"))
     380                .willReturn(aResponse()
    382381                        .withHeader("Expires", TestUtils.getHTTPDate(testStart + (JCSCachedTileLoaderJob.DEFAULT_EXPIRE_TIME / 10)))
    383382                        .withHeader("Cache-Control", "max-age=" +
     
    387386        TestCachedTileLoaderJob job = new TestCachedTileLoaderJob(tileServer.url("/test"), "test", minimumExpiryTimeSeconds);
    388387        Listener listener = submitJob(job, false);
    389         tileServer.verify(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/test")));
     388        tileServer.verify(1, getRequestedFor(urlEqualTo("/test")));
    390389        assertArrayEquals("mock entry".getBytes(StandardCharsets.UTF_8), listener.data);
    391390
     
    410409     * @throws IOException exception
    411410     */
    412 
    413411    @Test
    414412    public void testMaxAgeVsSMaxAge() throws IOException {
     
    416414        int minimumExpiryTimeSeconds = 0;
    417415
    418 
    419         tileServer.stubFor(
    420                 WireMock.get(WireMock.urlEqualTo("/test"))
    421                 .willReturn(WireMock.aResponse()
     416        tileServer.stubFor(get(urlEqualTo("/test"))
     417                .willReturn(aResponse()
    422418                        .withHeader("Cache-Control", "" +
    423419                                "max-age=" + TimeUnit.MILLISECONDS.toSeconds((JCSCachedTileLoaderJob.DEFAULT_EXPIRE_TIME / 10)) + "," +
     
    427423                        )
    428424                );
    429         tileServer.stubFor(
    430                 WireMock.head(WireMock.urlEqualTo("/test"))
    431                 .willReturn(WireMock.aResponse()
     425        tileServer.stubFor(head(urlEqualTo("/test"))
     426                .willReturn(aResponse()
    432427                        .withHeader("Cache-Control", "" +
    433428                                "max-age=" + TimeUnit.MILLISECONDS.toSeconds((JCSCachedTileLoaderJob.DEFAULT_EXPIRE_TIME / 10)) + "," +
     
    437432        TestCachedTileLoaderJob job = new TestCachedTileLoaderJob(tileServer.url("/test"), "test", minimumExpiryTimeSeconds);
    438433        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);
    442436
    443437        assertTrue("Cache entry expiration is " + (listener.attributes.getExpirationTime() - testStart) + " which is not larger than " +
     
    453447    }
    454448
    455 
    456449    /**
    457450     * Check if verifying cache entries using HEAD requests work properly
     
    468461                );
    469462
    470         tileServer.stubFor(
    471                 WireMock.get(WireMock.urlEqualTo("/test"))
    472                 .willReturn(WireMock.aResponse()
     463        tileServer.stubFor(get(urlEqualTo("/test"))
     464                .willReturn(aResponse()
    473465                        .withHeader("Expires", TestUtils.getHTTPDate(testStart + expires))
    474466                        .withHeader("Last-Modified", Long.toString(testStart))
     
    477469                        )
    478470                );
    479         tileServer.stubFor(
    480                 WireMock.head(WireMock.urlEqualTo("/test"))
    481                 .willReturn(WireMock.aResponse()
     471        tileServer.stubFor(head(urlEqualTo("/test"))
     472                .willReturn(aResponse()
    482473                        .withHeader("Expires", TestUtils.getHTTPDate(testStart + expires))
    483474                        .withHeader("Last-Modified", Long.toString(testStart))
     
    488479        TestCachedTileLoaderJob job = new TestCachedTileLoaderJob(tileServer.url("/test"), "test");
    489480        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")));
    491482        assertArrayEquals("mock entry".getBytes(StandardCharsets.UTF_8), listener.data);
    492483
    493484        // cache entry should be retrieved from cache
    494485        listener = submitJob(job, false);
    495         tileServer.verify(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/test")));
     486        tileServer.verify(1, getRequestedFor(urlEqualTo("/test")));
    496487        assertArrayEquals("mock entry".getBytes(StandardCharsets.UTF_8), listener.data);
    497488
     
    503494
    504495        // 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 now
     496        tileServer.verify(0, headRequestedFor(urlEqualTo("/test"))); // no head requests were made until now
    506497        listener = submitJob(job, false);
    507         tileServer.verify(1, WireMock.headRequestedFor(WireMock.urlEqualTo("/test"))); // verify head requests were made
    508         tileServer.verify(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/test"))); // verify no more get requests were made
     498        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
    509500        assertArrayEquals("mock entry".getBytes(StandardCharsets.UTF_8), listener.data);
    510501        assertTrue(listener.attributes.getExpirationTime() >= testStart + expires);
     
    512503        // cache entry should be retrieved from cache
    513504        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")));
    516507        assertArrayEquals("mock entry".getBytes(StandardCharsets.UTF_8), listener.data);
    517508    }
     
    531522                );
    532523
    533         tileServer.stubFor(
    534                 WireMock.get(WireMock.urlEqualTo("/test"))
    535                 .willReturn(WireMock.status(304)
     524        tileServer.stubFor(get(urlEqualTo("/test"))
     525                .willReturn(status(304)
    536526                        .withHeader("Expires", TestUtils.getHTTPDate(testStart + expires))
    537527                        .withHeader("Last-Modified", Long.toString(testStart))
     
    542532        TestCachedTileLoaderJob job = new TestCachedTileLoaderJob(tileServer.url("/test"), "test");
    543533        Listener listener = submitJob(job, false);
    544         tileServer.verify(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/test")));
     534        tileServer.verify(1, getRequestedFor(urlEqualTo("/test")));
    545535        assertArrayEquals("cached dummy".getBytes(StandardCharsets.UTF_8), listener.data);
    546536        assertTrue(testStart + expires <= listener.attributes.getExpirationTime());
    547537        submitJob(job, false);
    548         tileServer.verify(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/test"))); // no more requests were made
     538        tileServer.verify(1, getRequestedFor(urlEqualTo("/test"))); // no more requests were made
    549539    }
    550540
    551541    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()
    555544                        .withHeader("Expires", TestUtils.getHTTPDate(lastModified + expires))
    556545                        .withHeader("Last-Modified", Long.toString(lastModified))
     
    559548                        )
    560549                );
    561         tileServer.stubFor(
    562                 WireMock.head(url)
    563                 .willReturn(WireMock.aResponse()
     550        tileServer.stubFor(head(url)
     551                .willReturn(aResponse()
    564552                        .withHeader("Expires", TestUtils.getHTTPDate(lastModified + expires))
    565553                        .withHeader("Last-Modified", Long.toString(lastModified))
     
    567555                        )
    568556                );
    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);
    574557    }
    575558
     
    583566    }
    584567
    585     private static TestCachedTileLoaderJob 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);
    587570    }
    588571
Note: See TracChangeset for help on using the changeset viewer.