Skip to content

Commit

Permalink
fix: make client spec tests predictably ordered
Browse files Browse the repository at this point in the history
  • Loading branch information
sighphyre committed Jan 16, 2025
1 parent 5de3208 commit d3ed2fc
Showing 1 changed file with 3 additions and 58 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
package io.getunleash.integration;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.post;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import com.github.tomakehurst.wiremock.junit5.WireMockExtension;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import io.getunleash.DefaultUnleash;
Expand All @@ -21,8 +13,6 @@
import io.getunleash.strategy.constraints.DateParser;
import io.getunleash.util.UnleashConfig;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
Expand All @@ -35,17 +25,9 @@
import java.util.stream.Stream;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.TestFactory;
import org.junit.jupiter.api.extension.RegisterExtension;

public class ClientSpecificationTest {

@RegisterExtension
static WireMockExtension serverMock =
WireMockExtension.newInstance()
.configureStaticDsl(true)
.options(wireMockConfig().dynamicPort().dynamicHttpsPort())
.build();

@TestFactory
public Stream<DynamicTest> clientSpecification() throws IOException, URISyntaxException {
Reader content = getFileReader("/client-specification/specifications/index.json");
Expand Down Expand Up @@ -122,20 +104,14 @@ private List<DynamicTest> createVariantTests(String fileName)
}

private Unleash setupUnleash(TestDefinition testDefinition) throws URISyntaxException {
mockUnleashAPI(testDefinition);

// Required because the client is available before it may have had the chance to
// talk with
// the API
String backupFile = writeUnleashBackup(testDefinition);

// Set-up a unleash instance, using mocked API and backup-file
UnleashConfig config =
UnleashConfig.builder()
.appName(testDefinition.getName())
.unleashAPI(new URI("http://localhost:" + serverMock.getPort() + "/api/"))
.synchronousFetchOnInitialisation(true)
.backupFile(backupFile)
.disableMetrics()
.disablePolling()
.unleashAPI(new URI("http://notusedbutrequired:9999/api/"))
.build();

DefaultUnleash defaultUnleash = new DefaultUnleash(config);
Expand All @@ -144,18 +120,6 @@ private Unleash setupUnleash(TestDefinition testDefinition) throws URISyntaxExce
return defaultUnleash;
}

private void mockUnleashAPI(TestDefinition definition) {
stubFor(
get(urlEqualTo("/api/client/features"))
.withHeader("Accept", equalTo("application/json"))
.willReturn(
aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
.withBody(definition.getState().toString())));
stubFor(post(urlEqualTo("/api/client/register")).willReturn(aResponse().withStatus(200)));
}

private TestDefinition getTestDefinition(String fileName) throws IOException {
Reader content = getFileReader("/client-specification/specifications/" + fileName);
return new Gson().fromJson(content, TestDefinition.class);
Expand Down Expand Up @@ -190,23 +154,4 @@ private Reader getFileReader(String filename) throws IOException {
InputStreamReader reader = new InputStreamReader(in);
return new BufferedReader(reader);
}

private String writeUnleashBackup(TestDefinition definition) {
String backupFile =
System.getProperty("java.io.tmpdir")
+ File.separatorChar
+ "unleash-test-"
+ definition.getName()
+ ".json";

// TODO: we can probably drop this after introduction of
// `synchronousFetchOnInitialisation`.
try (FileWriter writer = new FileWriter(backupFile)) {
writer.write(definition.getState().toString());
} catch (IOException e) {
System.out.println("Unable to write toggles to file");
}

return backupFile;
}
}

0 comments on commit d3ed2fc

Please sign in to comment.