Skip to content

Commit

Permalink
Fix BlacklistUtilsTest
Browse files Browse the repository at this point in the history
Signed-off-by: Taylor Smock <[email protected]>
  • Loading branch information
tsmock committed Nov 28, 2023
1 parent 06b8548 commit f4093f8
Showing 1 changed file with 44 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,105 +6,112 @@
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}
*
* @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());
}
}

0 comments on commit f4093f8

Please sign in to comment.