Skip to content

Commit

Permalink
Add support for capture methods to set path (relative to project root…
Browse files Browse the repository at this point in the history
…) for exporting simulation
  • Loading branch information
tommysitu committed May 1, 2019
1 parent fd0eb6b commit 7a1cbd3
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 17 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ target/

# Test resources which shouldn't be part of SCM
src/test/resources/hoverfly/recorded-simulation.json
src/test/resources/hoverfly/first-multi-capture**
src/test/resources/hoverfly/third-multi-capture/
io_specto_hoverfly_junit5_HoverflyDefaultCaptureTest_NestedTest.json
*scenario.json
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version=0.11.6-SNAPSHOT
version=0.12.0-SNAPSHOT
title=Hoverfly Java
description=Hoverfly for Java. Capture and simulate HTTP(S) services in JUnit tests.
hoverfly_binary_version=v1.0.0
52 changes: 45 additions & 7 deletions src/main/java/io/specto/hoverfly/junit/rule/HoverflyRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import io.specto.hoverfly.junit.dsl.RequestMatcherBuilder;
import io.specto.hoverfly.junit.dsl.StubServiceBuilder;
import io.specto.hoverfly.junit.verification.VerificationCriteria;
import org.apache.commons.lang3.StringUtils;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule;
Expand All @@ -29,10 +30,12 @@

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.function.Predicate;

