Skip to content

Commit

Permalink
Refactors over response file saver contribution to fix styling, consi…
Browse files Browse the repository at this point in the history
…stency, more explicit naming and add jmx conversion
  • Loading branch information
rabelenda-abstracta committed May 2, 2024
1 parent d837858 commit 5678314
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
import org.apache.jmeter.testelement.TestElement;
import us.abstracta.jmeter.javadsl.codegeneration.MethodCall;
import us.abstracta.jmeter.javadsl.codegeneration.MethodCallContext;
import us.abstracta.jmeter.javadsl.codegeneration.MethodParam;
import us.abstracta.jmeter.javadsl.codegeneration.SingleTestElementCallBuilder;
import us.abstracta.jmeter.javadsl.codegeneration.TestElementParamBuilder;
import us.abstracta.jmeter.javadsl.codegeneration.params.BoolParam;

/**
* Generates one file for each response of a sample/request.
Expand All @@ -19,17 +21,22 @@
* generate files only for the associated sampler.
* <p>
* By default, it will generate one file for each response using the given (which might include the
* directory location) prefix to create the files and adding an incremental number to each response
* and an extension according to the response mime type. Both the incremental number and the
* extension can be set manually if skipAutoNumber and skipSuffix are set to true respectively.
* directory location) prefix to create the files and adding an incremental number and an extension
* according to the response mime type.
* <p>
* Eg: <pre>{@code responseFileSaver("responses/resp")}</pre> will generate files like
* "responses/resp1.json".
* <p>
* Both the incremental number and the file extension can be disabled setting
* {@link #autoNumber(boolean)} and {@link #autoFileExtension(boolean)} to false.
*
* @since 0.13
*/
public class ResponseFileSaver extends BaseListener {

protected String fileNamePrefix;
protected boolean skipAutoNumber = false;
protected boolean skipSuffix = false;
protected boolean autoNumber = true;
protected boolean autoFileExtension = true;

public ResponseFileSaver(String fileNamePrefix) {
super("Save Responses to a file", ResultSaverGui.class);
Expand All @@ -40,38 +47,40 @@ public ResponseFileSaver(String fileNamePrefix) {
protected TestElement buildTestElement() {
ResultSaver ret = new ResultSaver();
ret.setFilename(fileNamePrefix);
ret.setSkipAutoNumber(skipAutoNumber);
ret.setSkipSuffix(skipSuffix);
ret.setSkipAutoNumber(!autoNumber);
ret.setSkipSuffix(!autoFileExtension);
return ret;
}


/**
* Allows specifying whether the ResponseFileSaver appends a number to the end of the generated file.
* Specifies whether, or not, to append an auto incremental number to each generated response file
* name.
* <p>
* By default, the ResponseFileSaver will add a number based on the samplers in the scope of the
* ResponseFileSaver test element. If set to true then no number will be appended.
* <b>WARNING:</b> if you disable this feature you might not get the files for all generated
* responses (due to potential file name collision and file rewrite). Consider using some jmeter
* expression in file name to avoid file name collisions and overrides (eg:
* "responses/${__threadNum}-${__jm__Thread Group__idx}").
*
* @param skipAutoNumber Boolean determining whether the number is added.
* @param autoNumber specifies to add the auto incremental numbers to the file when set to true.
* By default, this is set to true.
* @return the ResponseFileSaver for further configuration or usage.
*/
public ResponseFileSaver setSkipAutoNumber(boolean skipAutoNumber) {
this.skipAutoNumber = skipAutoNumber;
public ResponseFileSaver autoNumber(boolean autoNumber) {
this.autoNumber = autoNumber;
return this;
}


/**
* Allows specifying whether the ResponseFileSaver will append the file type to the file name.
* Specifies whether, or not, to append an automatic file extension to the file name.
* <p>
* By default, the ResponseFileSaver will use the MIME type to append the file type to the end of the
* generated file. If this is set to true then no file type will be appended.
*
* @param skipSuffix Boolean determining whether a file type is added.
* The automatic file extension is solved according to the response MIME type.
*
* @param autoFileExtension specifies to use the automatic file type extension when set to true.
* By default, is set ti true.
* @return the ResponseFileSaver for further configuration or usage.
*/
public ResponseFileSaver setSkipSuffix(boolean skipSuffix) {
this.skipSuffix = skipSuffix;
public ResponseFileSaver autoFileExtension(boolean autoFileExtension) {
this.autoFileExtension = autoFileExtension;
return this;
}

Expand All @@ -83,8 +92,17 @@ public CodeBuilder(List<Method> builderMethods) {

@Override
protected MethodCall buildMethodCall(ResultSaver testElement, MethodCallContext context) {
return buildMethodCall(
new TestElementParamBuilder(testElement).stringParam(ResultSaver.FILENAME));
TestElementParamBuilder paramBuilder = new TestElementParamBuilder(testElement);
MethodCall ret = buildMethodCall(paramBuilder.stringParam(ResultSaver.FILENAME));
MethodParam skipAutoNumber = paramBuilder.boolParam(ResultSaver.SKIP_AUTO_NUMBER, false);
if (!skipAutoNumber.isDefault()) {
ret.chain("autoNumber", new BoolParam(false, true));
}
MethodParam skipSuffix = paramBuilder.boolParam(ResultSaver.SKIP_SUFFIX, false);
if (!skipSuffix.isDefault()) {
ret.chain("autoFileExtension", new BoolParam(false, true));
}
return ret;
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import static us.abstracta.jmeter.javadsl.JmeterDsl.testPlan;
import static us.abstracta.jmeter.javadsl.JmeterDsl.threadGroup;

import java.io.IOException;
import java.nio.file.Path;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
Expand All @@ -23,56 +24,51 @@ public class ResponseFileSaverTest extends JmeterDslTest {
private static final String RESPONSE_FILE_PREFIX = "response";

@Test
public void shouldWriteFileWithResponseContentWhenResponseFileSaverInPlan(@TempDir Path tempDir) throws Exception {
public void shouldWriteFileWithResponseContentWhenResponseFileSaverInPlan(@TempDir Path tempDir)
throws Exception {
checkGeneratedResponseFile(buildResponseFileSaver(tempDir),
tempDir.resolve("response1.unknown"));
}

private ResponseFileSaver buildResponseFileSaver(Path tempDir) {
return responseFileSaver(tempDir.resolve(RESPONSE_FILE_PREFIX).toString());
}

private void checkGeneratedResponseFile(ResponseFileSaver responseFileSaver, Path filePath)
throws IOException {
String body = "TEST BODY";
stubFor(any(anyUrl()).willReturn(aResponse().withBody(body)));
testPlan(
threadGroup(1, 1,
httpSampler(wiremockUri)
),
responseFileSaver(tempDir.resolve(RESPONSE_FILE_PREFIX).toString())
responseFileSaver
).run();
assertThat(tempDir.resolve("response1.unknown")).hasContent(body);
assertThat(filePath).hasContent(body);
}

@Test
public void shouldWriteFileWithNoAddedNumberWithResponseContentWhenResponseFileSaverInPlanAndSkipAutoNumberTrue(@TempDir Path tempDir) throws Exception {
String body = "TEST BODY";
ResponseFileSaver fileSaver = responseFileSaver(tempDir.resolve(RESPONSE_FILE_PREFIX).toString());
fileSaver.setSkipAutoNumber(true);
stubFor(any(anyUrl()).willReturn(aResponse().withBody(body)));
testPlan(
threadGroup(1, 1,
httpSampler(wiremockUri)
),
fileSaver
).run();
assertThat(tempDir.resolve("response.unknown")).hasContent(body);
public void shouldWriteFileWithNoNumberWhenResponseFileSaverWithoutAutoNumber(
@TempDir Path tempDir) throws Exception {
checkGeneratedResponseFile(buildResponseFileSaver(tempDir).autoNumber(false),
tempDir.resolve("response.unknown"));
}

@Test
public void shouldWriteFileWithNoAddedFileExtensionWithResponseContentWhenResponseFileSaverInPlanAndSkipAutoNumberTrue(@TempDir Path tempDir) throws Exception {
String body = "TEST BODY";
ResponseFileSaver fileSaver = responseFileSaver(tempDir.resolve(RESPONSE_FILE_PREFIX).toString());
fileSaver.setSkipSuffix(true);
stubFor(any(anyUrl()).willReturn(aResponse().withBody(body)));
testPlan(
threadGroup(1, 1,
httpSampler(wiremockUri)
),
fileSaver
).run();
assertThat(tempDir.resolve("response1")).hasContent(body);
public void shouldWriteFileWithNoExtensionWhenResponseFileSaverWithoutAutoExtension(
@TempDir Path tempDir) throws Exception {
checkGeneratedResponseFile(buildResponseFileSaver(tempDir).autoFileExtension(false),
tempDir.resolve("response1"));
}


@Test
public void shouldWriteOneFileForEachResponseWhenResponseFileSaverInPlan(@TempDir Path tempDir) throws Exception {
public void shouldWriteOneFileForEachResponseWhenResponseFileSaverInPlan(@TempDir Path tempDir)
throws Exception {
testPlan(
threadGroup(1, TEST_ITERATIONS,
httpSampler(wiremockUri)
),
responseFileSaver(tempDir.resolve(RESPONSE_FILE_PREFIX).toString())
buildResponseFileSaver(tempDir)
).run();
String[] responseFiles = tempDir.toFile().list((dir, name) -> name.startsWith(
RESPONSE_FILE_PREFIX));
Expand All @@ -91,6 +87,17 @@ public DslTestPlan testPlanWithResponseFileSaver() {
);
}

public DslTestPlan testPlanWithResponseFileSaverAndNonDefaultProperties() {
return testPlan(
threadGroup(1, 1,
httpSampler("http://localhost"),
responseFileSaver("response")
.autoNumber(false)
.autoFileExtension(false)
)
);
}

}

}

0 comments on commit 5678314

Please sign in to comment.