From f4093f8fa2273c7995a3081b131975e21bdab4dd Mon Sep 17 00:00:00 2001 From: Taylor Smock Date: Tue, 28 Nov 2023 11:55:03 -0700 Subject: [PATCH] Fix BlacklistUtilsTest Signed-off-by: Taylor Smock --- .../mapwithai/tools/BlacklistUtilsTest.java | 81 ++++++++++--------- 1 file changed, 44 insertions(+), 37 deletions(-) diff --git a/src/test/unit/org/openstreetmap/josm/plugins/mapwithai/tools/BlacklistUtilsTest.java b/src/test/unit/org/openstreetmap/josm/plugins/mapwithai/tools/BlacklistUtilsTest.java index 92c57ebc..c05de838 100644 --- a/src/test/unit/org/openstreetmap/josm/plugins/mapwithai/tools/BlacklistUtilsTest.java +++ b/src/test/unit/org/openstreetmap/josm/plugins/mapwithai/tools/BlacklistUtilsTest.java @@ -6,19 +6,24 @@ import static com.github.tomakehurst.wiremock.client.WireMock.noContent; import static com.github.tomakehurst.wiremock.client.WireMock.notFound; import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching; -import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; +import java.io.File; +import java.io.IOException; + import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.openstreetmap.josm.TestUtils; import org.openstreetmap.josm.plugins.mapwithai.MapWithAIPlugin; import org.openstreetmap.josm.plugins.mapwithai.testutils.MapWithAIPluginMock; +import org.openstreetmap.josm.spi.preferences.Config; -import com.github.tomakehurst.wiremock.WireMockServer; +import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo; +import com.github.tomakehurst.wiremock.junit5.WireMockTest; /** * Tests for {@link BlacklistUtils} @@ -26,85 +31,87 @@ * @author Taylor Smock * */ +@WireMockTest class BlacklistUtilsTest { - private static WireMockServer wireMock; + private static String blacklistUrl; @BeforeAll - static void setup() { + static void setup(WireMockRuntimeInfo wireMockRuntimeInfo) { TestUtils.assumeWorkingJMockit(); - wireMock = new WireMockServer(options().dynamicPort()); - wireMock.start(); - BlacklistUtils.setBlacklistUrl(wireMock.baseUrl() + "/MapWithAI/json/blacklisted_versions.json"); + blacklistUrl = wireMockRuntimeInfo.getHttpBaseUrl() + "/MapWithAI/json/blacklisted_versions.json"; + BlacklistUtils.setBlacklistUrl(blacklistUrl); new MapWithAIPluginMock(); } @BeforeEach - void clear() { - wireMock.resetMappings(); + @AfterEach + void cleanup(WireMockRuntimeInfo wireMockRuntimeInfo) throws IOException { + final var file = new File(Config.getDirs().getCacheDirectory(false), + "mirror_http___localhost_" + wireMockRuntimeInfo.getHttpPort() + "_MapWithAI_json_blacklisted_versions.json"); + // Ensure that we aren't reading a cached response from a different test + if (file.exists()) { + assertTrue(file.delete()); + } } @AfterAll static void tearDown() { BlacklistUtils.setBlacklistUrl(BlacklistUtils.DEFAULT_BLACKLIST_URL); - assertTrue(wireMock.findAllUnmatchedRequests().isEmpty()); - wireMock.stop(); + blacklistUrl = null; } @Test - void testArrayBad() { - wireMock.addStubMapping(get(urlMatching("/MapWithAI/json/blacklisted_versions.json")) - .willReturn(aResponse().withStatus(200).withBody("[\"" + MapWithAIPlugin.getVersionInfo() + "\"]")) - .build()); + void testArrayBad(WireMockRuntimeInfo wireMockRuntimeInfo) { + wireMockRuntimeInfo.getWireMock().register(get(urlMatching("/MapWithAI/json/blacklisted_versions.json")) + .willReturn(aResponse().withStatus(200).withBody("[\"" + MapWithAIPlugin.getVersionInfo() + "\"]"))); assertTrue(BlacklistUtils.isBlacklisted()); } @Test - void testArrayGood() { - wireMock.addStubMapping(get(urlMatching("/MapWithAI/json/blacklisted_versions.json")) - .willReturn(aResponse().withStatus(200).withBody("[null, 0, false]")).build()); + void testArrayGood(WireMockRuntimeInfo wireMockRuntimeInfo) { + wireMockRuntimeInfo.getWireMock().register(get(urlMatching("/MapWithAI/json/blacklisted_versions.json")) + .willReturn(aResponse().withStatus(200).withBody("[null, 0, false]"))); assertFalse(BlacklistUtils.isBlacklisted()); } @Test - void testObjectBad() { - wireMock.addStubMapping(get(urlMatching("/MapWithAI/json/blacklisted_versions.json")).willReturn(aResponse() - .withStatus(200).withBody("{ \"" + MapWithAIPlugin.getVersionInfo() + "\": \"reason here\"}")).build()); + void testObjectBad(WireMockRuntimeInfo wireMockRuntimeInfo) { + wireMockRuntimeInfo.getWireMock().register(get(urlMatching("/MapWithAI/json/blacklisted_versions.json")) + .willReturn(aResponse().withStatus(200) + .withBody("{ \"" + MapWithAIPlugin.getVersionInfo() + "\": \"reason here\"}"))); assertTrue(BlacklistUtils.isBlacklisted()); } @Test - void testObjectGood() { - wireMock.addStubMapping(get(urlMatching("/MapWithAI/json/blacklisted_versions.json")) - .willReturn(aResponse().withStatus(200).withBody("{ \"version\": \"reason here\"}")).build()); + void testObjectGood(WireMockRuntimeInfo wireMockRuntimeInfo) { + wireMockRuntimeInfo.getWireMock().register(get(urlMatching("/MapWithAI/json/blacklisted_versions.json")) + .willReturn(aResponse().withStatus(200).withBody("{ \"version\": \"reason here\"}"))); assertFalse(BlacklistUtils.isBlacklisted()); } @Test - void testNullJson() { - wireMock.addStubMapping(get(urlMatching("/MapWithAI/json/blacklisted_versions.json")) - .willReturn(aResponse().withStatus(200).withBody("null")).build()); + void testNullJson(WireMockRuntimeInfo wireMockRuntimeInfo) { + wireMockRuntimeInfo.getWireMock().register(get(urlMatching("/MapWithAI/json/blacklisted_versions.json")) + .willReturn(aResponse().withStatus(200).withBody("null"))); assertTrue(BlacklistUtils.isBlacklisted()); } @Test - void testBrokenJson() { - wireMock.addStubMapping(get(urlMatching("/MapWithAI/json/blacklisted_versions.json")).willReturn( - aResponse().withStatus(200).withBody("{ \"" + MapWithAIPlugin.getVersionInfo() + "\": \"reason here\"")) - .build()); + void testBrokenJson(WireMockRuntimeInfo wireMockRuntimeInfo) { + wireMockRuntimeInfo.getWireMock().register(get(urlMatching("/MapWithAI/json/blacklisted_versions.json")).willReturn( + aResponse().withStatus(200).withBody("{ \"" + MapWithAIPlugin.getVersionInfo() + "\": \"reason here\""))); assertTrue(BlacklistUtils.isBlacklisted()); } @Test - void testNoResponse() { - wireMock.addStubMapping( - get(urlMatching("/MapWithAI/json/blacklisted_versions.json")).willReturn(noContent()).build()); + void testNoResponse(WireMockRuntimeInfo wireMockRuntimeInfo) { + wireMockRuntimeInfo.getWireMock().register(get(urlMatching("/MapWithAI/json/blacklisted_versions.json")).willReturn(noContent())); assertTrue(BlacklistUtils.isBlacklisted()); } @Test - void testNotFound() { - wireMock.addStubMapping( - get(urlMatching("/MapWithAI/json/blacklisted_versions.json")).willReturn(notFound()).build()); + void testNotFound(WireMockRuntimeInfo wireMockRuntimeInfo) { + wireMockRuntimeInfo.getWireMock().register(get(urlMatching("/MapWithAI/json/blacklisted_versions.json")).willReturn(notFound())); assertTrue(BlacklistUtils.isBlacklisted()); } }