import static io.specto.hoverfly.junit.core.HoverflyConfig.localConfigs;
import static io.specto.hoverfly.junit.core.HoverflyConstants.DEFAULT_HOVERFLY_EXPORT_PATH;
import static io.specto.hoverfly.junit.core.HoverflyMode.*;
import static io.specto.hoverfly.junit.core.SimulationSource.file;
import static io.specto.hoverfly.junit.rule.HoverflyRuleUtils.*;
Expand Down Expand Up @@ -134,7 +137,7 @@ public static HoverflyRule inCaptureMode(HoverflyConfig hoverflyConfig) {

/**
* Instantiates a rule which runs {@link Hoverfly} in capture mode
* @param outputFilename the path to the recorded name relative to src/test/resources/hoverfly
* @param outputFilename the output simulation file name relative to src/test/resources/hoverfly
* @return the rule
*/
public static HoverflyRule inCaptureMode(String outputFilename) {
Expand All @@ -143,13 +146,37 @@ public static HoverflyRule inCaptureMode(String outputFilename) {

/**
* Instantiates a rule which runs {@link Hoverfly} in capture mode
* @param outputFilename the path to the recorded name relative to src/test/resources/hoverfly
* @param outputFilename the output simulation file name relative to src/test/resources/hoverfly
* @param hoverflyConfig the config
* @return the rule
*/
public static HoverflyRule inCaptureMode(String outputFilename, HoverflyConfig hoverflyConfig) {
createTestResourcesHoverflyDirectoryIfNoneExisting();
return new HoverflyRule(fileRelativeToTestResourcesHoverfly(outputFilename), hoverflyConfig);
return inCaptureMode(DEFAULT_HOVERFLY_EXPORT_PATH, outputFilename, hoverflyConfig);
}

/**
* Instantiates a rule which runs {@link Hoverfly} in capture mode
* @param outputDir the directory path relative to your project root for exporting the simulation file
* @param outputFilename the output simulation file name
* @return the rule
*/
public static HoverflyRule inCaptureMode(String outputDir, String outputFilename) {
return inCaptureMode(outputDir, outputFilename, localConfigs());
}

/**
* Instantiates a rule which runs {@link Hoverfly} in capture mode
* @param outputDir the directory path relative to your project root for exporting the simulation file
* @param outputFilename the output simulation file name
* @param hoverflyConfig the config
* @return the rule
*/
public static HoverflyRule inCaptureMode(String outputDir, String outputFilename, HoverflyConfig hoverflyConfig) {
if (StringUtils.isBlank(outputFilename)) {
throw new IllegalArgumentException("Output simulation file name can not be blank.");
}
Path exportPath = createDirectoryIfNotExist(outputDir);
return new HoverflyRule(exportPath.resolve(outputFilename), hoverflyConfig);
}


Expand Down Expand Up @@ -332,17 +359,28 @@ public void simulate(SimulationSource simulationSource, SimulationSource... sour
/**
* Stores what's currently been captured in the currently assigned file, reset simulations and journal logs, then starts capture again
* ready to store in the new file once complete.
* @param recordFile the path where captured or simulated traffic is taken. Relative to src/test/resources/hoverfly
* @param outputFilename the output simulation file name relative to src/test/resources/hoverfly
*/
public void capture(final String recordFile) {
public void capture(final String outputFilename) {
capture(DEFAULT_HOVERFLY_EXPORT_PATH, outputFilename);
}

/**
* Stores what's currently been captured in the currently assigned file, reset simulations and journal logs, then starts capture again
* ready to store in the new file once complete.
* @param outputDir the directory path relative to your project root for exporting the simulation file
* @param outputFilename the output simulation file name relative to src/test/resources/hoverfly
*/
public void capture(final String outputDir, final String outputFilename) {
checkMode(mode -> mode == CAPTURE);
if (capturePath != null) {
hoverfly.exportSimulation(capturePath);
}
hoverfly.reset();
capturePath = fileRelativeToTestResourcesHoverfly(recordFile);
capturePath = Paths.get(outputDir).resolve(outputFilename);
}


/**
* Get custom Hoverfly header name used by Http client to authenticate with secured Hoverfly proxy
* @return the custom Hoverfly authorization header name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,17 @@ static Path fileRelativeToTestResourcesHoverfly(String fileName) {
return Paths.get(DEFAULT_HOVERFLY_EXPORT_PATH).resolve(fileName);
}

/**
* Creates src/test/resources/hoverfly directory if it does not exist
*/
static void createTestResourcesHoverflyDirectoryIfNoneExisting() {
final Path path = Paths.get(DEFAULT_HOVERFLY_EXPORT_PATH);
static Path createDirectoryIfNotExist(String dirPath) {
final Path path = Paths.get(dirPath);

if (! existsAndIsDirectory(path)) {
// Delete in case src/test/resources/hoverfly is a file
if (!existsAndIsDirectory(path)) {
try {
Files.deleteIfExists(path);
Files.createDirectories(path);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return path;
}

private static boolean existsAndIsDirectory(Path path) {
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/io/specto/hoverfly/junit/core/MultiCaptureTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class MultiCaptureTest {
private static final Path FIRST_RECORDED_SIMULATION_FILE = Paths.get("src/test/resources/hoverfly/first-multi-capture/first-multi-capture-scenario.json");
private static final Path SECOND_RECORDED_SIMULATION_FILE = Paths.get("src/test/resources/hoverfly/second-multi-capture-scenario.json");
private static final Path THIRD_RECORDED_SIMULATION_FILE = Paths.get("src/test/resources/hoverfly/third-multi-capture/another/third-multi-capture-scenario.json");
private static final Path FORTH_RECORDED_SIMULATION_FILE = Paths.get("src/integration-test/resources/hoverfly/forth-multi-capture-scenario.json");
private static final String EXPECTED_SIMULATION_JSON = "expected-simulation.json";
private static final String OTHER_EXPECTED_SIMULATION_JSON = "expected-simulation-other.json";

Expand All @@ -47,6 +48,7 @@ public void setUp() throws Exception {

// Delete individual file
Files.deleteIfExists(SECOND_RECORDED_SIMULATION_FILE);
Files.deleteIfExists(FORTH_RECORDED_SIMULATION_FILE);

// Delete directory and contents
final File thirdSimulationDirectory = THIRD_RECORDED_SIMULATION_FILE.getParent().toFile();
Expand Down Expand Up @@ -76,6 +78,12 @@ public void shouldRecordMultipleScenariosInDifferentDirectories() {

// When
restTemplate.getForObject(webServerBaseUrl, String.class);

// Given
hoverflyRule.capture("src/integration-test/resources/hoverfly", "forth-multi-capture-scenario.json");

// When
restTemplate.getForObject(webServerBaseUrl, String.class);
}

// We have to assert after the rule has executed because that's when the classpath is written to the filesystem
Expand All @@ -87,10 +95,12 @@ public static void after() throws IOException, JSONException {
final String firstActualSimulation = new String(Files.readAllBytes(FIRST_RECORDED_SIMULATION_FILE), defaultCharset());
final String secondActualSimulation = new String(Files.readAllBytes(SECOND_RECORDED_SIMULATION_FILE), defaultCharset());
final String thirdActualSimulation = new String(Files.readAllBytes(THIRD_RECORDED_SIMULATION_FILE), defaultCharset());
final String forthActualSimulation = new String(Files.readAllBytes(FORTH_RECORDED_SIMULATION_FILE), defaultCharset());

JSONAssert.assertEquals(expectedSimulation, firstActualSimulation, JSONCompareMode.LENIENT);
JSONAssert.assertEquals(otherExpectedSimulation, secondActualSimulation, JSONCompareMode.LENIENT);
JSONAssert.assertEquals(expectedSimulation, thirdActualSimulation, JSONCompareMode.LENIENT);
JSONAssert.assertEquals(expectedSimulation, forthActualSimulation, JSONCompareMode.LENIENT);

CaptureModeTestWebServer.terminate();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ public void shouldResetJournalAndStateBeforeSimulate() {
verify(mockHoverfly).resetState();
}

@Test
public void shouldThrowExceptionIfOutputFilenameIsEmpty() {

assertThatThrownBy(() -> HoverflyRule.inCaptureMode(""))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Output simulation file name can not be blank.");
}

private Hoverfly getHoverflyMock(HoverflyRule hoverflyRule) {
Hoverfly mockHoverfly = mock(Hoverfly.class);
Whitebox.setInternalState(hoverflyRule, "hoverfly", mockHoverfly);
Expand Down

0 comments on commit 7a1cbd3

Please sign in to comment.