Skip to content

Commit

Permalink
Merge pull request #4 from CognitiveJ/0.2.11
Browse files Browse the repository at this point in the history
[wssimulator-0.2.11] -New wssimulationcontext feature (see change log…
  • Loading branch information
CognitiveJ authored Oct 24, 2016
2 parents cc048fd + fe40c53 commit 81fe00e
Show file tree
Hide file tree
Showing 13 changed files with 154 additions and 58 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# 0.2.11 (24/10/2016)
- Introduced WSSimulatorContext object as the response to a simulation add.

# 0.2.10 (21/08/2016)
- Bug fixes
- Added the ability to retrieve the last request from the route
Expand Down
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ WSSimulator is an open source library that easily allows for you to simulate ext
**Getting Started**

* Java 8
* The dependency from JCenter or the Standalone [distro](https://github.com/CognitiveJ/wssimulator/releases/download/0.2.10/wssimulator-0.2.10.zip "Download Standalone Version")
* The dependency from JCenter or the Standalone [distro](https://github.com/CognitiveJ/wssimulator/releases/download/0.2.11/wssimulator-0.2.11.zip "Download Standalone Version")


To simulate web service calls, you first need to describe the simulation. This process is very easy as Simulations are created in a YAML format and you don't need to 'simulate' much to get start as the only required field for you to define is _path_ and WSSimulator will default the other options.
Expand Down Expand Up @@ -54,15 +54,15 @@ There are 2 ways to use WSSimulator; as a standalone application or as an embedd
WSSimulator is packaged here and supports been executed on both *nix & windows systems;
* For *nix systems
- Unizip the ws-simulator-0.2.10.zip file to a local directory
- Unizip the ws-simulator-0.2.11.zip file to a local directory
- Then Call:
```shell
./wssimulator <options>
```

* For Windows
- Unizip the ws-simulator-0.2.10.zip file to a local directory
- Unizip the ws-simulator-0.2.11.zip file to a local directory
- Then Call:
```shell
./wssimulator.bat <options>
Expand Down Expand Up @@ -96,7 +96,7 @@ repositories {
}
dependencies {
compile "cognitivej:wssimulator:0.2.10"
compile "cognitivej:wssimulator:0.2.11"
...
}
Expand Down Expand Up @@ -177,7 +177,7 @@ _Filters by any request containing the phrase 'Hello World'_
WSSimulator can return the total call count and the last route request body for routes. For example
```java
int addedSimulationId = WSSimulator.addSimulation(new File(getClass().getResource("/lastRequest/request.yml").toURI()))
WSSimulationContext simulationResponse = WSSimulator.addSimulation(new File(getClass().getResource("/lastRequest/request.yml").toURI()))
given().port(port)
.contentType(ContentType.XML)
.body("Last Request - 1")
Expand All @@ -188,6 +188,7 @@ WSSimulator can return the total call count and the last route request body for
.body("Last Request - 2")
.post("/hello");

WSSimulator.calledCount(addedSimulationId) == 2;
WSSimulator.lastRequest(addedSimulationId) == "Last Request - 2";
simulationResponse.callCount() == 2;
simulationResponse.lastMessage() == "Last Request - 2";

```
11 changes: 0 additions & 11 deletions src/main/java/wssimulator/Bootstrap.java
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,6 @@ public static void main(String[] args) {
try {
CommandLine cmd = parser.parse(options, args);

if (cmd.hasOption("w")) {
webApp(options);
}
if (cmd.hasOption("h")) {
usage(options);
}
Expand Down Expand Up @@ -262,14 +259,6 @@ public static void main(String[] args) {

}

/**
* Invokes the launch of the web-application
* @param options
*/
private static void webApp(Options options) {
WebAppController webAppController = new WebAppController();
webAppController.startWebApp();
}

/**
* Prints out an example YAML simulation file to the default console stream
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/wssimulator/WSSimulation.java
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ public class WSSimulation {
public int resilienceFailureCode = 500;

/**
* Context object for this simulation
* Response object for this simulation
*/
@JsonIgnore
public final WSSimulationContext wsSimulationContext = new WSSimulationContext();
Expand Down
63 changes: 61 additions & 2 deletions src/main/java/wssimulator/WSSimulationContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,70 @@
package wssimulator;


import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
* Context object that provides a running view of the simulation.
*/
public class WSSimulationContext {

private static final org.slf4j.Logger LOG = org.slf4j.LoggerFactory.getLogger(WSSimulator.class);

private int callCount;
private String lastMessage;
private CountDownLatch countDownLatch;

/**
* Holds the call count for this simulation
*/
public int callCount;
public String lastRequest;
public void simulationInvoked(String request) {
callCount++;
lastMessage = request;
if (countDownLatch != null)
countDownLatch.countDown();
}

/**
* The number of timesk this simulation has been called.
*
* @return the number of times called
*/
public int callCount() {
return callCount;
}

/**
* Last message to reach the simulation
*
* @return the last message
*/
public String lastMessage() {
return lastMessage;
}

/**
* Blocks the current thread until this simulation is called.
*/
public void blockUntilCalled() {
blockUntilCalled(Integer.MAX_VALUE, TimeUnit.DAYS);
}


/**
* Blocks the current thread until this simulation is called.
*
* @param timeout the timeout length
* @param timeUnit the unit of time to lock for
*/
public void blockUntilCalled(int timeout, TimeUnit timeUnit) {
try {
countDownLatch = new CountDownLatch(1);
countDownLatch.await(timeout, timeUnit);
countDownLatch = null;
} catch (InterruptedException e) {
LOG.error("Interrupted", e);
}

}
}
27 changes: 21 additions & 6 deletions src/main/java/wssimulator/WSSimulator.java
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,6 @@

import java.io.File;
import java.util.Collection;
import java.util.concurrent.TimeUnit;

/**
* Base simulator for web services.
Expand Down Expand Up @@ -247,9 +246,9 @@ public static void addSimulationOnClasspath(@NotNull String classpathLocation) {
* Apply a simulation to be emulated
*
* @param WSSimulation the WSSimulation
* @return the id of this simulation
* @return the WSSimulationContext of this simulation
*/
public static int addSimulation(@NotNull WSSimulation WSSimulation) {
public static WSSimulationContext addSimulation(@NotNull WSSimulation WSSimulation) {
return wsSimulatorServiceManager.add(WSSimulation);
}

Expand All @@ -258,9 +257,9 @@ public static int addSimulation(@NotNull WSSimulation WSSimulation) {
* Adds a simulation from a YAML file
*
* @param echoSimulationAsYaml the yaml file
* @return the id of this simulation
* @return the WSSimulationContext of this simulation
*/
public static int addSimulation(@NotNull File echoSimulationAsYaml) {
public static WSSimulationContext addSimulation(@NotNull File echoSimulationAsYaml) {
LOG.info("adding simulation file: {}", echoSimulationAsYaml);
YamlToSimulation yamlToSimulation = new YamlToSimulation(echoSimulationAsYaml);
return addSimulation(yamlToSimulation.simulatorSimulation());
Expand Down Expand Up @@ -316,6 +315,7 @@ public static int loadedSimulationCount() {
*
* @param simulationId - the simulation id
* @return the number of times a service has been called.
* @deprecated use {@link WSSimulationContext#callCount()}
*/
public static int calledCount(int simulationId) {
return wsSimulatorServiceManager.calledCounter(simulationId);
Expand All @@ -327,6 +327,7 @@ public static int calledCount(int simulationId) {
*
* @param simulationId - the simulation id
* @return the the text of the last request
* @deprecated use {@link WSSimulationContext#lastMessage()} ()}
*/
public static String lastRequest(int simulationId) {
return wsSimulatorServiceManager.lastRequest(simulationId);
Expand All @@ -341,6 +342,20 @@ public static String lastRequest(int simulationId) {
* @return the id or -1 if not loaded.
*/
public static int findSimulationId(@NotNull String path, @NotNull HttpMethod httpMethod) {
return wsSimulatorServiceManager.findSimulationIdByPath(path, httpMethod);
int simulationIdByPath = wsSimulatorServiceManager.findSimulationIdByPath(path, httpMethod);
return simulationIdByPath;
}

/**
* Return simulation path based on its logical path
*
* @param path the path that the simulation
* @param httpMethod the httpmethod of the specification
* @return the found simulation
* @throws SimulationNotFoundException
*/
public static WSSimulation findSimulation(@NotNull String path, @NotNull HttpMethod httpMethod) {
int simulationId = findSimulationId(path, httpMethod);
return wsSimulatorServiceManager.getWSSimulation(simulationId);
}
}
15 changes: 8 additions & 7 deletions src/main/java/wssimulator/WSSimulatorServiceManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,6 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;

import static spark.Spark.*;

Expand Down Expand Up @@ -275,9 +274,9 @@ private static WSSimulatorServiceManager startup() {
* Adds and starts a web service simulator simulation
*
* @param simulation the simulation to be added
* @return the id of this simulation
* @return the WSResponse object of this simulation
*/
public int add(@NotNull WSSimulation simulation) {
public WSSimulationContext add(@NotNull WSSimulation simulation) {
WSSimulatorValidation.validate(simulation);
return setupRoute(simulation);
}
Expand All @@ -287,7 +286,7 @@ public int add(@NotNull WSSimulation simulation) {
*
* @param simulation the simulator simulation to setup.
*/
private int setupRoute(@NotNull WSSimulation simulation) {
private WSSimulationContext setupRoute(@NotNull WSSimulation simulation) {
BaseHandler handler = lookupHandler(simulation);
validSimulations.put(++counter, simulation);
if (handler.routeCount() == 1) {
Expand Down Expand Up @@ -318,7 +317,7 @@ private int setupRoute(@NotNull WSSimulation simulation) {
break;
}
}
return counter;
return simulation.wsSimulationContext;
}

private BaseHandler lookupHandler(@NotNull WSSimulation wsSimulation) {
Expand Down Expand Up @@ -376,10 +375,11 @@ public WSSimulation getWSSimulation(int simulationId) {
*
* @param simulationId the Simulation ID
* @return the current call count.
* @deprecated use {@link WSSimulationContext#callCount()}
*/
public int calledCounter(int simulationId) {
WSSimulation wsSimulation = getWSSimulation(simulationId);
return wsSimulation.wsSimulationContext.callCount;
return wsSimulation.wsSimulationContext.callCount();
}


Expand All @@ -388,10 +388,11 @@ public int calledCounter(int simulationId) {
*
* @param simulationId
* @return the last request (or null if not yet called)
* @deprecated use {@link WSSimulationContext#lastMessage()} ()}
*/
public String lastRequest(int simulationId) {
WSSimulation wsSimulation = getWSSimulation(simulationId);
return wsSimulation.wsSimulationContext.lastRequest;
return wsSimulation.wsSimulationContext.lastMessage();
}

public int findSimulationIdByPath(@NotNull String path, @NotNull HttpMethod httpMethod) {
Expand Down
13 changes: 0 additions & 13 deletions src/main/java/wssimulator/WebAppController.java
Original file line number Diff line number Diff line change
Expand Up @@ -226,19 +226,6 @@ public class WebAppController implements Route {
private String baseRoute = "/wssimulator";
private String route = baseRoute + "/:simulation";

/**
* Starts the web application
*/
public void startWebApp() {
LOG.info("Starting web application on route against static files in '/web' ");
staticFiles.location("/web");
Spark.put(baseRoute, this);
Spark.patch(route, this);
Spark.delete(route, this);
Spark.get(baseRoute, this);
Spark.get(route, this);
}

@Override
public Object handle(Request request, Response response) throws Exception {
String simulationId = request.params("simulation");
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/wssimulator/handler/BaseHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,7 @@ public final Object processRequest(@NotNull Request request, @NotNull Response r
//find the correct simulation to use
WSSimulation wsSimulation = loadSimulation(request);
if (wsSimulation != null) {
wsSimulation.wsSimulationContext.callCount++;
wsSimulation.wsSimulationContext.lastRequest = request.body();
wsSimulation.wsSimulationContext.simulationInvoked(request.body());
if (!validate(wsSimulation, request.body())) {
LOG.info("Validation failed for request, returning status code:{}", wsSimulation.badRequestResponseCode);
response.status(wsSimulation.badRequestResponseCode);
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/wssimulator-version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.2.10
0.2.11
Original file line number Diff line number Diff line change
Expand Up @@ -220,13 +220,13 @@ class CalledCounterTestSpecification extends Specification {
int port = TestUtils.randomPort()
when:
WSSimulator.setPort(port)
int addedSimulationId = WSSimulator.addSimulation(new File(getClass().getResource("/simple.yml").toURI()))
def response = WSSimulator.addSimulation(new File(getClass().getResource("/simple.yml").toURI()))
then:
given().port(port).get("/hello").then().assertThat()
.statusCode(200).and().body(equalTo("hello world"))
given().port(port).get("/hello").then().assertThat()
.statusCode(200).and().body(equalTo("hello world"))
WSSimulator.calledCount(addedSimulationId) == 2;
response.callCount() == 2;
cleanup:
WSSimulator.shutdown()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,6 @@ class RouteFilterSimulationSpecification extends Specification {
WSSimulator.addSimulation(new File(getClass().getResource("/route/route3.yml").toURI()))
then:
LOG.info("Validate the route filter feature:then")

given().port(port)
.contentType(ContentType.XML)
.body("This is just random test with Action1 contained within it")
Expand Down
Loading

0 comments on commit 81fe00e

Please sign in to comment.