Skip to content

Commit

Permalink
Implemented Simple Controller (#222)
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-norton-anaplan authored Sep 13, 2023
1 parent 174d7bf commit 83bc6df
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 0 deletions.
36 changes: 36 additions & 0 deletions docs/guide/request-generation/simple-controller.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
### Organization

Simple controllers offer a way of organizing your test plan into individual modules,
controllers, assertions, samplers etc. can all be added, even more Simple Controllers for further organization,
simple controllers will run each child sequentially from top to bottom

Look at the bellow example of how to use a `simpleController`:

```java
import static org.assertj.core.api.Assertions.assertThat;
import static us.abstracta.jmeter.javadsl.JmeterDsl.*;

import java.io.IOException;
import java.time.Duration;
import org.apache.http.entity.ContentType;
import org.junit.jupiter.api.Test;
import us.abstracta.jmeter.javadsl.core.TestPlanStats;

public class PerformanceTest {

@Test
public void testPerformance() throws Exception {
TestPlanStats testPlanStats = testPlan(
threadGroup(1, 1,
simpleController("Simple Controller",
httpSampler(wiremockUri),
dummySampler("Test")
)
)
).run();
}

}
```

Check [DslSimpleController](/jmeter-java-dsl/src/main/java/us/abstracta/jmeter/javadsl/core/controllers/DslSimpleController.java) and [JMeter Component documentation](https://jmeter.apache.org/usermanual/component_reference.html#Simple_Controller) for more details.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import us.abstracta.jmeter.javadsl.core.controllers.DslIfController;
import us.abstracta.jmeter.javadsl.core.controllers.DslOnceOnlyController;
import us.abstracta.jmeter.javadsl.core.controllers.DslRuntimeController;
import us.abstracta.jmeter.javadsl.core.controllers.DslSimpleController;
import us.abstracta.jmeter.javadsl.core.controllers.DslTransactionController;
import us.abstracta.jmeter.javadsl.core.controllers.DslWeightedSwitchController;
import us.abstracta.jmeter.javadsl.core.controllers.DslWhileController;
Expand Down Expand Up @@ -367,6 +368,19 @@ public static DslTransactionController transaction(String name, ThreadGroupChild
return new DslTransactionController(name, Arrays.asList(children));
}

/**
* Builds a new simple controller with the given name.
*
* @param name specifies the name to identify the controller.
* @param children contains the test elements that will be contained within the controller.
* @return the controller instance.
* @see DslSimpleController
* @since 1.21
*/
public static DslSimpleController simpleController(String name, ThreadGroupChild... children) {
return new DslSimpleController(name, Arrays.asList(children));
}

/**
* Builds an If Controller that allows to conditionally run specified children.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package us.abstracta.jmeter.javadsl.core.controllers;

import org.apache.jmeter.control.GenericController;
import org.apache.jmeter.control.gui.LogicControllerGui;
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.SingleTestElementCallBuilder;
import us.abstracta.jmeter.javadsl.codegeneration.params.ChildrenParam;
import us.abstracta.jmeter.javadsl.codegeneration.params.StringParam;
import us.abstracta.jmeter.javadsl.core.threadgroups.BaseThreadGroup.ThreadGroupChild;

import java.lang.reflect.Method;
import java.util.List;

/**
* The Simple Logic Controller lets you organize your Samplers and other Logic Controllers.
* This controller provides no additional functionality
* <p>
* Used primarily to organize other controllers and samples
*
* @since 1.21
*/
public class DslSimpleController extends BaseController<DslSimpleController> {

public DslSimpleController(String name, List<ThreadGroupChild> children) {
super(name, LogicControllerGui.class, children);
}

@Override
protected TestElement buildTestElement() {
return new GenericController();
}

public static class CodeBuilder extends SingleTestElementCallBuilder<GenericController> {

public CodeBuilder(List<Method> builderMethods) {
super(GenericController.class, builderMethods);
}

@Override
protected MethodCall buildMethodCall(GenericController testElement, MethodCallContext context) {
return buildMethodCall(new StringParam(testElement.getName()), new ChildrenParam<>(ThreadGroupChild[].class));
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package us.abstracta.jmeter.javadsl.core.controllers;

import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import us.abstracta.jmeter.javadsl.JmeterDslTest;
import us.abstracta.jmeter.javadsl.codegeneration.MethodCallBuilderTest;
import us.abstracta.jmeter.javadsl.core.DslTestPlan;
import us.abstracta.jmeter.javadsl.core.TestPlanStats;

import static org.assertj.core.api.Assertions.assertThat;
import static us.abstracta.jmeter.javadsl.JmeterDsl.dummySampler;
import static us.abstracta.jmeter.javadsl.JmeterDsl.httpSampler;
import static us.abstracta.jmeter.javadsl.JmeterDsl.simpleController;
import static us.abstracta.jmeter.javadsl.JmeterDsl.testPlan;
import static us.abstracta.jmeter.javadsl.JmeterDsl.threadGroup;

public class DslSimpleControllerTest extends JmeterDslTest {

@Test
public void shouldOnlyIncludeSpecifiedSamples()
throws Exception {

TestPlanStats testPlanStats = testPlan(
threadGroup(1, 1,
simpleController("Simple Controller",
httpSampler(wiremockUri),
dummySampler("Test")
)
)
).run();
assertThat(testPlanStats.overall().samplesCount()).isEqualTo(2);
assertThat(testPlanStats.byLabel("jp@gc - Dummy Sampler").samplesCount()).isEqualTo(1);
assertThat(testPlanStats.byLabel("HTTP Request").samplesCount()).isEqualTo(1);
assertThat(testPlanStats.byLabel("Simple Controller")).isNull();
}

@Test
public void allowsNestedSimpleControllers()
throws Exception {

TestPlanStats testPlanStats = testPlan(
threadGroup(1, 1,
simpleController("Simple Controller",
simpleController("Simple Controller",
httpSampler(wiremockUri)
),
httpSampler(wiremockUri),
dummySampler("Test")
)
)
).run();
assertThat(testPlanStats.overall().samplesCount()).isEqualTo(3);
assertThat(testPlanStats.byLabel("jp@gc - Dummy Sampler").samplesCount()).isEqualTo(1);
assertThat(testPlanStats.byLabel("HTTP Request").samplesCount()).isEqualTo(2);
}

@SuppressWarnings("unused")
@Nested
public class CodeBuilderTest extends MethodCallBuilderTest {

public DslTestPlan testPlanWithSimpleController() {
return testPlan(
threadGroup(1, 1,
simpleController("test",
httpSampler("http://localhost"),
httpSampler("http://mysite.com")
)
)
);
}

}

}

0 comments on commit 83bc6df

Please sign in to comment.