From 5678314428be00ac77ae352bd6937e06349eaf89 Mon Sep 17 00:00:00 2001 From: Roger Abelenda Date: Thu, 2 May 2024 12:15:48 -0300 Subject: [PATCH] Refactors over response file saver contribution to fix styling, consistency, more explicit naming and add jmx conversion --- .../core/listeners/ResponseFileSaver.java | 66 +++++++++++------- .../core/listeners/ResponseFileSaverTest.java | 67 ++++++++++--------- 2 files changed, 79 insertions(+), 54 deletions(-) diff --git a/jmeter-java-dsl/src/main/java/us/abstracta/jmeter/javadsl/core/listeners/ResponseFileSaver.java b/jmeter-java-dsl/src/main/java/us/abstracta/jmeter/javadsl/core/listeners/ResponseFileSaver.java index 1e62210f..46992bd7 100644 --- a/jmeter-java-dsl/src/main/java/us/abstracta/jmeter/javadsl/core/listeners/ResponseFileSaver.java +++ b/jmeter-java-dsl/src/main/java/us/abstracta/jmeter/javadsl/core/listeners/ResponseFileSaver.java @@ -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. @@ -19,17 +21,22 @@ * generate files only for the associated sampler. *

* 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. + *

+ * Eg:

{@code responseFileSaver("responses/resp")}
will generate files like + * "responses/resp1.json". + *

+ * 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); @@ -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. *

- * 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. + * WARNING: 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. *

- * 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; } @@ -83,8 +92,17 @@ public CodeBuilder(List 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; } } diff --git a/jmeter-java-dsl/src/test/java/us/abstracta/jmeter/javadsl/core/listeners/ResponseFileSaverTest.java b/jmeter-java-dsl/src/test/java/us/abstracta/jmeter/javadsl/core/listeners/ResponseFileSaverTest.java index 656b7850..e487960c 100644 --- a/jmeter-java-dsl/src/test/java/us/abstracta/jmeter/javadsl/core/listeners/ResponseFileSaverTest.java +++ b/jmeter-java-dsl/src/test/java/us/abstracta/jmeter/javadsl/core/listeners/ResponseFileSaverTest.java @@ -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; @@ -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)); @@ -91,6 +87,17 @@ public DslTestPlan testPlanWithResponseFileSaver() { ); } + public DslTestPlan testPlanWithResponseFileSaverAndNonDefaultProperties() { + return testPlan( + threadGroup(1, 1, + httpSampler("http://localhost"), + responseFileSaver("response") + .autoNumber(false) + .autoFileExtension(false) + ) + ); + } + } }